Merge pull request #115 from strongloop/refactor/remove-remoting

Remove remoting metadata
This commit is contained in:
Ritchie Martori 2014-05-20 13:59:33 -07:00
commit 69b0355fa7
5 changed files with 1 additions and 173 deletions

View File

@ -182,29 +182,6 @@ DataAccessObject.create = function (data, callback) {
return obj;
};
/*!
* Configure the remoting attributes for a given function
* @param {Function} fn The function
* @param {Object} options The options
* @private
*/
function setRemoting(fn, options) {
options = options || {};
for (var opt in options) {
if (options.hasOwnProperty(opt)) {
fn[opt] = options[opt];
}
}
fn.shared = true;
}
setRemoting(DataAccessObject.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: 'object', root: true},
http: {verb: 'post', path: '/'}
});
function stillConnecting(dataSource, obj, args) {
return dataSource.ready(obj, args);
}
@ -260,14 +237,6 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
}
};
// upsert ~ remoting attributes
setRemoting(DataAccessObject.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: 'object', root: true},
http: {verb: 'put', path: '/'}
});
/**
* Find one record, same as `all`, limited by 1 and return object, not collection,
* if not found, create using data provided as second argument
@ -313,15 +282,6 @@ DataAccessObject.exists = function exists(id, cb) {
}
};
// exists ~ remoting attributes
setRemoting(DataAccessObject.exists, {
description: 'Check whether a model instance exists in the data source',
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
returns: {arg: 'exists', type: 'any'},
http: {verb: 'get', path: '/:id/exists'}
});
/**
* Find object by id
*
@ -344,16 +304,6 @@ DataAccessObject.findById = function find(id, cb) {
}.bind(this));
};
// find ~ remoting attributes
setRemoting(DataAccessObject.findById, {
description: 'Find a model instance by id from the data source',
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
returns: {arg: 'data', type: 'any', root: true},
http: {verb: 'get', path: '/:id'},
rest: {after: convertNullToNotFoundError}
});
function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb();
@ -604,14 +554,6 @@ DataAccessObject.find = function find(query, cb) {
});
};
// all ~ remoting attributes
setRemoting(DataAccessObject.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: 'array', root: true},
http: {verb: 'get', path: '/'}
});
/**
* Find one record, same as `all`, limited by 1 and return object, not collection
*
@ -633,13 +575,6 @@ DataAccessObject.findOne = function findOne(query, cb) {
});
};
setRemoting(DataAccessObject.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: 'object', root: true},
http: {verb: 'get', path: '/findOne'}
});
/**
* Destroy all matching records
* @param {Object} [where] An object that defines the criteria
@ -688,14 +623,6 @@ DataAccessObject.removeById = DataAccessObject.deleteById = DataAccessObject.des
}.bind(this));
};
// deleteById ~ remoting attributes
setRemoting(DataAccessObject.deleteById, {
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'}
});
/**
* Return count of matched records
*
@ -714,14 +641,6 @@ DataAccessObject.count = function (where, cb) {
this.getDataSource().connector.count(this.modelName, cb, where);
};
// count ~ remoting attributes
setRemoting(DataAccessObject.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'}
});
/**
* Save instance. If the instance does not have an ID, call `create` instead.
* Triggers: validate, save, update or create.
@ -917,14 +836,6 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
}, data);
};
// updateAttributes ~ remoting attributes
setRemoting(DataAccessObject.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: 'object', root: true},
http: {verb: 'put', path: '/'}
});
/**
* Reload object from persistence
* Requires `id` member of `object` to be able to call `find`

View File

@ -66,8 +66,6 @@ exports.mixin = function (newClass, mixinClass, options) {
};
function mixInto(sourceScope, targetScope, options) {
var proxies = [];
Object.keys(sourceScope).forEach(function (propertyName, options) {
var targetPropertyExists = targetScope.hasOwnProperty(propertyName);
var sourceProperty = Object.getOwnPropertyDescriptor(sourceScope, propertyName);
@ -79,31 +77,10 @@ function mixInto(sourceScope, targetScope, options) {
if (shouldOverride) {
if (sourceIsFunc) {
sourceProperty.value = exports.proxy(sourceProperty.value, proxies);
sourceProperty.value = sourceProperty.value;
}
Object.defineProperty(targetScope, propertyName, sourceProperty);
}
});
}
exports.proxy = function createProxy(fn, proxies) {
// Make sure same methods referenced by different properties have the same proxy
// For example, deleteById is an alias of removeById
proxies = proxies || [];
for (var i = 0; i < proxies.length; i++) {
if (proxies[i]._delegate === fn) {
return proxies[i];
}
}
var f = function () {
return fn.apply(this, arguments);
};
f._delegate = fn;
proxies.push(f);
Object.keys(fn).forEach(function (x) {
f[x] = fn[x];
});
return f;
};

View File

@ -412,13 +412,6 @@ Relation.belongsTo = function (anotherClass, params) {
var f = this[methodName];
f.apply(this, arguments);
};
fn.shared = true;
fn.http = {verb: 'get', path: '/' + methodName};
fn.accepts = {arg: 'refresh', type: 'boolean', http: {source: 'query'}};
fn.description = 'Fetches belongsTo relation ' + methodName;
fn.returns = {arg: methodName, type: 'object', root: true};
this.prototype['__get__' + methodName] = fn;
};

View File

@ -118,12 +118,6 @@ function defineScope(cls, targetClass, name, params, methods) {
f.apply(this[name], arguments);
};
fn.shared = true;
fn.http = {verb: 'get', path: '/' + name};
fn.accepts = {arg: 'filter', type: 'object'};
fn.description = 'Queries ' + name + ' of this model.';
fn.returns = {arg: name, type: 'array', root: true};
cls['__get__' + name] = fn;
var fn_create = function () {
@ -131,22 +125,12 @@ function defineScope(cls, targetClass, name, params, methods) {
f.apply(this[name], arguments);
};
fn_create.shared = true;
fn_create.http = {verb: 'post', path: '/' + name};
fn_create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
fn_create.description = 'Creates a new instance in ' + name + ' of this model.';
fn_create.returns = {arg: 'data', type: 'object', root: true};
cls['__create__' + name] = fn_create;
var fn_delete = function () {
var f = this[name].destroyAll;
f.apply(this[name], arguments);
};
fn_delete.shared = true;
fn_delete.http = {verb: 'delete', path: '/' + name};
fn_delete.description = 'Deletes all ' + name + ' of this model.';
fn_delete.returns = {arg: 'data', type: 'object', root: true};
cls['__delete__' + name] = fn_delete;

View File

@ -1285,43 +1285,6 @@ describe('DataSource constructor', function () {
});
});
describe('Injected methods from connectors', function () {
it('are not shared across models for remote methods', function () {
var ds = new DataSource('memory');
var M1 = ds.createModel('M1');
var M2 = ds.createModel('M2');
// Remotable methods are not shared across models
assert.notEqual(M1.create, M2.create, 'Remotable methods are not shared');
assert.equal(M1.create.shared, true, 'M1.create is remotable');
assert.equal(M2.create.shared, true, 'M2.create is remotable');
M1.create.shared = false;
assert.equal(M1.create.shared, false, 'M1.create should be local now');
assert.equal(M2.create.shared, true, 'M2.create should stay remotable');
});
it('are not shared across models for non-remote methods', function () {
var ds = new DataSource('memory');
var M1 = ds.createModel('M1');
var M2 = ds.createModel('M2');
var m1 = M1.prototype.save;
var m2 = M2.prototype.save;
assert.notEqual(m1, m2, 'non-remote methods are not shared');
assert.equal(!!m1.shared, false, 'M1.save is not remotable');
assert.equal(!!m2.shared, false, 'M2.save is not remotable');
m1.shared = true;
assert.equal(m1.shared, true, 'M1.save is now remotable');
assert.equal(!!m2.shared, false, 'M2.save is not remotable');
assert.equal(M1.deleteById, M1.removeById,
'Same methods on the same model should have the same proxy');
assert.notEqual(M1.deleteById, M2.deleteById,
'Same methods on differnt models should have different proxies');
});
});
describe('ModelBuilder options.models', function () {
it('should inject model classes from models', function () {
var builder = new ModelBuilder();