Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Models projections by ReadModel query #1469

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@boostercloud/framework-core",
"comment": "Read Models projections by ReadModel query",
"type": "minor"
MarcAstr0 marked this conversation as resolved.
Show resolved Hide resolved
}
],
"packageName": "@boostercloud/framework-core"
}
119 changes: 58 additions & 61 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/framework-common-helpers/src/retrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Class, Logger } from '@boostercloud/framework-types'
* @returns The result of the first successful retry.
*/
export async function retryIfError<TReturn>(
logicToRetry: () => Promise<TReturn>,
logicToRetry: (tryNumber?: number) => Promise<TReturn>,
errorClassThatRetries: Class<Error>,
logger?: Logger,
maxRetries = 1000
Expand All @@ -20,7 +20,7 @@ export async function retryIfError<TReturn>(
for (tryNumber = 1; tryNumber <= maxRetries; tryNumber++) {
try {
logger?.debug(`[retryIfError] Try number ${tryNumber}`)
const result = await logicToRetry()
const result = await logicToRetry(tryNumber)
logger?.debug(`[retryIfError] Succeeded after ${tryNumber} retries`)
return result
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/framework-core/src/booster-event-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BoosterConfig, TraceActionTypes } from '@boostercloud/framework-types'
import { EventStore } from './services/event-store'
import { RawEventsParser } from './services/raw-events-parser'
import { ReadModelStore } from './services/read-model-store'
import { getLogger } from '@boostercloud/framework-common-helpers'
import { ReadModelStore } from './services/read-model-store'
import { Trace } from './instrumentation'
import { BoosterEventProcessor } from './booster-event-processor'

Expand Down
14 changes: 14 additions & 0 deletions packages/framework-core/src/booster-read-models-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
ReadModelMetadata,
ReadModelRequestEnvelope,
ReadOnlyNonEmptyArray,
SequenceKey,
SortFor,
SubscriptionEnvelope,
TraceActionTypes,
UUID,
} from '@boostercloud/framework-types'
import {
createInstance,
Expand Down Expand Up @@ -130,6 +132,18 @@ export class BoosterReadModelsReader {
return this.migrateReadModels(readModels, readModelName)
}

public async finderByIdFunction<TReadModel extends ReadModelInterface>(
readModelClass: AnyClass,
id: UUID,
sequenceKey?: SequenceKey
): Promise<ReadOnlyNonEmptyArray<TReadModel> | TReadModel> {
const readModels = await this.config.provider.readModels.fetch(this.config, readModelClass.name, id, sequenceKey)
if (sequenceKey) {
return readModels as ReadOnlyNonEmptyArray<TReadModel>
}
return readModels[0] as TReadModel
}

private async migrateReadModels<TReadModel extends ReadModelInterface>(
readModels: Array<TReadModel> | ReadModelListResult<TReadModel>,
readModelName: string
Expand Down
24 changes: 2 additions & 22 deletions packages/framework-core/src/booster.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { createInstance } from '@boostercloud/framework-common-helpers'
import {
AnyClass,
BoosterConfig,
BoosterConfigTag,
Class,
EntityInterface,
EventSearchParameters,
EventSearchResponse,
FinderByKeyFunction,
PaginatedEntitiesIdsResult,
ReadModelInterface,
ReadOnlyNonEmptyArray,
Searcher,
SearcherFunction,
SequenceKey,
UUID,
} from '@boostercloud/framework-types'
import { Importer } from './importer'
Expand All @@ -24,8 +19,8 @@ import { BoosterDataMigrationStarted } from './core-concepts/data-migration/even
import { BoosterDataMigrationFinished } from './core-concepts/data-migration/events/booster-data-migration-finished'
import { JwksUriTokenVerifier, JWT_ENV_VARS } from './services/token-verifiers'
import { BoosterAuthorizer } from './booster-authorizer'
import { BoosterReadModelsReader } from './booster-read-models-reader'
import { BoosterEntityTouched } from './core-concepts/touch-entity/events/booster-entity-touched'
import { readModelSearcher } from './services/read-model-searcher'
import { eventSearch } from './booster-event-search'
import { Effect, pipe } from 'effect'
import { Command } from '@effect/cli'
Expand Down Expand Up @@ -107,22 +102,7 @@ export class Booster {
public static readModel<TReadModel extends ReadModelInterface>(
readModelClass: Class<TReadModel>
): Searcher<TReadModel> {
const finderByIdFunction: FinderByKeyFunction<TReadModel> = async (
readModelClass: AnyClass,
id: UUID,
sequenceKey?: SequenceKey
) => {
const readModels = await this.config.provider.readModels.fetch(this.config, readModelClass.name, id, sequenceKey)
if (sequenceKey) {
return readModels as ReadOnlyNonEmptyArray<TReadModel>
}
return readModels[0] as TReadModel
}
const boosterReadModelsReader = new BoosterReadModelsReader(this.config)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const searcherFunction: SearcherFunction<TReadModel, any> =
boosterReadModelsReader.readModelSearch.bind(boosterReadModelsReader)
return new Searcher(readModelClass, searcherFunction, finderByIdFunction)
return readModelSearcher(this.config, readModelClass)
}

public static async events(request: EventSearchParameters): Promise<Array<EventSearchResponse>> {
Expand Down
Loading
Loading