-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cardano-services): extend typeormProvider with typeormService
- Loading branch information
1 parent
5f2d478
commit c078a91
Showing
4 changed files
with
22 additions
and
62 deletions.
There are no files selected for viewing
61 changes: 8 additions & 53 deletions
61
packages/cardano-services/src/util/TypeormProvider/TypeormProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,27 @@ | ||
import { BehaviorSubject, Observable, Subscription, filter, firstValueFrom, tap } from 'rxjs'; | ||
import { DataSource } from 'typeorm'; | ||
import { HealthCheckResponse, Provider } from '@cardano-sdk/core'; | ||
import { Logger } from 'ts-log'; | ||
import { Observable, skip } from 'rxjs'; | ||
import { PgConnectionConfig } from '@cardano-sdk/projection-typeorm'; | ||
import { RunnableModule, isNotNil } from '@cardano-sdk/util'; | ||
import { createTypeormDataSource } from './util'; | ||
import { TypeormService } from '../TypeormService'; | ||
|
||
export interface TypeormProviderDependencies { | ||
logger: Logger; | ||
entities: Function[]; | ||
connectionConfig$: Observable<PgConnectionConfig>; | ||
} | ||
|
||
export abstract class TypeormProvider extends RunnableModule implements Provider { | ||
#entities: Function[]; | ||
#subscription: Subscription | undefined; | ||
#connectionConfig$: Observable<PgConnectionConfig>; | ||
#dataSource$ = new BehaviorSubject<DataSource | null>(null); | ||
|
||
export abstract class TypeormProvider extends TypeormService implements Provider { | ||
health: HealthCheckResponse = { ok: false, reason: 'not started' }; | ||
|
||
constructor(name: string, { connectionConfig$, logger, entities }: TypeormProviderDependencies) { | ||
super(name, logger); | ||
this.#entities = entities; | ||
this.#connectionConfig$ = connectionConfig$; | ||
} | ||
|
||
#subscribeToDataSource() { | ||
this.#subscription = createTypeormDataSource(this.#connectionConfig$, this.#entities, this.logger) | ||
.pipe(tap(() => (this.health = { ok: true }))) | ||
.subscribe((dataSource) => this.#dataSource$.next(dataSource)); | ||
} | ||
|
||
#reset() { | ||
this.#subscription?.unsubscribe(); | ||
this.#subscription = undefined; | ||
this.#dataSource$.value !== null && this.#dataSource$.next(null); | ||
} | ||
|
||
onError(_: unknown) { | ||
this.#reset(); | ||
this.health = { ok: false, reason: 'Provider error' }; | ||
this.#subscribeToDataSource(); | ||
} | ||
|
||
async withDataSource<T>(callback: (dataSource: DataSource) => Promise<T>): Promise<T> { | ||
try { | ||
return await callback(await firstValueFrom(this.#dataSource$.pipe(filter(isNotNil)))); | ||
} catch (error) { | ||
this.onError(error); | ||
throw error; | ||
} | ||
super(name, { connectionConfig$, entities, logger }); | ||
// We skip 1 to omit the initial null value of the subject | ||
this.dataSource$.pipe(skip(1)).subscribe((dataSource) => { | ||
this.health = dataSource ? { ok: true } : { ok: false, reason: 'Provider error' }; | ||
}); | ||
} | ||
|
||
async healthCheck(): Promise<HealthCheckResponse> { | ||
return this.health; | ||
} | ||
|
||
async initializeImpl() { | ||
return Promise.resolve(); | ||
} | ||
|
||
async startImpl() { | ||
this.#subscribeToDataSource(); | ||
} | ||
|
||
async shutdownImpl() { | ||
this.#reset(); | ||
this.#dataSource$.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './TypeormProvider'; | ||
export * from './util'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.