Class

Dispatch

Dispatch()

Constructor

# new Dispatch()

The Dispatch class implements the Observable interface and is responsible for intercepting database operations from an Adapter and notifying observers when changes occur. It uses proxies to wrap the adapter's CRUD methods and automatically trigger observer updates after operations complete.

Dispatches database operation events to observers

Parameters:
Type Description
void

No constructor parameters

View Source persistence/Dispatch.ts, line 6

Example
```typescript
// Creating and using a Dispatch instance
const dispatch = new Dispatch<PostgresDriver>();

// Connect it to an adapter
const adapter = new PostgresAdapter(connection);
dispatch.observe(adapter);

// Now any CRUD operations on the adapter will automatically
// trigger observer notifications
await adapter.create('users', 123, userModel);
// Observers will be notified about the creation

// When done, you can disconnect
dispatch.unObserve(adapter);
```

Classes

Dispatch

Initializes a new Dispatch instance without any adapter

Dispatch

Initializes a new Dispatch instance without any adapter

Members

# adapter

Reference to the database adapter whose operations are being monitored

The adapter being observed

View Source persistence/Dispatch.ts, line 37

Adapter.<Y, any, any, any>

# protected adapter

Reference to the database adapter whose operations are being monitored

The adapter being observed

View Source persistence/Dispatch.ts, line 223

# log

Gets or initializes the logger for this dispatch instance

Accessor for the logger

View Source persistence/Dispatch.ts, line 58

# logger

Logger for recording dispatch activities

Logger instance

View Source persistence/Dispatch.ts, line 52

# models

Array of model constructors that are registered with the adapter

List of model constructors

View Source persistence/Dispatch.ts, line 47

Array

# protected models

Array of model constructors that are registered with the adapter

List of model constructors

View Source persistence/Dispatch.ts, line 237

# native

Reference to the underlying database driver from the adapter

The native database driver

View Source persistence/Dispatch.ts, line 42

Y

# protected native

Reference to the underlying database driver from the adapter

The native database driver

View Source persistence/Dispatch.ts, line 230

Methods

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

Performs any necessary cleanup when the dispatch is no longer needed

Closes the dispatch

View Source persistence/Dispatch.ts, line 307

A promise that resolves when closing is complete

Promise.<void>

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

Sets up proxies on the adapter's CRUD methods to intercept operations and notify observers. This method is called automatically when an adapter is observed.

Initializes the dispatch by proxying adapter methods

sequenceDiagram participant Dispatch participant Adapter participant Proxy Dispatch->>Dispatch: initialize() Dispatch->>Dispatch: Check if adapter exists alt No adapter Dispatch-->>Dispatch: Throw InternalError end loop For each CRUD method Dispatch->>Adapter: Check if method exists alt Method doesn't exist Dispatch-->>Dispatch: Throw InternalError end Dispatch->>Adapter: Get property descriptor loop While descriptor not found Dispatch->>Adapter: Check prototype chain end alt Descriptor not found or not writable Dispatch->>Dispatch: Log error and continue else Descriptor found and writable Dispatch->>Proxy: Create proxy for method Dispatch->>Adapter: Replace method with proxy end end

View Source persistence/Dispatch.ts, line 299

A promise that resolves when initialization is complete

Promise.<void>

# protected log() → {Logger}

Gets or initializes the logger for this dispatch instance

Accessor for the logger

View Source persistence/Dispatch.ts, line 252

The logger instance

Logger

# observe(observer) → {void}

Connects this dispatch to an adapter to monitor its operations

Starts observing an adapter

Parameters:
Name Type Description
observer Adapter.<Y, any, any, any>

The adapter to observe

View Source persistence/Dispatch.ts, line 315

void

# unObserve(observer) → {void}

Disconnects this dispatch from an adapter

Stops observing an adapter

Parameters:
Name Type Description
observer Observer

The adapter to stop observing

View Source persistence/Dispatch.ts, line 323

void

# async updateObservers(table, event, id) → {Promise.<void>}

Notifies observers about a change in the database

Updates observers about a database event

Parameters:
Name Type 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)

View Source persistence/Dispatch.ts, line 334

A promise that resolves when all observers have been notified

Promise.<void>