# nf-repository

> NF Repository is a abstract layer of Sequelize Application, that make application more easy to understand and flexible to maintain.

Latest version **1.0.2** (published 2018-08-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install nf-repository
pnpm add nf-repository
yarn add nf-repository
bun add nf-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.0.2 |
| Published | 2018-08-22 |
| First published | 2018-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 184.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Hieupv |
| Maintainers | nightfury |
| Keywords | Repository, Sequelize |

## Links

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

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^4.17.10
- [@codersvn/support](https://npm.io/package/@codersvn/support.md) ^1.0.9
- [@codersvn/container](https://npm.io/package/@codersvn/container.md) ^1.0.2
- [@codersvn/exceptions](https://npm.io/package/@codersvn/exceptions.md) ^1.0.0

## Recent versions

- 1.0.2 (latest) — 2018-08-22
- 1.0.1 — 2018-08-22

## README

# NF Repository

NF Repository is a abstract layer of Sequelize Application, that make application more easy to understand and flexible to maintain.

You want to know a little more about the Repository pattern? [Read this great article](http://bit.ly/1IdmRNS).

## Table of Contents

- <a href="#installation">Installation</a>
  - <a href="#npm">NPM</a>
- <a href="#methods">Methods</a>
- <a href="#usage">Usage</a>

## Installation

### NPM

Execute the following command to get the latest version of the package:

```terminal
npm install nf-repository
```

If Yarn

```terminal
yarn add nf-repository
```

## Methods

### RepositoryInterface

- get()
- first()
- paginate(limit = 20)
- findById(id)
- create(attributes)
- update(attributes, id)
- updateOrCreate(attributes, values)
- firstOrCreate(attributes, values)
- deleteById(id)
- delete()
- orderBy(column, direction = 'asc');
- with(relation);
- has(relation);
- whereHas(relation, callback);
- withScope(scope);

## Usage

### Create a Model

Create your Sequelize Model normally

```javascript
"use strict";

module.exports = (sequelize, DataTypes) => {
  var Demo = sequelize.define(
    "demo",
    {
      name: DataTypes.STRING,
      type: DataTypes.STRING
    },
    {
      underscored: true
    }
  );

  return Demo;
};
```

### Create a Repository

```javascript
import models from "your_sequelize_model_folder";
import { Exception } from "nf-repository/Exceptions";
import { Repository } from "nf-repository/Repository";

export default class DemoRepository extends Repository {
  Models() {
    return models.product;
  }
}
```

### Use methods

Find all results in Repository

```javascript
const repository = new DemoRepository();
const items = await repository.get();
```

Find all results in Repository with pagination

```javascript
const repository = new DemoRepository();
const items = await repository.paginate(10);
```

Find item by id

```javascript
const repository = new DemoRepository();
const item = await repository.findById(123);
```

Loading the Model relationships

```javascript
const repository = new DemoRepository();
const items = await repository.with("relation").get();
```

Find by result by field name

```javascript
const repository = new DemoRepository();
const items = await repository.where("name", "your_name").get();
```

Find by result by multiple values in one field

```javascript
const repository = new DemoRepository();
const items = await repository.whereIn("id", [1, 2, 3, 4, 5]).get();
```

Find by result by excluding multiple values in one field

```javascript
const repository = new DemoRepository();
const items = await repository.whereNotIn("id", [6, 7, 8, 9, 10]).get();
```

Find all using custom scope

```javascript
const repository = new DemoRepository();
const items = await repository
  .withScope("scope")
  .withScope("another_scope")
  .get();
```

Create new entry in Repository

```javascript
const repository = new DemoRepository();
const item = await repository.create(attributes);
```

Update entry in Repository

```javascript
const repository = new DemoRepository();
const item = await repository.update(attributes, id);
```

Delete entry in Repository

```javascript
const repository = new DemoRepository();
const item = await repository.deleteById(id);
```

Delete entry in Repository by multiple fields

```javascript
const repository = new DemoRepository();
const item = await repository
  .where("name", "item_should_be_deleted")
  .where("another_field", "another_case_should_be_deleted")
  .delete();
```

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