clever-orm
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -7,2 +7,3 @@ var path = require( 'path' ) | ||
, moduleLdr = env.moduleLoader | ||
, _ = require( 'underscore' ) | ||
, inflect = require( 'i' )(); | ||
@@ -20,85 +21,110 @@ | ||
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 !== 'ORM' ) { | ||
return cb(); | ||
} | ||
if ( !ModelType || !Models || ModelType.type !== 'ORM' ) { | ||
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 ); | ||
delete data.associations; | ||
ModelType.create( data ) | ||
.then(function( model ) { | ||
data.associations = assocs; | ||
ModelType | ||
.create( data ) | ||
.then(function( model ) { | ||
console.log( 'Created ' + modelName ); | ||
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 ]; | ||
console.log( 'Created ' + modelName ); | ||
assocMap[ modelName ].push( model ); | ||
if ( data.associations !== undefined ) { | ||
var assocLength = Object.keys( data.associations ).length | ||
, called = 0; | ||
if ( !ModelType || !Models || ModelType.type !== 'ORM' ) { | ||
return cb(); | ||
} | ||
Object.keys( data.associations ).forEach( function( assocModelName ) { | ||
var required = data.associations[ assocModelName ] | ||
, associations = []; | ||
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 } ); | ||
assocMap[ assocModelName ].forEach( function( m ) { | ||
var isMatched = null; | ||
Object.keys( data.associations ).forEach( function( assocModelName ) { | ||
var required = data.associations[ assocModelName ] | ||
, associations = []; | ||
Object.keys( required ).forEach( function( reqKey ) { | ||
if ( isMatched !== false ) { | ||
if ( m[ reqKey ] === required[ reqKey ] ) { | ||
isMatched = true; | ||
} else { | ||
isMatched = false; | ||
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 ) { | ||
var funcName = 'set' + inflect.pluralize( assocModelName.replace( /(Model)$/g,'' ) ); | ||
// Handle hasOne | ||
if ( typeof model[ funcName ] !== 'function' ) { | ||
funcName = 'set' + assocModelName; | ||
associations = associations[ 0 ]; | ||
} | ||
// strip "Model" from funcName | ||
funcName = funcName.replace( /(Model)$/g,'' ); | ||
// Handle hasOne | ||
if ( typeof model._model[ funcName ] !== 'function' ) { | ||
funcName = 'set' + assocModelName.replace( /(Model)$/g,'' ); | ||
associations = associations[ 0 ]; | ||
} | ||
console.log( 'Calling ' + funcName ); | ||
model[ funcName ]( associations ) | ||
.success(function() { | ||
called++; | ||
console.log( 'Calling ' + modelName + '.' + funcName + '()' ); | ||
model[ funcName ]( associations ) | ||
.then(function() { | ||
called++; | ||
if ( called == assocLength ) { | ||
modelCb( null ); | ||
} | ||
}) | ||
.error( modelCb ); | ||
} | ||
}); | ||
} else { | ||
modelCb( null ); | ||
} | ||
}) | ||
.catch( modelCb ); | ||
}, | ||
function forEachModelComplete( err ) { | ||
cb( err ); | ||
} | ||
); | ||
}, | ||
if ( called == assocLength ) { | ||
modelCb( null ); | ||
} | ||
}) | ||
.catch( modelCb ); | ||
} else { | ||
modelCb( null ); | ||
} | ||
}); | ||
} else { | ||
modelCb( null ); | ||
} | ||
}, | ||
cb | ||
); | ||
}, | ||
callback | ||
) | ||
} | ||
], | ||
function forEachModelTypeComplete( err ) { | ||
@@ -105,0 +131,0 @@ if ( err === null || err === undefined ) { |
var injector = require( 'injector' ) | ||
, Sequelize = require( 'sequelize' ) | ||
, Module = require( 'classes' ).Module | ||
, Model = require( 'classes' ).Model; | ||
, Model = require( 'classes' ).Model | ||
, Promise = require( 'bluebird' ) | ||
, i = require( 'i' )(); | ||
@@ -15,2 +17,6 @@ module.exports = Module.extend({ | ||
if ( !!this.config.db.options.logging ) { | ||
this.config.db.options.logging = console.log; | ||
} | ||
this.sequelize = new Sequelize( | ||
@@ -42,2 +48,62 @@ this.config.db.database, | ||
})); | ||
var models = require( 'models' ); | ||
Object.keys( this.models ).forEach( this.proxy( function( modelName ) { | ||
var model = this.models[ modelName ] | ||
, Model = models[ modelName ]; | ||
Object.keys( model.associations ).forEach( this.proxy( function( assocationName ) { | ||
var association = model.associations[ assocationName ]; | ||
models[ association.source.name ]._getters[ association.identifier ] = function() { | ||
if ( association.identifier === 'id' && Model.type.toLowerCase() === 'odm' ) { | ||
return this._model._id; | ||
} else { | ||
return this._model[ association.identifier ]; | ||
} | ||
} | ||
var as = i.camelize( i[ association.associationType === 'HasMany' ? 'pluralize' : 'singularize' ]( association.as ), false ); | ||
models[ association.source.name ]._getters[ as ] = function() { | ||
return this._model[ as ]; | ||
} | ||
models[ association.source.name ]._setters[ association.identifier ] = function( val ) { | ||
this._model[ association.identifier ] = val; | ||
}; | ||
Object.keys( association.accessors ).forEach( function( accessorName ) { | ||
var accessor = association.accessors[ accessorName ]; | ||
if ( typeof model.DAO.prototype[ accessor ] === 'function' ) { | ||
Model.prototype[ accessor ] = function( options ) { | ||
return new Promise( function( resolve, reject ) { | ||
if ( !/has/.test( accessor ) ) { | ||
options = options || {}; | ||
this._model[ accessor ]( options ) | ||
.success( function( _model ) { | ||
if ( _model instanceof Array ) { | ||
var wrappedModels = []; | ||
_model.forEach( function( m ) { | ||
wrappedModels.push( new ( models[ association.target.name ] )( m ) ); | ||
}); | ||
resolve( wrappedModels ); | ||
} else { | ||
resolve( new ( models[ association.target.name ] )( _model ) ); | ||
} | ||
}) | ||
.error( reject ); | ||
} else { | ||
this._model[ accessor ].success( resolve ).error( reject ); | ||
} | ||
}.bind( this )) | ||
} | ||
} | ||
}); | ||
})); | ||
})); | ||
}, | ||
@@ -44,0 +110,0 @@ |
{ | ||
"name": "clever-orm", | ||
"description": "CleverStack ORM (SQL) Module", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"main": "module.js", | ||
@@ -6,0 +6,0 @@ "author": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47990
940