express-crudify-mongoose
v5.0.1
Published
Express CRUDify Mongoose =========================
Downloads
24
Readme
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 = new Schema({
name : {type: String, required: true},
email: {type: String, required: true},
admin: {type: Boolean, default: false, required: true},
});
const Users = db.model('user', UserSchema);
const readonly = ['admin'];
const crud = crudify({
readonly,
Model: Users,
});
server.use('/users', crud);
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.
:warning: 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.model('user', UserSchema);
const preSave = async (user) => {
if (user.admin) {
let err = new Error("Can't change admin users through API");
err.statusCode = 400;
throw err;
}
let err = new Error('Validation failed');
const newData = data;
// return the newData that you want in the output
return newData;
}
const crud = crudify({
Model: Users,
readonly,
preSave, // preSave: [preSave, ..] is also supported
});
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.model('user', UserSchema);
const preOutput = async (data) => {
const newData = data;
// return the newData that you want in the output
return newData;
}
const crud = crudify({
Model: Users,
preOutput, // preOutput: [preOutput, ..] is also supported
});
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:
:tada:
Version Tag | :bookmark::bookmark:
New Feature | :sparkles::sparkles:
Bugfix | :bug::bug:
Metadata | :card_index::card_index:
Documentation | :books::books:
Performance | :racehorse::racehorse:
Cosmetic/refactor | :art::art:
Lint fixes | :shirt::shirt:
Tests | :white_check_mark::white_check_mark:
Removing files | :fire::fire:
Security | :lock::lock:
Upgrade deps | :arrow_up::arrow_up:
Downgrade deps | :arrow_down::arrow_down:
General Update | :zap::zap:
Adding to debt | :mask::mask:
Other | Be creative
Code
TBC
Run project:
make run
Deploy
TBC
Documentation
Team
- Development: