Plain operations: last write wins, hash-keyed events, simpler composite
- KV: one write primitive, write(changes) with { key, value? }; writeIfAbsent removed.
The server executes exactly what clients send and never reads values, so they can be
encrypted end to end. Adds setFields/getFields on the base class.
- HTTP: no server history or event ids; SSE relays every change (own writes included) and
the client emits resync when a dropped stream reconnects.
- Composite: writes apply locally, then go to the source (undone locally if refused);
pushed changes are applied and announced only where they differ from local values;
offline writes go to a queue store (any KV storage); sync() = replay, diff, replay.
Removes KVReplicaSet, KVOutbox, KeyCounter, SerialQueue and RetryTimer.
- Events: id = <time>_<sha256(time + canonical data)>, so concurrent appends never
collide and retries can't duplicate; no versions or ConcurrencyError.
- Event-sourced documents: read, resolve, append, apply; catchUp folds in foreign events;
getAt takes an event id.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
This commit is contained in:
@@ -42,43 +42,48 @@ describe('EventSourcedDocumentStorage', () => {
|
||||
return { kv, events, projections, docs: new EventSourcedDocumentStorage(events, projections) }
|
||||
}
|
||||
|
||||
it('records resolved changes as history and replays any version', async () => {
|
||||
it('records resolved changes as history and replays any event', async () => {
|
||||
const users = setup().docs.child('users')
|
||||
await users.set('1', { n: 5 })
|
||||
await users.update('1', { $inc: { n: 1 } })
|
||||
await users.delete('1')
|
||||
await users.set('1', { n: 10 })
|
||||
expect((await collect(users.history('1'))).map((e) => e.data)).toEqual([
|
||||
const history = await collect(users.history('1'))
|
||||
expect(history.map((e) => e.data)).toEqual([
|
||||
{ op: 'set', doc: { n: 5 } },
|
||||
{ op: 'patch', set: { n: 6 }, unset: [] },
|
||||
{ op: 'delete' },
|
||||
{ op: 'set', doc: { n: 10 } },
|
||||
])
|
||||
expect(await users.getAt('1', 2)).toEqual({ n: 6 })
|
||||
expect(await users.child('1').getAt(3)).toBeUndefined()
|
||||
expect(await users.getAt('1', history[1]!.id)).toEqual({ n: 6 })
|
||||
expect(await users.child('1').getAt(history[2]!.id)).toBeUndefined()
|
||||
expect(await users.get('1')).toEqual({ n: 10 })
|
||||
})
|
||||
|
||||
it('heals a projection left behind by a crash between append and projection write', async () => {
|
||||
it('catchUp folds in events written elsewhere', async () => {
|
||||
const { docs, events } = setup()
|
||||
const users = docs.child('users')
|
||||
await users.set('1', { n: 1 })
|
||||
await events.child('users').append('1', [{ op: 'patch', set: { n: 2 }, unset: [] }]) // projection never written
|
||||
await events.child('users').append('1', [{ op: 'patch', set: { m: 2 }, unset: [] }]) // e.g. synced from another device
|
||||
expect(await users.get('1')).toEqual({ n: 1 })
|
||||
expect(await users.update('1', { $inc: { n: 1 } })).toEqual({ n: 3 })
|
||||
await events.child('users').append('2', [{ op: 'set', doc: { n: 7 } }]) // first write crashed
|
||||
expect(await users.catchUp('2')).toEqual({ n: 7 })
|
||||
expect(await users.get('2')).toEqual({ n: 7 })
|
||||
expect(await users.catchUp('1')).toEqual({ n: 1, m: 2 })
|
||||
expect(await users.get('1')).toEqual({ n: 1, m: 2 })
|
||||
await events.child('users').append('2', [{ op: 'set', doc: { n: 7 } }, { op: 'delete' }])
|
||||
expect(await users.catchUp('2')).toBeUndefined()
|
||||
expect(await users.get('2')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('two instances incrementing the same document converge', async () => {
|
||||
it('two instances editing different fields both keep their edits; the same field ends with the later value', async () => {
|
||||
const kv = new KVStorageInMemory()
|
||||
const make = () => new EventSourcedDocumentStorage(new EventStorageKV(kv), new DocumentStorageKV(kv.child('projections'))).child('users')
|
||||
let t = 1000
|
||||
const make = () =>
|
||||
new EventSourcedDocumentStorage(new EventStorageKV(kv, { now: () => t++ }), new DocumentStorageKV(kv.child('projections'))).child('users')
|
||||
const a = make()
|
||||
const b = make()
|
||||
await a.set('1', { n: 0 })
|
||||
await Promise.all([...Array(5)].flatMap(() => [a.update('1', { $inc: { n: 1 } }), b.update('1', { $inc: { n: 1 } })]))
|
||||
expect(await a.get('1')).toEqual({ n: 10 })
|
||||
await a.set('1', { name: 'x', age: 1 })
|
||||
await Promise.all([a.update('1', { $set: { name: 'Ada' } }), b.update('1', { $set: { age: 2 } })])
|
||||
await b.update('1', { $set: { name: 'Bob' } })
|
||||
expect(await a.catchUp('1')).toEqual({ name: 'Bob', age: 2 })
|
||||
})
|
||||
|
||||
it('a write is a projection read, an append and a projection write', async () => {
|
||||
@@ -86,6 +91,6 @@ describe('EventSourcedDocumentStorage', () => {
|
||||
await docs.child('users').set('1', { n: 1 })
|
||||
kv.reset()
|
||||
await docs.child('users').update('1', { $inc: { n: 1 } })
|
||||
expect(kv.calls).toEqual({ readMany: 2, writeIfAbsent: 1, writeMany: 1 })
|
||||
expect(kv.calls).toEqual({ readMany: 2, writeMany: 2 })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user