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 175 176 | 574x 574x 574x 574x 1x 572x 572x 571x 567x 567x 567x 574x 17x 1x 573x 573x 132156x 572x 572x 572x 1x 571x 2x 2x 3x 2x 569x 569x 569x 4x 4x 4x 565x 569x 569x 569x 569x 569x 132151x 565x 565x 565x 565x 565x 561x 565x 102141x 565x | import type { BulkInsertBuilder } from '../bulk-insert-builder.ts';
import type { ModelDefinition } from '../model-definition.ts';
import type { ModelRegistry } from '../model-registry.ts';
import type { ModelValues } from '../model-values.ts';
import { OnConflictBuilder, type OnConflictOptions } from '../on-conflict-builder.ts';
import type {
AnyModel,
BulkCreateOptions,
ModelStatic,
MutableOptions,
Values,
} from '../options.ts';
import type { WrittenRows } from '../written-rows.ts';
import type { InsertOperation } from './insert-operation.ts';
import { ModelOperation, type OperationServices } from './model-operation.ts';
/** Collaborators of {@link BulkCreateOperation}. */
export type BulkCreateOperationServices = OperationServices &
Readonly<{
/** Model state. */
registry: ModelRegistry;
/** Value rules. */
values: ModelValues;
/** INSERT builder. */
bulkInserts: BulkInsertBuilder;
/** ON CONFLICT builder. */
conflicts: OnConflictBuilder;
/** Conflict-aware inserts. */
inserter: InsertOperation;
/** Alignment of `ignoreDuplicates` results. */
written: WrittenRows;
}>;
// One `Model.bulkCreate` call: the built instances, hook-visible options and caller's options.
type BulkCreateCall = Readonly<{
model: ModelStatic<AnyModel>;
definition: ModelDefinition;
instances: AnyModel[];
opts: MutableOptions;
options: BulkCreateOptions;
}>;
/**
* `Model.bulkCreate`: one multi-row INSERT (Sequelize v6 conflict options or `onConflict`), or
* one `save` per instance with `individualHooks` / nested includes.
*/
export class BulkCreateOperation extends ModelOperation<BulkCreateOperationServices> {
/**
* `Model.bulkCreate`.
*
* @param model - Model class.
* @param records - Values per row.
* @param options - Bulk create options.
* @returns Instances with values returned by the database.
* @throws Error when `onConflict` is combined with Sequelize v6 conflict options.
*/
async bulkCreate<M extends AnyModel>(
model: ModelStatic<M>,
records: readonly Values[],
options: BulkCreateOptions,
): Promise<M[]> {
const { definition } = this.services.registry.state(model);
const opts = BulkCreateOperation.#options(options);
const instances = BulkCreateOperation.#instances(model, records, options);
if (instances.length === 0) {
return [];
}
const call = { model, definition, instances, opts, options };
await this.#before(call);
const created = await this.#create(call);
Eif (opts['hooks'] === true) {
await this.services.lifecycle.runHooks(model, 'afterBulkCreate', created, opts);
}
return created as M[];
}
static #options(options: BulkCreateOptions): MutableOptions {
if (
options.onConflict !== undefined &&
OnConflictBuilder.V6_OPTIONS.some((name) => options[name] !== undefined)
) {
throw new Error(
`onConflict cannot be combined with ${OnConflictBuilder.V6_OPTIONS.join(', ')}`,
);
}
return {
hooks: true,
validate: false,
individualHooks: options.include !== undefined,
...options,
};
}
static #instances<M extends AnyModel>(
model: ModelStatic<M>,
records: readonly Values[],
options: BulkCreateOptions,
): M[] {
return records.map(
(values) =>
new model(values, options.include === undefined ? {} : { include: options.include }),
);
}
async #before({ model, instances, opts }: BulkCreateCall): Promise<void> {
Eif (opts['hooks'] === true) {
await this.services.lifecycle.runHooks(model, 'beforeBulkCreate', instances, opts);
}
if (opts['validate'] === true) {
await this.services.lifecycle.validateAll(instances, opts['individualHooks'] === true);
}
}
#create(call: BulkCreateCall): Promise<AnyModel[]> {
return call.opts['individualHooks'] === true
? BulkCreateOperation.#saveEach(call)
: this.#insert(call);
}
static async #saveEach({ instances, opts }: BulkCreateCall): Promise<AnyModel[]> {
const {
fields: _fields,
individualHooks: _individual,
ignoreDuplicates: _ignore,
include: _include,
...saveOptions
} = opts;
await instances.reduce<Promise<unknown>>(
(previous, instance) =>
previous.then(() => instance.save({ ...saveOptions, validate: false, hooks: true })),
Promise.resolve(),
);
return instances;
}
async #insert(call: BulkCreateCall): Promise<AnyModel[]> {
const { model, definition, options } = call;
const rows = this.#rows(call);
if (options.onConflict !== undefined) {
const onConflict = options.onConflict as OnConflictOptions<unknown>;
const conflict = this.services.conflicts.build(definition, onConflict);
return this.services.inserter.withConflict(model, rows, conflict, options);
}
return this.#insertRows(call, rows);
}
#rows({ definition, instances, opts, options }: BulkCreateCall): Values[] {
const { values } = this.services;
const now = new Date();
const fields = values.bulkFields(definition, options.fields as readonly string[] | undefined);
const silent = opts['silent'] === true;
return instances.map((instance) =>
values.pick(
values.withTimestamps(definition, instance.dataValues as Values, now, true, silent),
fields,
),
);
}
async #insertRows(call: BulkCreateCall, rows: Values[]): Promise<AnyModel[]> {
const { model, definition, instances, options } = call;
const { runtime } = this.services.registry.state(model);
const { bulkInserts, lifecycle, written } = this.services;
const query = bulkInserts.build(definition, rows, options);
const result = await runtime.execute(query, options);
const returned =
options.ignoreDuplicates === true
? written.aligned(definition, rows, result.rows)
: result.rows;
instances.forEach((instance, index) =>
lifecycle.refresh(instance, definition, returned[index]),
);
return instances;
}
}
|