* Sync all defined models to the DB. * * @param {object} [options={}] sync options * @param {boolean} [options.force=false] If force is true, each Model will run `DROP TABLE IF EXISTS`, before it tries to create its own table * @param {RegExp} [options.match] Match a regex against the da
(options)
| 778 | * @returns {Promise} |
| 779 | */ |
| 780 | async sync(options) { |
| 781 | options = { |
| 782 | ...this.options, |
| 783 | ...this.options.sync, |
| 784 | ...options, |
| 785 | hooks: options ? options.hooks !== false : true |
| 786 | }; |
| 787 | |
| 788 | if (options.match) { |
| 789 | if (!options.match.test(this.config.database)) { |
| 790 | throw new Error(`Database "${this.config.database}" does not match sync match parameter "${options.match}"`); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (options.hooks) { |
| 795 | await this.runHooks('beforeBulkSync', options); |
| 796 | } |
| 797 | if (options.force) { |
| 798 | await this.drop(options); |
| 799 | } |
| 800 | const models = []; |
| 801 | |
| 802 | // Topologically sort by foreign key constraints to give us an appropriate |
| 803 | // creation order |
| 804 | this.modelManager.forEachModel(model => { |
| 805 | if (model) { |
| 806 | models.push(model); |
| 807 | } else { |
| 808 | // DB should throw an SQL error if referencing non-existent table |
| 809 | } |
| 810 | }); |
| 811 | |
| 812 | // no models defined, just authenticate |
| 813 | if (!models.length) { |
| 814 | await this.authenticate(options); |
| 815 | } else { |
| 816 | for (const model of models) await model.sync(options); |
| 817 | } |
| 818 | if (options.hooks) { |
| 819 | await this.runHooks('afterBulkSync', options); |
| 820 | } |
| 821 | return this; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Truncate all tables defined through the sequelize models. |
no test coverage detected