node-entity
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -157,1 +157,76 @@ /** | ||
}; | ||
/** | ||
* Get the normalized schema of this Entity. | ||
* | ||
* @return {mschema} An mschema struct. | ||
*/ | ||
Entity.prototype.getSchema = function() { | ||
if (this._schema) { | ||
return this._schema; | ||
} | ||
var mongooseSchema = this.Model.schema.paths; | ||
this._schema = []; | ||
__.forIn(mongooseSchema, function(mongSchemaItem, path) { | ||
var schemaItem = { | ||
// TODO this helper needs a second arg, opt to check for expandedPaths key. | ||
canShow: this._canShow(mongSchemaItem), | ||
name: this._getName(path, this.opts), | ||
path: path, | ||
}; | ||
this._schema.push(schemaItem); | ||
}, this); | ||
return this._schema; | ||
}; | ||
/** | ||
* Return a proper label for the key. | ||
* | ||
* @param {string} path The full path name. | ||
* @param {Object} optOpts The CRUD-controller options object. | ||
* @return {string} The field's name. | ||
* @private | ||
*/ | ||
Entity.prototype._getName = function(path, optOpts) { | ||
var opts = optOpts || {}; | ||
var name; | ||
if (opts.expandPaths) { | ||
name = path; | ||
} else { | ||
name = path.split('.').pop(); | ||
} | ||
return name; | ||
}; | ||
/** | ||
* Determine if this schema item should be publicly displayed. | ||
* | ||
* @param {Object} schemaItem A single schema item (a column). | ||
* @param {Object=} optOpts The CRUD-controller options object. | ||
* @return {boolean} true to show. | ||
* @private | ||
*/ | ||
Entity.prototype._canShow = function(schemaItem, optOpts) { | ||
var opts = optOpts || {}; | ||
// check for custom excluded paths | ||
if (opts.viewExcludePaths && opts.viewExcludePaths.length) { | ||
if (0 <= opts.viewExcludePaths.indexOf(schemaItem.path)) { | ||
return false; | ||
} | ||
} | ||
// check for private vars (starting with underscore) | ||
if ('_' === schemaItem.path.charAt(0)) { | ||
if (opts.showId && '_id' === schemaItem.path) { | ||
return true; | ||
} | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}; |
@@ -7,2 +7,3 @@ /** | ||
var __ = require('lodash'); | ||
var Driver = require('./base.drv'); | ||
@@ -190,1 +191,50 @@ | ||
}; | ||
/** | ||
* Get the normalized schema of this Entity. | ||
* | ||
* @return {mschema} An mschema struct. | ||
*/ | ||
Entity.prototype.getSchema = function() { | ||
if (this._schema) { | ||
return this._schema; | ||
} | ||
var seqSchema = this.Model.rawAttributes; | ||
this._schema = []; | ||
__.forIn(seqSchema, function(seqSchemaItem, path) { | ||
var schemaItem = { | ||
canShow: this._canShow(path), | ||
name: path, | ||
path: path, | ||
}; | ||
this._schema.push(schemaItem); | ||
}, this); | ||
return this._schema; | ||
}; | ||
/** | ||
* Determine if this schema item should be publicly displayed. | ||
* | ||
* @param {string} path A single schema path (a key). | ||
* @return {boolean} true to show. | ||
* @private | ||
*/ | ||
Entity.prototype._canShow = function(path) { | ||
// check for private vars (starting with underscore) | ||
if ('_' === path.charAt(0)) { | ||
return false; | ||
} | ||
// check for known Sequelize variables | ||
if ([ | ||
'createdAt', | ||
'updatedAt', | ||
].indexOf(path) > -1) { | ||
return false; | ||
} | ||
return true; | ||
}; |
@@ -20,3 +20,14 @@ /*jshint unused:false */ | ||
/** @type {mschema?} Cached version of Entity's schema */ | ||
this._schema = null; | ||
}; | ||
util.inherits(Entity, EventEmitter); | ||
/** | ||
* A normalized format for fetching the Entity's schema. | ||
* | ||
* @return {Object} | ||
*/ | ||
Entity.prototype.getSchema = function() { | ||
throw new Error('Not Implemented'); | ||
}; |
{ | ||
"name": "node-entity", | ||
"description": "The MVCe implementation for Node.js", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"homepage": "https://github.com/thanpolas/entity", | ||
@@ -34,3 +34,4 @@ "author": { | ||
"lodash": "~1.3.1", | ||
"middlewarify": "~0.0.4" | ||
"middlewarify": "~0.0.4", | ||
"mschema": "~0.5.2" | ||
}, | ||
@@ -43,3 +44,5 @@ "devDependencies": { | ||
"sequelize": "~2.0.0-beta.0", | ||
"pg": "~2.7.0" | ||
"pg": "~2.7.0", | ||
"grunt": "~0.4.2", | ||
"grunt-release": "~0.6.0" | ||
}, | ||
@@ -46,0 +49,0 @@ "peerDependencies": {}, |
@@ -9,3 +9,3 @@ /** | ||
name: 'Oh Long Johnson', | ||
isActive: true, | ||
_isActive: true, | ||
}; | ||
@@ -15,3 +15,3 @@ | ||
name: 'once upon a time', | ||
isActive: false, | ||
_isActive: false, | ||
}; |
@@ -17,3 +17,3 @@ /** | ||
name: {type: String, trim: true, required: true}, | ||
isActive: {type: Boolean, required: true, default: true}, | ||
_isActive: {type: Boolean, required: true, default: true}, | ||
}; | ||
@@ -20,0 +20,0 @@ |
@@ -5,3 +5,2 @@ /** | ||
var exec = require('child_process').exec; | ||
var util = require('util'); | ||
@@ -20,3 +19,3 @@ var Sequelize = require('sequelize'); | ||
name: {type: Sequelize.STRING, allowNull: false}, | ||
isActive: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true}, | ||
_isActive: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true}, | ||
}; | ||
@@ -23,0 +22,0 @@ |
@@ -14,2 +14,3 @@ /** | ||
var testDriverDelete = require('./drivers-delete.test'); | ||
var testDriverSchema = require('./drivers-schema.test'); | ||
@@ -72,3 +73,3 @@ var core = module.exports = {}; | ||
testDriverDelete(driver, driver.majNum); | ||
testDriverSchema(driver, driver.majNum); | ||
}); | ||
@@ -75,0 +76,0 @@ }); |
@@ -32,3 +32,3 @@ /** | ||
assert.equal(data.name, fix.one.name, 'Name should be the same'); | ||
assert.equal(data.isActive, fix.one.isActive, 'isActive should be the same'); | ||
assert.equal(data._isActive, fix.one._isActive, 'isActive should be the same'); | ||
done(); | ||
@@ -35,0 +35,0 @@ }); |
@@ -22,3 +22,3 @@ /** | ||
suite(majNum + '.7 Instance integridy', function() { | ||
suite(majNum + '.7 Instance integrity', function() { | ||
test(majNum + '.7.1 Will throw error if not proper Model provided', function() { | ||
@@ -25,0 +25,0 @@ function factory() { |
@@ -36,2 +36,3 @@ /** | ||
test(majNum + '.1.1 Primitive Methods', function(){ | ||
assert.isFunction(ent.getSchema, 'Entity should have a "getSchema" method'); | ||
assert.isFunction(ent.create, 'Entity should have a "create" method'); | ||
@@ -38,0 +39,0 @@ assert.isFunction(ent.read, 'Entity should have a "read" method'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39922
26
1211
3
8
+ Addedmschema@~0.5.2
+ Addedmschema@0.5.6(transitive)