UNPKG

graph-common

Version:
79 lines (68 loc) 2.32 kB
(function() { var SchemaManager; SchemaManager = (function() { function SchemaManager(graph, core_model_mapping) { var self; self = this; this.graph = graph; this.database = this.graph.database; this.schemas = []; this.models = []; this.core_model_mapping = core_model_mapping; this.init_core_model(function() { return self.reload(); }); } SchemaManager.prototype.load_core_model = function(name, file) { var methods, model_definition, schema; model_definition = require(file); schema = model_definition.schema; methods = model_definition.methods; this.load_schema(schema, function(schema_object) { return Object.keys(model_definition.methods).forEach(function(method) { return schema_object.methods[method] = model_definition.methods[method]; }); }); return this.model(name); }; SchemaManager.prototype.init_core_model = function(done) { var core_model, file, _ref; _ref = this.core_model_mapping; for (core_model in _ref) { file = _ref[core_model]; this.load_core_model(core_model, file); } return done(); }; SchemaManager.prototype.reload = function() { var Schema; Schema = this.model('Schema'); return Schema.find({}, function(err, schemas) { return schemas.forEach(function(schema) { return this.load_schema(schema); }); }); }; SchemaManager.prototype.load_schema = function(schema, callback) { var Schema; this.graph.verbose('SchemaManager> add:', schema.name); Schema = this.database.Schema; this.schemas[schema.name] = new Schema(schema.definition); this.database.apply_plugins(this.schemas[schema.name]); if (callback) { return callback(this.schemas[schema.name]); } }; SchemaManager.prototype.model = function(schema_name) { if (this.models[schema_name]) { return this.models[schema_name]; } if (!this.schemas[schema_name]) { return null; } return this.models[schema_name] = this.database.model(schema_name, this.schemas[schema_name]); }; return SchemaManager; })(); module.exports = SchemaManager; }).call(this);