mcrud
v3.0.0
Published
Smallest CRUD wrapper over mongodb client with Custom ID generator
Downloads
3
Maintainers
Readme
mcrud
Smallest CRUD wrapper over mongodb client with Custom ID generator.
APIs
- count : Get number of records with filter
- list : Get records with filter, select, pagination and sorting
- get : Get record selected fields
- post : Create one or many record with custom ID generation
- put : Update a record with recordId
- delete : Delete a record with recordId
- collection : the MongoDB Collection Object
Example POST
const MCURD = require('mcrud');
const crud = new MCURD({
collection: 'test',
database: 'mcrud',
url: 'mongodb://localhost:27017',
idPattern: 'TE#####ST##'
});
const payload = {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
gender: 'Male'
};
crud.post(payload).then(doc => {
console.log('document inserted');
/**
* Inserted document
* {
* _id:'TE00000ST01',
* firstName: 'John',
* lastName: 'Doe',
* email: '[email protected]',
* gender: 'Male'
* }
*/
}).catch(err => {
console.log(err);
});
//or insert many
const payload = [
{
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
gender: 'Male'
},
{
firstName: 'Jane',
lastName: 'Doe',
email: '[email protected]',
gender: 'Female'
}
];
crud.post(payload).then(docs => {
console.log('documents inserted');
/**
* Inserted documents
* {
* _id:'TE00000ST02',
* firstName: 'John',
* lastName: 'Doe',
* email: '[email protected]',
* gender: 'Male'
* }
* {
* _id:'TE00000ST03',
* firstName: 'Jane',
* lastName: 'Doe',
* email: '[email protected]',
* gender: 'Female'
* }
*/
}).catch(err => {
console.log(err);
});
Example Express Routes
const router = require('express').Router();
const MCURD = require('mcrud');
const crud = new MCURD({
collection: 'test',
database: 'mcrud',
url: 'mongodb://localhost:27017',
idPattern: 'TE#####ST##'
});
router.use(crud.expressRoutes());
// consume
HTTP [GET] /
HTTP [POST] /
HTTP [GET] /:id
HTTP [PUT] /:id
HTTP [DELETE] /:id