Skip to content

Commit

Permalink
Read Models projections by ReadModel query (#1469)
Browse files Browse the repository at this point in the history
* WIP - Read Models projections by ReadModel query

* refactor

* fix retries

* fix tests

* update documentation. minor refactor

* Update pnpm-lock.yaml after merge

* Add rush change file

* Update pnpm-lock after merge

* Post-merge updates

* Update pnpm-lock after merge

* Update pnpm-lock.yaml after merge

* Refactor method names in integration test code and docs

* Change access modifiers to methods in ReadModelStore and add some comments

* Fix prettier formatting

---------

Co-authored-by: Castro, Mario <[email protected]>
  • Loading branch information
gonzalojaubert and Castro, Mario authored Jul 30, 2024
1 parent 37345cb commit 2abba63
Show file tree
Hide file tree
Showing 37 changed files with 1,844 additions and 649 deletions.
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"
}
],
"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

0 comments on commit 2abba63

Please sign in to comment.