Skip to content

jerrymannel/mongoose-express-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoose-express-middleware

Express CRUD middleware for mongoose

Build Status Known Vulnerabilities

Quickstart

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)

Table of contents

Constructor

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.

Methods

All methods in two parameters. An express request object and an express response object

MongooseExpressMiddleware.create(req, res)

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.

MongooseExpressMiddleware.update(req, res)

Update a single document where the :id matches the _id of the document

E.g. app.put("/foo/:id", fooCrud.update)

MongooseExpressMiddleware.index(req, res)

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.

MongooseExpressMiddleware.show(req, res)

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.

MongooseExpressMiddleware.destroy(req, res)

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.

MongooseExpressMiddleware.bulkShow(req, res)

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.

MongooseExpressMiddleware.bulkUpdate(req, res)

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

MongooseExpressMiddleware.bulkDestroy(req, res)

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.

About

mongoose-crud middleware for Express

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published