README
If you use Express.js and Mongoose, these common services may be helpful for you
USAGE:
const emcs = require("express-mongoose-common-service");
const testService = emcs.generate(dbTable, identifierName, returnMsg);
OR
import emcs from "express-mongoose-common-service";
const testService = emcs.generate(dbTable, identifierName, returnMsg);
In which,
- dbTable: is the mongoose table, ex. db.Test
- identifierName: the identifer field in your table, in my case that is 'id'
- returnMsg: you can leave this argument empty to use the default value as below
returnMsg = {
createFailDuplicate: (identifierValue) =>
`Id ${identifierValue} is already taken.`,
updateFailValidate: "Record can not be found",
updateFailDuplicate: (identifierValue) =>
`Id ${identifierValue} is already taken.`,
};
or override it on your own will.
Methods return on calling the method generate:
- getAll
- getById
- create
- update
- delete
- getAllByUserId
- getByIdByUserId
- createByUserId
- updateByUserId
- deleteByUserId
- backup
- getAllWithPagination
- getAllWithPaginationByUserId
To use getAllWithPagination
& getAllWithPaginationByUserId
, install mongoose-paginate-v2
and add it to your schema before using these functions