From 83371a93adda6cb7b83681041f79ec293b86ea39 Mon Sep 17 00:00:00 2001 From: Bryan Tong Date: Thu, 23 Jan 2014 12:44:26 -0700 Subject: [PATCH] added methods helper, ready for 0.2.2 --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ lib/crud.js | 15 +++++++++++++++ package.json | 2 +- test/crud.test.js | 18 ++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6da6a46..c86bfbb 100755 --- a/README.md +++ b/README.md @@ -26,6 +26,41 @@ var Crud = require('apx-helper-crud') module.exports = new Crud(Model,'test','Test Module') ``` +## Route Helper + +Helper CRUD will also help populate route methods to cut down on repetition. + +**config.js** +```js +var crud = require('apx-helper-crud') + +module.exports = { + express: { + routes: [ + {get: {path: '/myAction', file: 'actions/myAction.js', methods: crud.methods()}} + ] + } +} +``` + +You can also supply additional routes to the methods helper. + +**config.js** +```js +var crud = require('apx-helper-crud') + +module.exports = { + express: { + routes: [ + {get: {path: '/myAction', file: 'actions/myAction.js', methods: crud.methods(['extraAction','extraAction2'])}} + ] + } +} +``` + +Alternatively a string may be passed that will be added as a single method. Also a function may be passed that returns +either a string or an array of methods. + ## Constructor The constructor only takes the following arguments. @@ -127,6 +162,9 @@ Returns success or error only. ## Changelog +### 0.2.2 +* Added methods helper to populate routes + ### 0.2.1 * Crud helper now fills name and description along with default run task * Updated usage statement diff --git a/lib/crud.js b/lib/crud.js index 1564309..0be76f6 100755 --- a/lib/crud.js +++ b/lib/crud.js @@ -181,4 +181,19 @@ Helper.prototype.remove = function(apx,req,res,done){ } } +/** + * Static function to help populate routes + * @param userMethods + * @returns {string[]} + */ +Helper.methods = function(userMethods){ + var methods = ['find','findOne','list','save','remove'] + if('function' === typeof userMethods) userMethods = userMethods() + if('string' === typeof userMethods) userMethods = [userMethods] + if(userMethods instanceof Array){ + methods = methods.concat(userMethods) + } + return methods +} + module.exports = exports = Helper \ No newline at end of file diff --git a/package.json b/package.json index 760da00..f2f6955 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apx-helper-crud", - "version": "0.2.1", + "version": "0.2.2", "description": "CRUD (Create Read Update Delete) helper for APX API server actions ", "homepage": "https://github.com/snailjs/apx-helper-crud", "bugs": "https://github.com/snailjs/apx-helper-crud/issues", diff --git a/test/crud.test.js b/test/crud.test.js index 5cb1fc4..5905eb1 100755 --- a/test/crud.test.js +++ b/test/crud.test.js @@ -140,4 +140,22 @@ describe('HelperCrud',function(){ done() }) }) + it('should return default methods',function(){ + var methods = Crud.methods() + expect(methods).to.include.members(['find','findOne','list','save','remove']) + }) + it('should return default methods plus user methods',function(){ + var methods = Crud.methods(['login','logout']) + expect(methods).to.include.members(['find','findOne','list','save','remove','login','logout']) + }) + it('should add a single user method supplied as a string',function(){ + var methods = Crud.methods('login') + expect(methods).to.include.members(['find','findOne','list','save','remove','login']) + }) + it('should allow a function to be supplied to methods',function(){ + var methods = Crud.methods(function(){ + return ['login','logout'] + }) + expect(methods).to.include.members(['find','findOne','list','save','remove','login','logout']) + }) }) \ No newline at end of file