Initial commit

This commit is contained in:
Amir Jafarian 2016-05-10 21:33:23 -04:00
parent 14aed2251c
commit 8c2858d4e0
4 changed files with 77 additions and 3 deletions

View File

@ -87,6 +87,12 @@
"permission": "ALLOW",
"property": "resetPassword",
"accessType": "EXECUTE"
},
{
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "replaceById"
}
],
"relations": {

View File

@ -115,6 +115,18 @@ module.exports = function(registry) {
throwNotAttached(this.modelName, 'upsert');
};
/**
* Replace or insert a model instance
* @param {Object} data The model instance data to insert.
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
* @param {Object} model Replaced model instance.
*/
PersistedModel.replaceOrCreate = function replaceOrCreate(data, cb) {
throwNotAttached(this.modelName, 'replaceOrCreate');
};
/**
* Finds one record matching the optional filter object. If not found, creates
* the object using the data provided as second argument. In this sense it is
@ -482,6 +494,19 @@ module.exports = function(registry) {
throwNotAttached(this.modelName, 'updateAttributes');
};
/**
* Replace set of attributes. Performs validation before replacing.
*
* @param {Object} data Data to replace.
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://docs.strongloop.com/display/LB/Error+object).
* @param {Object} instance Repalced instance.
*/
PersistedModel.replaceById = function replaceById(data, cb) {
throwNotAttached(this.modelName, 'replaceById');
};
/**
* Reload object from persistence. Requires `id` member of `object` to be able to call `find`.
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
@ -549,6 +574,21 @@ module.exports = function(registry) {
var PersistedModel = this;
var typeName = PersistedModel.modelName;
var options = PersistedModel.settings;
// This is the defualt verb used for 2.X
var configurableVerb = {
updateAttributes: 'put',
updateOrCreate: 'put',
replaceOrCreate: 'patch',
replaceById: 'patch',
};
// we check options.newMapping when backporting to `2.x`. Thought?
if (options.newMapping) {
configurableVerb.replaceById = 'put';
configurableVerb.replaceOrCreate = 'put';
configurableVerb.updateAttributes = 'patch';
configurableVerb.updateOrCreate = 'patch';
}
function setRemoting(scope, name, options) {
var fn = scope[name];
@ -571,7 +611,16 @@ module.exports = function(registry) {
accessType: 'WRITE',
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
returns: {arg: 'data', type: typeName, root: true},
http: {verb: 'put', path: '/'}
http: { verb: configurableVerb.updateOrCreate, path: '/' },
});
setRemoting(PersistedModel, 'replaceOrCreate', {
description: 'Replace an existing model instance or insert a new one into the data source.',
accessType: 'WRITE',
accepts: { arg: 'data', type: 'object', http: { source: 'body' }, description:
'Model instance data' },
returns: { arg: 'data', type: typeName, root: true },
http: { verb: configurableVerb.replaceOrCreate, path: '/' },
});
setRemoting(PersistedModel, 'exists', {
@ -619,6 +668,20 @@ module.exports = function(registry) {
rest: {after: convertNullToNotFoundError}
});
setRemoting(PersistedModel, 'replaceById', {
description: 'Replace attributes for a model instance and persist it into the data source.',
accessType: 'WRITE',
accepts: [
{ arg: 'id', type: 'any', description: 'Model id', required: true,
http: { source: 'put' }},
{ arg: 'data', type: 'object', http: { source: 'body' }, description:
'An object of model property name/value pairs' },
],
returns: { arg: 'data', type: typeName, root: true },
http: { verb: configurableVerb.replaceById, path: '/:id' },
rest: { after: convertNullToNotFoundError },
});
setRemoting(PersistedModel, 'find', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
@ -692,7 +755,7 @@ module.exports = function(registry) {
accessType: 'WRITE',
accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'},
returns: {arg: 'data', type: typeName, root: true},
http: {verb: 'put', path: '/'}
http: {verb: configurableVerb.updateAttributes, path: '/'}
});
if (options.trackChanges || options.enableRemoteReplication) {

View File

@ -645,6 +645,8 @@ describe.onServer('Remote Methods', function() {
'upsert', 'updateOrCreate',
'exists',
'findById',
'replaceById',
'replaceOrCreate',
'find',
'findOne',
'updateAll', 'update',

View File

@ -122,12 +122,15 @@ describe('remoting - integration', function() {
.map(function(m) {
return formatMethod(m);
});
// This is the list of expected default endpoints
// for LB 3.X (They are different for LB 2.X)
var expectedMethods = [
'create(data:object):store POST /stores',
'upsert(data:object):store PUT /stores',
'replaceOrCreate(data:object):store PATCH /stores',
'exists(id:any):boolean GET /stores/:id/exists',
'findById(id:any,filter:object):store GET /stores/:id',
'replaceById(id:any,data:object):store PATCH /stores/:id',
'find(filter:object):store GET /stores',
'findOne(filter:object):store GET /stores/findOne',
'updateAll(where:object,data:object):object POST /stores/update',