Express CRUDify Mongoose
Like express-restify-mongoose but with way less ambition.
Produces a simple CRUD interface.
Dependencies / tools
- Babel
- ..
Usage
npm install express-crudify-mongoose --save
const UserSchema = name : type: String required: true email: type: String required: true admin: type: Boolean default: false required: true; const Users = db;const readonly = 'admin'; const crud = ; server;
Endpoints
- GET
/users
- POST
/users
- GET
/users:_id
- PATCH
/users/:_id
- DELETE
/users/:_id
Operations
Filter
GET /users?name=Alex
Select all users with where name Alex
.
The query gets forwarded straight to MongoDB, so it's possible to write queries like /posts?date[$gt]=2016-01-01T00:00:00.000Z
to get all posts dated after 2016.
⚠️ If you're making a public-facing API, you might want to use preBuildQuery
middleware to validate the params used.
Selecting partial outputs
GET /users?$select=name,email
GET /users/:id?$select=name,email
Only output name & email.
Pagination
GET /users?$limit=10
GET /users?$limit=10&skip=10
Middlewares
Middlewares can be async by being ES7 async functions or functions returning promises.
If you pass in an array of functions, they'll be executed sequentially.
preSave
Useful for custom validation, that you'd only want to run when change is done through API request and can therefore simply not be done by Mongoose's .pre('save', fn)
.
const readonly = 'admin';const Users = db; const preSave = async { if useradmin let err = "Can't change admin users through API"; errstatusCode = 400; throw err; let err = 'Validation failed'; const newData = data; // return the newData that you want in the output return newData;} const crud = ;
preOutput
preOutput
functions will receive an object with properties data
and req
.
req
is the currentdata
is the data that is currently set to be output to client.
Note that preOutput
is run the same for all endpoints, so sometimes data
is an array and sometimes it's a singular item.
const Users = db; const preOutput = async { const newData = data; // return the newData that you want in the output return newData;} const crud = ;
Development
- Clone project
make setup
make run
You can run example project with make example-babel
Git Commit Messages
Based on atom & dannyfritz/commit-message-emoji.
-
Use the present tense ("Add feature" not "Added feature")
-
Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
-
Limit the first line to 72 characters or less
-
Reference issues and pull requests liberally
-
Start the commit message with an applicable emoji:
Commit Type Emoji Initial Commit 🎉 :tada:
Version Tag 🔖 :bookmark:
New Feature ✨ :sparkles:
Bugfix 🐛 :bug:
Metadata 📇 :card_index:
Documentation 📚 :books:
Performance 🐎 :racehorse:
Cosmetic/refactor 🎨 :art:
Lint fixes 👕 :shirt:
Tests ✅ :white_check_mark:
Removing files 🔥 :fire:
Security 🔒 :lock:
Upgrade deps ⬆️ :arrow_up:
Downgrade deps ⬇️ :arrow_down:
General Update ⚡️ :zap:
Adding to debt 😷 :mask:
Other Be creative
Code
TBC
Run project:
make run
Deploy
TBC
Documentation
Team
- Development: