node-entity
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -26,7 +26,2 @@ /** | ||
// stub internal methods, all should be private to instance | ||
this._mongFindOne = noop; | ||
this._mongFindById = noop; | ||
this._mongFind = noop; | ||
this._mongCount = noop; | ||
this._mongSave = noop; | ||
this._mongRemove = noop; | ||
@@ -58,5 +53,2 @@ }); | ||
MongooseAdapter.prototype._defineMethods = function() { | ||
this._mongFindOne = Promise.promisify(this.Model.findOne, this.Model); | ||
this._mongFindById = Promise.promisify(this.Model.findById, this.Model); | ||
this._mongFind = Promise.promisify(this.Model.find, this.Model); | ||
this._mongRemove = Promise.promisify(this.Model.remove, this.Model); | ||
@@ -92,17 +84,26 @@ }; | ||
var prom; | ||
if (__.isObject(id)) { | ||
prom = this._mongFindOne(id); | ||
} else { | ||
prom = this._mongFindById(id); | ||
} | ||
var self = this; | ||
return new Promise(function(resolve, reject) { | ||
var query; | ||
if (__.isObject(id)) { | ||
query = self.Model.findOne(id); | ||
} else { | ||
query = self.Model.findById(id); | ||
} | ||
// intercept "Cast to ID" error types and return null instead | ||
return prom.catch(function(err) { | ||
if (err.name === 'CastError' && err.type === 'ObjectId') { | ||
return null; | ||
} | ||
throw err; | ||
query = self._checkEagerLoad(query); | ||
// intercept "Cast to ID" error types and return null instead | ||
return query.exec(function(err, res) { | ||
if (err) { | ||
if (err.name === 'CastError' && err.type === 'ObjectId') { | ||
resolve(null); | ||
} else { | ||
reject(err); | ||
} | ||
return; | ||
} | ||
resolve(res); | ||
}); | ||
}); | ||
}; | ||
@@ -274,2 +275,4 @@ | ||
findMethod = this._checkEagerLoad(findMethod); | ||
return this._buildQuery(findMethod, fullQuery.selectors); | ||
@@ -330,1 +333,19 @@ }; | ||
}; | ||
/** | ||
* Checks if eager loading exists for any attribute and applies it. | ||
* | ||
* @param {mongoose.Query} query The query object. | ||
* @return {mongoose.Query} The query object. | ||
* @private | ||
*/ | ||
MongooseAdapter.prototype._checkEagerLoad = function(query) { | ||
if (!this._hasEagerLoad) { | ||
return query; | ||
} | ||
this._eagerLoad.forEach(function(attr) { | ||
query = query.populate(attr); | ||
}); | ||
return query; | ||
}; |
@@ -67,7 +67,11 @@ /** | ||
return Promise.cast(this.Model.find({ | ||
var findOpts = { | ||
where: query, | ||
offset: 0, | ||
limit: 1, | ||
})); | ||
}; | ||
findOpts = this._checkEagerLoad(findOpts); | ||
return Promise.cast(this.Model.find(findOpts)); | ||
}; | ||
@@ -89,2 +93,4 @@ | ||
findOpts = this._checkEagerLoad(findOpts); | ||
return Promise.cast(this.Model.findAll(findOpts)); | ||
@@ -112,2 +118,4 @@ }; | ||
findOpts = this._checkEagerLoad(findOpts); | ||
if (query) { | ||
@@ -238,1 +246,21 @@ findOpts.where = query; | ||
}; | ||
/** | ||
* Checks if eager loading exists for any attribute and applies it. | ||
* | ||
* @param {sequelize.Query} query The query object. | ||
* @return {sequelize.Query} The query object. | ||
* @private | ||
*/ | ||
SequelizeAdaptor.prototype._checkEagerLoad = function(query) { | ||
if (!this._hasEagerLoad) { | ||
return query; | ||
} | ||
query.include = []; | ||
this._eagerLoad.forEach(function(attr) { | ||
query.include.push(attr); | ||
}); | ||
return query; | ||
}; |
@@ -12,5 +12,2 @@ /*jshint camelcase:false */ | ||
// Load local tasks | ||
grunt.loadTasks('tasks'); | ||
grunt.loadNpmTasks('grunt-release'); | ||
@@ -17,0 +14,0 @@ grunt.loadNpmTasks('grunt-services'); |
@@ -25,2 +25,9 @@ /*jshint unused:false */ | ||
this._idName = 'id'; | ||
/** @type {Array} Attributes to eager load on queries */ | ||
this._eagerLoad = []; | ||
/** @type {boolean} Cached switch to let us know there are attrs to eager load */ | ||
this._hasEagerLoad = false; | ||
// Create primitive methods | ||
@@ -44,3 +51,17 @@ this.method('create', this._create.bind(this)); | ||
/** | ||
* Mongoose only method for now | ||
* | ||
* @param {string|Array|Object} values stuff to eager load | ||
*/ | ||
EntityCrud.prototype.eagerLoad = function(values) { | ||
this._hasEagerLoad = true; | ||
if (Array.isArray(values)) { | ||
this._eagerLoad = this._eagerLoad.concat(values); | ||
} else { | ||
this._eagerLoad.push(values); | ||
} | ||
}; | ||
/** | ||
@@ -47,0 +68,0 @@ * Create an entity item. |
{ | ||
"name": "node-entity", | ||
"description": "The MVCe implementation for Node.js", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"homepage": "https://github.com/thanpolas/entity", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -475,4 +475,6 @@ # Entity | ||
- **v0.2.8**, *03 Jul 2014* | ||
- Introduce eager loading mechanisms. | ||
- **v0.2.7**, *01 Jul 2014* | ||
- Mute mongoose cast to ID errors | ||
- Mute mongoose cast to ID errors. | ||
- **v0.2.6**, *27 Jun 2014* | ||
@@ -479,0 +481,0 @@ - support more complex queries using `between` selector. |
@@ -20,2 +20,14 @@ /** | ||
/** | ||
* Define the mong schema related to the original | ||
* @type {Object} | ||
*/ | ||
mong.SchemaRel = { | ||
darname: {type: String}, | ||
schem: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'stubModel', | ||
}, | ||
}; | ||
/** @type {?Mongoose.Model} */ | ||
@@ -30,6 +42,9 @@ mong.Model = null; | ||
mong._initModels = function() { | ||
// init schema | ||
// init schemas | ||
mong.schema = new mongoose.Schema(mong.Schema); | ||
// init model | ||
mong.schemaRel = new mongoose.Schema(mong.SchemaRel); | ||
// init models | ||
mong.Model = mongoose.model('stubModel', mong.schema); | ||
mong.ModelRel = mongoose.model('stubModelRel', mong.schemaRel); | ||
}; | ||
@@ -36,0 +51,0 @@ |
@@ -21,2 +21,10 @@ /** | ||
/** | ||
* Define the seq schema relation | ||
* @type {Object} | ||
*/ | ||
seq.SchemaRel = { | ||
darname: {type: Sequelize.STRING, allowNull: false}, | ||
}; | ||
/** @type {?Sequelize} The main Sequelize instance */ | ||
@@ -28,2 +36,5 @@ seq.instance = null; | ||
/** @type {?Sequelize.Model} The sequelize model related */ | ||
seq.ModelRel = null; | ||
/** | ||
@@ -59,2 +70,4 @@ * Create a connection with Postgres and init models. | ||
seq.Model = seq.instance.define('stubModel', seq.Schema); | ||
seq.ModelRel = seq.instance.define('stubModelRel', seq.SchemaRel); | ||
seq.Model.hasMany(seq.ModelRel); | ||
@@ -61,0 +74,0 @@ seq.nukedb(done); |
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
81312
1915
516