# new Repository(adapteropt, clazzopt, …argsopt)
Provides CRUD operations, querying capabilities, and observer pattern implementation for model persistence.
Core repository implementation for database operations on models on a table by table way.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
adapter |
A
|
<optional> |
Optional adapter instance for database operations. |
clazz |
Constructor.<M>
|
<optional> |
Optional constructor for the model class. |
args |
Array.<any>
|
<optional> <repeatable> |
Additional arguments for repository initialization. |
Example
// Creating a repository for User model
const userRepo = Repository.forModel(User);
// Using the repository for CRUD operations
const user = await userRepo.create(new User({ name: 'John' }));
const retrievedUser = await userRepo.read(user.id);
user.name = 'Jane';
await userRepo.update(user);
await userRepo.delete(user.id);
// Querying with conditions
const users = await userRepo
.select()
.where({ name: 'Jane' })
.orderBy('createdAt', OrderDirection.DSC)
.limit(10)
.execute();
Members
# adapter
Provides access to the adapter instance for this repository.
Adapter for database operations.
# log
Provides access to the logger for this repository instance.
Logger instance for this repository.
# pkProps
Gets the sequence options containing primary key information.
Primary key properties for this repository's model.
# tableName
Gets the database table name associated with this repository's model.
Table name for this repository's model.
Methods
# protected ObserverHandler() → {ObserverHandler}
Factory method for creating an observer handler instance.
Creates a new observer handler.
A new observer handler instance.
# protected adapter() → {A}
Provides access to the adapter instance for this repository.
Adapter for database operations.
If no adapter is found.
InternalError
The adapter instance.
A
# async create(model, …args) → {Promise.<M>}
Persists a model instance to the database.
Creates a model in the database.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
model |
M
|
The model to create. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The created model with updated properties.
Promise.<M>
# async createAll(models, …args) → {Promise.<Array.<M>>}
Persists multiple model instances to the database in a batch operation.
Creates multiple models in the database.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
models |
Array.<M>
|
The models to create. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The created models with updated properties.
Promise.<Array.<M>>
# async protected createAllPrefix(models, …args)
Validates multiple models and prepares them for creation in the database.
Prepares multiple models for creation.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
models |
Array.<M>
|
The models to create. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
If any model fails validation.
ValidationError
The prepared models and context arguments.
# async protected createPrefix(model, …args)
Validates the model and prepares it for creation in the database.
Prepares a model for creation.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
model |
M
|
The model to create. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
If the model fails validation.
ValidationError
The prepared model and context arguments.
# async createSuffix(model, context) → {Promise.<M>}
Executes after a model is created to perform additional operations.
Post-creation hook.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The created model. |
context |
C
|
The operation context. |
The processed model.
Promise.<M>
# async delete(id, …args) → {Promise.<M>}
Removes a model instance from the database using its primary key.
Deletes a model from the database by ID.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
string
|
number
|
bigint
|
The primary key of the model to delete. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The deleted model instance.
Promise.<M>
# async deleteAll(keys, …args) → {Promise.<Array.<M>>}
Removes multiple model instances from the database using their primary keys.
Deletes multiple models from the database by IDs.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
keys |
Array.<string>
|
Array.<number>
|
The primary keys of the models to delete. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The deleted model instances.
Promise.<Array.<M>>
# async protected deleteAllPrefix(keys, …args)
Prepares the context and enforces decorators before deleting multiple models.
Prepares for deleting multiple models by IDs.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
keys |
Array.<string>
|
Array.<number>
|
The primary keys of the models to delete. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The keys and context arguments.
# async protected deletePrefix(key, …args)
Prepares the context and enforces decorators before deleting a model.
Prepares for deleting a model by ID.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
any
|
The primary key of the model to delete. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The key and context arguments.
# log() → {Logger}
Provides access to the logger for this repository instance.
Logger instance for this repository.
The logger instance.
Logger
# observe(observer, filteropt) → {void}
Adds an observer that will be notified of changes to models in this repository.
Registers an observer for this repository.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
observer |
Observer
|
The observer to register. |
|
filter |
ObserverFilter
|
<optional> |
Optional filter to limit which events the observer receives. |
void
# override(flags) → {Repository}
Returns a proxy of this repository with the specified flags overridden.
Creates a proxy with overridden repository flags.
Parameters:
Name | Type | Description |
---|---|---|
flags |
Partial.<F>
|
The flags to override. |
A proxy of this repository with overridden flags.
# protected pkProps() → {SequenceOptions}
Gets the sequence options containing primary key information.
Primary key properties for this repository's model.
The primary key properties.
# async query(condition, orderBy, orderopt, limitopt, skipopt) → {Promise.<Array.<M>>}
Provides a simplified way to query the database with common query parameters.
Executes a query with the specified conditions and options.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
condition |
Condition.<M>
|
The condition to filter records. |
||
orderBy |
The field to order results by. |
|||
order |
OrderDirection
|
<optional> |
OrderDirection.ASC | The sort direction. |
limit |
number
|
<optional> |
Optional maximum number of results to return. |
|
skip |
number
|
<optional> |
Optional number of results to skip. |
The query results as model instances.
Promise.<Array.<M>>
# async read(id, …args) → {Promise.<M>}
Retrieves a model instance from the database using its primary key.
Reads a model from the database by ID.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
string
|
number
|
bigint
|
The primary key of the model to read. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The retrieved model instance.
Promise.<M>
# async readAll(keys, …args) → {Promise.<Array.<M>>}
Retrieves multiple model instances from the database using their primary keys.
Reads multiple models from the database by IDs.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
keys |
Array.<string>
|
Array.<number>
|
The primary keys of the models to read. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The retrieved model instances.
Promise.<Array.<M>>
# async protected readAllPrefix(keys, …args)
Prepares the context and enforces decorators before reading multiple models.
Prepares for reading multiple models by IDs.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
keys |
Array.<string>
|
Array.<number>
|
The primary keys of the models to read. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The keys and context arguments.
# async protected readPrefix(key, …args)
Prepares the context and enforces decorators before reading a model.
Prepares for reading a model by ID.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string
|
The primary key of the model to read. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The key and context arguments.
# async refresh(table, event, id, …args) → {Promise.<void>}
Notifies all observers of an event (alias for updateObservers).
Alias for updateObservers.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
table |
string
|
The table name where the event occurred. |
|
event |
OperationKeys
|
BulkCrudOperationKeys
|
string
|
The type of event that occurred. |
|
id |
EventIds
|
The ID or IDs of the affected records. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
A promise that resolves when all observers have been notified.
Promise.<void>
# select(selectoropt)
Creates a query builder for the model with optional field selection.
Implementation of the select method.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
selector |
<optional> |
Optional fields to select. |
A query builder.
# protected tableName() → {string}
Gets the database table name associated with this repository's model.
Table name for this repository's model.
The table name.
string
# unObserve(observer) → {void}
Removes an observer so it will no longer receive notifications of changes.
Unregisters an observer from this repository.
Parameters:
Name | Type | Description |
---|---|---|
observer |
Observer
|
The observer to unregister. |
If the observer handler is not initialized.
InternalError
void
# async update(model, …args) → {Promise.<M>}
Persists changes to an existing model instance in the database.
Updates a model in the database.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
model |
M
|
The model to update. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The updated model with refreshed properties.
Promise.<M>
# async updateAll(models, …args) → {Promise.<Array.<M>>}
Persists changes to multiple existing model instances in the database in a batch operation.
Updates multiple models in the database.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
models |
Array.<M>
|
The models to update. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
The updated models with refreshed properties.
Promise.<Array.<M>>
# async protected updateAllPrefix(models, …args) → {Promise.<Array.<any>>}
Validates multiple models and prepares them for update in the database.
Prepares multiple models for update.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
models |
Array.<M>
|
The models to update. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
If any model has no primary key value.
InternalError
If any model fails validation.
ValidationError
The prepared models and context arguments.
Promise.<Array.<any>>
# async updateObservers(table, event, id, …args) → {Promise.<void>}
Updates all registered observers with information about a database event.
Notifies all observers of an event.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
table |
string
|
The table name where the event occurred. |
|
event |
OperationKeys
|
BulkCrudOperationKeys
|
string
|
The type of event that occurred. |
|
id |
EventIds
|
The ID or IDs of the affected records. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
If the observer handler is not initialized.
InternalError
A promise that resolves when all observers have been notified.
Promise.<void>
# async protected updatePrefix(model, …args)
Validates the model and prepares it for update in the database.
Prepares a model for update.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
model |
M
|
The model to update. |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments. |
If the model has no primary key value.
InternalError
If the model fails validation.
ValidationError
The prepared model and context arguments.
# static column(model, attribute) → {string}
Retrieves the database column name for a model property.
Gets the column name for a model attribute.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance. |
attribute |
string
|
The attribute/property name. |
The column name for the attribute.
string
# static forModel(model, defaultFlavouropt, …argsopt) → {R}
Factory method that returns a repository instance for the specified model.
Creates or retrieves a repository for a model.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
model |
Constructor.<M>
|
The model constructor. |
|
defaultFlavour |
string
|
<optional> |
Optional default adapter flavour if not specified on the model. |
args |
Array.<any>
|
<optional> <repeatable> |
Additional arguments to pass to the repository constructor. |
If no adapter is registered for the flavour.
InternalError
A repository instance for the model.
R
# static get(model) → {Constructor.<Repo.<M>>|Repo.<M>}
Gets a repository constructor or instance for the specified model from the internal cache.
Retrieves a repository for a model from the cache.
Parameters:
Name | Type | Description |
---|---|---|
model |
Constructor.<M>
|
The model constructor. |
If no repository is registered for the model.
InternalError
The repository constructor or instance.
Constructor.<Repo.<M>>
|
Repo.<M>
# static getMetadata(model) → {any}
Retrieves previously attached metadata from a model instance.
Gets metadata from a model instance.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance. |
The metadata or undefined if not found.
any
# static getSequenceOptions(model) → {SequenceOptions}
Retrieves the sequence configuration for a model's primary key from metadata.
Gets sequence options for a model's primary key.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance. |
If no sequence options are defined for the model.
InternalError
The sequence options for the model's primary key.
# static indexes(model) → {Record.<string, Record.<string, IndexMetadata>>}
Retrieves all index metadata from a model's property decorators.
Gets all indexes defined on a model.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
Constructor.<M>
|
The model instance or constructor. |
A nested record of property names to index metadata.
Record.<string, Record.<string, IndexMetadata>>
# static register(model, repo)
Associates a repository constructor or instance with a model in the internal cache.
Registers a repository for a model.
Parameters:
Name | Type | Description |
---|---|---|
model |
Constructor.<M>
|
The model constructor. |
repo |
Constructor.<Repo.<M>>
|
Repo.<M>
|
The repository constructor or instance. |
If a repository is already registered for the model.
InternalError
# static relations(model) → {Array.<string>}
Retrieves the names of all properties marked as relations in the model hierarchy.
Gets all relation properties defined on a model.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
Constructor.<M>
|
The model instance or constructor. |
An array of property names that are relations.
Array.<string>
# static removeMetadata(model)
Deletes the metadata property from a model instance.
Removes metadata from a model instance.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance. |
# static setMetadata(model, metadata)
Attaches metadata to a model instance using a non-enumerable property.
Sets metadata on a model instance.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance. |
metadata |
any
|
The metadata to attach to the model. |
# static table(model) → {string}
Retrieves the database table name associated with a model.
Gets the table name for a model.
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
Constructor.<M>
|
The model instance or constructor. |
The table name for the model.
string