Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clever-orm

Package Overview
Dependencies
Maintainers
3
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clever-orm - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

.travis.yml

59

bin/rebase.js
var injector = require( 'injector' )
, fs = require( 'fs' )
, utils = require( 'utils' )
, async = require( 'async' )
, config = require( 'config' )
, path = require( 'path' )
, ormUtils = require( path.resolve( path.join( __dirname, '..', 'lib', 'utils.js' ) ) )
, env = utils.bootstrapEnv()
, moduleLdr = env.moduleLoader
, moduleName = process.argv && process.argv[ 2 ] != 'null' ? process.argv[ 2 ] : false;
, moduleLdr = env.moduleLoader;
// Rebase once our modules have loaded
moduleLdr.on( 'modulesLoaded', function() {
var sequelize = injector.getInstance( 'sequelize' );
console.log('Forcing Database to be created! (Note: All your data will disapear!)');
async.waterfall(
[
function createDatabase( callback ) {
var query = 'CREATE DATABASE ' + ( config[ 'clever-orm' ].db.options.dialect === 'mysql' ? 'IF NOT EXISTS ' : '' ) + config[ 'clever-orm' ].db.database;
// Force a sync
injector.getInstance( 'sequelize' )
.sync( { force: true } )
.success(function () {
console.log( 'Database is rebased' );
// @TODO implement dialect specific SQL running after rebase (triggers etc)
sequelize.query( query, { raw: true } )
.success( function() {
callback( null );
})
.error( callback );
},
env.moduleLoader.shutdown();
})
.error(function( err ) {
console.error('Error ' + env.config['clever-orm'].db.options.dialect, err);
env.moduleLoader.shutdown();
});
function rebaseDatabase( callback ) {
sequelize
.sync( { force: true } )
.success( function() {
callback( null );
})
.error( callback );
}
],
function shutdown( err ) {
if ( err === null ) {
console.log( 'Database is rebased' );
env.moduleLoader.shutdown();
} else {
console.error('Error ' + env.config['clever-orm'].db.options.dialect, err);
env.moduleLoader.shutdown();
}
}
);
});
// Allow loading only one model at a time to rebase
if ( moduleName ) {
env.packageJson.bundledDependencies.length = 0
env.packageJson.bundledDependencies.push( 'clever-orm', env );
env.packageJson.bundledDependencies.push( moduleName, env );
}
ormUtils.supportSingleModule( env, process.argv && process.argv[ 2 ] != 'null' ? process.argv[ 2 ] : false );
// Load
moduleLdr.loadModules();

@@ -1,5 +0,3 @@

var injector = require( 'injector' )
, fs = require( 'fs' )
, path = require( 'path' )
, crypto = require( 'crypto' )
var path = require( 'path' )
, ormUtils = require( path.resolve( path.join( __dirname, '..', 'lib', 'utils.js' ) ) )
, async = require( 'async' )

@@ -9,4 +7,3 @@ , utils = require( 'utils' )

, moduleLdr = env.moduleLoader
, inflect = require( 'i' )()
, moduleName = process.argv && process.argv[ 2 ] != 'null' ? process.argv[ 2 ] : false;
, inflect = require( 'i' )();

@@ -19,11 +16,11 @@ // Seed once our modules have loaded

var assocMap = {};
Object.keys(seedData).forEach(function( modelName ) {
assocMap[modelName] = [];
Object.keys( seedData ).forEach(function( modelName ) {
assocMap[ modelName ] = [];
});
async.forEachSeries(
Object.keys(seedData),
Object.keys( seedData ),
function forEachModelType( modelName, cb ) {
var ModelType = models[ modelName.replace( 'Model', '' ) ]
, Models = seedData[modelName];
, Models = seedData[ modelName ];

@@ -40,61 +37,66 @@ if ( !ModelType || !Models ) {

ModelType.create(data).then(function( model ) {
data.associations = assocs;
ModelType.create( data )
.then(function( model ) {
data.associations = assocs;
console.log('Created ' + modelName);
assocMap[modelName].push(model);
if ( data.associations !== undefined ) {
var assocLength = Object.keys(data.associations).length,
called = 0;
console.log( 'Created ' + modelName );
assocMap[ modelName ].push( model );
if ( data.associations !== undefined ) {
var assocLength = Object.keys( data.associations ).length
, called = 0;
Object.keys(data.associations).forEach(function( assocModelName ) {
var required = data.associations[assocModelName]
, associations = [];
Object.keys( data.associations ).forEach( function( assocModelName ) {
var required = data.associations[ assocModelName ]
, associations = [];
assocMap[assocModelName].forEach(function( m ) {
var isMatched = null;
assocMap[ assocModelName ].forEach( function( m ) {
var isMatched = null;
Object.keys(required).forEach(function( reqKey ) {
if ( isMatched !== false ) {
if ( m[reqKey] === required[reqKey] ) {
isMatched = true;
} else {
isMatched = false;
Object.keys( required ).forEach( function( reqKey ) {
if ( isMatched !== false ) {
if ( m[ reqKey ] === required[ reqKey ] ) {
isMatched = true;
} else {
isMatched = false;
}
}
});
if ( isMatched ) {
associations.push( m );
}
});
if ( isMatched ) {
associations.push(m);
}
});
if ( associations.length ) {
var funcName = 'set' + inflect.pluralize( assocModelName );
if ( associations.length ) {
var funcName = 'set' + inflect.pluralize(assocModelName);
// 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[funcName] !== 'function' ) {
funcName = 'set' + assocModelName;
associations = associations[0];
console.log( 'Calling ' + funcName );
model[ funcName ]( associations )
.success(function() {
called++;
if ( called == assocLength ) {
modelCb( null );
}
})
.error( modelCb );
}
// strip "Model" from funcName
funcName = funcName.replace(/(Model)$/g,'');
console.log('Calling ' + funcName);
model[funcName](associations).success(function() {
called++;
if ( called == assocLength )
modelCb(null);
}).error(modelCb);
}
});
} else {
modelCb(null);
}
}).catch(modelCb);
});
} else {
modelCb( null );
}
})
.catch( modelCb );
},
function forEachModelComplete( err ) {
cb(err);
cb( err );
}

@@ -104,3 +106,3 @@ );

function forEachModelTypeComplete( err ) {
console.log(err ? 'Error: ' : 'Seed completed with no errors', err);
console.log( err ? 'Error: ' : 'Seed completed with no errors', err );
env.moduleLoader.shutdown();

@@ -111,10 +113,5 @@ }

// Allow loading only one model at a time to rebase
if ( moduleName ) {
env.packageJson.bundledDependencies.length = 0
env.packageJson.bundledDependencies.push( 'clever-orm', env );
env.packageJson.bundledDependencies.push( moduleName, env );
}
ormUtils.supportSingleModule( env, process.argv && process.argv[ 2 ] != 'null' ? process.argv[ 2 ] : false );
// Load
moduleLdr.loadModules();

@@ -63,3 +63,3 @@ var injector = require( 'injector' )

parseModelSchema: function( Static, Proto ) {
parseModelSchema: function( Static ) {
var parseDebug = this.proxy(function( msg ) {

@@ -130,72 +130,99 @@ this.debug( Static._name + 'Model: ' + msg );

getFieldType: function( Static, options ) {
var field;
switch( options.type ) {
case Number:
var integer = !!options.length ? Sequelize.INTEGER( options.length ) : Sequelize.INTEGER;
if ( !!options.unsigned && !!options.zerofill ) {
return integer.UNSIGNED.ZEROFILL;
} else if ( !!options.unsigned && !options.zerofill ) {
return integer.UNSIGNED;
} else if ( !options.unsigned && !!options.zerofill ) {
return integer.ZEROFILL;
} else {
return integer;
}
case String:
return Sequelize.STRING;
case Boolean:
return Sequelize.BOOLEAN;
case Date:
return Sequelize.DATE;
case Array:
return options.of ? Sequelize.ARRAY( this.getFieldType( Static, { type: options.of } ) ) : Sequelize.ARRAY( Sequelize.STRING );
case Buffer:
return Sequelize.STRING.BINARY;
case Model.Types.ENUM:
return Sequelize.ENUM( options.values );
case Model.Types.BIGINT:
var bigint = !!options.length ? Sequelize.BIGINT( options.length ) : Sequelize.BIGINT;
if ( !!options.unsigned && !!options.zerofill ) {
return bigint.UNSIGNED.ZEROFILL;
} else if ( !!options.unsigned && !options.zerofill ) {
return bigint.UNSIGNED;
} else if ( !options.unsigned && !!options.zerofill ) {
return bigint.ZEROFILL;
} else {
return bigint;
}
case Model.Types.FLOAT:
var float = Sequelize.FLOAT;
if ( !!options.decimals ) {
float = Sequelize.FLOAT( options.length, options.decimals );
} else if ( !!options.length ) {
float = Sequelize.FLOAT( options.length );
}
if ( !!options.unsigned && !!options.zerofill ) {
return float.UNSIGNED.ZEROFILL;
} else if ( !!options.unsigned && !options.zerofill ) {
return float.UNSIGNED;
} else if ( !options.unsigned && !!options.zerofill ) {
return float.ZEROFILL;
} else {
return float;
}
case Model.Types.DECIMAL:
if ( !!options.scale ) {
return Sequelize.DECIMAL( options.precision, options.scale );
} else if ( !!options.precision ) {
return Sequelize.DECIMAL( options.precision );
} else {
return Sequelize.DECIMAL;
}
case Model.Types.TEXT:
return Sequelize.TEXT;
case undefined:
throw new Error( [ 'You must define the type of field that', '"' + name + '"', 'is on the', '"' + Static.name + '" model' ].join( ' ' ) );
break;
default:
throw new Error( [ 'You must define a valid type for the field named', '"' + name + '"', 'on the', '"' + Static.name + '" model' ].join( ' ' ) );
break;
case Number:
field = this.numberType( options );
break;
case String:
field = Sequelize.STRING;
break;
case Boolean:
field = Sequelize.BOOLEAN;
break;
case Date:
field = Sequelize.DATE;
break;
case Array:
field = options.of ? Sequelize.ARRAY( this.getFieldType( Static, { type: options.of } ) ) : Sequelize.ARRAY( Sequelize.STRING );
break;
case Buffer:
field = Sequelize.STRING.BINARY;
break;
case Model.Types.ENUM:
field = Sequelize.ENUM( options.values );
break;
case Model.Types.BIGINT:
field = this.bigIntType( options );
break;
case Model.Types.FLOAT:
field = this.floatType( options );
break;
case Model.Types.DECIMAL:
field = this.decimalType( options );
break;
case Model.Types.TEXT:
field = Sequelize.TEXT;
break;
case undefined:
throw new Error( [ 'You must define the type of field that', '"' + name + '"', 'is on the', '"' + Static.name + '" model' ].join( ' ' ) );
default:
throw new Error( [ 'You must define a valid type for the field named', '"' + name + '"', 'on the', '"' + Static.name + '" model' ].join( ' ' ) );
}
return field;
},
numberType: function( options ) {
var field = !!options.length ? Sequelize.INTEGER( options.length ) : Sequelize.INTEGER;
if ( !!options.unsigned && !!options.zerofill ) {
field = field.UNSIGNED.ZEROFILL;
} else if ( !!options.unsigned && !options.zerofill ) {
field = field.UNSIGNED;
} else if ( !options.unsigned && !!options.zerofill ) {
field = field.ZEROFILL;
}
return field;
},
bigIntType: function( options ) {
var field = !!options.length ? Sequelize.BIGINT( options.length ) : Sequelize.BIGINT;
if ( !!options.unsigned && !!options.zerofill ) {
field = bigint.UNSIGNED.ZEROFILL;
} else if ( !!options.unsigned && !options.zerofill ) {
field = bigint.UNSIGNED;
} else if ( !options.unsigned && !!options.zerofill ) {
field = bigint.ZEROFILL;
}
return field;
},
floatType: function( options ) {
var field = Sequelize.FLOAT;
if ( !!options.decimals ) {
field = Sequelize.FLOAT( options.length, options.decimals );
} else if ( !!options.length ) {
field = Sequelize.FLOAT( options.length );
}
if ( !!options.unsigned && !!options.zerofill ) {
field = field.UNSIGNED.ZEROFILL;
} else if ( !!options.unsigned && !options.zerofill ) {
field = field.UNSIGNED;
} else if ( !options.unsigned && !!options.zerofill ) {
field = field.ZEROFILL;
}
return field;
},
decimalType: function( options ) {
var field = Sequelize.DECIMAL;
if ( !!options.scale ) {
field = Sequelize.DECIMAL( options.precision, options.scale );
} else if ( !!options.precision ) {
field = Sequelize.DECIMAL( options.precision );
}
return field;
}
});
{
"name": "clever-orm",
"version": "1.0.4",
"version": "1.0.5",
"dependencies": {

@@ -8,2 +8,5 @@ "mysql": "2.0.0-rc2",

},
"scripts": {
"test": "grunt test:unit"
},
"author": {

@@ -30,3 +33,3 @@ "name": "CleverStack",

"grunt": "~0.4.2",
"grunt-prompt": "~0.2.0"
"grunt-prompt": "~1.1.0"
},

@@ -33,0 +36,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc