Skip to main content

foundation-comms.entitydatasource

Home > @genesislcap/foundation-comms > EntityDatasource

EntityDatasource interface

This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

EntityDatasource DI interface.

Signature:

export interface EntityDatasource<TDTO, TEntity> extends Datasource 

Extends: Datasource

Remarks

A simple streaming datasource cache used mainly by services that employ DTO mappers.

Example

An example service using an underlying EntityDatasource:

export class DefaultDomainService implements DomainService {
constructor(
@EntityDatasource protected datasource: EntityDatasource<DomainDTO, DomainEntity>,
@DomainDTOMapper protected mapper: DomainDTOMapper,
@optional(DomainServiceConfig) private config = defaultConfig
) {}

get initialized = () {
return this.datasource.initialized;
}

initialize = async (init) => {
await this.datasource.initialize({
mapper: this.mapper,
options: {
resourceName: this.config.resourceName,
...init,
},
});
return this.initialized;
};

list = () => this.datasource.cache;

get = async (id: string) => this.datasource.cache.find((entity) => entity.id === id);

action = async (id: string, message?: string): Promise<Message> => {
return this.commitEvent('EVENT_DOMAIN_ACTION', { id, message });
};

private async commitEvent(event: string, entity: Partial<DomainEntity>) {
const msg = await this.connect.commitEvent(event, {
DETAILS: this.mapper.toDTO(entity),
IGNORE_WARNINGS: true,
VALIDATE: false,
});
return messageOrThrow(msg);
}

Properties

Property

Modifiers

Type

Description

cache

readonly

TEntity[]

(ALPHA)

isEmpty

readonly

boolean

(ALPHA)

Methods

Method

Description

disconnect()

(ALPHA)

initialize(init)

(ALPHA)