{ "version": 3, "sources": ["../../lib/model-manager.js"], "sourcesContent": ["'use strict';\n\nconst Toposort = require('toposort-class');\nconst _ = require('lodash');\n\nclass ModelManager {\n constructor(sequelize) {\n this.models = [];\n this.sequelize = sequelize;\n }\n\n addModel(model) {\n this.models.push(model);\n this.sequelize.models[model.name] = model;\n\n return model;\n }\n\n removeModel(modelToRemove) {\n this.models = this.models.filter(model => model.name !== modelToRemove.name);\n\n delete this.sequelize.models[modelToRemove.name];\n }\n\n getModel(against, options) {\n options = _.defaults(options || {}, {\n attribute: 'name'\n });\n\n return this.models.find(model => model[options.attribute] === against);\n }\n\n get all() {\n return this.models;\n }\n\n /**\n * Iterate over Models in an order suitable for e.g. creating tables.\n * Will take foreign key constraints into account so that dependencies are visited before dependents.\n *\n * @param {Function} iterator method to execute on each model\n * @param {object} [options] iterator options\n * @private\n */\n forEachModel(iterator, options) {\n const models = {};\n const sorter = new Toposort();\n let sorted;\n let dep;\n\n options = _.defaults(options || {}, {\n reverse: true\n });\n\n for (const model of this.models) {\n let deps = [];\n let tableName = model.getTableName();\n\n if (_.isObject(tableName)) {\n tableName = `${tableName.schema}.${tableName.tableName}`;\n }\n\n models[tableName] = model;\n\n for (const attrName in model.rawAttributes) {\n if (Object.prototype.hasOwnProperty.call(model.rawAttributes, attrName)) {\n const attribute = model.rawAttributes[attrName];\n\n if (attribute.references) {\n dep = attribute.references.model;\n\n if (_.isObject(dep)) {\n dep = `${dep.schema}.${dep.tableName}`;\n }\n\n deps.push(dep);\n }\n }\n }\n\n deps = deps.filter(dep => tableName !== dep);\n\n sorter.add(tableName, deps);\n }\n\n sorted = sorter.sort();\n if (options.reverse) {\n sorted = sorted.reverse();\n }\n for (const name of sorted) {\n iterator(models[name], name);\n }\n }\n}\n\nmodule.exports = ModelManager;\nmodule.exports.ModelManager = ModelManager;\nmodule.exports.default = ModelManager;\n"], "mappings": ";AAEA,MAAM,WAAW,QAAQ;AACzB,MAAM,IAAI,QAAQ;AAElB,mBAAmB;AAAA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