Skip to content

Commit

Permalink
entity-store: fetchRecord returns version -1 when stream is empty
Browse files Browse the repository at this point in the history
Returning -1 in this scenario is beneficial for handlers that might initialize _or_ append to an entity stream. Those handlers can simply pass `version` in as the `expectedVersion` for subsequent writes.
  • Loading branch information
mpareja committed Jul 8, 2020
1 parent 964b6e9 commit e46f3df
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion entity-store/entity-store-substitute.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.createEntityStoreSubstitute = ({ entity: EntityClass }) => {
}

const fetchRecord = async (id) => {
const record = records[id] || { entity: new EntityClass(), metadata: {} }
const record = records[id] || { entity: new EntityClass(), metadata: { version: -1 } }

return [record.entity, record.metadata]
}
Expand Down
2 changes: 1 addition & 1 deletion entity-store/entity-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.createEntityStore = (options) => {
const fetchRecord = async (id) => {
const entity = new EntityClass()
const streamName = StreamName.create(category, id)
const recordMetadata = {}
const recordMetadata = { version: -1 }
for await (const messageData of messageStore.read(streamName)) {
const { handler, messageClass } = registry.get(messageData.type)

Expand Down
4 changes: 2 additions & 2 deletions entity-store/test/entity-store-substitute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ describe('entity-store-substitute', () => {
expect(entity.someMessagesApplied).toBe(false)
})

it('returns no other record metadata', async () => {
it('record version is -1, stream not initialized', async () => {
const [entity, record] = await setup().entityStore.fetchRecord('unknown-id')

expect(entity).toBeDefined()
expect(record.version).toBeUndefined()
expect(record.version).toBe(-1)
})
})
})
Expand Down
38 changes: 30 additions & 8 deletions entity-store/test/fetch-test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,38 @@ module.exports.generateEntityStoreSuite = ({
})

describe('fetchRecord', () => {
it('returns entity with version', async () => {
const message = exampleMessage(MessageClassA)
const streamName = exampleStreamName(A_CATEGORY)
const id = StreamName.getId(streamName)
const position = await write(message, streamName)
describe('stream exists in store', () => {
it('returns entity with version', async () => {
const message = exampleMessage(MessageClassA)
const streamName = exampleStreamName(A_CATEGORY)
const id = StreamName.getId(streamName)
const position = await write(message, streamName)

const [entity, { version }] = await entityStore.fetchRecord(id)

expect(entity.applied).toHaveLength(1)
expect(version).toEqual(position)
})
})

const [entity, { version }] = await entityStore.fetchRecord(id)
describe('stream does not exist in store', () => {
it('returns entity with no messages applied', async () => {
const streamName = exampleStreamName(A_CATEGORY)
const id = StreamName.getId(streamName)

expect(entity.applied).toHaveLength(1)
expect(version).toEqual(position)
const [entity] = await entityStore.fetchRecord(id)

expect(entity.applied).toHaveLength(0)
})

it('record version is -1, stream not initialized', async () => {
const streamName = exampleStreamName(A_CATEGORY)
const id = StreamName.getId(streamName)

const [_, { version }] = await entityStore.fetchRecord(id) // eslint-disable-line no-unused-vars

expect(version).toBe(-1)
})
})
})

Expand Down

0 comments on commit e46f3df

Please sign in to comment.