Class

Adapter

Adapter(_native, flavour, _aliasopt)

Constructor

# 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

classDiagram class Adapter { +Y native +string flavour +string alias +create(tableName, id, model) +read(tableName, id) +update(tableName, id, model) +delete(tableName, id) +observe(observer, filter) +unObserve(observer) +static current +static get(flavour) +static setCurrent(flavour) } class RawExecutor { +raw(query) } class Observable { +observe(observer, filter) +unObserve(observer) +updateObservers(table, event, id) } class Observer { +refresh(table, event, id) } class ErrorParser { +parseError(err) } Adapter --|> RawExecutor Adapter --|> Observable Adapter --|> Observer Adapter --|> ErrorParser
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

View Source persistence/Adapter.ts, line 26

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

View Source persistence/Adapter.ts, line 229

# protected Context

Reference to the context class constructor used by this adapter

The context constructor for this adapter

View Source persistence/Adapter.ts, line 704

# alias

Returns the alias if set, otherwise returns the flavor name

Gets the adapter's alias or flavor name

View Source persistence/Adapter.ts, line 150

# log

Gets or initializes the logger for this adapter instance

Logger accessor

View Source persistence/Adapter.ts, line 132

# native

Provides access to the underlying database driver instance

Gets the native database driver

View Source persistence/Adapter.ts, line 142

# static current

Retrieves the adapter that is currently set as the default for operations

Gets the current default adapter

View Source persistence/Adapter.ts, line 481

Methods

# protected Dispatch() → {Dispatch.<Y>}

Factory method that creates a dispatch instance for this adapter

Creates a new dispatch instance

View Source persistence/Adapter.ts, line 640

A new dispatch instance

Dispatch.<Y>

# protected ObserverHandler() → {ObserverHandler}

Factory method that creates an observer handler for this adapter

Creates a new observer handler

View Source persistence/Adapter.ts, line 648

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

View Source persistence/Adapter.ts, line 684

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

View Source persistence/Adapter.ts, line 632

A statement builder for the model

Statement

# alias() → {string}

Returns the alias if set, otherwise returns the flavor name

Gets the adapter's alias or flavor name

View Source persistence/Adapter.ts, line 604

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

View Source persistence/Adapter.ts, line 718

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

View Source persistence/Adapter.ts, line 755

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

View Source persistence/Adapter.ts, line 767

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

View Source persistence/Adapter.ts, line 824

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

View Source persistence/Adapter.ts, line 835

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

View Source persistence/Adapter.ts, line 698

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

View Source persistence/Adapter.ts, line 675

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

View Source persistence/Adapter.ts, line 657

True if the attribute is reserved, false otherwise

boolean

# protected log() → {Logger}

Gets or initializes the logger for this adapter instance

Logger accessor

View Source persistence/Adapter.ts, line 590

The logger instance

Logger

# native() → {Y}

Provides access to the underlying database driver instance

Gets the native database driver

View Source persistence/Adapter.ts, line 597

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

View Source persistence/Adapter.ts, line 857

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

View Source persistence/Adapter.ts, line 666

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

View Source persistence/Adapter.ts, line 729

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

View Source persistence/Adapter.ts, line 847

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

View Source persistence/Adapter.ts, line 778

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

View Source persistence/Adapter.ts, line 789

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

View Source persistence/Adapter.ts, line 890

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

View Source persistence/Adapter.ts, line 612

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

View Source persistence/Adapter.ts, line 743

The reconstructed model instance

M

# toString() → {string}

Returns a human-readable string identifying this adapter

Gets a string representation of the adapter

View Source persistence/Adapter.ts, line 897

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

View Source persistence/Adapter.ts, line 865

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

View Source persistence/Adapter.ts, line 801

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

View Source persistence/Adapter.ts, line 813

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

View Source persistence/Adapter.ts, line 878

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

View Source persistence/Adapter.ts, line 913

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

View Source persistence/Adapter.ts, line 906

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

View Source persistence/Adapter.ts, line 925

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

View Source persistence/Adapter.ts, line 941

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

View Source persistence/Adapter.ts, line 950

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

View Source persistence/Adapter.ts, line 933

void