Like express-restify-mongoose but with way less ambition.
Produces a simple CRUD interface.
- Babel
- ..
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);
- GET
/users
- POST
/users
- GET
/users:_id
- PATCH
/users/:_id
- DELETE
/users/:_id
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.
preBuildQuery
middleware to validate the params used.
GET /users?$select=name,email
GET /users/:id?$select=name,email
Only output name & email.
GET /users?$limit=10
GET /users?$limit=10&skip=10
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.
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
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
});
- Clone project
make setup
make run
You can run example project with make example-babel
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
TBC
make run
TBC
- Development: