QuickCrud.js
An easy CRUD operation based on Factory pattern with Mongoose. There are four CRUD operations we can do:
-
index({mode, where, populateOptions, paginationOptions})
: Fetch all documents with pagination.model
- Mongoose model.where
- MongoDB filter object.populateOptions
- Mongoose population object/string.paginationOptions
can get three parameters.limit:number
- Resource count to show. Default is 10page:number
- Pagination page number. Default is 1.sort:string
- MongoDB property sort key. Default is'-createdAt'
-
store({model, data})
- You can create a doc and store it to MongoDB.model
- Mongoose model.data
- An object of data to store in MongoDB based on Mongoose Schema
-
show({model, where, populationOptions})
: Fetch a single document via filter key.model
- Mongoose model.where
- MongoDB filter object.populateOptions
- Mongoose population object/string.
-
update({model, where, data})
- updates the first document that matcheswhere
.data
is the object where you want to update the data.model
- Mongoose model.where
- MongoDB filter object.data
- An object of data to update that matches with where filter key(s).
-
destroy({model, where})
- Deletes the first documents that matcheswhere
from the collection. it returns the document that has been deleted.model
- Mongoose model.where
- MongoDB filter object.
-
destroyAll({model: where})
- Deletes all documents that matcheswhere
from the collection.model
- Mongoose model.where
- MongoDB filter object.
Examples
Store
const qc = qc
Example Response
_id: 'xxxx' name: 'John' username: 'johnDoe' __v: 0
Index
Fetch all resources with pagination
Fetch all users
const qc = qcindexmodel: UserModel
Response
currentPage: 1 pageCount: 1 resourceCount: 4 data: _id: 'xxxxxx' name: 'User 1' username: 'username1' __v: 0 _id: 'xxxxxx' name: 'User 2' username: 'username2' __v: 0 //..............
Fetch all posts with user polulation
const qc = qcindexmodel: PostModel paginationOptions: page: 2 limit: 5 populationOptions: path : 'user'
Response
Fetch all posts those are published
qcindexmodel: PostModel where: published: true paginationOptions: page: 2 limit: 5
Response
currentPage: 1 pageCount: 1 resourceCount: 3 data: _id: 'xxxx' title: 'post1' published: true __v: 0 _id: 'xxxx' title: 'post1' published: true __v: 0 _id: 'xxxx' title: 'post1' published: true __v: 0
Update
Update a post by id
const qc = qc
Response
_id: 'xxxx' title: 'title updated' body: 'example body 10' user: 'xxxx' __v: 0
Destroy
Delete a user with username
const qc = qc
Response
_id: 'xxxx' name: 'new name' __v: 0 username: 'newusername'
Throws exception if no document found
// ExceptionUnhandledPromiseRejectionWarning: QuickCrudException: Resource not found