MCPcopy
hub / github.com/sequelize/sequelize / recursiveBulkCreate

Method recursiveBulkCreate

lib/model.js:2577–2873  ·  lib/model.js::Model.recursiveBulkCreate
(instances, options)

Source from the content-addressed store, hash-verified

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 {
2614 throw new Error(class="st">'updateOnDuplicate option only supports non-empty array.');
2615 }
2616 }
2617
2618 class="cm">// Run before hook
2619 if (options.hooks) {
2620 await model.runHooks(class="st">'beforeBulkCreate', instances, options);
2621 }
2622 class="cm">// Validate
2623 if (options.validate) {
2624 const errors = [];
2625 const validateOptions = { ...options };
2626 validateOptions.hooks = options.individualHooks;
2627
2628 await Promise.all(instances.map(async instance => {
2629 try {
2630 await instance.validate(validateOptions);
2631 } catch (err) {
2632 errors.push(new sequelizeErrors.BulkRecordError(err, instance));
2633 }
2634 }));

Callers

nothing calls this directly

Calls 9

allMethod · 0.80
saveMethod · 0.80
bulkInsertMethod · 0.80
getTableNameMethod · 0.80
bulkBuildMethod · 0.80
changedMethod · 0.80
validateMethod · 0.65
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected