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

Method recursiveBulkCreate

src/model.js:2673–2975  ·  view source on GitHub ↗
(instances, options)

Source from the content-addressed store, hash-verified

2671 const instances = records.map(values => this.build(values, { isNewRecord: true, include: options.include }));
2672
2673 const recursiveBulkCreate = async (instances, options) => {
2674 options = {
2675 validate: false,
2676 hooks: true,
2677 individualHooks: false,
2678 ignoreDuplicates: false,
2679 ...options
2680 };
2681
2682 if (options.returning === undefined) {
2683 if (options.association) {
2684 options.returning = false;
2685 } else {
2686 options.returning = true;
2687 }
2688 }
2689 if (options.ignoreDuplicates && !this.sequelize.dialect.supports.inserts.ignoreDuplicates &&
2690 !this.sequelize.dialect.supports.inserts.onConflictDoNothing) {
2691 throw new Error(`${dialect} does not support the ignoreDuplicates option.`);
2692 }
2693 if (options.updateOnDuplicate && (dialect !== 'mysql' && dialect !== 'mariadb' && dialect !== 'sqlite' && dialect !== 'postgres')) {
2694 throw new Error(`${dialect} does not support the updateOnDuplicate option.`);
2695 }
2696
2697 const model = options.model;
2698
2699 options.fields = options.fields || Object.keys(model.rawAttributes);
2700 const createdAtAttr = model._timestampAttributes.createdAt;
2701 const updatedAtAttr = model._timestampAttributes.updatedAt;
2702
2703 if (options.updateOnDuplicate !== undefined) {
2704 if (Array.isArray(options.updateOnDuplicate) && options.updateOnDuplicate.length) {
2705 options.updateOnDuplicate = _.intersection(
2706 _.without(Object.keys(model.tableAttributes), createdAtAttr),
2707 options.updateOnDuplicate
2708 );
2709 } else {
2710 throw new Error('updateOnDuplicate option only supports non-empty array.');
2711 }
2712 }
2713
2714 // Run before hook
2715 if (options.hooks) {
2716 await model.runHooks('beforeBulkCreate', instances, options);
2717 }
2718 // Validate
2719 if (options.validate) {
2720 const errors = [];
2721 const validateOptions = { ...options };
2722 validateOptions.hooks = options.individualHooks;
2723
2724 await Promise.all(instances.map(async instance => {
2725 try {
2726 await instance.validate(validateOptions);
2727 } catch (err) {
2728 errors.push(new sequelizeErrors.BulkRecordError(err, instance));
2729 }
2730 }));

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