Skip to content

Commit

Permalink
Merge pull request #84 from CleverStack/feature/model-field-expansion
Browse files Browse the repository at this point in the history
Feature/model field expansion
  • Loading branch information
pilsy committed Jun 15, 2014
2 parents 65e9e8f + 39743a1 commit 1ec71d8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
97 changes: 89 additions & 8 deletions lib/classes/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var injector = require( 'injector' )
, moduleLdr = injector.getInstance( 'moduleLoader' )
, models = {};

module.exports = Class.extend(
var Model = Class.extend(
/* @Static */
{
// Either 'ORM' or 'ODM'
Expand All @@ -23,6 +23,65 @@ module.exports = Class.extend(
versionable: false,
timeStampable: true,

Types: {
ENUM: function() {
return {
type: this.ENUM,
values: [].slice.call( arguments ),
toString: function() {
return this.type;
}
}
},
BIGINT: function( length ) {
return {
type: this.BIGINT,
length: length,
toString: function() {
return this.type;
}
}
},
FLOAT: function( length, decimals ) {
var field = {
type: this.FLOAT,
length: length,
toString: function() {
return this.type;
}
}

if ( decimals !== undefined ) {
field.decimals = decimals;
}

return field;
},
DECIMAL: function( precision, scale ) {
var field = {
type: this.DECIMAL,
precision: precision,
toString: function() {
return this.type;
}
}

if ( scale !== undefined ) {
field.scale = scale;
}

return field;
},
TEXT: function() {
return {
type: this.TEXT,
toString: function() {
return this.type;
}
}
}
},

// The function you call to create a new model
extend: function() {
var extendingArgs = [].slice.call( arguments )
Expand Down Expand Up @@ -70,11 +129,6 @@ module.exports = Class.extend(
Static._driver = driver = injector.getInstance( modelType.toLowerCase() === 'orm' ? 'cleverOrm' : 'cleverOdm' );
}

debug( 'Defining models this.debug() helper...' );
Proto.debug = Static.debug = function( msg ) {
driver.debug( modelName + 'Model: ' + msg );
};

debug( 'Checking for defined getters and setters...' );

if ( Proto.getters !== undefined ) {
Expand All @@ -90,6 +144,11 @@ module.exports = Class.extend(
debug( 'Defining schema...' );
Object.keys( Proto ).forEach( this.callback( 'getSchemaFromProto', Proto, Static ) );

debug( 'Defining models this.debug() helper...' );
Proto.debug = Static.debug = function( msg ) {
driver.debug( modelName + 'Model: ' + msg );
};

debug( 'Setting up behaviours...' );
[ 'softDeletable', 'versionable', 'timeStampable' ].forEach(function( behaviour ) {

Expand Down Expand Up @@ -131,7 +190,7 @@ module.exports = Class.extend(
getSchemaFromProto: function( Proto, Static, key ) {
var prop = Proto[ key ];

if ( prop === Date || prop === String || prop instanceof String || prop === Number || prop === Boolean || ( typeof prop === 'object' ) ) {
if ( key !== 'defaults' ) {
if ( typeof Static._schema !== 'object' ) {
Static._schema = {};
}
Expand Down Expand Up @@ -498,4 +557,26 @@ module.exports = Class.extend(

return json;
}
});
});

Model.Types.ENUM.toString = function() {
return 'ENUM';
};

Model.Types.BIGINT.toString = function() {
return 'BIGINT';
};

Model.Types.FLOAT.toString = function() {
return 'FLOAT';
};

Model.Types.DECIMAL.toString = function() {
return 'DECIMAL';
};

Model.Types.TEXT.toString = function() {
return 'TEXT';
};

module.exports = Model;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "app.js",
"name": "node-seed",
"description": "Cleverstack Node-Seed",
"version": "1.0.3",
"version": "1.0.4",
"author": {
"name": "CleverStack",
"email": "[email protected]",
Expand Down Expand Up @@ -56,4 +56,4 @@
"supertest": ""
},
"bundledDependencies": []
}
}

0 comments on commit 1ec71d8

Please sign in to comment.