Remove remoting metadata

This commit is contained in:
Ritchie Martori 2014-05-16 12:33:17 -07:00
parent a4af82a2c9
commit dc9c9b2bd4
3 changed files with 1 additions and 153 deletions

View File

@ -182,29 +182,6 @@ DataAccessObject.create = function (data, callback) {
return obj; 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) { function stillConnecting(dataSource, obj, args) {
return dataSource.ready(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, * Find one record, same as `all`, limited by 1 and return object, not collection,
* if not found, create using data provided as second argument * 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 * Find object by id
* *
@ -344,16 +304,6 @@ DataAccessObject.findById = function find(id, cb) {
}.bind(this)); }.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) { function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return 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 * 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 * Destroy all matching records
* @param {Object} [where] An object that defines the criteria * @param {Object} [where] An object that defines the criteria
@ -688,14 +623,6 @@ DataAccessObject.removeById = DataAccessObject.deleteById = DataAccessObject.des
}.bind(this)); }.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 * Return count of matched records
* *
@ -714,14 +641,6 @@ DataAccessObject.count = function (where, cb) {
this.getDataSource().connector.count(this.modelName, cb, where); 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. * Save instance. If the instance does not have an ID, call `create` instead.
* Triggers: validate, save, update or create. * Triggers: validate, save, update or create.
@ -917,14 +836,6 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
}, data); }, 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 * Reload object from persistence
* Requires `id` member of `object` to be able to call `find` * Requires `id` member of `object` to be able to call `find`

View File

@ -73,37 +73,11 @@ function mixInto(sourceScope, targetScope, options) {
var sourceProperty = Object.getOwnPropertyDescriptor(sourceScope, propertyName); var sourceProperty = Object.getOwnPropertyDescriptor(sourceScope, propertyName);
var targetProperty = targetPropertyExists && Object.getOwnPropertyDescriptor(targetScope, propertyName); var targetProperty = targetPropertyExists && Object.getOwnPropertyDescriptor(targetScope, propertyName);
var sourceIsFunc = typeof sourceProperty.value === 'function'; var sourceIsFunc = typeof sourceProperty.value === 'function';
var isFunc = targetPropertyExists && typeof targetProperty.value === 'function'; var shouldOverride = options.override || !targetPropertyExists || sourceIsFunc;
var isDelegate = isFunc && targetProperty.value._delegate;
var shouldOverride = options.override || !targetPropertyExists || isDelegate;
if (shouldOverride) { if (shouldOverride) {
if (sourceIsFunc) {
sourceProperty.value = exports.proxy(sourceProperty.value, proxies);
}
Object.defineProperty(targetScope, propertyName, sourceProperty); 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

@ -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 () { describe('ModelBuilder options.models', function () {
it('should inject model classes from models', function () { it('should inject model classes from models', function () {
var builder = new ModelBuilder(); var builder = new ModelBuilder();