MCPcopy
hub / github.com/sequelize/sequelize / bulkCreate

Method bulkCreate

lib/model.js:2556–2876  ·  lib/model.js::Model.bulkCreate

* Create and insert multiple instances in bulk. * * The success handler is passed an array of instances, but please notice that these may not completely represent the state of the rows in the DB. This is because MySQL * and SQLite do not make it easy to obtain back automatically generated I

(records, options = {})

Source from the content-addressed store, hash-verified

2554 * @returns {Promise<Array<Model>>}
2555 */
2556 static async bulkCreate(records, options = {}) {
2557 if (!records.length) {
2558 return [];
2559 }
2560
2561 const dialect = this.sequelize.options.dialect;
2562 const now = Utils.now(this.sequelize.options.dialect);
2563 options = Utils.cloneDeep(options);
2564
2565 options.model = this;
2566
2567 if (!options.includeValidated) {
2568 this._conformIncludes(options, this);
2569 if (options.include) {
2570 this._expandIncludeAll(options);
2571 this._validateIncludedElements(options);
2572 }
2573 }
2574
2575 const instances = records.map(values => this.build(values, { isNewRecord: true, include: options.include }));
2576
2577 const recursiveBulkCreate = async (instances, options) => {
2578 options = {
2579 validate: false,
2580 hooks: true,
2581 individualHooks: false,
2582 ignoreDuplicates: false,
2583 ...options
2584 };
2585
2586 if (options.returning === undefined) {
2587 if (options.association) {
2588 options.returning = false;
2589 } else {
2590 options.returning = true;
2591 }
2592 }
2593
2594 if (options.ignoreDuplicates && [class="st">'mssql', class="st">'db2'].includes(dialect)) {
2595 throw new Error(`${dialect} does not support the ignoreDuplicates option.`);
2596 }
2597 if (options.updateOnDuplicate && (dialect !== class="st">'mysql' && dialect !== class="st">'mariadb' && dialect !== class="st">'sqlite' && dialect !== class="st">'postgres')) {
2598 throw new Error(`${dialect} does not support the updateOnDuplicate option.`);
2599 }
2600
2601 const model = options.model;
2602
2603 options.fields = options.fields || Object.keys(model.rawAttributes);
2604 const createdAtAttr = model._timestampAttributes.createdAt;
2605 const updatedAtAttr = model._timestampAttributes.updatedAt;
2606
2607 if (options.updateOnDuplicate !== undefined) {
2608 if (Array.isArray(options.updateOnDuplicate) && options.updateOnDuplicate.length) {
2609 options.updateOnDuplicate = _.intersection(
2610 _.without(Object.keys(model.tableAttributes), createdAtAttr),
2611 options.updateOnDuplicate
2612 );
2613 } else {

Callers 15

model.tsFile · 0.80
updateAssociationsMethod · 0.80
utils.test.jsFile · 0.80
model.test.jsFile · 0.80
operators.test.jsFile · 0.80
instance.test.jsFile · 0.80
include.test.jsFile · 0.80
createUsersAndItemsFunction · 0.80
dao.test.jsFile · 0.80

Calls 4

_conformIncludesMethod · 0.95
_expandIncludeAllMethod · 0.95
buildMethod · 0.95

Tested by 1

createUsersAndItemsFunction · 0.64