clever-odm
Advanced tools
Comparing version 0.0.2 to 1.0.0
{ | ||
"odm": { | ||
"uri": "localhost" | ||
"clever-odm": { | ||
"uri": "mongodb://localhost/nodeseed", | ||
"mongoose": { | ||
"debug": true | ||
} | ||
} | ||
} |
115
module.js
@@ -1,46 +0,105 @@ | ||
var mongoose = require( 'mongoose' ) | ||
, debug = require( 'debug' )( 'ODM' ) | ||
, modelInjector = require( 'utils' ).modelInjector | ||
, ModuleClass = require( 'classes' ).ModuleClass | ||
, Module; | ||
var mongoose = require( 'mongoose' ) | ||
, injector = require( 'injector' ) | ||
, Module = require( 'classes' ).Module; | ||
Module = ModuleClass.extend({ | ||
models: null, | ||
module.exports = new Module.extend({ | ||
models: {}, | ||
mongoose: mongoose, | ||
preSetup: function() { | ||
this.models = {}; | ||
this.debug( 'Opening database connection to Mongo ' + this.config.uri + '...' ); | ||
mongoose.connect( this.config.uri ); | ||
mongoose.set( 'debug', this.config.mongoose.debug ); | ||
}, | ||
preInit: function() { | ||
debug( 'Opening connection to database' ); | ||
// Connect to mongo | ||
mongoose.connect( this.config.uri ); | ||
this.debug( 'Adding mongoose to the injector...' ); | ||
// Add the mongoose instance to the injector | ||
injector.instance( 'mongoose', mongoose ); | ||
}, | ||
getModel: function( modelPath ) { | ||
var modelName = modelPath.split( '/' ).pop().split( '.' ).shift(); | ||
if ( typeof this.models[ modelName ] === 'undefined' ) { | ||
debug( [ 'Loading model', modelName, 'from', modelPath ].join( ' ' ) ); | ||
parseModelSchema: function( Static, Proto ) { | ||
var parseDebug = this.proxy(function( msg ) { | ||
this.debug( Static._name + 'Model: ' + msg ); | ||
}) | ||
, mongooseConf = {} | ||
, fields = {}; | ||
// Load (require) the model | ||
this.models[ modelName ] = require( modelPath )( mongoose ); | ||
// Add the model to the injector | ||
injector.instance( 'ODM' + modelName, this.models[ modelName ] ); | ||
if ( this.models[ Static._name ] !== undefined ) { | ||
parseDebug( 'Returning previously parsed, generated and cached model...' ); | ||
return this.models[ Static._name ]; | ||
} | ||
return this.models[ modelName ]; | ||
parseDebug( 'Parsing schema...' ); | ||
Object.keys( Static._schema ).forEach(function( name ) { | ||
var options = Static._schema[ name ] | ||
, fieldDefinition = {}; | ||
// If a type has been specified outside of the object, handle that | ||
if ( typeof options !== 'object' ) { | ||
options = { | ||
type: options | ||
} | ||
} | ||
// Figure out the type mapping for mongoosejs | ||
switch( options.type ) { | ||
case Number: | ||
if ( !!options.primaryKey ) { | ||
if ( name !== 'id' ) { | ||
throw new Error( [ 'You cannot have a primaryKey that is not called id with the ODM module.' ] ); | ||
} | ||
Static.primaryKey = name; | ||
mongooseConf.id = true; | ||
mongooseConf._id = true; | ||
fieldDefinition = mongoose.Schema.Types.ObjectId; | ||
break; | ||
} | ||
case String: | ||
case Boolean: | ||
case Date: | ||
fieldDefinition.type = options.type; | ||
// Handle options | ||
[ 'unique', 'required', 'default' ].forEach(function( optionName ) { | ||
if ( options[ optionName ] !== undefined ) { | ||
fieldDefinition[ optionName ] = options[ optionName ]; | ||
} | ||
}); | ||
break; | ||
case mongoose.Schema.Types.ObjectId: | ||
fieldDefinition = mongoose.Schema.Types.ObjectId; | ||
break; | ||
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; | ||
} | ||
if ( fieldDefinition !== null ) { | ||
fields[ name ] = fieldDefinition; | ||
} | ||
}); | ||
parseDebug( 'Set _db to mongoose...' ); | ||
Static._db = mongoose; | ||
parseDebug( 'Generating new native model using computed schema...' ); | ||
var model = mongoose.model( Static._name, new mongoose.Schema( fields, mongooseConf ) ); | ||
parseDebug( 'Caching completed model...' ); | ||
this.models[ Static._name ] = model; | ||
return model; | ||
}, | ||
preShutdown: function() { | ||
// Disconnect mongo | ||
this.debug( 'Closing database connection...' ); | ||
mongoose.disconnect(); | ||
} | ||
}); | ||
module.exports = new Module( 'odm', injector ); | ||
}); |
{ | ||
"name": "clever-odm", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
@@ -17,3 +17,3 @@ "mongoose": "~3.6.20" | ||
"collaborators": [ | ||
"Richard Gustin <richard@clevertech.biz>" | ||
"Richard Gustin <richard.gustin86@gmail.com>" | ||
], | ||
@@ -20,0 +20,0 @@ "license": "BSD-2-Clause", |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12795
251
1
0
0