Skip to content

Commit

Permalink
Remove optimisticConcurrencyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalojaubert committed Jun 23, 2023
1 parent 2f159c2 commit bd7b7dd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReadModelEnvelope, ReadModelInterface, SortFor, UUID } from '@boostercloud/framework-types'
import { ReadModelEnvelope, SortFor, UUID } from '@boostercloud/framework-types'
import * as DataStore from 'nedb'
import { readModelsDatabase } from '../paths'

Expand Down Expand Up @@ -37,24 +37,19 @@ export class ReadModelRegistry {
const queryPromise = new Promise((resolve, reject) =>
cursor.exec((err, docs) => {
if (err) reject(err)
else {
docs.map((doc: ReadModelEnvelope) => {
doc.value.boosterMetadata!.optimisticConcurrencyValue = this.generateOptimisticConcurrencyValue(doc.value)
})
resolve(docs)
}
else resolve(docs)
})
)
return queryPromise as Promise<Array<ReadModelEnvelope>>
}

public async store(readModel: ReadModelEnvelope, expectedCurrentVersion: number): Promise<void> {
const readModelOptimistic: ReadModelEnvelope & { uniqueKey?: string } = readModel
readModelOptimistic.uniqueKey = `${readModel.typeName}_${readModel.value.id}_${readModel.value.boosterMetadata?.version}`
if (readModelOptimistic.value.boosterMetadata?.version === 1) {
const uniqueReadModel: ReadModelEnvelope & { uniqueKey?: string } = readModel
uniqueReadModel.uniqueKey = `${readModel.typeName}_${readModel.value.id}_${readModel.value.boosterMetadata?.version}`
if (uniqueReadModel.value.boosterMetadata?.version === 1) {
return this.insert(readModel)
}
return this.update(readModelOptimistic, expectedCurrentVersion)
return this.update(uniqueReadModel, expectedCurrentVersion)
}

private insert(readModel: ReadModelEnvelope): Promise<void> {
Expand All @@ -71,10 +66,7 @@ export class ReadModelRegistry {
{
typeName: readModel.typeName,
'value.id': readModel.value.id,
$or: [
{ 'value.boosterMetadata.optimisticConcurrencyValue': { $exists: false } },
{ 'value.boosterMetadata.optimisticConcurrencyValue': expectedCurrentVersion },
],
'value.boosterMetadata.version': expectedCurrentVersion,
},
readModel,
{ upsert: false, returnUpdatedDocs: true },
Expand All @@ -94,10 +86,6 @@ export class ReadModelRegistry {
})
}

private generateOptimisticConcurrencyValue(value: ReadModelInterface): number {
return (value.boosterMetadata?.version ?? 0) + 1 // the next version number that we are going to persist
}

public async deleteById(id: UUID, typeName: string): Promise<number> {
const deletePromise = new Promise((resolve, reject) =>
this.readModels.remove({ typeName: typeName, 'value.id': id }, { multi: false }, (err, numRemoved: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function createMockReadModelEnvelope(): ReadModelEnvelope {
boosterMetadata: {
version: 1,
schemaVersion: 1,
optimisticConcurrencyValue: 2,
},
},
typeName: random.word(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,10 @@ describe('the read model registry', () => {
it('should upsert read models into the read models database', async () => {
const readModel: ReadModelEnvelope = createMockReadModelEnvelope()
readModel.value.boosterMetadata!.version = 2
readModel.value.boosterMetadata!.optimisticConcurrencyValue = 2
const expectedQuery = {
typeName: readModel.typeName,
'value.id': readModel.value.id,
$or: [
{ 'value.boosterMetadata.optimisticConcurrencyValue': { $exists: false } },
{ 'value.boosterMetadata.optimisticConcurrencyValue': 2 },
],
'value.boosterMetadata.version': 2,
}

readModelRegistry.readModels.update = stub().yields(null, readModel)
Expand Down

0 comments on commit bd7b7dd

Please sign in to comment.