# new Adapter(_native, flavour, _aliasopt)
Provides the foundation for all database adapters in the persistence layer. This class implements several interfaces to provide a consistent API for database operations, observer pattern support, and error handling. It manages adapter registration, CRUD operations, and observer notifications.
Abstract base class for database adapters
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
_native |
Y
|
The underlying database driver instance |
|
flavour |
string
|
The identifier for this adapter type |
|
_alias |
string
|
<optional> |
Optional alternative name for this adapter |
Example
```typescript
// Implementing a concrete adapter
class PostgresAdapter extends Adapter<pg.Client, pg.Query, PostgresFlags, PostgresContext> {
constructor(client: pg.Client) {
super(client, 'postgres');
}
async initialize() {
// Set up the adapter
await this.native.connect();
}
async create(tableName, id, model) {
// Implementation for creating records
const columns = Object.keys(model).join(', ');
const values = Object.values(model);
const placeholders = values.map((_, i) => `$${i+1}`).join(', ');
const query = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders}) RETURNING *`;
const result = await this.native.query(query, values);
return result.rows[0];
}
// Other required method implementations...
}
// Using the adapter
const pgClient = new pg.Client(connectionString);
const adapter = new PostgresAdapter(pgClient);
await adapter.initialize();
// Set as the default adapter
Adapter.setCurrent('postgres');
// Perform operations
const user = await adapter.create('users', 1, { name: 'John', email: 'john@example.com' });
```
Classes
- Adapter
Initializes the adapter with the native driver and registers it in the adapter cache
- Adapter
Initializes the adapter with the native driver and registers it in the adapter cache
Members
# Context
Reference to the context class constructor used by this adapter
The context constructor for this adapter
# protected Context
Reference to the context class constructor used by this adapter
The context constructor for this adapter
# alias
Returns the alias if set, otherwise returns the flavor name
Gets the adapter's alias or flavor name
# static current
Retrieves the adapter that is currently set as the default for operations
Gets the current default adapter
Methods
# protected Dispatch() → {Dispatch.<Y>}
Factory method that creates a dispatch instance for this adapter
Creates a new dispatch instance
A new dispatch instance
Dispatch.<Y>
# protected ObserverHandler() → {ObserverHandler}
Factory method that creates an observer handler for this adapter
Creates a new observer handler
A new observer handler instance
# abstract Sequence(options) → {Promise.<Sequence>}
Factory method that creates a sequence generator for generating sequential values
Creates a sequence generator
Parameters:
Name | Type | Description |
---|---|---|
options |
SequenceOptions
|
Configuration options for the sequence |
A promise that resolves to a new sequence instance
Promise.<Sequence>
# abstract Statement() → {Statement}
Returns a statement builder that can be used to construct queries for a specific model
Creates a new statement builder for a model
A statement builder for the model
# alias() → {string}
Returns the alias if set, otherwise returns the flavor name
Gets the adapter's alias or flavor name
The adapter's identifier
string
# async context(operation, overrides, model, …args) → {Promise.<C>}
Generates a context object that describes a database operation, used for tracking and auditing
Creates a context for a database operation
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
operation |
OperationKeys.CREATE
|
OperationKeys.READ
|
OperationKeys.UPDATE
|
OperationKeys.DELETE
|
The type of operation |
|
overrides |
Partial.<F>
|
Custom flag overrides |
|
model |
Constructor.<M>
|
The model constructor |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments |
A promise that resolves to the context object
Promise.<C>
# abstract create(tableName, id, model, args)
Inserts a new record with the given ID and data into the specified table
Creates a new record in the database
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table to insert into |
id |
string
|
number
|
The identifier for the new record |
model |
The data to insert |
|
args |
Array.<any>
|
Additional arguments specific to the adapter implementation |
A promise that resolves to the created record
# async createAll(tableName, id, model, …args)
Inserts multiple records with the given IDs and data into the specified table
Creates multiple records in the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to insert into |
|
id |
The identifiers for the new records |
||
model |
The data to insert for each record |
||
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to an array of created records
# abstract delete(tableName, id, …args)
Removes a record with the given ID from the specified table
Deletes a record from the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to delete from |
|
id |
string
|
number
|
bigint
|
The identifier of the record to delete |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to the deleted record
# async deleteAll(tableName, id, …args)
Removes multiple records with the given IDs from the specified table
Deletes multiple records from the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to delete from |
|
id |
The identifiers of the records to delete |
||
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to an array of deleted records
# protected flags(operation, model, flags, …args) → {F}
Generates a set of flags that describe a database operation, combining default flags with overrides
Creates repository flags for an operation
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
operation |
OperationKeys
|
The type of operation being performed |
|
model |
Constructor.<M>
|
The model constructor |
|
flags |
Partial.<F>
|
Custom flag overrides |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments |
The complete set of flags
F
# abstract initialize(…args) → {Promise.<void>}
Performs any necessary setup for the adapter, such as establishing connections
Initializes the adapter
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
args |
Array.<any>
|
<repeatable> |
Initialization arguments |
A promise that resolves when initialization is complete
Promise.<void>
# protected isReserved(attr) → {boolean}
Determines if a given attribute name is reserved and cannot be used as a column name
Checks if an attribute name is reserved
Parameters:
Name | Type | Description |
---|---|---|
attr |
string
|
The attribute name to check |
True if the attribute is reserved, false otherwise
boolean
# protected log() → {Logger}
Gets or initializes the logger for this adapter instance
Logger accessor
The logger instance
Logger
# native() → {Y}
Provides access to the underlying database driver instance
Gets the native database driver
The native database driver
Y
# observe(observer, filteropt) → {void}
Adds an observer to be notified about database changes. The observer can optionally provide a filter function to receive only specific events.
Registers an observer for database events
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
observer |
Observer
|
The observer to register |
|
filter |
ObserverFilter
|
<optional> |
Optional filter function to determine which events the observer receives |
void
# abstract parseError(err) → {BaseError}
Converts database-specific errors into standardized application errors
Parses a database error into a standardized error
Parameters:
Name | Type | Description |
---|---|---|
err |
Error
|
The original database error |
A standardized error
BaseError
# prepare(model, pk)
Converts a model instance into a format suitable for database storage, handling column mapping and separating transient properties
Prepares a model for persistence
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance to prepare |
pk |
The primary key property name |
The prepared data
# abstract raw(rawInput, …args) → {Promise.<R>}
Allows executing database-specific queries directly
Executes a raw query against the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
rawInput |
Q
|
The query to execute |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to the query result
Promise.<R>
# abstract read(tableName, id, …args)
Fetches a record with the given ID from the specified table
Retrieves a record from the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to read from |
|
id |
string
|
number
|
bigint
|
The identifier of the record to retrieve |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to the retrieved record
# async readAll(tableName, id, …args)
Fetches multiple records with the given IDs from the specified table
Retrieves multiple records from the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to read from |
|
id |
The identifiers of the records to retrieve |
||
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to an array of retrieved records
# async refresh(table, event, id, …args) → {Promise.<void>}
Implementation of the Observer interface method that delegates to updateObservers
Refreshes data based on a database event
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
table |
string
|
The name of the table where the change occurred |
|
event |
OperationKeys
|
BulkCrudOperationKeys
|
string
|
The type of operation that occurred |
|
id |
EventIds
|
The identifier(s) of the affected record(s) |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments related to the event |
A promise that resolves when the refresh is complete
Promise.<void>
# repository() → {Constructor.<Repository.<M, Q, Adapter.<Y, Q, F, C>, F, C>>}
Returns the constructor for creating repositories that work with this adapter
Gets the repository constructor for this adapter
The repository constructor
Constructor.<Repository.<M, Q, Adapter.<Y, Q, F, C>, F, C>>
# revert(obj, clazz, pk, id, transientopt) → {M}
Reconstructs a model instance from database data, handling column mapping and reattaching transient properties
Converts database data back into a model instance
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
The database record |
||
clazz |
string
|
Constructor.<M>
|
The model class or name |
|
pk |
The primary key property name |
||
id |
string
|
number
|
bigint
|
The primary key value |
|
transient |
<optional> |
Transient properties to reattach |
The reconstructed model instance
M
# toString() → {string}
Returns a human-readable string identifying this adapter
Gets a string representation of the adapter
A string representation of the adapter
string
# unObserve(observer) → {void}
Removes a previously registered observer so it no longer receives database event notifications
Unregisters an observer
Parameters:
Name | Type | Description |
---|---|---|
observer |
Observer
|
The observer to unregister |
void
# abstract update(tableName, id, model, …args)
Modifies an existing record with the given ID in the specified table
Updates a record in the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to update |
|
id |
string
|
number
|
The identifier of the record to update |
|
model |
The new data for the record |
||
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to the updated record
# async updateAll(tableName, id, model, …args)
Modifies multiple existing records with the given IDs in the specified table
Updates multiple records in the database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tableName |
string
|
The name of the table to update |
|
id |
Array.<string>
|
Array.<number>
|
The identifiers of the records to update |
|
model |
The new data for each record |
||
args |
Array.<any>
|
<repeatable> |
Additional arguments specific to the adapter implementation |
A promise that resolves to an array of updated records
# async updateObservers(table, event, id, …args) → {Promise.<void>}
Sends notifications to all registered observers about a change in the database, filtering based on each observer's filter function
Notifies all observers about a database event
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
table |
string
|
The name of the table where the change occurred |
|
event |
OperationKeys
|
BulkCrudOperationKeys
|
string
|
The type of operation that occurred |
|
id |
EventIds
|
The identifier(s) of the affected record(s) |
|
args |
Array.<any>
|
<repeatable> |
Additional arguments to pass to the observers |
A promise that resolves when all observers have been notified
Promise.<void>
# static current() → {Adapter.<any, any, any, any>}
Retrieves the adapter that is currently set as the default for operations
Gets the current default adapter
The current adapter
Adapter.<any, any, any, any>
# static flavourOf(model) → {string}
Retrieves the adapter flavor that should be used for a specific model class
Gets the adapter flavor associated with a model
Parameters:
Name | Type | Description |
---|---|---|
model |
Constructor.<M>
|
The model constructor |
The adapter flavor name
string
# static get(flavour) → {Adapter.<Y, Q, F, C>|undefined}
Retrieves a registered adapter by its flavor name
Gets an adapter by flavor
Parameters:
Name | Type | Description |
---|---|---|
flavour |
string
|
The flavor name of the adapter to retrieve |
The adapter instance or undefined if not found
Adapter.<Y, Q, F, C>
|
undefined
# static key(key) → {string}
Generates a standardized metadata key for persistence-related metadata
Creates a metadata key
Parameters:
Name | Type | Description |
---|---|---|
key |
string
|
The base key name |
The formatted metadata key
string
# static models(flavour)
Retrieves all model constructors that are configured to use a specific adapter flavor
Gets all models associated with an adapter flavor
Parameters:
Name | Type | Description |
---|---|---|
flavour |
string
|
The adapter flavor to find models for |
An array of model constructors
# static setCurrent(flavour) → {void}
Changes which adapter is used as the default for operations
Sets the current default adapter
Parameters:
Name | Type | Description |
---|---|---|
flavour |
string
|
The flavor name of the adapter to set as current |
void