Mongoose REST api generator for Rill with flexible defaults and easy overrides.
npm install @rill/mongoose
- Feature packed default routes.
- Automatically sanitize inputs/outputs (query/body) for hidden fields.
- Easily override or add to default middleware.
- Uses lean / optimized queries.
- Good query api with many features including populate, select, limit, skip and sort.
const Rill =const resource =const app =// Create any mongoose model.const User = mongoose// Setup a "GET/find" request for the user route using the optimized default middleware.app
// Consume using WHATWG Fetch api.
- resource([path: String], Model: Mongoose.model, methods: Object) : Creates a REST api for the provided model with the configured methods.
// Choose which methods to enable.app// Add more middleware to methods (while optionally still using defaults).app// Also override the default path for the handlers.app
querystring is sanitized then passed into
Model.find allowing for any sort of Mongoose style query in a safe way.
All queries will also add an
X-Total-Count http header which is the total number of documents found ignoring
$limit, useful for pagination.
// Find all users who are age 10 and have logged in after 2015.
Special Query Operators
There are some special operators to make working with the api more flexible.
- $skip : skip a number of results.
// Skip the first 10 users.
- $limit : limit the number of results.
// Get at most 10 users.
- $sort : sort the results.
// Sort users by name, then email.// Sort users by name in descending order.
- $select : limit the fields in the results.
// Get all users, but only with their name and email and _id.// Same query, but omit _id this time.
- $populate : populate related fields for the Model.
// Will `populate` the `posts` and `friends` field if they are not hidden on the api for all users.// populate specific fields. (wrap fields in "")// `populate` posts for each user but only include the post title and date.// nested populate. (separate nested populate with ":")// `populate` all `posts`, with only the `author` - then populate all posts authors with only their `name`.
POST to the api to create new documents for the
// Create a new user.
You can use
PUT/:id to replace an existing document and
PATCH/:id to update a document.
// Overwrite an existing user.// Update an existing user.
You can use
DELETE/:id to permanently remove a document.
// Remove an existing user.
Mongoose validation is also automatically handled for
npm test for tests and feel free to create a PR!