npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-express-middleware

v2.0.1

Published

An opinionated mongoose middleware for Express

Downloads

36

Readme

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.