# 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 |
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
Adapter.<Y, any, any, any>
# protected adapter
Reference to the database adapter whose operations are being monitored
The adapter being observed
# models
Array of model constructors that are registered with the adapter
List of model constructors
Array
# protected models
Array of model constructors that are registered with the adapter
List of model constructors
Y
# protected native
Reference to the underlying database driver from the adapter
The native database driver
Methods
# async close() → {Promise.<void>}
Performs any necessary cleanup when the dispatch is no longer needed
Closes the dispatch
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
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
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 |
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 |
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) |
A promise that resolves when all observers have been notified
Promise.<void>