Class: EntityRepository

EntityRepository

Abstraction on top of BookshelfRepository, Bookshelf and Knex. Provies basic CRUD operations for a specific type.


new EntityRepository(Entity, Mapping)

Parameters:
Name Type Description
Entity Class | function

Class or constructor function. Entities from this repository will be instances of this Type

Mapping BookshelfMapping

Compiled Mapping which describes this type and its relations

Source:

Methods


newEntity( [flatModel])

Create new instance of Entity.

Parameters:
Name Type Argument Description
flatModel object <optional>

Simple object representation of Entity, e.g. after deserializing JSON. Properties missing in Mapping are dropped

Source:
Returns:
  • Instance of Entity, with given properties if any
Type
Entity

findOne(id [, options])

Fetch one Entity from this Repository

Parameters:
Name Type Argument Default Description
id ID

Identifier of Entity, specified in Mapping by "identifiedBy"

options object <optional>
null

Bookshelf fetch options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

exclude Array.<string> <optional>

Relation names to exclude, deep relations in dot notation. Specify wildcards using "*"

Source:
Returns:
  • Returns Promise resolved with entity, or null if not found
Type
Promise.<(Entity|null)>

findAll(ids [, options])

Fetch all Entities, or Entities with given Ids from this Repository

Parameters:
Name Type Argument Default Description
ids Array.<ID>

Identifiers of Entities, specified in Mapping by "identifiedBy"

options object <optional>
null

Bookshelf fetch options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

exclude Array.<string> <optional>

Relation names to exclude, deep relations in dot notation. Specify wildcards using "*"

Source:
Returns:
  • Returns Promise resolved with array of entities, or empty list if not found.
    If ids were specified, Entities are sorted statically by given ids
Type
Promise.<Array.<Entity>>

findAllWhere(q [, options])

Fetch Entities using a query

Parameters:
Name Type Argument Default Description
q function

Callback, used as Bookshelf/Knex where condition.

options object <optional>
null

Bookshelf fetch options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

exclude Array.<string> <optional>

Relation names to exclude, deep relations in dot notation. Specify wildcards using "*"

Source:
Returns:
  • Returns Promise resolved with array of entities, or empty list if not found.
Type
Promise.<Array.<Entity>>

findWhere(q [, options])

Fetch Entity using a query

Parameters:
Name Type Argument Default Description
q function

Callback, used as Bookshelf/Knex where condition.

options object <optional>
null

Bookshelf fetch options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

exclude Array.<string> <optional>

Relation names to exclude, deep relations in dot notation. Specify wildcards using "*"

Source:
Returns:
  • Returns Promise resolved with entity, or null if not found
Type
Promise.<(Entity|null)>

save(entity [, options])

Save one or multiple Entities to this Repository

Parameters:
Name Type Argument Default Description
entity Entity | Array.<Entity>

Entity or Entities to save

options object <optional>
null

Bookshelf save options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

method string <optional>

Specify "update" or "insert". Defaults to "update", or "insert" if Id is null

Source:
Returns:
  • Returns Promise resolved with saved entity, or array of saved entities
Type
Promise.<(Entity|Array.<Entity>)>

afterSave(id)

Hook, is called once after every successful save operation

Parameters:
Name Type Description
id ID

Identifier of saved Entity

Source:

remove(entity [, options])

Remove one or multiple Entities from this Repository

Parameters:
Name Type Argument Default Description
entity Entity | Array.<Entity> | ID | Array.<ID>

Entity or Entities, Id or Ids to remove

options object <optional>
null

Bookshelf save options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

Source:
Returns:
  • Returns Promise resolved after removal
Type
Promise.<Void>

afterRemove(id)

Hook, is called once after every successful remove operation

Parameters:
Name Type Description
id ID

Identifier of removed Entity

Source:

executeTransactional(operation, options)

Execute an operation in a running or new transaction

Parameters:
Name Type Default Description
operation function

Callback to execute in a transaction

options object null

Bookshelf options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

Source:
Returns:
  • Promise resolved with result of operation. If operation fails, Promise is rejected
Type
Promise.<*>

addTransactionToQuery(query, options)

Add an already started transaction to given query. If not yet started, no transaction will be added

Parameters:
Name Type Argument Description
options.transacting Transaction <optional>

Add Transaction object to given query

query KnexQuery

Add transaction to query, if one was started.

options object

Bookshelf options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

Source:
Returns:

query - Returns KnexQuery for chaining

Type
KnexQuery

exists(id [, options])

Returns whether an Entity with the given Identifier exists.

Parameters:
Name Type Argument Default Description
id ID

Identifier

options object <optional>
null

Bookshelf save options

Properties
Name Type Argument Description
transacting Transaction <optional>

Run in given transaction

transactional boolean <optional>

Run in a transaction, start new one if not already transacting

Source:
Returns:
  • Returns Promise resolved with flag indicating whether an Entity with the given Identifier exists
Type
Promise.<boolean>