From a1e445779487d1f80077f8c6655b79bebdbc64c9 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 22 Aug 2013 12:24:02 -0700 Subject: [PATCH] Refactor the shared method declaration and add descriptions --- lib/dao.js | 158 ++++++++++++++++++++++++++--------------------------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index d1da0138..0e9e7c6f 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -177,10 +177,28 @@ DataAccessObject.create = function (data, callback) { return obj; }; -DataAccessObject.create.shared = true; -DataAccessObject.create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}}; -DataAccessObject.create.returns = {arg: 'data', type: 'object', root: true}; -DataAccessObject.create.http = {verb: 'post', path: '/'}; +/*! + * 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 new instance of the model and save it in database', + accepts: {arg: 'data', type: 'object', http: {source: 'body'}}, + returns: {arg: 'data', type: 'object', root: true}, + http: {verb: 'post', path: '/'} +}); function stillConnecting(dataSource, obj, args) { if (dataSource.connected) return false; // Connected @@ -232,12 +250,13 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data }; // upsert ~ remoting attributes -DataAccessObject.upsert.shared = true; -DataAccessObject.upsert.accepts = {arg: 'data', type: 'object', http: {source: 'body'}}; -DataAccessObject.upsert.returns = {arg: 'data', type: 'object', root: true}; -DataAccessObject.upsert.http = [ - {verb: 'put', path: '/'} -]; +setRemoting(DataAccessObject.upsert, { + description: 'Update or insert a model instance', + accepts: {arg: 'data', type: 'object', 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, @@ -268,7 +287,7 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) { }; /** - * Check whether object exitst in database + * Check whether a model instance exists in database * * @param {id} id - identifier of object (primary key value) * @param {Function} cb - callbacl called with (err, exists: Bool) @@ -284,13 +303,12 @@ DataAccessObject.exists = function exists(id, cb) { }; // exists ~ remoting attributes -DataAccessObject.exists.shared = true; -DataAccessObject.exists.accepts = {arg: 'id', type: 'any'}; -DataAccessObject.exists.returns = {arg: 'exists', type: 'any'}; - -DataAccessObject.exists.http = [ - {verb: 'get', path: '/exists'} -]; +setRemoting(DataAccessObject.exists, { + description: 'Check whether a model instance exists in database', + accepts: {arg: 'id', type: 'any'}, + returns: {arg: 'exists', type: 'any'}, + http: {verb: 'get', path: '/exists'} +}); /** * Find object by id @@ -315,21 +333,18 @@ DataAccessObject.findById = function find(id, cb) { }; // find ~ remoting attributes -DataAccessObject.findById.accepts = [ - {arg: 'id', type: 'any'} -]; -DataAccessObject.findById.returns = [ - {arg: 'data', type: 'any', root: true} -]; -DataAccessObject.findById.shared = true; -DataAccessObject.findById.http = [ - {verb: 'get', path: '/:id'} -]; +setRemoting(DataAccessObject.findById, { + description: 'Find a model instance by id', + accepts: {arg: 'id', type: 'any'}, + returns: {arg: 'data', type: 'any', root: true}, + http: {verb: 'get', path: '/:id'} +}); + // alias function for backwards compat. DataAccessObject.all = function () { DataAccessObject.find.apply(this, arguments); -} +}; /** * Find all instances of Model, matched by query @@ -433,12 +448,12 @@ DataAccessObject.find = function find(params, cb) { }; // all ~ remoting attributes -DataAccessObject.find.accepts = {arg: 'filter', type: 'object'}; -DataAccessObject.find.returns = {arg: 'data', type: 'array', root: true}; -DataAccessObject.find.shared = true; -DataAccessObject.find.http = [ - {verb: 'get', path: '/'} -]; +setRemoting(DataAccessObject.find, { + description: 'Find all instances of the model, matched by the filter', + accepts: {arg: 'filter', type: 'object'}, + 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 @@ -460,13 +475,13 @@ DataAccessObject.findOne = function findOne(params, cb) { }); }; -DataAccessObject.findOne.shared = true; -DataAccessObject.findOne.accepts = {arg: 'filter', type: 'object'}; -DataAccessObject.findOne.returns = {arg: 'data', type: 'object', root: true}; +setRemoting(DataAccessObject.findOne, { + description: 'Find first instance of the model, matched by the filter', + accepts: {arg: 'filter', type: 'object'}, + returns: {arg: 'data', type: 'object', root: true}, + http: {verb: 'get', path: '/findOne'} +}); -DataAccessObject.findOne.http = [ - {verb: 'get', path: '/findOne'} -]; /** * Destroy all matching records @@ -512,13 +527,11 @@ DataAccessObject.deleteById = }; // deleteById ~ remoting attributes -DataAccessObject.deleteById.accepts = [ - {arg: 'id', type: 'any'} -]; -DataAccessObject.deleteById.shared = true; -DataAccessObject.deleteById.http = [ - {verb: 'del', path: '/:id'} -]; +setRemoting(DataAccessObject.deleteById, { + description: 'Delete a model instance by id', + accepts: {arg: 'id', type: 'any'}, + http: {verb: 'del', path: '/:id'} +}); /** @@ -539,14 +552,13 @@ DataAccessObject.count = function (where, cb) { // count ~ remoting attributes -DataAccessObject.count.shared = true; -DataAccessObject.count.accepts = [ - {arg: 'where', type: 'object'} -]; -DataAccessObject.count.returns = [ - {arg: 'count', type: 'number'} -]; -DataAccessObject.count.http = {verb: 'get', path: '/count'}; +setRemoting(DataAccessObject.count, { + description: 'Count instances of the model, matched by the where condition', + accepts: {arg: 'where', type: 'object'}, + returns: {arg: 'count', type: 'number'}, + http: {verb: 'get', path: '/count'} +}); + /** * Save instance. When instance haven't id, create method called instead. @@ -619,14 +631,6 @@ DataAccessObject.prototype.save = function (options, callback) { } }; -/* -// save ~ remoting attributes -DataAccessObject.prototype.save.shared = true; -DataAccessObject.prototype.save.returns = {arg: 'obj', type: 'object', root: true}; -DataAccessObject.prototype.save.http = [ - {verb: 'put', path: '/'} -]; -*/ DataAccessObject.prototype.isNewRecord = function () { return !getIdValue(this.constructor, this); @@ -663,13 +667,6 @@ DataAccessObject.prototype.destroy = function (cb) { }); }; -/* -// destroy ~ remoting attributes -DataAccessObject.prototype.destroy.shared = true; -DataAccessObject.prototype.destroy.http = [ - {verb: 'del', path: '/'} -]; -*/ /** * Update single attribute @@ -748,12 +745,13 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb }; // updateAttributes ~ remoting attributes -DataAccessObject.prototype.updateAttributes.shared = true; -DataAccessObject.prototype.updateAttributes.accepts = {arg: 'data', type: 'object', http: {source: 'body'}}; -DataAccessObject.prototype.updateAttributes.returns = {arg: 'data', type: 'object', root: true}; -DataAccessObject.prototype.updateAttributes.http = [ - {verb: 'put', path: '/'} -]; +setRemoting(DataAccessObject.prototype.updateAttributes, { + description: 'Update set of attributes for a model instance', + accepts: {arg: 'data', type: 'object', http: {source: 'body'}}, + returns: {arg: 'data', type: 'object', root: true}, + http: {verb: 'put', path: '/'} +}); + /** * Reload object from persistence @@ -767,8 +765,10 @@ DataAccessObject.prototype.reload = function reload(callback) { this.constructor.findById(getIdValue(this.constructor, this), callback); }; -DataAccessObject.prototype.reload.shared = true; -DataAccessObject.prototype.reload.returns = {arg: 'data', type: 'object', root: true}; +setRemoting(DataAccessObject.prototype.reload, { + description: 'Reload a model instance from database', + returns: {arg: 'data', type: 'object', root: true} +}); /** * Define readonly property on object