Entities provide object-oriented abstractions for data and allow for extending functionality through inheritance and middleware hooks. Key features include:
- Extending from EventEmitter for event handling
- CRUD methods like create, read, update, and delete
- Before, after, and last hooks for adding middleware like validation
- Ability to extend entities for additional methods and inheritance
- Options to connect entities to data sources like MongoDB through adapters
Entities aim to abstract data access, inheritance, and middleware handling for building robust data models and APIs.
3. Entities useEntities use
CIP
for Classical Inheritance
/thanpolas/cip
Bluebird
for the 100% Promises API
/petkaantonov/bluebird
Middlewarify
for creating middleware
/thanpolas/middlewarify
10. Entity HooksEntity Hooks
beforebefore
// a middleware with synchronous resolution
entity.read.before(function(data){
if (!data.name) {
throw new TypeError('No go my friend');
}
});
// then...
entity.read({}).then(function(document) {
// you'll never get here
}, function(err) {
err instanceof Error; // true
err.message === 'No go my friend'; // true
});
13. Before HooksBefore Hooks
Get the exact same number or arguments
After & Last HooksAfter & Last Hooks
Gets the result plus the original number or arguments
16. Extending EntitiesExtending Entities
Just use the prototypeJust use the prototype
var Entity = require('entity');
var UserEntity = module.exports = Entity.extend();
UserEntity.prototype.report = function(userId) {
return promiseReturningAction(userId);
};
17. Extending EntitiesExtending Entities
Using method()Using method()
var Entity = require('entity');
var UserEntity = module.exports = Entity.extend(function() {
this.method('report', this._report.bind(this));
this.report.before(this._checkUserId.bind(this));
this.report.after(this._normalize.bind(this));
});
UserEntity.prototype._report = function(userId) {
return promiseReturningAction(userId);
};