# objection-repositories

> Entity repositories for Objection.js ORM.

Latest version **1.8.1** (published 2019-10-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install objection-repositories
pnpm add objection-repositories
yarn add objection-repositories
bun add objection-repositories
```

## 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.8.1 |
| Published | 2019-10-23 |
| First published | 2018-04-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Igor Savin |
| Maintainers | kibertoad |
| Keywords | objection, sql, query |

## Links

- npm: https://www.npmjs.com/package/objection-repositories
- Repository: https://github.com/kibertoad/objection-utils
- Homepage: https://github.com/kibertoad/objection-utils#readme
- Issues: https://github.com/kibertoad/objection-utils/issues
- npm.io page: https://npm.io/package/objection-repositories

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [validation-utils](https://npm.io/package/validation-utils.md) ^4.0.2

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.8.1 (latest) — 2019-10-23
- 1.8.0 — 2019-07-17
- 1.7.0 — 2018-06-21
- 1.6.0 — 2018-06-21
- 1.5.0 — 2018-05-04
- 1.4.1 — 2018-05-04
- 1.4.0 — 2018-05-04
- 1.3.0 — 2018-05-03
- 1.2.0 — 2018-05-02
- 1.1.0 — 2018-05-02
- 1.0.8 — 2018-04-30
- 1.0.7 — 2018-04-30
- 1.0.6 — 2018-04-30
- 1.0.5 — 2018-04-30

## README

# objection-repositories

Repositories are wrappers around specific DB connection and Objection.js Model that expose a number of methods to perform CRUD operations:

**Create new entity without persisting

```js
  /**
   * @param {Object} [attributeValues] - attributes of a new entity
   * @returns {Object} entity instance of a repository Model
   */
  fromJson(attributeValues)
```

**Persist new entity or an array of entities non-recursively (without relations). Note that batch insert only works on PostgreSQL

```js
  /**
   * @param {Object} entity - model instance or parameters for a new entity
   * @param {Object} [trx] - knex transaction instance. If not specified, new implicit transaction will be used.
   * @returns {Promise<Object>} - created entity
   */
  create(instance, trx)
```

**Persist updated entity. If previously set fields are not present, performs an incremental update (does not remove fields unless explicitly set to null)

```js
  /**
   * @param {Object} entity - single entity instance
   * @param {Object} [trx] - knex transaction instance. If not specified, new implicit transaction will be used.
   * @returns {integer} number of affected rows
   */
  update(entity, trx)
```

**Persist updated entity. If previously set fields are not present, performs an incremental update (does not remove fields unless explicitly set to null)

```js
  /**
   * @param {Object} entity - single entity instance
   * @param {Object} [trx] - knex transaction instance. If not specified, new implicit transaction will be used.
   * @returns {PromiseLike<Object>} updatedEntity
   */
  updateAndFetch(entity, trx)
```

**Find list of entities with specified attributes

```js
  /**
   * @param {Object} attributeValues - values to filter retrieved entities by
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {Promise<Object[]>} - search result
   */
  find(attributeValues = {}, withRelations)
```

**Find list of entities with attribute values that are different from specified ones

```js
  /**
   * @param {Object} attributeValues - values to filter retrieved entities by
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {PromiseLike<Object[]>} - query builder. You can chain additional methods to it or call "await" or then() on it to execute
   */
  findWhereNot(attributeValues = {}, withRelations)
```

**Find list of entities with specified attributes (any of multiple specified values)

```js
  /**
   * Supports both ('attrName', ['value1', 'value2]) and ({attrName: ['value1', 'value2']} formats)
   * @param {string|Object} searchParam - attribute name or search criteria object
   * @param {*[]} attributeValues - attribute values to filter retrieved entities by
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {Promise<Object[]>}
   */
  findWhereIn(searchParam, attributeValues, withRelations)
```

**Find first entity with specified attributes

```js
  /**
   * @param {Object} attributeValues - values to filter retrieved entities by
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {Promise<Object>}
   */
  findOne(attributeValues = {}, withRelations)
```

**Find entity with specified id (as defined by idColumn entity attribute)

```js
  /**
   * @param {string || number} id - value of id column of the entity
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {Promise<Object>}
   */
  findOneById(id, withRelations)
```

**Delete entities with specified attributes

```js
  /**
   * @param {Object} attributeValues - values to filter deleted entities by
   * @param {Object} [trx]
   * @returns {PromiseLike<integer>} Query builder. After promise is resolved, returns count of deleted rows
   */
  deleteBy(attributeValues, trx)
```

**Delete entity with specified id (as defined by idColumn entity attribute)

```js
  /**
   * @param {string || number} id - value of id column of the entity
   * @param {Object} [trx]
   * @returns {PromiseLike<integer>} Query builder. After promise is resolved, returns count of deleted rows
   */
  deleteById(id, trx)
```

Recommended repository initialization and exposal pattern (JSDoc is used to assist with autocompletion):


```js
const { repositoryFactory } = require('objection-repositories');
const knex = require('../db/application.db').getKnexInstance(); //replace with how you provide knex in your application
const SomeEntityModel = require('../models/some-entity.model');
const AnotherEntityModel = require('../models/another-entity.model');

const someEntityRepository = repositoryFactory.getRepository(knex, SomeEntityModel);
const anotherEntityRepository = repositoryFactory.getRepository(knex, AnotherEntityModel);

module.exports = {
	/**
	 * @type EntityRepository
	 */
	someEntityRepository,

	/**
	 * @type EntityRepository
	 */
	anotherEntityRepository
};
```

In case you are only instantiating repositories in a single place (and thus don't care about ensuring them to be memoized singletons), you don't
need to use factory and can instantiate EntityRepository directly.

** Extending repositories with custom logic

You can extend base EntityRepository class and pass it as a parameter to the factory to have custom methods in your repositories:

```js
class CustomRepository extends EntityRepository {
  // custom logic
}

const someEntityRepository = repositoryFactory.getCustomRepository(CustomRepository, knex, SomeEntityModel);
```

Note that all constructions arguments will be passed to your custom class verbatim (starting with the second argument).

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