# mongoose-restful-crud

> A Mongoose CRUD for RESTful API based applications

Latest version **0.5.0** (published 2016-09-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install mongoose-restful-crud
pnpm add mongoose-restful-crud
yarn add mongoose-restful-crud
bun add mongoose-restful-crud
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2016-09-21 |
| First published | 2016-09-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| Author | Rafael Goulart |
| Maintainers | rafaelgou |
| Keywords | mongoose, nodejs, express, crud, api, middleware |

## Links

- npm: https://www.npmjs.com/package/mongoose-restful-crud
- Repository: https://github.com/rafaelgou/mongoose-api-crud
- Homepage: https://github.com/rafaelgou/mongoose-api-crud#readme
- Issues: https://github.com/rafaelgou/mongoose-api-crud/issues
- npm.io page: https://npm.io/package/mongoose-restful-crud

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^2.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.15.0
- [express](https://npm.io/package/express.md) ^4.14.0
- [mongoose](https://npm.io/package/mongoose.md) ^4.5.9

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.5.0 (latest) — 2016-09-21

## README

# Mongoose RESTful CRUD

**TODO: @jsdoc: [http://usejsdoc.org/](http://usejsdoc.org/)**

mongoose-restfull-crud is a simple express middleware to create a JSON API for
Mongoose models. As a middleware it can be nested with other middlewares
and it's very customizable (and extensible).

## install

```bash
npm install --save mongoose-restfull-crud
```

## Usage

3 Steps:

### 1. Create your Mongoose model

Something starting with:

```javascript
// Sample models/organization.js file

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const schema = new Schema({
  name: {
    type: String,
    required: 'Organization name is required.'
  },
  created: {
    type: Date,
    default: Date.now
  }
});
const Model = mongoose.model('Organization', schema);

module.exports = Model;
```

### 2. Creat a controller using the follwing pattern

```javascript
// Sample controller/organization.js file
'use strict';

const express = require('express');
const router = express.Router();
const Model = require('../models/organization');

const options = {
  // select: {}
  // filter: {}
  // sort: { _id: 'asc' }
  // page: 1
  // perPage: 20
  // population: null
};

const crud = require('mongoose-restfull-crud')(Model, options);

router.get('/', crud.get);
router.get('/:id', crud.getOne);
router.post('/', crud.post);
router.put('/:id', crud.put);
router.delete('/:id', crud.delete);

module.exports = router;
```

### 3. Add the routes to your express app

Don't forget to add [body-parser](https://github.com/expressjs/body-parser)
to get JSON response.

```javascript
// Sample ./app.js file
'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

require('./mongoose');

app.use(bodyParser.json());
app.use(require('./controllers'));

module.exports = app;
```

Then start the server as you like.

## CRUD options

**TODO: Explain how to use options.**

## Extending

**TODO: Explain how to extend.**

## Bulk Post

**TODO: Explain post using bulk.**

## JSON return

**TODO: Details, details.**

*    `GET /api/MODEL_NAME` (collection)

```javascript
{
  "_id": "57b51b95825a1d6354638c17",
  "name": "My boy",
  "__v": 0,
  "created": "2016-08-18T02:21:09.794Z"
}
```

*    `GET /api/MODEL_NAME/:id`

```javascript
{
  "page": 1,
  "pages": 1,
  "perPage": 20,
  "totalCount": 5,
  "results": [
    {
      "_id": "57b51b95825a1d6354638c17",
      "name": "My boy",
      "__v": 0,
      "created": "2016-08-18T02:21:09.794Z"
    },
    {
      "_id": "57b51c45f0d98dcf54603294",
      "name": "My boy",
      "__v": 0,
      "created": "2016-08-18T02:24:05.146Z"
    },
    {
      "_id": "57b51ca8528e550355c326c2",
      "name": "My boy",
      "__v": 0,
      "created": "2016-08-18T02:25:44.713Z"
    },
    {
      "_id": "57b51eb68dde44ad5680d531",
      "name": "My boy",
      "__v": 0,
      "created": "2016-08-18T02:34:30.090Z"
    },
    {
      "_id": "57b51f2145096eed56cbba42",
      "name": "My boy again",
      "__v": 0,
      "created": "2016-08-18T02:36:17.721Z"
    }
  ]
}
```

*    `POST /api/MODEL_NAME` or `PUT /api/MODEL_NAME/:id`

Returns the object created/updated (as `GET /api/MODEL_NAME/:id` or validation errors:

```javascript
{
  "errors": [
    {
      "name": "Organization name is required."
    }
  ]
}
```

*    `DELETE /api/MODEL_NAME/:id`

Returns a message:

```javascript
{
  "message": "Record removed"
}
```

OR

```javascript
{
  "errors": [
    "Record not found"
  ]
}
```

## Filtering, sorting, changing pagination

**TODO Explain.**


## Example

```bash
cd example
npm install
npm start
```

You have two models, `people` and `organization`. In this example you'll have
a [http://localhost:3000](http://localhost:3000) with the following endpoints:

*   GET /api/peoples
*   GET /api/peoples/:id
*   POST /api/peoples
*   PUT /api/peoples/:id
*   DELETE /api/peoples/:id

*   GET /api/organizations
*   GET /api/organizations/:id
*   POST /api/organizations
*   PUT /api/organizations/:id
*   DELETE /api/organizations/:id

You need to send requests using body and `Content-Type: application/json`.

CURL examples:

```bash
# Organization GET
curl -X GET -H "Cache-Control: no-cache" -H "Postman-Token: e12b3769-fb7a-eee5-9838-1fb0873fe322" "http://localhost:3000/api/organizations"

# Organization GET One
curl -X GET -H "Cache-Control: no-cache" -H "Postman-Token: 2d6df4da-1096-8314-0401-5912748c2ec0" "http://localhost:3000/api/organizations/57b51b95825a1d6354638c17"

# Organization POST
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 8063f2bd-001c-ef01-3c49-aa8f59d2ae8e" -d '{"name": "My Company"}' "http://localhost:3000/api/organizations"

# Organization PUT
curl -X PUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: f9878eb1-84f9-8844-9da5-5c7731daaac5" -d '{"name": "My Company has changed"}' "http://localhost:3000/api/organizations/57b624f63a1b29aa0f4e0821"

# Organization DELETE
curl -X DELETE -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 8c8add52-2c61-9a28-47ed-262163579623" -d '{}' "http://localhost:3000/api/organizations/57b51b95825a1d6354638c17"
```

---
_Source: https://npm.io/package/mongoose-restful-crud · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
