crud-mongoose-express
This package allows to generate CRUD routes in a Mongoose/Express app.
Installation
npm install crud-mongoose-express
Usage
const express = ;const router = express;const basicCrud = ;... app;
the make()
function returns a router and take 2 mandatory arguments:
- an Express router instance
- an array of object or an object (explained below)
To handle relations between models you have to add a foreignKey attribute in your Model:
const productSchema = mongoose; const storeSchema = mongoose;
For each model in the option array, the following routes will be generated:
Note {prefix} corresponds to the value you pass to the basicCrudOptions object and is the pluralized name of the model by default
{related} corresponds to the local key of a reference in the schema (for example with the 2 schemas above, in the StoreSchema {related} could be equals to "products")
Route name | Verb | Url | Definition | Parameters |
---|---|---|---|---|
get | GET | /{prefix} | Get all documents of a collection | include={related} fields=fieldname1,fieldname2... fields[{related}]=fieldname1,fieldname2... sort=fieldname1,... or -fieldname1... limit=number skip=number filter= string (OData query) filter[{related}]= string (OData query) e.g: mydomain.com/products?filter=type%20eq%20%27beverage%27&fields=name&include=stores&fields[stores]=name |
getById | GET | /{prefix}/:id | Get document by id | include={related}, fields=fieldname1,fieldname2... |
post | POST | /{prefix} | Create new document | POST data: {fieldname1: value1, ..} |
patch | PATCH | /{prefix}/:id | Update a document | POST data: {fieldname1: value1, ..} |
delete | DELETE | {prefix}/:id | Delete a document |
the following routes will be generated if the model contains a reference to another one:
Note: {related} corresponds to the local key of a reference in the schema (for example with the 2 schemas above, in the StoreSchema {related} could be equals to "products")
Route name | Verb | Url | Definition | Parameters |
---|---|---|---|---|
getRelation | GET | {prefix}/:id/{related} | get the documents associated to the document with :id | include={related} fields[{related}]=fieldname1,fieldname2... filter[{related}]= string (OData query) e.g: mydomain.com/products/1/stores?include=stocks&fields[stocks]=quantity&fields[stores]=name,stocks |
associate | POST | {prefix}/:id/{related} | associate a document with another | {related} : Array e.g: mydomain.com/products/1/stores Payload: { stores: [1, 2, 3] } |
deleteAssociation | DELETE | {prefix}/:id/{related} | remove document association | same as above |
Exemple 1
const express = ;const router = express;const mongoose = ;const basicCrud = ; //Mongoose Models const Product = ;const Shop = ; //An object containing middlewares to apply to the generated routes const authMiddlewares = ; ... ... const basicCrudOptions = Model: Product middlewares: 'deleteById, patch': authMiddlewares Model: Shop middlewares: 'deleteById, patch, associate': authMiddlewares ; app;app;
Exemple 2
You can also use it in a separated route definition file
app.js
const express = ;const path = ;const cookieParser = ;const bodyParser = ;const mongoose = ;const userRoutes = ; const app = ;mongoose; app;app;app; app;app;
/routes/user.js
const express = ;const router = express;const User = ;const basicCrud = ;const authMiddlewares = ; //Implement your own POST method (to register a user for example) router; //add any route you want to (here signIn for example)router; const basicCrudOptions = Model: User middlewares: 'getById, deleteById, patch': authMiddlewares 'get': authMiddlewares disableRoutes: 'post' // as we re-implemented it we have to disable the default one prefix: '' // as we set the prefix in the app.js file, we set it to empty here ; basicCrud;moduleexports = router;
BasicCrudOptions (object):
Key | Type | Definition |
---|---|---|
Model | Mongoose model | |
middlewares | Object | Comma separated names of the routes as key and middleware to apply as value |
disableRoutes | Array of strings | names of the routes to disable |
prefix | String | override the prefix used for the route. Pluralized model name is used by default (for example Product -> products) |
Extra Options
You can pass an object as third parameter to the make()
function. For the moment there is only one extra option and you can use it like this:
const extra = endpoints: routes: enabled: true params: url: '/mcrud/routes' ; basicCrud;
this option creates an extra route accessible by default at yourApp/mcrud/routes
(or at the value passed to params['url']).
This route lists all the routes that were created with crud-mongoose-express.
Note: if you prefixed the routes with app.use( "prefix", routes), as in Example 2, the prefix won't appear in the routes list.
available parameters are: url or prefix (which add a prefix to /mcrud/routes
). If you set both, prefix will be ignored.