# @rapida/rest

> An abstraction helper for REST API that creates an abstract controller that communicate with the database in a consistent way

Latest version **1.3.2** (published 2021-05-25) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @rapida/rest
pnpm add @rapida/rest
yarn add @rapida/rest
bun add @rapida/rest
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.3.2 |
| Published | 2021-05-25 |
| First published | 2019-12-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Yahia Refaiea |
| Maintainers | yahiarefaiea |

## Links

- npm: https://www.npmjs.com/package/@rapida/rest
- Repository: https://github.com/yahiarefaiea/rapida/tree/master/packages/rapida-rest
- Homepage: https://github.com/yahiarefaiea/rapida
- npm.io page: https://npm.io/package/@rapida/rest

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) 4.17.15
- [@rapida/response](https://npm.io/package/@rapida/response.md) 1.3.2

## Recent versions

- 1.3.2 (latest) — 2021-05-25
- 1.3.0 — 2020-07-06
- 1.2.0 — 2020-04-12
- 1.1.0 — 2020-01-05
- 1.0.2 — 2020-01-02
- 1.0.1 — 2019-12-11
- 1.0.0 — 2019-12-06

## README

![Rapida Logo](https://user-images.githubusercontent.com/13030990/71755098-db251e80-2e91-11ea-8ce9-25349e6c087f.png)

**Rapida REST** - An abstraction helper for REST API that creates an abstract controller that communicate with the database in a consistent way.

**Warning** - Rapida REST has been deprecated and it's no longer maintained.

This package is part of [Rapida](https://github.com/yahiarefaiea/rapida).

## Usage
In your project directory, install `express`, `mongoose`, and `@rapida/rest` by running:
```
npm i express mongoose @rapida/rest --save
```

Import `express`, `mongoose`, and `@rapida/rest` to your project:
```js
const express = require('express')
const mongoose = require('mongoose')
const AbstractController = require('@rapida/rest')
```

After setting up express and mongoose, create a new instance of the `AbstractController` and pass a model to it:
```js
const app = express()
mongoose.connect('mongodb://localhost:27017/rapida-rest')

const Model = mongoose.model('Book', new mongoose.Schema({title: String, author: String}))
const controller = new AbstractController(Model)
```

Now, you have all the endpoints needed for you to use:
```js
app.get('/book', controller.getAll)
```

## Methods
The available methods are:
+ `controller.getAll`: Get (read) all resources.
+ `controller.post`: Post (create) a new resource.
+ `controller.get`: Get (read) an existing resource.
+ `controller.patch`: Patch (update) an existing resource.
+ `controller.delete`: Delete an existing resource.

**Note**: The `get`, `patch`, and `delete` methods must have an `:id` specified in the Express params object (`req.params.id`). Check the [Advanced Usage](#advanced-usage) to see an example.

## Advanced Usage
You can extend the `AbstractController` to update a specific endpoint (or maybe create a new one):
```js
// controller.js
const AbstractController = require('@rapida/rest')
const Model = require('./path/to/model')

class Controller extends AbstractController {
  constructor(model) {
    super(model)
  }

  getAll(req, res, next) {
    // do something here before calling the super.getAll()
    super.getAll(req, res, next)
  }
}

module.exports = new Controller(Model)
```

**Note**: The code above uses ES6 syntax which uses the `class` keyword. You might need to use [babel](https://github.com/babel/babel) to act as your transpiler.

And now you can use the controller anywhere in your project:
```js
// anywhere.js
const controller = require('./path/to/controller')
const router = express.Router()

router.route('/')
  .get(controller.getAll)
  .post(controller.post)

router.route('/:id')
  .get(controller.get)
  .patch(controller.patch)
  .delete(controller.delete)
```

## Demo
To run a working demo, run the following command:
```
npm start -s
```

You can find the demo inside `demo/index.js`.

## License
Copyright (c) 2020 Nuotron.
Released under the [MIT license](https://github.com/github/choosealicense.com/blob/gh-pages/LICENSE.md).

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