Skip to content

Commit

Permalink
added methods helper, ready for 0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nullivex committed Jan 23, 2014
1 parent fe10304 commit 83371a9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
38 changes: 38 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/crud.js
Expand Up @@ -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
2 changes: 1 addition & 1 deletion 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",
Expand Down
18 changes: 18 additions & 0 deletions test/crud.test.js
Expand Up @@ -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'])
})
})

0 comments on commit 83371a9

Please sign in to comment.