import type { Contract } from '@internal/contract/types'; import type { SqlStorage } from '@internal/sql-contract/types'; import { assertInsertConflictSkipCapability, resolveInsertConflictColumns, resolvePolymorphismInfo, resolveUniqueKeyColumnSets, } from '../collection-contract'; import { ormError } from '../orm-errors'; import { storageTableForContract } from '../storage-resolution'; import type { NestedCreateConflictAction } from '../types'; export type ConflictKeys = readonly (readonly string[])[]; export function conflictKeysOf( contract: Contract, namespaceId: string, tableName: string, onConflict: NestedCreateConflictAction, conflictColumns: readonly string[], ): ConflictKeys { if (conflictColumns.length > 0) { return [conflictColumns]; } const table = storageTableForContract(contract, namespaceId, tableName); if (onConflict === 'connect') { const primaryKey = table.primaryKey?.columns; return primaryKey === undefined || primaryKey.length === 0 ? [] : [primaryKey]; } const uniqueIndexes = table.indexes.flatMap((index) => index.unique && index.columns !== undefined ? [index.columns] : [], ); return [...resolveUniqueKeyColumnSets(contract, namespaceId, tableName), ...uniqueIndexes]; } export function rowThatCannotConflict( rows: readonly Readonly>[], keys: ConflictKeys, parentLinkColumns: ReadonlySet, ): number | undefined { const index = rows.findIndex( (row) => !keys.some((key) => key.every( (column) => parentLinkColumns.has(column) || (row[column] !== undefined && row[column] !== null), ), ), ); return index === -1 ? undefined : index; } export interface ConflictCall { readonly action: string; readonly modelName: string; readonly meta: Readonly>; } export interface ConflictOption { readonly onConflict: unknown; readonly conflictOn?: readonly string[] | undefined; } export interface VariantInOwnTable { readonly variant: string; readonly model: string; } export interface ResolvedConflict { readonly onConflict: NestedCreateConflictAction; readonly columns: readonly string[]; } export function variantInOwnTableOf( contract: Contract, namespaceId: string, modelName: string, variantValue: string | undefined, ): VariantInOwnTable | undefined { const variant = variantValue === undefined ? undefined : resolvePolymorphismInfo(contract, namespaceId, modelName)?.variants.get(variantValue); return variant?.strategy === 'mti' ? { variant: variant.modelName, model: modelName } : undefined; } export function resolveConflict( contract: Contract, call: ConflictCall, namespaceId: string, { onConflict, conflictOn }: ConflictOption, accepted: readonly NestedCreateConflictAction[], variantInOwnTable: VariantInOwnTable | undefined, ): ResolvedConflict { const action = accepted.find((value) => value === onConflict); if (action === undefined) { throw ormError( 'ORM.ARGUMENT_INVALID', `${call.action} takes onConflict ${accepted.map((value) => `"${value}"`).join(' or ')}; received ${JSON.stringify(onConflict)}`, { meta: { ...call.meta, problem: 'invalid-on-conflict' } }, ); } if (variantInOwnTable !== undefined) { throw ormError( 'ORM.OPERATION_UNSUPPORTED', `The onConflict option is not supported on variant "${variantInOwnTable.variant}" of model "${variantInOwnTable.model}" because the variant is stored in its own table. Leave the option out; a duplicate row then makes the call fail.`, { meta: { ...call.meta, variant: variantInOwnTable.variant, reason: 'mti-variant' } }, ); } const fields = conflictOn ?? []; if (action === 'skip') { assertInsertConflictSkipCapability(contract, call.action, fields.length > 0); } return { onConflict: action, columns: resolveInsertConflictColumns(contract, namespaceId, call.modelName, fields), }; } export function requireRowsThatCanConflict( contract: Contract, call: ConflictCall, { namespaceId, tableName }: { readonly namespaceId: string; readonly tableName: string }, { onConflict, columns }: ResolvedConflict, rows: readonly Readonly>[], filledColumns: (index: number) => Iterable, ): ConflictKeys { const keys = conflictKeysOf(contract, namespaceId, tableName, onConflict, columns); if (keys.length === 0) { throw ormError( 'ORM.ARGUMENT_INVALID', onConflict === 'skip' ? `${call.action} on model "${call.modelName}" has nothing to skip on: table "${tableName}" has no primary key, unique constraint or unique index over columns, so onConflict "skip" cannot skip any row` : `${call.action} on model "${call.modelName}" has nothing to connect on: table "${tableName}" has no primary key, so onConflict "connect" needs conflictOn`, { meta: { ...call.meta, problem: 'no-conflict-key' } }, ); } const index = rows.findIndex( (row, at) => rowThatCannotConflict([row], keys, new Set(filledColumns(at))) !== undefined, ); if (index !== -1) { throw ormError( 'ORM.ARGUMENT_INVALID', `${call.action} row ${index} for model "${call.modelName}" gives no value for any conflict key: ${keys.map((key) => `(${key.join(', ')})`).join(', ')}`, { meta: { ...call.meta, problem: 'row-cannot-conflict', index, conflictKeys: keys } }, ); } return keys; }