Class

Adapter

Adapter(_config, flavour, _aliasopt)

Constructor

# new Adapter(_config, 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 Facade class for persistence 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
_config CONFIG

The underlying persistence driver config

flavour string

The identifier for this adapter type

_alias string <optional>

Optional alternative name for this adapter

View Source persistence/Adapter.ts, line 44

Example
```typescript
// Implementing a concrete adapter
class PostgresAdapter extends Adapter<pg.PoolConfig, pg.Query, PostgresFlags, PostgresContext> {
  constructor(client: pg.PoolConfig) {
    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 306

Constructor.<CONTEXT>

# protected Context

Reference to the context class constructor used by this adapter

The context constructor for this adapter

View Source persistence/Adapter.ts, line 840

# 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 162

# config

Provides access to the underlying persistence driver config

Gets the native persistence config

View Source persistence/Adapter.ts, line 154

# 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 565

Methods

# protected Dispatch() → {Dispatch}

Factory method that creates a dispatch instance for this adapter

Creates a new dispatch instance

View Source persistence/Adapter.ts, line 773

A new dispatch instance

Dispatch

# protected ObserverHandler() → {ObserverHandler}

Factory method that creates an observer handler for this adapter

Creates a new observer handler

View Source persistence/Adapter.ts, line 781

A new observer handler instance

# async 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 818

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 765

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 728

The adapter's identifier

string

# config() → {CONF}

Provides access to the underlying persistence driver config

Gets the native persistence config

View Source persistence/Adapter.ts, line 721

The native persistence driver config

CONF

# 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 854

A promise that resolves to the context object

Promise.<C>

# abstract create(clazz, 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
clazz string

The name of the table to insert into

id PrimaryKeyType

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 893

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 905

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 963

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 974

A promise that resolves to an array of deleted records

# async protected flags(operation, model, flags, …args) → {Promise.<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 833

The complete set of flags

Promise.<F>

# async 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 809

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 790

True if the attribute is reserved, false otherwise

boolean

# 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 996

void

# abstract parseError(err, args) → {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

args

View Source persistence/Adapter.ts, line 800

A standardized error

BaseError

# prepare(model, args)

Converts a model instance into a format suitable for database storage, handling column mapping and separating transient properties handling column mapping and separating transient properties

Prepares a model for persistence

Parameters:
Name Type Description
model M

The model instance to prepare

args

optional args for subclassing purposes

View Source persistence/Adapter.ts, line 866

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 986

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 916

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 927

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 1029

A promise that resolves when the refresh is complete

Promise.<void>

# repository() → {Constructor.<Repository.<any, Adapter.<CONF, CONN, QUERY, CONTEXT>>>}

Returns the constructor for creating repositories that work with this adapter

Gets the repository constructor for this adapter

View Source persistence/Adapter.ts, line 736

The repository constructor

Constructor.<Repository.<any, Adapter.<CONF, CONN, QUERY, CONTEXT>>>

# revert(obj, clazz, pk, id, transientopt, argsopt) → {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 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

args <optional>

options args for subclassing purposes

View Source persistence/Adapter.ts, line 881

The reconstructed model instance

M

# async shutdown() → {Promise.<void>}

Performs any necessary cleanup tasks, such as closing connections When overriding this method, ensure to call the base method first

Shuts down the adapter

View Source persistence/Adapter.ts, line 745

A promise that resolves when shutdown is complete

Promise.<void>

# toString() → {string}

Returns a human-readable string identifying this adapter

Gets a string representation of the adapter

View Source persistence/Adapter.ts, line 1036

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 1004

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 Constructor.<M>

The name of the table to update

id PrimaryKeyType

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 940

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 Constructor.<M>

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 952

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 1017

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 1052

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 1045

The adapter flavor name

string

# static get(flavour) → {Adapter.<CONF, CONN, QUERY, CONTEXT>|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 1064

The adapter instance or undefined if not found

Adapter.<CONF, CONN, QUERY, CONTEXT> | undefined

# static logCtx(args, method) → {ContextualizedArgs.<CONTEXT, ARGS>}

NOTE: if the last argument was a context, this removes the context from the arg list

retrieves the context from args and returns it, the logger and the args (with context at the end)

Parameters:
Name Type Description
args
method

View Source persistence/Adapter.ts, line 1090

ContextualizedArgs.<CONTEXT, ARGS>

# 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 1081

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 1072

void