clever-orm
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -95,3 +95,3 @@ var utils = require( 'utils' ) | ||
if ( ( associatedModel = _.findWhere( assocMap[ assocModelName ], requiredModel )) !== undefined ) { | ||
associations.push( associatedModel._model ); | ||
associations.push( associatedModel.entity ); | ||
} | ||
@@ -106,7 +106,7 @@ }); | ||
Object.keys( assocConfig ).every( function( associatedModelType ) { | ||
assocConfig[ associatedModelType ].forEach( function( _model ) { | ||
_model = _model instanceof Array ? _model : [ _model, {} ]; | ||
assocConfig[ associatedModelType ].forEach( function( entity ) { | ||
entity = entity instanceof Array ? entity : [ entity, {} ]; | ||
if ( assocModelName === _model[ 0 ] ) { | ||
assocModelName = inflect.camelize( _model[ 1 ].as ? _model[ 1 ].as : _model[ 0 ] ); | ||
if ( assocModelName === entity[ 0 ] ) { | ||
assocModelName = inflect.camelize( entity[ 1 ].as ? entity[ 1 ].as : entity[ 0 ] ); | ||
return false; | ||
@@ -122,3 +122,3 @@ } | ||
// Handle hasOne | ||
if ( typeof model._model[ funcName ] !== 'function' ) { | ||
if ( typeof model.entity[ funcName ] !== 'function' ) { | ||
funcName = 'set' + assocModelName.replace( /(Model)$/g,'' ); | ||
@@ -125,0 +125,0 @@ associations = associations[ 0 ]; |
@@ -59,7 +59,7 @@ var injector = require( 'injector' ) | ||
models[ association.source.name ]._getters[ association.identifier ] = function() { | ||
models[ association.source.name ].getters[ association.identifier ] = function() { | ||
if ( association.identifier === 'id' && Model.type.toLowerCase() === 'odm' ) { | ||
return this._model._id; | ||
return this.entity._id; | ||
} else { | ||
return this._model[ association.identifier ]; | ||
return this.entity[ association.identifier ]; | ||
} | ||
@@ -69,9 +69,9 @@ } | ||
var as = i[ association.associationType === 'HasMany' ? 'pluralize' : 'singularize' ]( association.as ); | ||
models[ association.source.name ]._getters[ as ] = function() { | ||
return this._model[ as ]; | ||
models[ association.source.name ].getters[ as ] = function() { | ||
return this.entity[ as ]; | ||
} | ||
models[ association.source.name ]._setters[ association.identifier ] = | ||
models[ association.source.name ]._setters[ as ] = function( val ) { | ||
this._model[ association.as ] = val; | ||
models[ association.source.name ].setters[ association.identifier ] = | ||
models[ association.source.name ].setters[ as ] = function( val ) { | ||
this.entity[ association.as ] = val; | ||
}; | ||
@@ -89,7 +89,7 @@ | ||
if ( where._model ) { | ||
where = where._model; | ||
} else if ( where instanceof Array && where[ 0 ]._model ) { | ||
where = where.map( function( _model ) { | ||
return _model._model; | ||
if ( where.entity ) { | ||
where = where.entity; | ||
} else if ( where instanceof Array && where[ 0 ].entity ) { | ||
where = where.map( function( entity ) { | ||
return entity.entity; | ||
}); | ||
@@ -99,12 +99,12 @@ } | ||
if ( !!options && options.save === false ) { | ||
this._model[ accessor ]( where, options ); | ||
this.entity[ accessor ]( where, options ); | ||
resolve(this); | ||
} else { | ||
this._model[ accessor ]( where, options ) | ||
.then( function( _model ) { | ||
if (/set/.test(accessor) && this._model[ as ]) { | ||
this._model[ as ]._model = where; | ||
this.entity[ accessor ]( where, options ) | ||
.then( function( entity ) { | ||
if (/set/.test(accessor) && this.entity[ as ]) { | ||
this.entity[ as ].entity = where; | ||
resolve( this ); | ||
} else { | ||
resolve( _model ); | ||
resolve( entity ); | ||
} | ||
@@ -115,3 +115,3 @@ }.bind(this)) | ||
} else { | ||
this._model[ accessor ].then( resolve ).catch( reject ); | ||
this.entity[ accessor ].then( resolve ).catch( reject ); | ||
} | ||
@@ -155,3 +155,3 @@ }.bind( this )) | ||
sourceModel.on('beforeCreate', function(modelData, queryOptions, callback) { | ||
if (modelData[as] !== undefined && modelData[as]._model === undefined && (typeof modelData[as] !== 'object' || modelData[as][targetModel.primaryKey[0]] === undefined)) { | ||
if (modelData[as] !== undefined && modelData[as].entity === undefined && (typeof modelData[as] !== 'object' || modelData[as][targetModel.primaryKey[0]] === undefined)) { | ||
targetModel | ||
@@ -170,5 +170,5 @@ .find(typeof modelData[as] === 'object' ? _.clone(modelData[as]) : modelData[as], queryOptions) | ||
sourceModel.on('afterCreate', function(instance, modelData, queryOptions, callback) { | ||
if (modelData[as] !== undefined && modelData[as]._model !== undefined) { | ||
instance._model[as] = modelData[as]; | ||
instance._model.values[as] = modelData[as]; | ||
if (modelData[as] !== undefined && modelData[as].entity !== undefined) { | ||
instance.entity[as] = modelData[as]; | ||
instance.entity.values[as] = modelData[as]; | ||
@@ -182,3 +182,3 @@ callback(null); | ||
// sourceModel.on('beforeUpdate', function(modelData, queryOptions, callback) { | ||
// if (modelData[as] !== undefined && modelData[as]._model === undefined && (typeof modelData[as] !== 'object' || modelData[as][targetModel.primaryKey[0]] === undefined)) { | ||
// if (modelData[as] !== undefined && modelData[as].entity === undefined && (typeof modelData[as] !== 'object' || modelData[as][targetModel.primaryKey[0]] === undefined)) { | ||
// targetModel | ||
@@ -197,3 +197,3 @@ // .find(typeof modelData[as] === 'object' ? _.clone(modelData[as]) : modelData[as], queryOptions) | ||
sourceModel.on('afterCreate', function(instance, modelData, queryOptions, callback) { | ||
var association = instance.Class._model.associations[as]; | ||
var association = instance.Class.entity.associations[as]; | ||
@@ -222,4 +222,4 @@ // handle single association creation via the singular name | ||
if (!err) { | ||
instance._model[as] = associations; | ||
instance._model.values[as] = associations; | ||
instance.entity[as] = associations; | ||
instance.entity.values[as] = associations; | ||
@@ -240,5 +240,5 @@ callback(null); | ||
sourceModel.on('afterCreate', function(instance, modelData, queryOptions, callback) { | ||
var association = instance.Class._model.associations[as]; | ||
var association = instance.Class.entity.associations[as]; | ||
if (modelData[as] !== undefined && modelData[as]._model === undefined && typeof modelData[as] === 'object') { | ||
if (modelData[as] !== undefined && modelData[as].entity === undefined && typeof modelData[as] === 'object') { | ||
var data = _.extend( | ||
@@ -250,4 +250,4 @@ typeof modelData[as] === 'object' ? _.clone(modelData[as]) : { label: modelData[as] }, | ||
targetModel.create(data, queryOptions).then(function(targetInstance) { | ||
instance._model[as] = targetInstance; | ||
instance._model.values[as] = targetInstance; | ||
instance.entity[as] = targetInstance; | ||
instance.entity.values[as] = targetInstance; | ||
@@ -268,3 +268,3 @@ callback(null); | ||
var parseDebug = this.proxy(function( msg ) { | ||
this.debug( Static._name + 'Model: ' + msg ); | ||
this.debug( Static.modelName + 'Model: ' + msg ); | ||
}) | ||
@@ -274,9 +274,9 @@ , sequelizeConf = { paranoid: false, timestamps: false } | ||
if ( this.models[ Static._name ] !== undefined ) { | ||
if ( this.models[ Static.modelName ] !== undefined ) { | ||
parseDebug( 'Returning previously parsed and generated model...' ); | ||
return this.models[ Static._name ]; | ||
return this.models[ Static.modelName ]; | ||
} | ||
parseDebug( 'Parsing schema for model...' ); | ||
Object.keys( Static._schema ).forEach( this.proxy( 'parseSchemaField', Static, fields ) ); | ||
Object.keys( Static.fields ).forEach( this.proxy( 'defineField', Static, fields ) ); | ||
@@ -293,10 +293,10 @@ parseDebug( 'Configuring static object for sequelize...' ); | ||
parseDebug( 'Setting sequelize as the _db (adapter) for the Model...' ); | ||
Static._db = this.sequelize; | ||
parseDebug( 'Setting sequelize as the connection (adapter) for the Model...' ); | ||
Static.connection = this.sequelize; | ||
parseDebug( 'Generating new sequelize model using computed schema...' ); | ||
var model = this.sequelize.define( Static._name, fields, sequelizeConf ); | ||
var model = this.sequelize.define( Static.modelName, fields, sequelizeConf ); | ||
parseDebug( 'Caching completed native model...' ); | ||
this.models[ Static._name ] = model; | ||
this.models[ Static.modelName ] = model; | ||
@@ -360,3 +360,3 @@ return model; | ||
if ( Static.deletedAt !== 'deletedAt' ) { | ||
Static._aliases.push({ | ||
Static.aliases.push({ | ||
key : 'deletedAt', | ||
@@ -377,6 +377,6 @@ columnName : Static.deletedAt | ||
parseSchemaField: function( Static, fields, name ) { | ||
defineField: function( Static, fields, name ) { | ||
var fieldDefinition = {} | ||
, columnName = name | ||
, options = Static._schema[ name ] | ||
, options = Static.fields[ name ] | ||
@@ -411,3 +411,10 @@ // Allow direct syntax | ||
if ( optionName === 'primaryKey' ) { | ||
Static.primaryKey.push( name ); | ||
Static.primaryKeys.push( name ); | ||
if (!Static.primaryKey) { | ||
Static.hasPrimaryKey = true; | ||
Static.singlePrimaryKey = true; | ||
Static.primaryKey = name; | ||
} else { | ||
Static.singlePrimaryKey = false; | ||
} | ||
} | ||
@@ -468,5 +475,5 @@ | ||
case undefined: | ||
throw new Error( [ 'You must define the type of field that', '"' + name + '"', 'is on the', '"' + Static._name + '" model' ].join( ' ' ) ); | ||
throw new Error( [ 'You must define the type of field that', '"' + name + '"', 'is on the', '"' + Static.modelName + '" model' ].join( ' ' ) ); | ||
default: | ||
throw new Error( [ 'You must define a valid type for the field named', '"' + name + '"', 'on the', '"' + Static._name + '" model' ].join( ' ' ) ); | ||
throw new Error( [ 'You must define a valid type for the field named', '"' + name + '"', 'on the', '"' + Static.modelName + '" model' ].join( ' ' ) ); | ||
} | ||
@@ -473,0 +480,0 @@ |
{ | ||
"name": "clever-orm", | ||
"description": "CleverStack ORM (SQL) Module", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"main": "module.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -37,4 +37,4 @@ var utils = require( 'utils' ) | ||
it( 'Should have String field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'str' ); | ||
expect( OrmModel._schema.str ).to.eql( String ); | ||
expect( OrmModel.fields ).to.have.property( 'str' ); | ||
expect( OrmModel.fields.str ).to.eql( String ); | ||
@@ -45,4 +45,4 @@ done(); | ||
it( 'Should have Boolean field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'bool' ); | ||
expect( OrmModel._schema.bool ).to.eql( Boolean ); | ||
expect( OrmModel.fields ).to.have.property( 'bool' ); | ||
expect( OrmModel.fields.bool ).to.eql( Boolean ); | ||
@@ -53,4 +53,4 @@ done(); | ||
it( 'Should have Date field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'date' ); | ||
expect( OrmModel._schema.date ).to.eql( Date ); | ||
expect( OrmModel.fields ).to.have.property( 'date' ); | ||
expect( OrmModel.fields.date ).to.eql( Date ); | ||
@@ -61,9 +61,9 @@ done(); | ||
it( 'Should have Enum field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'enum' ); | ||
expect( OrmModel._schema.enum ).to.have.property( 'type' ); | ||
expect( OrmModel._schema.enum.type.toString() ).to.equal( 'ENUM' ); | ||
expect( OrmModel._schema.enum ).to.have.property( 'values' ); | ||
expect( OrmModel._schema.enum.values ).to.be.an( 'array' ); | ||
expect( OrmModel._schema.enum.values.length ).to.equal( 1 ); | ||
expect( OrmModel._schema.enum.values[ 0 ] ).to.equal( 'test' ); | ||
expect( OrmModel.fields ).to.have.property( 'enum' ); | ||
expect( OrmModel.fields.enum ).to.have.property( 'type' ); | ||
expect( OrmModel.fields.enum.type.toString() ).to.equal( 'ENUM' ); | ||
expect( OrmModel.fields.enum ).to.have.property( 'values' ); | ||
expect( OrmModel.fields.enum.values ).to.be.an( 'array' ); | ||
expect( OrmModel.fields.enum.values.length ).to.equal( 1 ); | ||
expect( OrmModel.fields.enum.values[ 0 ] ).to.equal( 'test' ); | ||
@@ -74,9 +74,9 @@ done(); | ||
it( 'Should have Enum (defined by object notation) field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'enumObj' ); | ||
expect( OrmModel._schema.enumObj ).to.have.property( 'type' ); | ||
expect( OrmModel._schema.enumObj.type.toString() ).to.equal( 'ENUM' ); | ||
expect( OrmModel._schema.enumObj ).to.have.property( 'values' ); | ||
expect( OrmModel._schema.enumObj.values ).to.be.an( 'array' ); | ||
expect( OrmModel._schema.enumObj.values.length ).to.equal( 1 ); | ||
expect( OrmModel._schema.enumObj.values[ 0 ] ).to.equal( 'test' ); | ||
expect( OrmModel.fields ).to.have.property( 'enumObj' ); | ||
expect( OrmModel.fields.enumObj ).to.have.property( 'type' ); | ||
expect( OrmModel.fields.enumObj.type.toString() ).to.equal( 'ENUM' ); | ||
expect( OrmModel.fields.enumObj ).to.have.property( 'values' ); | ||
expect( OrmModel.fields.enumObj.values ).to.be.an( 'array' ); | ||
expect( OrmModel.fields.enumObj.values.length ).to.equal( 1 ); | ||
expect( OrmModel.fields.enumObj.values[ 0 ] ).to.equal( 'test' ); | ||
@@ -87,4 +87,4 @@ done(); | ||
it( 'Should have Buffer field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'buf' ); | ||
expect( OrmModel._schema.buf ).to.eql( Buffer ); | ||
expect( OrmModel.fields ).to.have.property( 'buf' ); | ||
expect( OrmModel.fields.buf ).to.eql( Buffer ); | ||
@@ -95,4 +95,4 @@ done(); | ||
it( 'Should have bigint field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'bigint' ); | ||
expect( OrmModel._schema.bigint.toString() ).to.equal( 'BIGINT' ); | ||
expect( OrmModel.fields ).to.have.property( 'bigint' ); | ||
expect( OrmModel.fields.bigint.toString() ).to.equal( 'BIGINT' ); | ||
@@ -103,6 +103,6 @@ done(); | ||
it( 'Should have bigint with length field', function( done ) { | ||
expect( OrmModel._schema ).to.have.property( 'bigintLen' ); | ||
expect( OrmModel._schema.bigintLen ).to.have.property( 'length' ); | ||
expect( OrmModel._schema.bigintLen.length ).to.equal( 11 ); | ||
expect( OrmModel._schema.bigintLen.type.toString() ).to.equal( 'BIGINT' ); | ||
expect( OrmModel.fields ).to.have.property( 'bigintLen' ); | ||
expect( OrmModel.fields.bigintLen ).to.have.property( 'length' ); | ||
expect( OrmModel.fields.bigintLen.length ).to.equal( 11 ); | ||
expect( OrmModel.fields.bigintLen.type.toString() ).to.equal( 'BIGINT' ); | ||
@@ -114,6 +114,6 @@ done(); | ||
it.skip( 'Should have defined Orms table in MySQL', function( done ) { | ||
injector.getInstance( 'sequelize' ).query( 'describe ' + OrmModel._model.tableName + ';', { raw: true }) | ||
injector.getInstance( 'sequelize' ).query( 'describe ' + OrmModel.entity.tableName + ';', { raw: true }) | ||
.then(function( desc ) { | ||
var modelSchemaKeys = Object.keys( OrmModel._schema).map( function( fieldName ) { | ||
var field = OrmModel._schema[ fieldName ]; | ||
var modelSchemaKeys = Object.keys( OrmModel.fields).map( function( fieldName ) { | ||
var field = OrmModel.fields[ fieldName ]; | ||
return field.field ? field.field : fieldName; | ||
@@ -131,3 +131,3 @@ }); | ||
it( 'Should be able to create a new model instance', function( done ) { | ||
var _model = { | ||
var entity = { | ||
str : 'String', | ||
@@ -152,6 +152,6 @@ bool : true, | ||
OrmModel | ||
.create( _.clone( _model ) ) | ||
.create( _.clone( entity ) ) | ||
.then( function( model ) { | ||
Object.keys( _model ).forEach( function( key ) { | ||
expect( model[ key ] ).to.eql( _model[ key ] ); | ||
Object.keys( entity ).forEach( function( key ) { | ||
expect( model[ key ] ).to.eql( entity[ key ] ); | ||
}); | ||
@@ -158,0 +158,0 @@ done(); |
@@ -27,6 +27,6 @@ var inflect = require('i')() | ||
if (findOptions.include[i]._model) { | ||
findOptions.include[i] = findOptions.include[i]._model; | ||
} else if (findOptions.include[i].model && findOptions.include[i].model._model) { | ||
findOptions.include[i].model = findOptions.include[i].model._model; | ||
if (findOptions.include[i].entity) { | ||
findOptions.include[i] = findOptions.include[i].entity; | ||
} else if (findOptions.include[i].model && findOptions.include[i].model.entity) { | ||
findOptions.include[i].model = findOptions.include[i].model.entity; | ||
} | ||
@@ -48,7 +48,7 @@ | ||
afterEagerLoad: function(findOptions, model) { | ||
if (model !== null && findOptions.include && findOptions.include.length && model._model.options.include) { | ||
if (model !== null && findOptions.include && findOptions.include.length && model.entity.options.include) { | ||
var models = this.getDefinedModels(); | ||
Object.keys(model._model.options.includeMap).forEach(function(modelName) { | ||
var _include = model._model.options.includeMap[modelName] | ||
Object.keys(model.entity.options.includeMap).forEach(function(modelName) { | ||
var _include = model.entity.options.includeMap[modelName] | ||
, as = inflect.camelize(_include.as, false) | ||
@@ -59,14 +59,14 @@ , csModel = models[_include.association.target ? _include.association.target.name : modelName]; | ||
if (!!csModel && !!model._model[as]) { | ||
if (model._model[as] instanceof Array) { | ||
for (var i = 0; i < model._model[as].length; i++) { | ||
if (!(model._model[as][i] instanceof csModel)) { | ||
model._model[as][i] = new csModel(model._model[as][i]); | ||
ormUtils.afterEagerLoad.apply(csModel, [model._model[as][i]._model.options, model._model[as][i]]); | ||
if (!!csModel && !!model.entity[as]) { | ||
if (model.entity[as] instanceof Array) { | ||
for (var i = 0; i < model.entity[as].length; i++) { | ||
if (!(model.entity[as][i] instanceof csModel)) { | ||
model.entity[as][i] = new csModel(model.entity[as][i]); | ||
ormUtils.afterEagerLoad.apply(csModel, [model.entity[as][i].entity.options, model.entity[as][i]]); | ||
} | ||
} | ||
} else { | ||
if (!(model._model[as] instanceof csModel)) { | ||
model._model[as] = new csModel(model._model[as]); | ||
ormUtils.afterEagerLoad.apply(csModel, [model._model[as]._model.options, model._model[as]]); | ||
if (!(model.entity[as] instanceof csModel)) { | ||
model.entity[as] = new csModel(model.entity[as]); | ||
ormUtils.afterEagerLoad.apply(csModel, [model.entity[as].entity.options, model.entity[as]]); | ||
} | ||
@@ -85,3 +85,3 @@ } | ||
this._model | ||
this.entity | ||
.find(findOptions, queryOptions) | ||
@@ -109,3 +109,3 @@ .then(function( model ) { | ||
this._model | ||
this.entity | ||
.findAll(findOptions, queryOptions) | ||
@@ -123,7 +123,7 @@ .then(this.callback(function(results) { | ||
create: function(modelData, queryOptions, callback) { | ||
var data = underscore.pick(modelData, Object.keys(this._model.attributes)); | ||
var data = underscore.pick(modelData, Object.keys(this.entity.attributes)); | ||
this.debug(util.format('ormUtils.create(%s)', Object.keys(data).join(', '))); | ||
this._model.create(data, queryOptions).then(this.callback(function(_model) { | ||
callback(null, new this(_model)); | ||
this.entity.create(data, queryOptions).then(this.callback(function(entity) { | ||
callback(null, new this(entity)); | ||
})) | ||
@@ -140,6 +140,13 @@ .catch(sequelize.UniqueConstraintError, this.callback(function(e) { | ||
update: function(values, queryOptions, callback) { | ||
this.entity | ||
.update(values, queryOptions) | ||
.then(callback.bind(null, null)) | ||
.catch(callback); | ||
}, | ||
save: function(data, queryOptions, callback) { | ||
this.debug('ormUtils.save()'); | ||
this._model | ||
this.entity | ||
.save(data, queryOptions) | ||
@@ -153,3 +160,3 @@ .then(callback.bind(null, null)) | ||
this._model | ||
this.entity | ||
.destroy(queryOptions) | ||
@@ -156,0 +163,0 @@ .then(callback.bind(null, null)) |
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
69817
1276