# @leonardosarmentocastro/crud

> crud

Latest version **2.4.5** (published 2024-06-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install @leonardosarmentocastro/crud
pnpm add @leonardosarmentocastro/crud
yarn add @leonardosarmentocastro/crud
bun add @leonardosarmentocastro/crud
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.5 |
| Published | 2024-06-24 |
| First published | 2021-05-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| Author | leonardosarmentocastro |
| Keywords | crud, mongoose |

## Links

- npm: https://www.npmjs.com/package/@leonardosarmentocastro/crud
- npm.io page: https://npm.io/package/@leonardosarmentocastro/crud

## Dependencies (8)

- [mongoose](https://npm.io/package/mongoose.md) 6.5.0
- [pluralize](https://npm.io/package/pluralize.md) ^8.0.0
- [validator](https://npm.io/package/validator.md) ^13.6.0
- [@leonardosarmentocastro/i18n](https://npm.io/package/@leonardosarmentocastro/i18n.md) ^2.0.0
- [@leonardosarmentocastro/server](https://npm.io/package/@leonardosarmentocastro/server.md) ^2.0.0
- [@leonardosarmentocastro/database](https://npm.io/package/@leonardosarmentocastro/database.md) ^2.4.5
- [@leonardosarmentocastro/validate](https://npm.io/package/@leonardosarmentocastro/validate.md) ^2.1.3
- [@leonardosarmentocastro/pagination](https://npm.io/package/@leonardosarmentocastro/pagination.md) ^2.4.5

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

- 2.4.5 (latest) — 2024-06-24
- 2.4.2 — 2024-06-24
- 2.1.3 — 2024-02-12
- 2.0.0 — 2023-12-07
- 1.4.27 — 2023-04-30
- 1.4.26 — 2023-04-24
- 1.4.25 — 2023-04-20
- 1.4.24 — 2023-04-20
- 1.4.20 — 2023-03-05
- 1.4.16 — 2023-02-02
- 1.4.5 — 2023-02-01
- 1.4.0 — 2023-02-01
- 1.3.1 — 2022-08-30
- 1.3.0 — 2022-08-29
- 1.2.0 — 2022-08-29
- … 11 more at https://npm.io/package/@leonardosarmentocastro/crud/versions

## README

# crud

create endpoints for mongoose models

```js
const mongoose = require('mongoose');
const server = require('@leonardosarmentocastro/server');
const i18n = require('@leonardosarmentocastro/i18n'); // mandatory
const { crud } = require('@leonardosarmentocastro/crud');

(async () => {
  const api = await server.start(8080, {
    middlewares: (app) => {
      i18n.connect(app); // mandatory
    },
    routes: (app) => {
      const schema = new mongoose.Schema({ name: String });
      const model = new mongoose.model('Product', schema);
      crud.connect(app, model); // creates "[GET, POST] /users" + "[GET, PUT, DELETE] /users/:id"
    },
  });
})();
```

then see it yourself

```js
const got = require('got');

const PRODUCT1 = { name: 'product1' };
const url = (id = '') => `http://127.0.0.1:8080/products${id ? '/' + id : ''}`;

(async () => {
  const response1 = await got.post(url(), { json: PRODUCT1 }); // "[POST] /products"
  const { _id, name } = response1.body; // crud [C]reated

  const response2 = await got(url(_id)); // "[GET] /products/:id"
  console.log('products:', response2.body); // crud [R]ead...
})();
```

## api

### crud.connect(app = express(), model = mongoose.model())

Connects a mongoose model to a set of CRUD (create, read, update, delete) routes.

* `app`: an express app (`const app = require('express')()`)
* `model`: a mongoose model (which represents a document in your MongoDB)

e.g.

```js
const mongoose = require('mongoose');
const { crud } = require('@leonardosarmentocastro/crud');

const app = require('express')();
const schema = new mongoose.Schema({ name: String });
const model = new mongoose.model('Product', schema);
crud.connect(app, model);

// following endpoints are created:
// [GET] /users (comes with "@leonardosarmentocastro/pagination")
// [POST] /users
// [GET] /users/:id
// [PUT] /users/:id
// [DELETE] /users/:id
```

### peer dependencies

* `@leonardosarmentocastro/pagination`
* `@leonardosarmentocastro/i18n`

## translations

its mandatory to set these translations codes to their respective files:

* `ERROR_DOCUMENT_NOT_FOUND`
* `ERROR_UNEXPECTED`

e.g.

```yml
# ./translations/en-us.yml
ERROR_DOCUMENT_NOT_FOUND: Document for id "{id}" was not found.
ERROR_UNEXPECTED: Sorry, an unexpected error has occurred.

# ./translations/pt-br.yml
ERROR_DOCUMENT_NOT_FOUND: Documento para id "{id}" não foi encontrado.
ERROR_UNEXPECTED: Desculpe, ocorreu um erro inesperado.
```

thats it.

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