From ea5b9d16fc50d733b7aab705aba51e504f13bdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 5 Jun 2014 09:45:09 +0200 Subject: [PATCH] Rename DataModel to PersistedModel --- example/client-server/models.js | 2 +- lib/loopback.js | 31 +++- lib/models/acl.js | 2 +- lib/models/application.js | 2 +- lib/models/change.js | 14 +- lib/models/checkpoint.js | 6 +- .../{data-model.js => persisted-model.js} | 162 +++++++++--------- lib/models/user.js | 6 +- test/access-token.test.js | 2 +- test/acl.test.js | 2 +- test/app.test.js | 10 +- test/change.test.js | 2 +- test/data-source.test.js | 4 +- test/fixtures/e2e/models.js | 4 +- test/hidden-properties.test.js | 4 +- test/model.test.js | 10 +- test/remote-connector.test.js | 4 +- test/replication.test.js | 6 +- test/util/model-tests.js | 10 +- 19 files changed, 154 insertions(+), 129 deletions(-) rename lib/models/{data-model.js => persisted-model.js} (84%) diff --git a/example/client-server/models.js b/example/client-server/models.js index c14485c8..f6dc594b 100644 --- a/example/client-server/models.js +++ b/example/client-server/models.js @@ -1,6 +1,6 @@ var loopback = require('../../'); -var CartItem = exports.CartItem = loopback.DataModel.extend('CartItem', { +var CartItem = exports.CartItem = loopback.PersistedModel.extend('CartItem', { tax: {type: Number, default: 0.1}, price: Number, item: String, diff --git a/lib/loopback.js b/lib/loopback.js index 4f9859ca..6e10d4fa 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -178,10 +178,22 @@ loopback.createModel = function (name, properties, options) { var BaseModel = options.base || options.super; if(typeof BaseModel === 'string') { + var baseName = BaseModel; BaseModel = loopback.getModel(BaseModel); + + if (BaseModel === undefined) { + if (baseName === 'DataModel') { + console.warn('Model `%s` is extending deprecated `DataModel. ' + + 'Use `PeristedModel` instead.', name); + BaseModel = loopback.PersistedModel; + } else { + console.warn('Model `%s` is extending an unknown model `%s`. ' + + 'Using `PersistedModel` as the base.', name, baseName); + } + } } - BaseModel = BaseModel || loopback.DataModel; + BaseModel = BaseModel || loopback.PersistedModel; var model = BaseModel.extend(name, properties, options); @@ -337,7 +349,20 @@ loopback.autoAttachModel = function(ModelCtor) { */ loopback.Model = require('./models/model'); -loopback.DataModel = require('./models/data-model'); +loopback.PersistedModel = require('./models/persisted-model'); + +// temporary alias to simplify migration of code based on <=2.0.0-beta3 +Object.defineProperty(loopback, 'DataModel', { + get: function() { + var stackLines = new Error().stack.split('\n'); + console.warn('loopback.DataModel is deprecated, ' + + 'use loopback.PersistedModel instead.'); + // Log the location where loopback.DataModel was called + console.warn(stackLines[2]); + return loopback.PersistedModel; + } +}); + loopback.Email = require('./models/email'); loopback.User = require('./models/user'); loopback.Application = require('./models/application'); @@ -358,7 +383,7 @@ var dataSourceTypes = { }; loopback.Email.autoAttach = dataSourceTypes.MAIL; -loopback.DataModel.autoAttach = dataSourceTypes.DB; +loopback.PersistedModel.autoAttach = dataSourceTypes.DB; loopback.User.autoAttach = dataSourceTypes.DB; loopback.AccessToken.autoAttach = dataSourceTypes.DB; loopback.Role.autoAttach = dataSourceTypes.DB; diff --git a/lib/models/acl.js b/lib/models/acl.js index 6c2456bb..7dc1d03c 100644 --- a/lib/models/acl.js +++ b/lib/models/acl.js @@ -91,7 +91,7 @@ var ACLSchema = { * @inherits Model */ -var ACL = loopback.DataModel.extend('ACL', ACLSchema); +var ACL = loopback.PersistedModel.extend('ACL', ACLSchema); ACL.ALL = AccessContext.ALL; diff --git a/lib/models/application.js b/lib/models/application.js index 8d70f47e..1201fca3 100644 --- a/lib/models/application.js +++ b/lib/models/application.js @@ -113,7 +113,7 @@ function generateKey(hmacKey, algorithm, encoding) { * @inherits {Model} */ -var Application = loopback.DataModel.extend('Application', ApplicationSchema); +var Application = loopback.PersistedModel.extend('Application', ApplicationSchema); /*! * A hook to generate keys before creation diff --git a/lib/models/change.js b/lib/models/change.js index 60a6042e..65420725 100644 --- a/lib/models/change.js +++ b/lib/models/change.js @@ -2,7 +2,7 @@ * Module Dependencies. */ -var DataModel = require('./data-model') +var PersistedModel = require('./persisted-model') , loopback = require('../loopback') , crypto = require('crypto') , CJSON = {stringify: require('canonical-json')} @@ -45,7 +45,7 @@ var options = { * @inherits {Model} */ -var Change = module.exports = DataModel.extend('Change', properties, options); +var Change = module.exports = PersistedModel.extend('Change', properties, options); /*! * Constants @@ -67,7 +67,7 @@ Change.Conflict = Conflict; */ Change.setup = function() { - DataModel.setup.call(this); + PersistedModel.setup.call(this); var Change = this; Change.getter.id = function() { @@ -497,8 +497,8 @@ Change.prototype.getModel = function(callback) { * **Note: call `conflict.fetch()` to get the `target` and `source` models. * * @param {*} modelId - * @param {DataModel} SourceModel - * @param {DataModel} TargetModel + * @param {PersistedModel} SourceModel + * @param {PersistedModel} TargetModel * @property {ModelClass} source The source model instance * @property {ModelClass} target The target model instance */ @@ -516,8 +516,8 @@ function Conflict(modelId, SourceModel, TargetModel) { * * @callback {Function} callback * @param {Error} - * @param {DataModel} source - * @param {DataModel} target + * @param {PersistedModel} source + * @param {PersistedModel} target */ Conflict.prototype.models = function(cb) { diff --git a/lib/models/checkpoint.js b/lib/models/checkpoint.js index c1fa7d70..704296c2 100644 --- a/lib/models/checkpoint.js +++ b/lib/models/checkpoint.js @@ -2,7 +2,7 @@ * Module Dependencies. */ -var DataModel = require('../loopback').DataModel +var PersistedModel = require('../loopback').PersistedModel , loopback = require('../loopback') , assert = require('assert'); @@ -32,10 +32,10 @@ var options = { * @property sourceId {String} the source identifier * * @class - * @inherits {DataModel} + * @inherits {PersistedModel} */ -var Checkpoint = module.exports = DataModel.extend('Checkpoint', properties, options); +var Checkpoint = module.exports = PersistedModel.extend('Checkpoint', properties, options); /** * Get the current checkpoint id diff --git a/lib/models/data-model.js b/lib/models/persisted-model.js similarity index 84% rename from lib/models/data-model.js rename to lib/models/persisted-model.js index 048a563d..540eb29b 100644 --- a/lib/models/data-model.js +++ b/lib/models/persisted-model.js @@ -16,43 +16,43 @@ var async = require('async'); * Listen for model changes using the `change` event. * * ```js - * MyDataModel.on('changed', function(obj) { + * MyPersistedModel.on('changed', function(obj) { * console.log(obj) // => the changed model * }); * ``` * - * @class DataModel + * @class PersistedModel * @param {Object} data * @param {Number} data.id The default id property */ -var DataModel = module.exports = Model.extend('DataModel'); +var PersistedModel = module.exports = Model.extend('PersistedModel'); /*! - * Setup the `DataModel` constructor. + * Setup the `PersistedModel` constructor. */ -DataModel.setup = function setupDataModel() { +PersistedModel.setup = function setupPersistedModel() { // call Model.setup first Model.setup.call(this); - var DataModel = this; + var PersistedModel = this; var typeName = this.modelName; // setup a remoting type converter for this model RemoteObjects.convert(typeName, function(val) { - return val ? new DataModel(val) : val; + return val ? new PersistedModel(val) : val; }); // enable change tracking (usually for replication) if(this.settings.trackChanges) { - DataModel._defineChangeModel(); - DataModel.once('dataSourceAttached', function() { - DataModel.enableChangeTracking(); + PersistedModel._defineChangeModel(); + PersistedModel.once('dataSourceAttached', function() { + PersistedModel.enableChangeTracking(); }); } - DataModel.setupRemoting(); + PersistedModel.setupRemoting(); } /*! @@ -63,7 +63,7 @@ function throwNotAttached(modelName, methodName) { throw new Error( 'Cannot call ' + modelName + '.'+ methodName + '().' + ' The ' + methodName + ' method has not been setup.' - + ' The DataModel has not been correctly attached to a DataSource!' + + ' The PersistedModel has not been correctly attached to a DataSource!' ); } @@ -95,7 +95,7 @@ function convertNullToNotFoundError(ctx, cb) { * - instance (null or Model) */ -DataModel.create = function (data, callback) { +PersistedModel.create = function (data, callback) { throwNotAttached(this.modelName, 'create'); }; @@ -105,7 +105,7 @@ DataModel.create = function (data, callback) { * @param {Function} [callback] The callback function */ -DataModel.upsert = DataModel.updateOrCreate = function upsert(data, callback) { +PersistedModel.upsert = PersistedModel.updateOrCreate = function upsert(data, callback) { throwNotAttached(this.modelName, 'upsert'); }; @@ -118,7 +118,7 @@ DataModel.upsert = DataModel.updateOrCreate = function upsert(data, callback) { * @param {Function} cb - callback called with (err, instance) */ -DataModel.findOrCreate = function findOrCreate(query, data, callback) { +PersistedModel.findOrCreate = function findOrCreate(query, data, callback) { throwNotAttached(this.modelName, 'findOrCreate'); }; @@ -129,7 +129,7 @@ DataModel.findOrCreate = function findOrCreate(query, data, callback) { * @param {Function} cb - callbacl called with (err, exists: Bool) */ -DataModel.exists = function exists(id, cb) { +PersistedModel.exists = function exists(id, cb) { throwNotAttached(this.modelName, 'exists'); }; @@ -140,7 +140,7 @@ DataModel.exists = function exists(id, cb) { * @param {Function} cb - callback called with (err, instance) */ -DataModel.findById = function find(id, cb) { +PersistedModel.findById = function find(id, cb) { throwNotAttached(this.modelName, 'findById'); }; @@ -151,7 +151,7 @@ DataModel.findById = function find(id, cb) { * @param {Object} params (optional) * * - where: Object `{ key: val, key2: {gt: 'val2'}}` - * - include: String, Object or Array. See DataModel.include documentation. + * - include: String, Object or Array. See PersistedModel.include documentation. * - order: String * - limit: Number * - skip: Number @@ -162,7 +162,7 @@ DataModel.findById = function find(id, cb) { * - Array of instances */ -DataModel.find = function find(params, cb) { +PersistedModel.find = function find(params, cb) { throwNotAttached(this.modelName, 'find'); }; @@ -173,7 +173,7 @@ DataModel.find = function find(params, cb) { * @param {Function} cb - callback called with (err, instance) */ -DataModel.findOne = function findOne(params, cb) { +PersistedModel.findOne = function findOne(params, cb) { throwNotAttached(this.modelName, 'findOne'); }; @@ -183,9 +183,9 @@ DataModel.findOne = function findOne(params, cb) { * @param {Function} [cb] - callback called with (err) */ -DataModel.remove = -DataModel.deleteAll = -DataModel.destroyAll = function destroyAll(where, cb) { +PersistedModel.remove = +PersistedModel.deleteAll = +PersistedModel.destroyAll = function destroyAll(where, cb) { throwNotAttached(this.modelName, 'destroyAll'); }; @@ -195,9 +195,9 @@ DataModel.destroyAll = function destroyAll(where, cb) { * @param {Function} cb - callback called with (err) */ -DataModel.removeById = -DataModel.deleteById = -DataModel.destroyById = function deleteById(id, cb) { +PersistedModel.removeById = +PersistedModel.deleteById = +PersistedModel.destroyById = function deleteById(id, cb) { throwNotAttached(this.modelName, 'deleteById'); }; @@ -208,7 +208,7 @@ DataModel.destroyById = function deleteById(id, cb) { * @param {Function} cb - callback, called with (err, count) */ -DataModel.count = function (where, cb) { +PersistedModel.count = function (where, cb) { throwNotAttached(this.modelName, 'count'); }; @@ -219,7 +219,7 @@ DataModel.count = function (where, cb) { * @param callback(err, obj) */ -DataModel.prototype.save = function (options, callback) { +PersistedModel.prototype.save = function (options, callback) { var Model = this.constructor; if (typeof options == 'function') { @@ -286,7 +286,7 @@ DataModel.prototype.save = function (options, callback) { * @returns {Boolean} */ -DataModel.prototype.isNewRecord = function () { +PersistedModel.prototype.isNewRecord = function () { throwNotAttached(this.constructor.modelName, 'isNewRecord'); }; @@ -296,13 +296,13 @@ DataModel.prototype.isNewRecord = function () { * @triggers `destroy` hook (async) before and after destroying object */ -DataModel.prototype.remove = -DataModel.prototype.delete = -DataModel.prototype.destroy = function (cb) { +PersistedModel.prototype.remove = +PersistedModel.prototype.delete = +PersistedModel.prototype.destroy = function (cb) { throwNotAttached(this.constructor.modelName, 'destroy'); }; -DataModel.prototype.destroy._delegate = true; +PersistedModel.prototype.destroy._delegate = true; /** * Update single attribute @@ -314,7 +314,7 @@ DataModel.prototype.destroy._delegate = true; * @param {Function} callback - callback called with (err, instance) */ -DataModel.prototype.updateAttribute = function updateAttribute(name, value, callback) { +PersistedModel.prototype.updateAttribute = function updateAttribute(name, value, callback) { throwNotAttached(this.constructor.modelName, 'updateAttribute'); }; @@ -328,7 +328,7 @@ DataModel.prototype.updateAttribute = function updateAttribute(name, value, call * @param {Function} callback - callback called with (err, instance) */ -DataModel.prototype.updateAttributes = function updateAttributes(data, cb) { +PersistedModel.prototype.updateAttributes = function updateAttributes(data, cb) { throwNotAttached(this.modelName, 'updateAttributes'); }; @@ -339,12 +339,12 @@ DataModel.prototype.updateAttributes = function updateAttributes(data, cb) { * @param {Function} callback - called with (err, instance) arguments */ -DataModel.prototype.reload = function reload(callback) { +PersistedModel.prototype.reload = function reload(callback) { throwNotAttached(this.constructor.modelName, 'reload'); }; /** - * Set the correct `id` property for the `DataModel`. If a `Connector` defines + * Set the correct `id` property for the `PersistedModel`. If a `Connector` defines * a `setId` method it will be used. Otherwise the default lookup is used. You * should override this method to handle complex ids. * @@ -352,18 +352,18 @@ DataModel.prototype.reload = function reload(callback) { * specifies. */ -DataModel.prototype.setId = function(val) { +PersistedModel.prototype.setId = function(val) { var ds = this.getDataSource(); this[this.getIdName()] = val; } /** - * Get the `id` value for the `DataModel`. + * Get the `id` value for the `PersistedModel`. * * @returns {*} The `id` value */ -DataModel.prototype.getId = function() { +PersistedModel.prototype.getId = function() { var data = this.toObject(); if(!data) return; return data[this.getIdName()]; @@ -375,7 +375,7 @@ DataModel.prototype.getId = function() { * @returns {String} The `id` property name */ -DataModel.prototype.getIdName = function() { +PersistedModel.prototype.getIdName = function() { return this.constructor.getIdName(); } @@ -385,7 +385,7 @@ DataModel.prototype.getIdName = function() { * @returns {String} The `id` property name */ -DataModel.getIdName = function() { +PersistedModel.getIdName = function() { var Model = this; var ds = Model.getDataSource(); @@ -396,40 +396,40 @@ DataModel.getIdName = function() { } } -DataModel.setupRemoting = function() { - var DataModel = this; - var typeName = DataModel.modelName; - var options = DataModel.settings; +PersistedModel.setupRemoting = function() { + var PersistedModel = this; + var typeName = PersistedModel.modelName; + var options = PersistedModel.settings; function setRemoting(scope, name, options) { var fn = scope[name]; fn._delegate = true; - options.isStatic = scope === DataModel; - DataModel.remoteMethod(name, options); + options.isStatic = scope === PersistedModel; + PersistedModel.remoteMethod(name, options); } - setRemoting(DataModel, 'create', { + setRemoting(PersistedModel, 'create', { description: 'Create a new instance of the model and persist it into the data source', accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}}, returns: {arg: 'data', type: typeName, root: true}, http: {verb: 'post', path: '/'} }); - setRemoting(DataModel, 'upsert', { + setRemoting(PersistedModel, 'upsert', { description: 'Update an existing model instance or insert a new one into the data source', accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}}, returns: {arg: 'data', type: typeName, root: true}, http: {verb: 'put', path: '/'} }); - setRemoting(DataModel, 'exists', { + setRemoting(PersistedModel, 'exists', { description: 'Check whether a model instance exists in the data source', accepts: {arg: 'id', type: 'any', description: 'Model id', required: true}, returns: {arg: 'exists', type: 'boolean'}, http: {verb: 'get', path: '/:id/exists'} }); - setRemoting(DataModel, 'findById', { + setRemoting(PersistedModel, 'findById', { description: 'Find a model instance by id from the data source', accepts: { arg: 'id', type: 'any', description: 'Model id', required: true, @@ -440,42 +440,42 @@ DataModel.setupRemoting = function() { rest: {after: convertNullToNotFoundError} }); - setRemoting(DataModel, 'find', { + setRemoting(PersistedModel, 'find', { description: 'Find all instances of the model matched by filter from the data source', accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'}, returns: {arg: 'data', type: [typeName], root: true}, http: {verb: 'get', path: '/'} }); - setRemoting(DataModel, 'findOne', { + setRemoting(PersistedModel, 'findOne', { description: 'Find first instance of the model matched by filter from the data source', accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'}, returns: {arg: 'data', type: typeName, root: true}, http: {verb: 'get', path: '/findOne'} }); - setRemoting(DataModel, 'destroyAll', { + setRemoting(PersistedModel, 'destroyAll', { description: 'Delete all matching records', accepts: {arg: 'where', type: 'object', description: 'filter.where object'}, http: {verb: 'del', path: '/'}, shared: false }); - setRemoting(DataModel, 'removeById', { + setRemoting(PersistedModel, 'removeById', { description: 'Delete a model instance by id from the data source', accepts: {arg: 'id', type: 'any', description: 'Model id', required: true, http: {source: 'path'}}, http: {verb: 'del', path: '/:id'} }); - setRemoting(DataModel, 'count', { + setRemoting(PersistedModel, 'count', { description: 'Count instances of the model matched by where from the data source', accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'}, returns: {arg: 'count', type: 'number'}, http: {verb: 'get', path: '/count'} }); - setRemoting(DataModel.prototype, 'updateAttributes', { + setRemoting(PersistedModel.prototype, 'updateAttributes', { description: 'Update attributes for a model instance and persist it into the data source', accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'}, returns: {arg: 'data', type: typeName, root: true}, @@ -483,7 +483,7 @@ DataModel.setupRemoting = function() { }); if(options.trackChanges) { - setRemoting(DataModel, 'diff', { + setRemoting(PersistedModel, 'diff', { description: 'Get a set of deltas and conflicts since the given checkpoint', accepts: [ {arg: 'since', type: 'number', description: 'Find deltas since this checkpoint'}, @@ -494,7 +494,7 @@ DataModel.setupRemoting = function() { http: {verb: 'post', path: '/diff'} }); - setRemoting(DataModel, 'changes', { + setRemoting(PersistedModel, 'changes', { description: 'Get the changes to a model since a given checkpoint.' + 'Provide a filter object to reduce the number of results returned.', accepts: [ @@ -505,37 +505,37 @@ DataModel.setupRemoting = function() { http: {verb: 'get', path: '/changes'} }); - setRemoting(DataModel, 'checkpoint', { + setRemoting(PersistedModel, 'checkpoint', { description: 'Create a checkpoint.', returns: {arg: 'checkpoint', type: 'object', root: true}, http: {verb: 'post', path: '/checkpoint'} }); - setRemoting(DataModel, 'currentCheckpoint', { + setRemoting(PersistedModel, 'currentCheckpoint', { description: 'Get the current checkpoint.', returns: {arg: 'checkpoint', type: 'object', root: true}, http: {verb: 'get', path: '/checkpoint'} }); - setRemoting(DataModel, 'createUpdates', { + setRemoting(PersistedModel, 'createUpdates', { description: 'Create an update list from a delta list', accepts: {arg: 'deltas', type: 'array', http: {source: 'body'}}, returns: {arg: 'updates', type: 'array', root: true}, http: {verb: 'post', path: '/create-updates'} }); - setRemoting(DataModel, 'bulkUpdate', { + setRemoting(PersistedModel, 'bulkUpdate', { description: 'Run multiple updates at once. Note: this is not atomic.', accepts: {arg: 'updates', type: 'array'}, http: {verb: 'post', path: '/bulk-update'} }); - setRemoting(DataModel, 'rectifyAllChanges', { + setRemoting(PersistedModel, 'rectifyAllChanges', { description: 'Rectify all Model changes.', http: {verb: 'post', path: '/rectify-all'} }); - setRemoting(DataModel, 'rectifyChange', { + setRemoting(PersistedModel, 'rectifyChange', { description: 'Tell loopback that a change to the model with the given id has occurred.', accepts: {arg: 'id', type: 'any', http: {source: 'path'}}, http: {verb: 'post', path: '/:id/rectify-change'} @@ -553,7 +553,7 @@ DataModel.setupRemoting = function() { * @param {Function} callback */ -DataModel.diff = function(since, remoteChanges, callback) { +PersistedModel.diff = function(since, remoteChanges, callback) { var Change = this.getChangeModel(); Change.diff(this.modelName, since, remoteChanges, callback); } @@ -570,7 +570,7 @@ DataModel.diff = function(since, remoteChanges, callback) { * @end */ -DataModel.changes = function(since, filter, callback) { +PersistedModel.changes = function(since, filter, callback) { if(typeof since === 'function') { filter = {}; callback = since; @@ -620,7 +620,7 @@ DataModel.changes = function(since, filter, callback) { * @param {Function} callback */ -DataModel.checkpoint = function(cb) { +PersistedModel.checkpoint = function(cb) { var Checkpoint = this.getChangeModel().getCheckpointModel(); this.getSourceId(function(err, sourceId) { if(err) return cb(err); @@ -639,7 +639,7 @@ DataModel.checkpoint = function(cb) { * @end */ -DataModel.currentCheckpoint = function(cb) { +PersistedModel.currentCheckpoint = function(cb) { var Checkpoint = this.getChangeModel().getCheckpointModel(); Checkpoint.current(cb); } @@ -657,7 +657,7 @@ DataModel.currentCheckpoint = function(cb) { * due to conflicts. */ -DataModel.replicate = function(since, targetModel, options, callback) { +PersistedModel.replicate = function(since, targetModel, options, callback) { var lastArg = arguments[arguments.length - 1]; if(typeof lastArg === 'function' && arguments.length > 1) { @@ -750,7 +750,7 @@ DataModel.replicate = function(since, targetModel, options, callback) { * @param {Function} callback */ -DataModel.createUpdates = function(deltas, cb) { +PersistedModel.createUpdates = function(deltas, cb) { var Change = this.getChangeModel(); var updates = []; var Model = this; @@ -802,7 +802,7 @@ DataModel.createUpdates = function(deltas, cb) { * @param {Function} callback */ -DataModel.bulkUpdate = function(updates, callback) { +PersistedModel.bulkUpdate = function(updates, callback) { var tasks = []; var Model = this; var idName = this.dataSource.idName(this.modelName); @@ -838,7 +838,7 @@ DataModel.bulkUpdate = function(updates, callback) { * @return {Change} */ -DataModel.getChangeModel = function() { +PersistedModel.getChangeModel = function() { var changeModel = this.Change; var isSetup = changeModel && changeModel.dataSource; @@ -855,7 +855,7 @@ DataModel.getChangeModel = function() { * @param {String} sourceId */ -DataModel.getSourceId = function(cb) { +PersistedModel.getSourceId = function(cb) { var dataSource = this.dataSource; if(!dataSource) { this.once('dataSourceAttached', this.getSourceId.bind(this, cb)); @@ -872,7 +872,7 @@ DataModel.getSourceId = function(cb) { * Enable the tracking of changes made to the model. Usually for replication. */ -DataModel.enableChangeTracking = function() { +PersistedModel.enableChangeTracking = function() { var Model = this; var Change = this.Change || this._defineChangeModel(); var cleanupInterval = Model.settings.changeCleanupInterval || 30000; @@ -911,7 +911,7 @@ DataModel.enableChangeTracking = function() { } } -DataModel._defineChangeModel = function() { +PersistedModel._defineChangeModel = function() { var BaseChangeModel = require('./change'); return this.Change = BaseChangeModel.extend(this.modelName + '-change', {}, @@ -921,7 +921,7 @@ DataModel._defineChangeModel = function() { ); } -DataModel.rectifyAllChanges = function(callback) { +PersistedModel.rectifyAllChanges = function(callback) { this.getChangeModel().rectifyAll(callback); } @@ -932,7 +932,7 @@ DataModel.rectifyAllChanges = function(callback) { * @param {Error} err */ -DataModel.handleChangeError = function(err) { +PersistedModel.handleChangeError = function(err) { if(err) { console.error(Model.modelName + ' Change Tracking Error:'); console.error(err); @@ -947,9 +947,9 @@ DataModel.handleChangeError = function(err) { * @param {Error} err */ -DataModel.rectifyChange = function(id, callback) { +PersistedModel.rectifyChange = function(id, callback) { var Change = this.getChangeModel(); Change.rectifyModelChanges(this.modelName, [id], callback); } -DataModel.setup(); +PersistedModel.setup(); diff --git a/lib/models/user.js b/lib/models/user.js index ef612403..ae3ad569 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -2,7 +2,7 @@ * Module Dependencies. */ -var DataModel = require('../loopback').DataModel +var PersistedModel = require('../loopback').PersistedModel , loopback = require('../loopback') , path = require('path') , SALT_WORK_FACTOR = 10 @@ -126,7 +126,7 @@ var options = { * @inherits {Model} */ -var User = module.exports = DataModel.extend('User', properties, options); +var User = module.exports = PersistedModel.extend('User', properties, options); /** * Login a user by with the given `credentials`. @@ -415,7 +415,7 @@ User.resetPassword = function(options, cb) { User.setup = function () { // We need to call the base class's setup method - DataModel.setup.call(this); + PersistedModel.setup.call(this); var UserModel = this; // max ttl diff --git a/test/access-token.test.js b/test/access-token.test.js index cd4bc0d6..a9e47aab 100644 --- a/test/access-token.test.js +++ b/test/access-token.test.js @@ -125,7 +125,7 @@ function createTestApp(testToken, done) { app.use(loopback.rest()); app.enableAuth(); - var TestModel = loopback.DataModel.extend('test', {}, { + var TestModel = loopback.PersistedModel.extend('test', {}, { acls: [ { principalType: "ROLE", diff --git a/test/acl.test.js b/test/acl.test.js index bf7bdb86..3f8892bd 100644 --- a/test/acl.test.js +++ b/test/acl.test.js @@ -22,7 +22,7 @@ before(function() { describe('security scopes', function () { beforeEach(function() { var ds = this.ds = loopback.createDataSource({connector: loopback.Memory}); - testModel = loopback.DataModel.extend('testModel'); + testModel = loopback.PersistedModel.extend('testModel'); ACL.attachTo(ds); Role.attachTo(ds); RoleMapping.attachTo(ds); diff --git a/test/app.test.js b/test/app.test.js index 2ab4e287..e14f861f 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -1,7 +1,7 @@ var path = require('path'); var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app'); var loopback = require('../'); -var DataModel = loopback.DataModel; +var PersistedModel = loopback.PersistedModel; var describe = require('./util/describe'); var it = require('./util/it'); @@ -16,7 +16,7 @@ describe('app', function() { }); it("Expose a `Model` to remote clients", function() { - var Color = DataModel.extend('color', {name: String}); + var Color = PersistedModel.extend('color', {name: String}); app.model(Color); Color.attachTo(db); @@ -24,14 +24,14 @@ describe('app', function() { }); it('uses singlar name as app.remoteObjects() key', function() { - var Color = DataModel.extend('color', {name: String}); + var Color = PersistedModel.extend('color', {name: String}); app.model(Color); Color.attachTo(db); expect(app.remoteObjects()).to.eql({ color: Color }); }); it('uses singular name as shared class name', function() { - var Color = DataModel.extend('color', {name: String}); + var Color = PersistedModel.extend('color', {name: String}); app.model(Color); Color.attachTo(db); var classes = app.remotes().classes().map(function(c) {return c.name}); @@ -42,7 +42,7 @@ describe('app', function() { app.use(loopback.rest()); request(app).get('/colors').expect(404, function(err, res) { if (err) return done(err); - var Color = DataModel.extend('color', {name: String}); + var Color = PersistedModel.extend('color', {name: String}); app.model(Color); Color.attachTo(db); request(app).get('/colors').expect(200, done); diff --git a/test/change.test.js b/test/change.test.js index e0c53eeb..4c34eace 100644 --- a/test/change.test.js +++ b/test/change.test.js @@ -6,7 +6,7 @@ describe('Change', function(){ var memory = loopback.createDataSource({ connector: loopback.Memory }); - TestModel = loopback.DataModel.extend('chtest', {}, { + TestModel = loopback.PersistedModel.extend('chtest', {}, { trackChanges: true }); this.modelName = TestModel.modelName; diff --git a/test/data-source.test.js b/test/data-source.test.js index 552da12d..7f09a1c4 100644 --- a/test/data-source.test.js +++ b/test/data-source.test.js @@ -36,9 +36,9 @@ describe('DataSource', function() { }); }); - describe.skip('DataModel Methods', function() { + describe.skip('PersistedModel Methods', function() { it("List the enabled and disabled methods", function() { - var TestModel = loopback.DataModel.extend('TestDataModel'); + var TestModel = loopback.PersistedModel.extend('TestPersistedModel'); TestModel.attachTo(loopback.memory()); // assert the defaults diff --git a/test/fixtures/e2e/models.js b/test/fixtures/e2e/models.js index e1c22d6e..3574c5bc 100644 --- a/test/fixtures/e2e/models.js +++ b/test/fixtures/e2e/models.js @@ -1,6 +1,6 @@ var loopback = require('../../../'); -var DataModel = loopback.DataModel; +var PersistedModel = loopback.PersistedModel; -exports.TestModel = DataModel.extend('TestModel', {}, { +exports.TestModel = PersistedModel.extend('TestModel', {}, { trackChanges: true }); diff --git a/test/hidden-properties.test.js b/test/hidden-properties.test.js index d1d8740b..d30d32f7 100644 --- a/test/hidden-properties.test.js +++ b/test/hidden-properties.test.js @@ -3,13 +3,13 @@ var loopback = require('../'); describe('hidden properties', function () { beforeEach(function (done) { var app = this.app = loopback(); - var Product = this.Product = loopback.DataModel.extend('product', + var Product = this.Product = loopback.PersistedModel.extend('product', {}, {hidden: ['secret']} ); Product.attachTo(loopback.memory()); - var Category = this.Category = loopback.DataModel.extend('category'); + var Category = this.Category = loopback.PersistedModel.extend('category'); Category.attachTo(loopback.memory()); Category.hasMany(Product); diff --git a/test/model.test.js b/test/model.test.js index 10d02a7b..145f0185 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -3,11 +3,11 @@ var loopback = require('../'); var ACL = loopback.ACL; var Change = loopback.Change; var defineModelTestsWithDataSource = require('./util/model-tests'); -var DataModel = loopback.DataModel; +var PersistedModel = loopback.PersistedModel; var describe = require('./util/describe'); -describe('Model / DataModel', function() { +describe('Model / PersistedModel', function() { defineModelTestsWithDataSource({ dataSource: { connector: loopback.Memory @@ -16,7 +16,7 @@ describe('Model / DataModel', function() { describe('Model.validatesUniquenessOf(property, options)', function() { it("Ensure the value for `property` is unique", function(done) { - var User = DataModel.extend('user', { + var User = PersistedModel.extend('user', { 'first': String, 'last': String, 'age': Number, @@ -71,7 +71,7 @@ describe.onServer('Remote Methods', function(){ var app; beforeEach(function () { - User = DataModel.extend('user', { + User = PersistedModel.extend('user', { 'first': String, 'last': String, 'age': Number, @@ -362,7 +362,7 @@ describe.onServer('Remote Methods', function(){ describe('Model.extend()', function(){ it('Create a new model by extending an existing model', function() { - var User = loopback.DataModel.extend('test-user', { + var User = loopback.PersistedModel.extend('test-user', { email: String }); diff --git a/test/remote-connector.test.js b/test/remote-connector.test.js index 129525b8..b0b31135 100644 --- a/test/remote-connector.test.js +++ b/test/remote-connector.test.js @@ -32,7 +32,7 @@ describe('RemoteConnector', function() { var test = this; remoteApp = this.remoteApp = loopback(); remoteApp.use(loopback.rest()); - var ServerModel = this.ServerModel = loopback.DataModel.extend('TestModel'); + var ServerModel = this.ServerModel = loopback.PersistedModel.extend('TestModel'); remoteApp.model(ServerModel); @@ -48,7 +48,7 @@ describe('RemoteConnector', function() { it('should support the save method', function (done) { var calledServerCreate = false; - var RemoteModel = loopback.DataModel.extend('TestModel'); + var RemoteModel = loopback.PersistedModel.extend('TestModel'); RemoteModel.attachTo(this.remote); var ServerModel = this.ServerModel; diff --git a/test/replication.test.js b/test/replication.test.js index c22b1396..286535b3 100644 --- a/test/replication.test.js +++ b/test/replication.test.js @@ -3,7 +3,7 @@ var loopback = require('../'); var ACL = loopback.ACL; var Change = loopback.Change; var defineModelTestsWithDataSource = require('./util/model-tests'); -var DataModel = loopback.DataModel; +var PersistedModel = loopback.PersistedModel; describe('Replication / Change APIs', function() { beforeEach(function() { @@ -11,12 +11,12 @@ describe('Replication / Change APIs', function() { var dataSource = this.dataSource = loopback.createDataSource({ connector: loopback.Memory }); - var SourceModel = this.SourceModel = DataModel.extend('SourceModel', {}, { + var SourceModel = this.SourceModel = PersistedModel.extend('SourceModel', {}, { trackChanges: true }); SourceModel.attachTo(dataSource); - var TargetModel = this.TargetModel = DataModel.extend('TargetModel', {}, { + var TargetModel = this.TargetModel = PersistedModel.extend('TargetModel', {}, { trackChanges: true }); TargetModel.attachTo(dataSource); diff --git a/test/util/model-tests.js b/test/util/model-tests.js index c8948aa6..a3362b58 100644 --- a/test/util/model-tests.js +++ b/test/util/model-tests.js @@ -3,7 +3,7 @@ var describe = require('./describe'); var loopback = require('../../'); var ACL = loopback.ACL; var Change = loopback.Change; -var DataModel = loopback.DataModel; +var PersistedModel = loopback.PersistedModel; var RemoteObjects = require('strong-remoting'); module.exports = function defineModelTestsWithDataSource(options) { @@ -22,11 +22,11 @@ describe('Model Tests', function() { // setup a model / datasource dataSource = this.dataSource || loopback.createDataSource(options.dataSource); - var extend = DataModel.extend; + var extend = PersistedModel.extend; // create model hook - DataModel.extend = function() { - var extendedModel = extend.apply(DataModel, arguments); + PersistedModel.extend = function() { + var extendedModel = extend.apply(PersistedModel, arguments); if(options.onDefine) { options.onDefine.call(test, extendedModel); @@ -35,7 +35,7 @@ describe('Model Tests', function() { return extendedModel; } - User = DataModel.extend('user', { + User = PersistedModel.extend('user', { 'first': String, 'last': String, 'age': Number,