Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 131x 580x 131x 65x 200x 7x 6x 11x 6x 11x 6x 204x 211x 131x 204x 370x 131x 500x 66x 434x 139x 139x 139x 139x 139x 305x 139x 66x 1x 65x 67x 65x 1x 66x 66x 66x 203x 139x 305x 295x 4x 135x 131x 131x 135x 135x 291x 17x 135x | import type { ConflictTarget, ExprNode, OnConflictNode } from '../sql/ast.ts';
import { WhereCompiler } from '../where/where.ts';
import type { AttributeColumns } from './attribute-columns.ts';
import type { ModelDefinition } from './model-definition.ts';
import type { BulkCreateOptions, Values } from './options.ts';
type UniqueKey = Readonly<{ fields: readonly string[]; where?: unknown }>;
const fieldsOf = (definition: ModelDefinition, names: readonly string[]): readonly string[] =>
names.map((name) => definition.attributes.get(name)?.field ?? name);
const firstUniqueKey = (definition: ModelDefinition): readonly string[] | undefined =>
[...definition.uniqueKeys.values()].find((fields) => fields.length > 0);
/** Attribute names (and partial-index predicate) of each unique index declared in `indexes`. */
const uniqueIndexKeys = (definition: ModelDefinition, withPartial: boolean): readonly UniqueKey[] =>
(definition.options.indexes ?? [])
.filter((index) => index.unique === true && (withPartial || index.where === undefined))
.filter((index) =>
(index.fields ?? []).every((field) => typeof field === 'string' || field.name !== undefined),
)
.map((index) => ({
fields: (index.fields ?? []).map((field) =>
typeof field === 'string' ? field : (field.name as string),
),
...(index.where === undefined ? {} : { where: index.where }),
}))
.filter((key) => key.fields.length > 0);
const columnsTarget = (target: readonly string[], where: ExprNode | undefined): ConflictTarget => ({
kind: 'columns',
items: target.map((field) => ({ kind: 'ident', path: [field] }) as const),
...(where === undefined ? {} : { where }),
});
const conflictUpdate = (
attributes: readonly string[],
target: readonly string[],
where?: ExprNode,
): OnConflictNode => ({
target: columnsTarget(target, where),
action:
attributes.length === 0
? { kind: 'nothing' }
: {
kind: 'update',
set: attributes.map((field) => ({
column: field,
value: { kind: 'excluded', column: field } as const,
})),
},
});
/**
* Translates the Sequelize v6 conflict rules into ON CONFLICT clauses: `bulkCreate`
* (`ignoreDuplicates`, `updateOnDuplicate`, `conflictAttributes`, `conflictWhere`) and `upsert`
* (conflict target from unique keys / indexes).
*/
export class V6ConflictBuilder {
readonly #columns: AttributeColumns;
/**
* @param columns - Attribute to column mapping.
*/
constructor(columns: AttributeColumns) {
this.#columns = columns;
}
/**
* ON CONFLICT clause of Sequelize v6 `bulkCreate` options.
*
* @param definition - Model definition.
* @param options - `updateOnDuplicate`, `conflictAttributes`, `conflictWhere`, `ignoreDuplicates`.
* @returns `{ onConflict }`, or `{}` without conflict handling.
* @throws Error for an empty `updateOnDuplicate`.
*/
bulk(
definition: ModelDefinition,
options: BulkCreateOptions,
): Readonly<{ onConflict?: OnConflictNode }> {
if (options.updateOnDuplicate !== undefined) {
return {
onConflict: this.#updateOnDuplicate(definition, options, options.updateOnDuplicate),
};
}
return options.ignoreDuplicates === true ? { onConflict: { action: { kind: 'nothing' } } } : {};
}
/**
* ON CONFLICT clause of Sequelize v6 `upsert`.
*
* @param definition - Model definition.
* @param insertValues - Inserted values.
* @param updatable - Attributes updated on conflict.
* @param conflictFields - `conflictFields` option (column names).
* @returns ON CONFLICT node.
*/
upsert(
definition: ModelDefinition,
insertValues: Values,
updatable: readonly string[],
conflictFields: readonly string[] | undefined,
): OnConflictNode {
const update = V6ConflictBuilder.#upsertUpdate(definition, insertValues, updatable);
const uniqueKey = V6ConflictBuilder.#upsertKey(definition, update);
const target =
conflictFields ?? fieldsOf(definition, uniqueKey?.fields ?? definition.primaryKeys);
const where =
conflictFields === undefined ? this.#compile(definition, uniqueKey?.where) : undefined;
const assigned = update.filter(
(name) => definition.attributes.get(name)?.identity !== 'always',
);
return conflictUpdate(fieldsOf(definition, assigned), [...new Set(target)], where);
}
#updateOnDuplicate(
definition: ModelDefinition,
options: BulkCreateOptions,
updateOnDuplicate: readonly string[],
): OnConflictNode {
if (updateOnDuplicate.length === 0) {
throw new Error('updateOnDuplicate option only supports non-empty array.');
}
const updatable = updateOnDuplicate.filter(
(name) => definition.attributes.has(name) && name !== definition.createdAt,
);
const indexed = [
...uniqueIndexKeys(definition, false).flatMap((key) => key.fields),
...(firstUniqueKey(definition) ?? []),
];
const target =
options.conflictAttributes ?? (indexed.length > 0 ? indexed : definition.primaryKeys);
const where = this.#compile(definition, options.conflictWhere);
return conflictUpdate(fieldsOf(definition, updatable), fieldsOf(definition, target), where);
}
#compile(definition: ModelDefinition, where: unknown): ExprNode | undefined {
return WhereCompiler.isEmpty(where)
? undefined
: this.#columns.whereCompiler(definition, undefined).compile(where);
}
static #upsertUpdate(
definition: ModelDefinition,
insertValues: Values,
updatable: readonly string[],
): readonly string[] {
return [
...updatable,
...(definition.updatedAt === undefined || updatable.includes(definition.updatedAt)
? []
: [definition.updatedAt]),
].filter((name) => name in insertValues);
}
/** The first unique key (or unique index) containing an updated attribute, unless a primary key is updated. */
static #upsertKey(definition: ModelDefinition, update: readonly string[]): UniqueKey | undefined {
if (update.some((name) => definition.primaryKeys.includes(name))) {
return undefined;
}
const uniqueKeys = [...definition.uniqueKeys.values()]
.filter((fields) => fields.length > 0)
.map((fields): UniqueKey => ({ fields }));
const indexKeys = uniqueIndexKeys(definition, true);
return update
.map(
(name) =>
uniqueKeys.find((key) => key.fields.includes(name)) ??
indexKeys.find((key) => key.fields.includes(name)),
)
.find((key) => key !== undefined);
}
}
|