CRUD (Create Read Update Delete) helper for APX API server actions
Requires Mongoose Models and the mongoose-list and mongoose-merge plugin plugins.
Simply require the helper within your actions
$ npm install apx-helper-crud --save
var Crud = require('apx-helper-crud')
, Model = require('./models/test').model
//will populate all the following crud functions
// * find
// * findOne
// * list (which required the model to use the mongoose-list plugin)
// * save
// * remove
module.exports = new Crud(Model,'test','Test Module')
Helper CRUD will also help populate route methods to cut down on repetition.
config.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
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.
The constructor only takes the following arguments.
var Crud = require('apx-helper-crud')
, Model = require('./models/test').model
, crud = new Crud(Model,'test','Test Module')
All of the Crud methods are APX actions so they accept request data directly and return APX responses.
Find records by the criteria in the data object.
Takes a Mongoose query object as the only argument.
{name: 'test doc'}
Returns an object of results.
{results: [{name: 'test doc'}]}
Find a single record by the criteriea in the data object. Useful for finding items by ObjectID
Takes a mongoose query object as the only argument.
{name: 'test doc'}
Returns an object of results.
{result: {name: 'test doc'}}
Utilizes the mongoose-list plugin to iterate records in a collection.
Takes a mongoose-list arugment object.
{start: 0, limit: 10, find: 'foo', sort: 'bar'}
Returns a results object
{count: 10, results: [{name: 'test doc'}]}
Save one or many documents.
IMPORTANT requires the mongoose-merge plugin to be loaded in the model.
Accepts a document or an array of documents.
[{name: 'test doc'}]
Returns a result object
{results: [{name: 'test doc'}]}
Remove one or many documents.
Accepts a query object or an array of query objects.
[{name: 'test doc'}]
Returns success or error only.
If apx-roles is loaded at initialization they will be implemented by the crud helper unless explicitly disabled.
The use of roles also requires apx-session to be loaded as it will look for
apx.session.get('profile')
to test against and will default to the profile guest
.
Take a look at the following example
module.exports = new Crud(Model,'foo','my crud action')
This will expose the following roles.
find
, findOne
, and list
are all considered part of the find
permission.
To disable roles even with the module loaded, try the following
module.exports = new Crud(Model,'foo','my crud action',{useRoles: false})
'use strict';
in all filesitems
in the requestsave
, remove
and findOne
save
, remove
and findOne
doc.toJSON({virtuals: true})
Language | javascript |
Version | 0.2.5 |
Git URL | https://github.com/nullivex/apx-helper-crud |
License | MIT |
Description | CRUD (Create Read Update Delete) helper for APX API server actions |
Keywords |