MCPcopy
hub / github.com/sequelize/sequelize / sync

Method sync

lib/sequelize.js:780–822  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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.

Callers 1

prepareTransactionTestFunction · 0.95

Calls 3

dropMethod · 0.95
authenticateMethod · 0.95
forEachModelMethod · 0.80

Tested by

no test coverage detected