2.0.1 • Published 7 months ago

mongoose-express-middleware v2.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 months ago

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. 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

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)

RequestResponseStatus CodeCondition
JSONJSON200 OKSuccess
JSONJSON400 Bad RequestError in payload
Array of JSONArray of JSON200 OKSuccess
Array of JSONArray of JSON207 Multi-StatusSome of the documents where inserted, some had errors. The response array has the same order of input array.
Array of JSONArray of JSON400 Bad RequestAll 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.

ParamTypeDescription
filterJSONFilter condition for the documents. This filter gets merged with defaultFilter if one was defined when the MongooseExpressMiddleware object was instantiated.
countBooleanReturns the count of the documents after applying the filter. When count is enabled only filter paramerter takes effect.
pageNumberSpecify the page number of the paginated data. Default 1.
limitNumberSpecify the number for documents per page. Default 10.
selectStringList of comma-separated attributes of the document to display. If the attribute is preceded by a "-", then the attribute is omitted.
sortStringThe 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)

ParamTypeDescription
selectStringList 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)

ParamTypeDescription
idStringList of comma-separated ids of the document to display
selectStringList of comma-separated attributes of the document to display. If the attribute is preceded by a "-", then the attribute is omitted.
sortStringThe 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)

ParamTypeDescription
idStringList 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.

2.0.1

7 months ago

2.0.0

7 months ago

1.2.0

3 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago