* 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 = {})
| 2642 | * @returns {Promise<Array<Model>>} |
| 2643 | */ |
| 2644 | static async bulkCreate(records, options = {}) { |
| 2645 | if (!records.length) { |
| 2646 | return []; |
| 2647 | } |
| 2648 | |
| 2649 | const dialect = this.sequelize.options.dialect; |
| 2650 | const now = Utils.now(this.sequelize.options.dialect); |
| 2651 | options = Utils.cloneDeep(options); |
| 2652 | |
| 2653 | // Add CLS transaction |
| 2654 | if (options.transaction === undefined && this.sequelize.constructor._cls) { |
| 2655 | const t = this.sequelize.constructor._cls.get('transaction'); |
| 2656 | if (t) { |
| 2657 | options.transaction = t; |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | options.model = this; |
| 2662 | |
| 2663 | if (!options.includeValidated) { |
| 2664 | this._conformIncludes(options, this); |
| 2665 | if (options.include) { |
| 2666 | this._expandIncludeAll(options); |
| 2667 | this._validateIncludedElements(options); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 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; |