UNPKG

hydrate-mongodb

Version:
119 lines (118 loc) 5.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var async = require("async"); var namingStrategies_1 = require("./namingStrategies"); var mappingRegistry_1 = require("../mapping/mappingRegistry"); var mappingModel_1 = require("../mapping/mappingModel"); var sessionFactory_1 = require("../sessionFactory"); var objectIdGenerator_1 = require("./objectIdGenerator"); var persistenceError_1 = require("../persistenceError"); var Configuration = (function () { function Configuration() { this.identityGenerator = new objectIdGenerator_1.ObjectIdGenerator(); this.versioned = true; this.nullable = false; this.versionField = "__v"; this.discriminatorField = "__t"; this.changeTracking = 1; this.collectionNamingStrategy = namingStrategies_1.NamingStrategies.CamelCase; this.fieldNamingStrategy = namingStrategies_1.NamingStrategies.CamelCase; this.discriminatorNamingStrategy = namingStrategies_1.NamingStrategies.None; this.propertyConverters = {}; this._mappings = []; } Configuration.prototype.addMapping = function (mapping) { this._mappings.push(mapping); }; Configuration.prototype.createSessionFactory = function (connection, callbackOrDatabaseName, callback) { var _this = this; var registry = new mappingRegistry_1.MappingRegistry(), databaseName; if (typeof callbackOrDatabaseName === "function") { callback = callbackOrDatabaseName; } else { databaseName = callbackOrDatabaseName; } if (!this._mappings || this._mappings.length == 0) { return callback(new persistenceError_1.PersistenceError("No mappings were added to the configuration.")); } async.each(this._mappings, function (provider, done) { provider.getMapping(_this, function (err, r) { if (err) return done(err); registry.addMappings(r); done(); }); }, function (err) { if (err) return callback(err); _this._buildCollections(connection, databaseName, registry, function (err, collections) { if (err) return callback(err); var factory = new sessionFactory_1.SessionFactoryImpl(connection, collections, registry); factory.logger = _this.logger; if (!_this.createIndexes) { callback(null, factory); } else { factory.createIndexes(function (err) { if (err) return callback(err); callback(null, factory); }); } }); }); }; Configuration.prototype._buildCollections = function (connection, databaseName, registry, callback) { var _this = this; var collections = []; var namesSeen = new Map(); async.each(registry.getEntityMappings(), function (mapping, done) { if (!(mapping.flags & 4096)) { process.nextTick(done); return; } if (!mapping.collectionName) { return done(new persistenceError_1.PersistenceError("Missing collection name on mapping for type '" + mapping.name + "'.")); } var dbName = mapping.databaseName || databaseName || _this.databaseName; if (!dbName) { return done(new Error("Could not determine database name for '" + mapping.collectionName + "'. Please make sure to specify " + "the database name in either the Configuration, the call to createSessionFactory, or the @Collection decorator.")); } var key = dbName + "/" + mapping.collectionName; if (namesSeen.has(key)) { return done(new persistenceError_1.PersistenceError("Duplicate collection name '" + key + "' on type '" + mapping.name + "' .")); } namesSeen.set(key, true); var db = connection.db(dbName); db.listCollections({ name: mapping.collectionName }).toArray(function (err, names) { if (err) return done(err); if (names.length == 0) { db.createCollection(mapping.collectionName, mapping.collectionOptions || {}, function (err, collection) { if (err) return done(err); collections[mapping.id] = collection; process.nextTick(done); }); } else { db.collection(mapping.collectionName, { strict: true }, function (err, collection) { if (err) return done(err); collections[mapping.id] = collection; process.nextTick(done); }); } }); }, function (err) { if (err) return callback(err); callback(null, collections); }); }; return Configuration; }()); exports.Configuration = Configuration;