Refactor the shared method declaration and add descriptions
This commit is contained in:
parent
21d427e6d1
commit
a1e4457794
158
lib/dao.js
158
lib/dao.js
|
@ -177,10 +177,28 @@ DataAccessObject.create = function (data, callback) {
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
DataAccessObject.create.shared = true;
|
/*!
|
||||||
DataAccessObject.create.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
|
* Configure the remoting attributes for a given function
|
||||||
DataAccessObject.create.returns = {arg: 'data', type: 'object', root: true};
|
* @param {Function} fn The function
|
||||||
DataAccessObject.create.http = {verb: 'post', path: '/'};
|
* @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) {
|
function stillConnecting(dataSource, obj, args) {
|
||||||
if (dataSource.connected) return false; // Connected
|
if (dataSource.connected) return false; // Connected
|
||||||
|
@ -232,12 +250,13 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
|
||||||
};
|
};
|
||||||
|
|
||||||
// upsert ~ remoting attributes
|
// upsert ~ remoting attributes
|
||||||
DataAccessObject.upsert.shared = true;
|
setRemoting(DataAccessObject.upsert, {
|
||||||
DataAccessObject.upsert.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
|
description: 'Update or insert a model instance',
|
||||||
DataAccessObject.upsert.returns = {arg: 'data', type: 'object', root: true};
|
accepts: {arg: 'data', type: 'object', http: {source: 'body'}},
|
||||||
DataAccessObject.upsert.http = [
|
returns: {arg: 'data', type: 'object', root: true},
|
||||||
{verb: 'put', path: '/'}
|
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,
|
||||||
|
@ -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 {id} id - identifier of object (primary key value)
|
||||||
* @param {Function} cb - callbacl called with (err, exists: Bool)
|
* @param {Function} cb - callbacl called with (err, exists: Bool)
|
||||||
|
@ -284,13 +303,12 @@ DataAccessObject.exists = function exists(id, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// exists ~ remoting attributes
|
// exists ~ remoting attributes
|
||||||
DataAccessObject.exists.shared = true;
|
setRemoting(DataAccessObject.exists, {
|
||||||
DataAccessObject.exists.accepts = {arg: 'id', type: 'any'};
|
description: 'Check whether a model instance exists in database',
|
||||||
DataAccessObject.exists.returns = {arg: 'exists', type: 'any'};
|
accepts: {arg: 'id', type: 'any'},
|
||||||
|
returns: {arg: 'exists', type: 'any'},
|
||||||
DataAccessObject.exists.http = [
|
http: {verb: 'get', path: '/exists'}
|
||||||
{verb: 'get', path: '/exists'}
|
});
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find object by id
|
* Find object by id
|
||||||
|
@ -315,21 +333,18 @@ DataAccessObject.findById = function find(id, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// find ~ remoting attributes
|
// find ~ remoting attributes
|
||||||
DataAccessObject.findById.accepts = [
|
setRemoting(DataAccessObject.findById, {
|
||||||
{arg: 'id', type: 'any'}
|
description: 'Find a model instance by id',
|
||||||
];
|
accepts: {arg: 'id', type: 'any'},
|
||||||
DataAccessObject.findById.returns = [
|
returns: {arg: 'data', type: 'any', root: true},
|
||||||
{arg: 'data', type: 'any', root: true}
|
http: {verb: 'get', path: '/:id'}
|
||||||
];
|
});
|
||||||
DataAccessObject.findById.shared = true;
|
|
||||||
DataAccessObject.findById.http = [
|
|
||||||
{verb: 'get', path: '/:id'}
|
|
||||||
];
|
|
||||||
|
|
||||||
// alias function for backwards compat.
|
// alias function for backwards compat.
|
||||||
DataAccessObject.all = function () {
|
DataAccessObject.all = function () {
|
||||||
DataAccessObject.find.apply(this, arguments);
|
DataAccessObject.find.apply(this, arguments);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all instances of Model, matched by query
|
* Find all instances of Model, matched by query
|
||||||
|
@ -433,12 +448,12 @@ DataAccessObject.find = function find(params, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// all ~ remoting attributes
|
// all ~ remoting attributes
|
||||||
DataAccessObject.find.accepts = {arg: 'filter', type: 'object'};
|
setRemoting(DataAccessObject.find, {
|
||||||
DataAccessObject.find.returns = {arg: 'data', type: 'array', root: true};
|
description: 'Find all instances of the model, matched by the filter',
|
||||||
DataAccessObject.find.shared = true;
|
accepts: {arg: 'filter', type: 'object'},
|
||||||
DataAccessObject.find.http = [
|
returns: {arg: 'data', type: 'array', root: true},
|
||||||
{verb: 'get', path: '/'}
|
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
|
||||||
|
@ -460,13 +475,13 @@ DataAccessObject.findOne = function findOne(params, cb) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
DataAccessObject.findOne.shared = true;
|
setRemoting(DataAccessObject.findOne, {
|
||||||
DataAccessObject.findOne.accepts = {arg: 'filter', type: 'object'};
|
description: 'Find first instance of the model, matched by the filter',
|
||||||
DataAccessObject.findOne.returns = {arg: 'data', type: 'object', root: true};
|
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
|
* Destroy all matching records
|
||||||
|
@ -512,13 +527,11 @@ DataAccessObject.deleteById =
|
||||||
};
|
};
|
||||||
|
|
||||||
// deleteById ~ remoting attributes
|
// deleteById ~ remoting attributes
|
||||||
DataAccessObject.deleteById.accepts = [
|
setRemoting(DataAccessObject.deleteById, {
|
||||||
{arg: 'id', type: 'any'}
|
description: 'Delete a model instance by id',
|
||||||
];
|
accepts: {arg: 'id', type: 'any'},
|
||||||
DataAccessObject.deleteById.shared = true;
|
http: {verb: 'del', path: '/:id'}
|
||||||
DataAccessObject.deleteById.http = [
|
});
|
||||||
{verb: 'del', path: '/:id'}
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -539,14 +552,13 @@ DataAccessObject.count = function (where, cb) {
|
||||||
|
|
||||||
|
|
||||||
// count ~ remoting attributes
|
// count ~ remoting attributes
|
||||||
DataAccessObject.count.shared = true;
|
setRemoting(DataAccessObject.count, {
|
||||||
DataAccessObject.count.accepts = [
|
description: 'Count instances of the model, matched by the where condition',
|
||||||
{arg: 'where', type: 'object'}
|
accepts: {arg: 'where', type: 'object'},
|
||||||
];
|
returns: {arg: 'count', type: 'number'},
|
||||||
DataAccessObject.count.returns = [
|
http: {verb: 'get', path: '/count'}
|
||||||
{arg: 'count', type: 'number'}
|
});
|
||||||
];
|
|
||||||
DataAccessObject.count.http = {verb: 'get', path: '/count'};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save instance. When instance haven't id, create method called instead.
|
* 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 () {
|
DataAccessObject.prototype.isNewRecord = function () {
|
||||||
return !getIdValue(this.constructor, this);
|
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
|
* Update single attribute
|
||||||
|
@ -748,12 +745,13 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
|
||||||
};
|
};
|
||||||
|
|
||||||
// updateAttributes ~ remoting attributes
|
// updateAttributes ~ remoting attributes
|
||||||
DataAccessObject.prototype.updateAttributes.shared = true;
|
setRemoting(DataAccessObject.prototype.updateAttributes, {
|
||||||
DataAccessObject.prototype.updateAttributes.accepts = {arg: 'data', type: 'object', http: {source: 'body'}};
|
description: 'Update set of attributes for a model instance',
|
||||||
DataAccessObject.prototype.updateAttributes.returns = {arg: 'data', type: 'object', root: true};
|
accepts: {arg: 'data', type: 'object', http: {source: 'body'}},
|
||||||
DataAccessObject.prototype.updateAttributes.http = [
|
returns: {arg: 'data', type: 'object', root: true},
|
||||||
{verb: 'put', path: '/'}
|
http: {verb: 'put', path: '/'}
|
||||||
];
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reload object from persistence
|
* Reload object from persistence
|
||||||
|
@ -767,8 +765,10 @@ DataAccessObject.prototype.reload = function reload(callback) {
|
||||||
this.constructor.findById(getIdValue(this.constructor, this), callback);
|
this.constructor.findById(getIdValue(this.constructor, this), callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
DataAccessObject.prototype.reload.shared = true;
|
setRemoting(DataAccessObject.prototype.reload, {
|
||||||
DataAccessObject.prototype.reload.returns = {arg: 'data', type: 'object', root: true};
|
description: 'Reload a model instance from database',
|
||||||
|
returns: {arg: 'data', type: 'object', root: true}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define readonly property on object
|
* Define readonly property on object
|
||||||
|
|
Loading…
Reference in New Issue