clever-odm
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -5,3 +5,3 @@ var injector = require( 'injector' ) | ||
, path = require( 'path' ) | ||
, odmUtils = require( path.resolve( path.join( __dirname, '..', 'lib', 'utils.js' ) ) ) | ||
, odmUtils = require( path.resolve( path.join( __dirname, '..', 'utils', 'odmUtils.js' ) ) ) | ||
, env = utils.bootstrapEnv() | ||
@@ -8,0 +8,0 @@ , moduleLdr = env.moduleLoader; |
@@ -1,3 +0,2 @@ | ||
var path = require( 'path' ) | ||
, odmUtils = require( path.resolve( path.join( __dirname, '..', 'lib', 'utils.js' ) ) ) | ||
var odmUtils = require( 'utils' ).odmUtils | ||
, async = require( 'async' ) | ||
@@ -7,2 +6,4 @@ , utils = require( 'utils' ) | ||
, moduleLdr = env.moduleLoader | ||
, _ = require( 'underscore' ) | ||
, config = require( 'config' ) | ||
, inflect = require( 'i' )(); | ||
@@ -20,85 +21,131 @@ | ||
async.forEachSeries( | ||
Object.keys( seedData ), | ||
function forEachModelType( modelName, cb ) { | ||
var ModelType = models[ modelName.replace( 'Model', '' ) ] | ||
, Models = seedData[ modelName ]; | ||
async.waterfall( | ||
[ | ||
function createModels( callback ) { | ||
async.forEach( | ||
Object.keys( seedData ), | ||
function forEachSeedDataModel( modelName, cb ) { | ||
var ModelType = models[ modelName.replace( 'Model', '' ) ] | ||
, Models = seedData[ modelName ]; | ||
if ( !ModelType || !Models || ModelType.type !== 'ODM' ) { | ||
return cb(); | ||
} | ||
if ( !ModelType || !Models || ModelType.type.toUpperCase() !== 'ODM' ) { | ||
return cb(); | ||
} | ||
async.forEachSeries( | ||
Models, | ||
function forEachModel( data, modelCb ) { | ||
var assocs = data.associations; | ||
delete data.associations; | ||
async.forEach( | ||
Models, | ||
function createModel( modelData, modelCb ) { | ||
var data = _.clone( modelData ) | ||
, associations = data.associations; | ||
ModelType.create( data ) | ||
.then(function( model ) { | ||
data.associations = assocs; | ||
delete data.associations; | ||
console.log( 'Created ' + modelName ); | ||
assocMap[ modelName ].push( model ); | ||
if ( data.associations !== undefined ) { | ||
var assocLength = Object.keys( data.associations ).length | ||
, called = 0; | ||
ModelType | ||
.create( data ) | ||
.then(function( model ) { | ||
console.log( 'Created ' + modelName ); | ||
if ( associations ) { | ||
model.associations = associations; | ||
} | ||
assocMap[ modelName ].push( model ); | ||
modelData.id = model.id; | ||
modelCb( null ); | ||
}) | ||
.catch( modelCb ); | ||
}, | ||
cb | ||
); | ||
}, | ||
callback | ||
) | ||
}, | ||
function associateModels( callback ) { | ||
async.forEachSeries( | ||
Object.keys( seedData ), | ||
function forEachSeedDataModel( modelName, cb ) { | ||
var ModelType = models[ modelName.replace( 'Model', '' ) ] | ||
, Models = seedData[ modelName ]; | ||
Object.keys( data.associations ).forEach( function( assocModelName ) { | ||
var required = data.associations[ assocModelName ] | ||
, associations = []; | ||
if ( !ModelType || !Models || ModelType.type.toUpperCase() !== 'ODM' ) { | ||
return cb(); | ||
} | ||
assocMap[ assocModelName ].forEach( function( m ) { | ||
var isMatched = null; | ||
async.forEachSeries( | ||
Models, | ||
function associateModel( data, modelCb ) { | ||
if ( data.associations !== undefined ) { | ||
var assocLength = Object.keys( data.associations ).length | ||
, called = 0 | ||
, model = _.findWhere( assocMap[ modelName ], { id: data.id } ); | ||
Object.keys( required ).forEach( function( reqKey ) { | ||
if ( isMatched !== false ) { | ||
if ( m[ reqKey ] === required[ reqKey ] ) { | ||
isMatched = true; | ||
} else { | ||
isMatched = false; | ||
Object.keys( data.associations ).forEach( function( assocModelName ) { | ||
var required = data.associations[ assocModelName ] | ||
, associations = []; | ||
if ( !( required instanceof Array ) ) { | ||
required = [ required ]; | ||
} | ||
required.forEach( function( requiredModels ) { | ||
if ( typeof requiredModels !== 'array' ) { | ||
requiredModels = [ requiredModels ]; | ||
} | ||
requiredModels.forEach( function( requiredModel ) { | ||
if ( ( associatedModel = _.findWhere( assocMap[ assocModelName ], requiredModel )) !== undefined ) { | ||
associations.push( associatedModel._model ); | ||
} | ||
} | ||
}); | ||
}); | ||
if ( isMatched ) { | ||
associations.push( m ); | ||
} | ||
}); | ||
if ( associations.length ) { | ||
var funcName = 'set' + inflect.pluralize( assocModelName ); | ||
if ( associations.length ) { | ||
assocModelName = assocModelName.replace( /(Model)$/g, '' ); | ||
var assocConfig = config[ 'clever-odm' ].modelAssociations[ modelName.replace( /(Model)$/g, '' ) ]; | ||
Object.keys( assocConfig ).every( function( associatedModelType ) { | ||
assocConfig[ associatedModelType ].forEach( function( _model ) { | ||
_model = _model instanceof Array ? _model : [ _model, {} ]; | ||
// Handle hasOne | ||
if ( typeof model[ funcName ] !== 'function' ) { | ||
funcName = 'set' + assocModelName; | ||
associations = associations[ 0 ]; | ||
} | ||
// strip "Model" from funcName | ||
funcName = funcName.replace( /(Model)$/g,'' ); | ||
if ( assocModelName === _model[ 0 ] ) { | ||
assocModelName = inflect.camelize( _model[ 1 ].as ? _model[ 1 ].as : _model[ 0 ] ); | ||
return false; | ||
} | ||
console.log( 'Calling ' + funcName ); | ||
model[ funcName ]( associations ) | ||
.success(function() { | ||
called++; | ||
return true; | ||
}); | ||
}); | ||
if ( called == assocLength ) { | ||
modelCb( null ); | ||
} | ||
}) | ||
.error( modelCb ); | ||
} | ||
}); | ||
} else { | ||
modelCb( null ); | ||
} | ||
}) | ||
.catch( modelCb ); | ||
}, | ||
function forEachModelComplete( err ) { | ||
cb( err ); | ||
} | ||
); | ||
}, | ||
var funcName = 'set' + inflect.pluralize( assocModelName.replace( /(Model)$/g,'' ) ); | ||
// Handle hasOne | ||
if ( typeof model._model[ funcName ] !== 'function' ) { | ||
funcName = 'set' + assocModelName.replace( /(Model)$/g,'' ); | ||
associations = associations[ 0 ]; | ||
} | ||
console.log( 'Calling ' + modelName + '.' + funcName + '()' ); | ||
model[ funcName ]( associations ) | ||
.then(function() { | ||
called++; | ||
if ( called == assocLength ) { | ||
modelCb( null ); | ||
} | ||
}) | ||
.catch( modelCb ); | ||
} else { | ||
modelCb( null ); | ||
} | ||
}); | ||
} else { | ||
modelCb( null ); | ||
} | ||
}, | ||
cb | ||
); | ||
}, | ||
callback | ||
) | ||
} | ||
], | ||
function forEachModelTypeComplete( err ) { | ||
@@ -119,2 +166,2 @@ if ( err === null || err === undefined ) { | ||
// Load | ||
moduleLdr.loadModules(); | ||
moduleLdr.loadModules(); |
@@ -1,7 +0,12 @@ | ||
var mongoose = require( 'mongoose' ) | ||
, injector = require( 'injector' ) | ||
, Module = require( 'classes' ).Module; | ||
var mongoose = require( 'mongoose' ) | ||
, injector = require( 'injector' ) | ||
, Module = require( 'classes' ).Module | ||
, Model = require( 'classes' ).Model | ||
, odmUtils = require( 'utils' ).odmUtils | ||
, dbRef = require( 'mongoose-dbref' ) | ||
, deepPopulate = require( 'mongoose-deep-populate' ); | ||
module.exports = Module.extend({ | ||
module.exports = Module.extend({ | ||
models: {}, | ||
@@ -14,5 +19,15 @@ | ||
mongoose.set( 'debug', this.config.mongoose.debug ); | ||
if ( this.config.mongoose && undefined !== this.config.mongoose.debug ) { | ||
mongoose.set( 'debug', this.config.mongoose.debug ); | ||
} | ||
}, | ||
preInit: function() { | ||
this.debug( 'Adding mongoose to the injector...' ); | ||
injector.instance( 'mongoose', mongoose ); | ||
injector.instance( 'mongooseDbRef', dbRef ); | ||
injector.instance( 'mongooseDeepPopulate', deepPopulate ); | ||
}, | ||
modulesLoaded: function() { | ||
@@ -22,13 +37,48 @@ mongoose.connect( this.config.uri, this.proxy( 'handleMongoConnect' ) ); | ||
handleMongoConnect: function( err ) { | ||
this.emit( 'ready', err ); | ||
defineModelsAssociations: function() { | ||
this.debug( 'Defining model assocations' ); | ||
Object.keys( this.config.modelAssociations ).forEach( this.proxy( function( modelName ) { | ||
Object.keys( this.config.modelAssociations[ modelName ] ).forEach( this.proxy( 'defineModelAssociations', modelName ) ); | ||
})); | ||
}, | ||
preInit: function() { | ||
this.debug( 'Adding mongoose to the injector...' ); | ||
defineModelAssociations: function( Static, Proto, assocType ) { | ||
var modelName = Static._name | ||
, associatedWith = this.config.modelAssociations[ modelName ][ assocType ]; | ||
injector.instance( 'mongoose', mongoose ); | ||
if ( ! associatedWith instanceof Array ) { | ||
associatedWith = [ associatedWith ]; | ||
} | ||
associatedWith.forEach( this.proxy( 'associateModels', Static, Proto, assocType ) ); | ||
}, | ||
parseModelSchema: function( Static ) { | ||
associateModels: function( Static, Proto, assocType, assocTo ) { | ||
var modelName = Static._name | ||
, models = require( 'models' ); | ||
if ( Static.type.toLowerCase() === 'odm' ) { | ||
// Support second argument | ||
if ( assocTo instanceof Array ) { | ||
this.debug( '%s %s %s with second argument of ', modelName, assocType, assocTo[0], assocTo[1] ); | ||
if ( assocTo[ 1 ].through ) { | ||
assocTo[ 1 ].through = models[ assocTo[ 1 ].through.replace( 'Model', '' ) ]; | ||
} | ||
models[ modelName ][ assocType ]( models[ assocTo[0] ], assocTo[1] ); | ||
} else { | ||
this.debug( '%s %s %s', modelName, assocType, assocTo ); | ||
odmUtils[ assocType ]( mongoose, Static, Proto, assocTo ); | ||
} | ||
} | ||
}, | ||
handleMongoConnect: function( err ) { | ||
dbRef.install( mongoose ); | ||
this.emit( 'ready', err ); | ||
}, | ||
parseModelSchema: function( Static, Proto ) { | ||
var parseDebug = this.proxy(function( msg ) { | ||
@@ -50,2 +100,6 @@ this.debug( Static._name + 'Model: ' + msg ); | ||
if ( this.config.modelAssociations[ Static._name ] ) { | ||
Object.keys( this.config.modelAssociations[ Static._name ] ).forEach( this.proxy( 'defineModelAssociations', Static, Proto ) ); | ||
} | ||
parseDebug( 'Parsing schema...' ); | ||
@@ -57,5 +111,11 @@ Object.keys( Static._schema ).forEach( this.proxy( 'parseSchemaField', Static, fields, mongooseConf ) ); | ||
parseDebug( 'Generating mongoose schema...' ); | ||
var schema = new mongoose.Schema( fields, mongooseConf ); | ||
parseDebug( 'Generating new native model using computed schema...' ); | ||
var model = mongoose.model( Static._name, new mongoose.Schema( fields, mongooseConf ) ); | ||
var model = mongoose.model( Static._name, schema ); | ||
parseDebug( 'Registering deepPopulate plugin...' ); | ||
schema.plugin( deepPopulate, {} ); | ||
parseDebug( 'Caching completed model...' ); | ||
@@ -104,2 +164,7 @@ this.models[ Static._name ] = model; | ||
if ( typeof fieldDefinition.type === 'object' && fieldDefinition.type.type ) { | ||
fieldDefinition.ref = fieldDefinition.type.ref; | ||
fieldDefinition.type = fieldDefinition.type.type; | ||
} | ||
fields[ name ] = fieldDefinition; | ||
@@ -113,2 +178,7 @@ }, | ||
case mongoose.Schema.ObjectId: | ||
options.ref = options.of; | ||
delete options.of; | ||
field = options; | ||
break; | ||
case Number: | ||
@@ -122,3 +192,3 @@ case String: | ||
case Array: | ||
field = options.of ? [ this.getFieldType( Static, { type: options.of } ) ] : [ String ]; | ||
field = options.of ? [ this.getFieldType( Static, options.of ) ] : [ String ]; | ||
break; | ||
@@ -125,0 +195,0 @@ case Model.Types.ENUM: |
{ | ||
"name": "clever-odm", | ||
"description": "CleverStack ODM (NoSQL) Module", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"main": "module.js", | ||
@@ -35,3 +35,4 @@ "author": { | ||
"dependencies": { | ||
"mongoose": "~3.6.20" | ||
"mongoose": "~3.8.22", | ||
"mongoose-deep-populate": "~0.0.7" | ||
}, | ||
@@ -42,2 +43,2 @@ "devDependencies": { | ||
} | ||
} | ||
} |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
38103
631
7
2
+ Addedabort-controller@3.0.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbluebird@2.10.2(transitive)
+ Addedbson@0.2.22(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addeddebug@2.2.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedkerberos@0.0.11(transitive)
+ Addedmongodb@1.4.38(transitive)
+ Addedmongoose@3.8.40(transitive)
+ Addedmongoose-deep-populate@0.0.7(transitive)
+ Addedmpromise@0.4.3(transitive)
+ Addedmquery@1.10.0(transitive)
+ Addedms@0.7.1(transitive)
+ Addedmuri@1.1.0(transitive)
+ Addednan@1.8.4(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedreadable-stream@4.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedbson@0.2.2(transitive)
- Removedkerberos@0.0.3(transitive)
- Removedmongodb@1.3.19(transitive)
- Removedmongoose@3.6.20(transitive)
- Removedmpromise@0.2.1(transitive)
- Removedmuri@0.3.1(transitive)
- Removedsliced@0.0.4(transitive)
Updatedmongoose@~3.8.22