2013-04-10 17:55:13 +00:00
|
|
|
/**
|
|
|
|
* Expose `Model`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = Model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter
|
2013-04-10 17:55:13 +00:00
|
|
|
, debug = require('debug')('model')
|
|
|
|
, util = require('util')
|
|
|
|
, inherits = util.inherits
|
|
|
|
, assert = require('assert');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new `Model` with the given `options`.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Model}
|
|
|
|
*/
|
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
function Model(data) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
|
|
|
|
var ModelCtor = this.constructor;
|
|
|
|
var schema = ModelCtor.schema;
|
|
|
|
|
|
|
|
// get properties that match the schema
|
|
|
|
var matchedProperties = schema.getMatchedProperties(data);
|
|
|
|
|
|
|
|
// set properties that match the schema
|
|
|
|
Object.keys(matchedProperties).forEach(function (property) {
|
|
|
|
this[property] = matchedProperties[property];
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit from `EventEmitter`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inherits(Model, EventEmitter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Model class from the given options.
|
|
|
|
*
|
|
|
|
* @param options {Object}
|
|
|
|
* @return {Model}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Model.extend = function (options) {
|
|
|
|
var Super = this;
|
|
|
|
|
|
|
|
// the new constructor
|
|
|
|
function Model() {
|
|
|
|
Super.apply(this, arguments);
|
|
|
|
}
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
Model.options = options;
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
assert(options.name, 'must provide a name when extending from model');
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
// model namespace
|
|
|
|
Model.namespace = options.name;
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
// define the remote namespace
|
|
|
|
Model.remoteNamespace = options.plural || pluralize(Model.namespace);
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
// inherit all static methods
|
|
|
|
Object.keys(Super).forEach(function (key) {
|
|
|
|
if(typeof Super[key] === 'function') {
|
|
|
|
MyModel[key] = Super[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// inherit all other things
|
|
|
|
inherits(MyModel, Super);
|
|
|
|
|
|
|
|
return Model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a model instance for remote use.
|
|
|
|
*/
|
|
|
|
|
2013-05-24 14:59:23 +00:00
|
|
|
Model.sharedCtor = function (data, fn) {
|
2013-05-01 19:11:43 +00:00
|
|
|
var ModelCtor = this;
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
fn(null, new ModelCtor(data));
|
2013-04-10 17:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-01 19:11:43 +00:00
|
|
|
* Define the data the model represents using the data definition language.
|
2013-04-10 17:55:13 +00:00
|
|
|
*/
|
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
Model.defineSchema = function (schema) {
|
|
|
|
throw new Error('not implemented');
|
|
|
|
}
|
2013-04-10 17:55:13 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-01 19:11:43 +00:00
|
|
|
* Set the data source for the model. Must provide a name of an existing data source.
|
|
|
|
* If the namespace is not provided the plural model name (eg. colors) will be used.
|
|
|
|
*
|
|
|
|
* **Note:** If you do not set a data source or a map (or both) the default data source
|
|
|
|
* will be used (an in memory database).
|
2013-04-10 17:55:13 +00:00
|
|
|
*/
|
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
Model.dataSource = function (dataSourceName, namespace) {
|
|
|
|
namespace = namespace || this.namespace;
|
|
|
|
throw new Error('not implemented');
|
|
|
|
}
|
2013-04-10 17:55:13 +00:00
|
|
|
|
2013-04-19 16:51:06 +00:00
|
|
|
/**
|
2013-05-01 19:11:43 +00:00
|
|
|
* Define a mapping between the data source representation of your data and your app's representation.
|
2013-04-19 16:51:06 +00:00
|
|
|
*/
|
|
|
|
|
2013-05-01 19:11:43 +00:00
|
|
|
Model.defineMap = function (mapping) {
|
|
|
|
// see: https://github.com/strongloop/asteroid/tree/master/node_modules/model#mymodeldefinemapmap
|
|
|
|
throw new Error('not implemented');
|
2013-05-24 14:59:23 +00:00
|
|
|
}
|