Socket
Socket
Sign inDemoInstall

clever-odm

Package Overview
Dependencies
11
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 1.0.0

7

config/default.json
{
"odm": {
"uri": "localhost"
"clever-odm": {
"uri": "mongodb://localhost/nodeseed",
"mongoose": {
"debug": true
}
}
}

@@ -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",

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc