# sequelize-repository

> Implementation of repository pattern for sequelize

Latest version **1.1.2** (published 2022-01-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install sequelize-repository
pnpm add sequelize-repository
yarn add sequelize-repository
bun add sequelize-repository
```

## 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 | 1.1.2 |
| Published | 2022-01-15 |
| First published | 2022-01-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Fabian Frangella |
| Maintainers | fabianfrangella |

## Links

- npm: https://www.npmjs.com/package/sequelize-repository
- Repository: https://github.com/fabianfrangella/sequelize-repository
- Homepage: https://github.com/fabianfrangella/sequelize-repository#readme
- Issues: https://github.com/fabianfrangella/sequelize-repository/issues
- npm.io page: https://npm.io/package/sequelize-repository

## Dependencies (3)

- [chai](https://npm.io/package/chai.md) ^4.3.4
- [jest](https://npm.io/package/jest.md) ^27.4.7
- [sequelize-mock](https://npm.io/package/sequelize-mock.md) ^0.10.2

## Recent versions

- 1.1.2 (latest) — 2022-01-15
- 1.1.1 — 2022-01-15
- 1.1.0 — 2022-01-15
- 1.0.9 — 2022-01-15
- 1.0.8 — 2022-01-12
- 1.0.7 — 2022-01-11
- 1.0.6 — 2022-01-11
- 1.0.5 — 2022-01-11
- 1.0.4 — 2022-01-11
- 1.0.3 — 2022-01-11
- 1.0.2 — 2022-01-11
- 1.0.1 — 2022-01-11
- 1.0.0 — 2022-01-11

## README

# sequelize-repository
sequelize-repository is a simple implementation of the repository pattern for sequelize

# Table of contents
- [SQRepository](#SQRepository)
- [Query Methods](#Query-Methods)
- [Criteria](#Criteria)
- [TransactionRunner](#TransactionRunner)



### SQRepository
The SQRepository is the entry point to the library, it contains all repository methods, you must extend from this class to use it.

Example: 
```
class MyRepository extends SQRepository {
    constructor() {
        super(MyRepository, db.MyModel, 'my_id' })
    }
}

const repository = new MyRepository()

const entities = await this.repository.findById(1)
```
The SQRepository contains the following methods:
```
save(model,t)
findAll()
findAllWhere(query)
findAllByCriteriaAndPaginated({criteria, page, size, orderAttr})
findAndCountAll({condition, order, page, size, include})
findOne(query)
findById(id)
sum(field, criteria)
```

### Query-Methods
Query methods are and easy way to generate simple queries without almost any code. You just have to write the method signature and parameters and SQRepository will do the rest.


| Keyword     | Sample              | Sequelize Snippet                                                                  | Returns
| :---        | :---                |:---                                                                                |:----
| And         | findAllByNameAndRole|`model.findAll({ where: { name, role }, include: { all: true } })`                  | ```[{entity}]```
| Paginated   | findByNamePaginated |`model.findAndCountAll({ where: { name }, limit, offset, include: { all: true } })` | ```{ totalItems: 10, rows: [{entity}], totalPages: 15, currentPage: 0 }```

Example:
```
class MyRepository extends SQRepository {
    constructor() {
        super(MyRepository, db.MyModel, 'my_id' })
    }
    async findAllByNameAndEmail(name,email) {}
}

const repository = new MyRepository()
    
const entities = await this.repository.findAllByNameAndEmail("John", "Doe")
```

### Criteria
Criteria is a class that represents sequelize queries in a simpler way, the CriteriaBuilder is used to build complex criterias easily. There are methods of the repository that expect a Criteria Object
Examples:
```
const { Op } = require('sequelize')
const criteria = new Criteria([{ field: 'client_id', value: 1234, condition: { [Op.eq]: 1234 }])
criteria.getCriteria() // -> { client_id: { [Op.eq] : 1234 } }
await myRepository.findAllWhere(criteria) // -> [{ client_id: 1234 }]
```
```
const builder = new CriteriaBuilder()
const criteria = builder.add('client_id', clientId, { [Op.eq]: clientId })
   .add('created_date', fromDate, { [Op.eq]: clientId })
   .add('status', 'FAILURE', { [Op.ne]: 'FAILURE' })
   .add('request_id', null, { [Op.ne]: null })
   .build()
criteria.getCriteria() // { client_id:  { [Op.eq]: 1234 }, created_date: { [Op.eq]: 10/10/2021 } ...}
```
### TransactionRunner
The transaction runner is a solution to handle the sequelize transactions in just one place.
In order to use the "save" method of the repository you must execute it inside a transaction.
Example:
```
const db = require('../models')
const result = await TransactionRunner.run(db, async (t) => {
    const entity = await repository.save({ id: 1, name: 'John', lastName: 'Doe' }, t)
    return entity
})
```

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