Express CRUD middleware for mongoose
Install
npm install mongoose-express-middleware
Define your schema and create a new mongoose-express-middleware
var definition = {
"_id": { "type": String },
"name": { "type": String },
"description": { "type": String },
"age" : {"type" : Number}
}
var schema = Mongoose.Schema(definition)
var modelName = "foobar"
var options = {
collectionName: "foobar",
defaultFilter: {
"age": {"$gte": 10}
}
}
schema.pre("save", function(next){
if(!this._id) this._id = new Mongoose.Types.ObjectId();
next()
})
var fooCrud = new MongooseExpressMiddleware(modelName, schema, options)
Add the middleware to express
var app = express()
app.use(express.json())
app.get('/', crud.find);
app.get('/:id', crud.findById);
app.get('/utils/count', crud.count);
app.post('/', crud.create);
app.put('/:id', crud.update);
app.delete('/', crud.deleteMany);
app.delete('/:id', crud.deleteById);
app.post('/utils/aggregate', crud.aggregate);
app.listen(8080)
- mongoose-express-middleware
- Quickstart
- Table of contents
- Constructor
- Methods
- MongooseExpressMiddleware.create(
req
,res
) - MongooseExpressMiddleware.update(
req
,res
) - MongooseExpressMiddleware.index(
req
,res
) - MongooseExpressMiddleware.show(
req
,res
) - MongooseExpressMiddleware.destroy(
req
,res
) - MongooseExpressMiddleware.bulkShow(
req
,res
) - MongooseExpressMiddleware.bulkUpdate(
req
,res
) - MongooseExpressMiddleware.bulkDestroy(
req
,res
)
- MongooseExpressMiddleware.create(
var fooCrud = new MongooseExpressMiddleware(modelName, schema, options)
The constructor takes 3 values,
- modelName(Required): Name of the mongoose model.
- schema(Required): The schema object returned by
Mongoose.Schema()
- *options(Optional): An optional options object. This has two properties.
- collectionName: By default Mongoose uses the pluralised model name as the collection name. If you wish to override this, then provide your custom collection name here.
- defaultFilter: A default filter to be applied to all
GET
calls. - logger: A logger object. By default this will use log4js
All methods in two parameters. An express request object and an express response object
Create a new document using the data in req.body
.
E.g. app.post("/foo", fooCrud.create)
Request | Response | Status Code | Condition |
---|---|---|---|
JSON | JSON | 200 OK |
Success |
JSON | JSON | 400 Bad Request |
Error in payload |
Array of JSON | Array of JSON | 200 OK |
Success |
Array of JSON | Array of JSON | 207 Multi-Status |
Some of the documents where inserted, some had errors. The response array has the same order of input array. |
Array of JSON | Array of JSON | 400 Bad Request |
All documents in the array had errors. |
Update a single document where the :id
matches the _id
of the document
E.g. app.put("/foo/:id", fooCrud.update)
Displays the documents in the collection. URL parameters are used to influence the output generated.
E.g. app.get("/foo", fooCrud.index)
The following are URL params are available.
Param | Type | Description |
---|---|---|
filter |
JSON | Filter condition for the documents. This filter gets merged with defaultFilter if one was defined when the MongooseExpressMiddleware object was instantiated. |
count |
Boolean | Returns the count of the documents after applying the filter. When count is enabled only filter paramerter takes effect. |
page |
Number | Specify the page number of the paginated data. Default 1. |
limit |
Number | Specify the number for documents per page. Default 10. |
select |
String | List of comma-separated attributes of the document to display. If the attribute is preceded by a "-", then the attribute is omitted. |
sort |
String | The attributes on which the results have to be sorted. By default, the documents are sorted in ascending order. If the attribute is preceded by a "-", then the sorting is done in descending order. |
Display a single document where the :id
matches the _id
of the document.
E.g. app.get("/foo/:id", fooCrud.show)
Param | Type | Description |
---|---|---|
select |
String | List of comma-separated attributes of the document to display. If the attribute is preceded by a "-", then the attribute is omitted. |
Deletes a single document where the :id
matches the _id
of the document.
E.g. app.delete("/foo/:id", fooCrud.destroy)
If the document is found and deleted, then 200 OK
is returned. If no document gets deleted, then 204 No Content
is returned.
Display multiple documents for the given set of _ids. This is a convenience function over MongooseExpressMiddleware.index()
with filter
.
E.g. app.get("/foo/bulkShow", fooCrud.bulkShow)
Param | Type | Description |
---|---|---|
id |
String | List of comma-separated ids of the document to display |
select |
String | List of comma-separated attributes of the document to display. If the attribute is preceded by a "-", then the attribute is omitted. |
sort |
String | The attributes on which the results have to be sorted. By default, the documents are sorted in ascending order. If the attribute is preceded by a "-", then the sorting is done in descending order. |
Update multiple documents in a single request. The request has to be an array, with each document having an _id
.
E.g. app.put("/foo/bulkUpdate", fooCrud.bulkUpdate)
The response is an array of updated documents in the same order of input request. If an _id
can't be located, then the response would be null
for that document
Delete multiple documents for the given set of _ids.
E.g. app.delete("/foo/bulkDelete", fooCrud.bulkDestroy)
Param | Type | Description |
---|---|---|
id |
String | List of comma-separated ids of the document to delete |
If all the document were found and deleted, then 200 OK
is returned. If no documents get deleted, then 204 No Content
is returned.