Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var account = new Schema({
- email: String,
- password: String
- });
- var environment = new Schema({
- dbName: String
- });
- var user = new Schema({
- email: String,
- username: String
- });
- var moduleExports = {
- account: account,
- environment: environment,
- user: user
- };
- // A list of models which belong to a 'global' database.
- var globalModels = ['account'];
- // A list of models which are 'namespaced'.
- var nameSpacedModels = _.map(_.keys( moduleExports ), function(n) {
- if( globalModels.indexOf( n ) === -1 ) {
- return n;
- }
- });
- function NamespacedModel(environmentModel) {
- var self = this;
- self.globalEnvironmentModel = {
- name: config.db.globalDbName,
- dbName: config.db.globalDbName,
- };
- if( environmentModel === undefined) {
- self.currentEnvironmentModel = self.globalEnvironmentModel;
- } else {
- self.currentEnvironmentModel = environmentModel;
- }
- self.setDb = function( environmentModel ) {
- if( !environmentModel.dbName ) {
- return new Error("Failed to set the database, property dbName is not set.");
- } else {
- self.currentEnvironmentModel = environmentModel;
- console.log("Setting db to ", environmentModel.dbName);
- }
- var conn = connectionManager.getConnection(environmentModel);
- // Bind models to connection
- _.forOwn(nameSpacedModels, function( item ) {
- if( item !== undefined ) {
- self[item] = conn.model(item, moduleExports[item]);
- }
- });
- };
- // DRY anyone? ;)
- _.forOwn(nameSpacedModels, function( item ) {
- var conn = connectionManager.getConnection(self.quizModel);
- if( item !== undefined ) {
- self[item] = conn.model(item, moduleExports[item]);
- }
- });
- _.forOwn(globalModels, function( item ) {
- var conn = connectionManager.getConnection(self.defaultQuizModel);
- if( item !== undefined ) {
- self[item] = conn.model(item, moduleExports[item]);
- }
- });
- };
- module.exports = function(ns) {
- return new NamespacedModel(ns);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement