MCPcopy
hub / github.com/sequelize/sequelize / forEachModel

Method forEachModel

lib/model-manager.js:45–93  ·  lib/model-manager.js::ModelManager.forEachModel

* Iterate over Models in an order suitable for e.g. creating tables. * Will take foreign key constraints into account so that dependencies are visited before dependents. * * @param {Function} iterator method to execute on each model * @param {object} [options] iterator options * @priv

(iterator, options)

Source from the content-addressed store, hash-verified

43 * @private
44 */
45 forEachModel(iterator, options) {
46 const models = {};
47 const sorter = new Toposort();
48 let sorted;
49 let dep;
50
51 options = _.defaults(options || {}, {
52 reverse: true
53 });
54
55 for (const model of this.models) {
56 let deps = [];
57 let tableName = model.getTableName();
58
59 if (_.isObject(tableName)) {
60 tableName = `${tableName.schema}.${tableName.tableName}`;
61 }
62
63 models[tableName] = model;
64
65 for (const attrName in model.rawAttributes) {
66 if (Object.prototype.hasOwnProperty.call(model.rawAttributes, attrName)) {
67 const attribute = model.rawAttributes[attrName];
68
69 if (attribute.references) {
70 dep = attribute.references.model;
71
72 if (_.isObject(dep)) {
73 dep = `${dep.schema}.${dep.tableName}`;
74 }
75
76 deps.push(dep);
77 }
78 }
79 }
80
81 deps = deps.filter(dep => tableName !== dep);
82
83 sorter.add(tableName, deps);
84 }
85
86 sorted = sorter.sort();
87 if (options.reverse) {
88 sorted = sorted.reverse();
89 }
90 for (const name of sorted) {
91 iterator(models[name], name);
92 }
93 }
94}
95
96module.exports = ModelManager;

Callers 3

syncMethod · 0.80
truncateMethod · 0.80
dropMethod · 0.80

Calls 2

getTableNameMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected