Files
Claude 631c75428b
CI / Node 22 (pull_request) Successful in 12s
CI / Node 26 (pull_request) Successful in 13s
CI / Node 24 (pull_request) Successful in 14s
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
2026-10-23 13:33:07 +00:00

90 lines
4.0 KiB
TypeScript

import { describe, expectTypeOf, it } from 'vitest'
import type { BaseKVStorage } from '../src/kv-storage/base-kv-storage.js'
import { KVStorageInMemory } from '../src/kv-storage/kv-storage-in-memory.js'
import { EventStorageKV } from '../src/event-storage/event-storage-kv.js'
import type { StoredEvent } from '../src/event-storage/base-event-storage.js'
import { DocumentStorageInMemory } from '../src/document-storage/document-storage-in-memory.js'
import { EventSourcedDocumentStorage } from '../src/document-storage/event-sourced-document-storage.js'
import type { DocChange } from '../src/utils/document-update.js'
import type { FindResult } from '../src/utils/document-query.js'
import type { Fields, Many } from '../src/utils/schema.js'
type User = { username: string; age: number }
type Post = { title: string; createdAt: string }
type Schema = {
children: {
users: Many<User, { posts: Many<Post> }>
profile: { children: { [id: string]: { children: Fields<User> } } }
}
}
describe('KV types', () => {
const db = new KVStorageInMemory<Schema>()
it('types reads at dynamic and named levels', () => {
expectTypeOf(db.child('users').get('123')).toEqualTypeOf<Promise<User | undefined>>()
expectTypeOf(db.child('users').child('123').get()).toEqualTypeOf<Promise<User | undefined>>()
expectTypeOf(db.child('profile').child('9').get('age')).toEqualTypeOf<Promise<number | undefined>>()
expectTypeOf(db.child('users').child('123').child('posts')).toEqualTypeOf<BaseKVStorage<Many<Post>>>()
// @ts-expect-error unknown child
db.child('users').child('123').child('postz')
// @ts-expect-error unknown field
db.child('profile').child('9').get('agee')
})
it('types writes', () => {
db.child('users').set('1', { username: 'a', age: 1 })
db.child('users').child('1').set({ username: 'a', age: 1 })
// @ts-expect-error missing field
db.child('users').set('1', { username: 'a' })
db.child('profile')
.child('9')
.setMany([
['age', 3],
// @ts-expect-error value doesn't match its key
['username', 3],
])
})
it('types listings', () => {
expectTypeOf(db.child('profile').child('9').keys()).toEqualTypeOf<AsyncGenerator<'username' | 'age'>>()
expectTypeOf(db.child('users').entries()).toEqualTypeOf<AsyncGenerator<[string, User]>>()
})
it('is untyped by default', () => {
expectTypeOf(new KVStorageInMemory().child('a').child(1).get('b')).toEqualTypeOf<Promise<unknown>>()
})
})
describe('event and document types', () => {
type OrderEvent = { type: 'created'; total: number } | { type: 'paid' }
const orders = new EventStorageKV<{ children: { orders: Many<OrderEvent> } }>(new KVStorageInMemory()).child('orders')
const users = new DocumentStorageInMemory<{ children: { users: Many<User, { posts: Many<Post> }> } }>().child('users')
it('types events', () => {
orders.append('o1', [{ type: 'created', total: 1 }])
orders.child('o1').append([{ type: 'paid' }])
// @ts-expect-error not an OrderEvent
orders.append('o1', [{ type: 'refunded' }])
expectTypeOf(orders.read('o1')).toEqualTypeOf<AsyncGenerator<StoredEvent<OrderEvent>>>()
})
it('types documents', () => {
expectTypeOf(users.get('1')).toEqualTypeOf<Promise<User | undefined>>()
expectTypeOf(users.update('1', { $inc: { age: 1 } })).toEqualTypeOf<Promise<User | undefined>>()
expectTypeOf(users.child('1').child('posts').get('p')).toEqualTypeOf<Promise<Post | undefined>>()
expectTypeOf(users.find()).toEqualTypeOf<AsyncIterable<FindResult<User>>>()
// @ts-expect-error missing field
users.insert({ username: 'a' })
})
it('keeps event-sourced methods on children', () => {
const docs = new EventSourcedDocumentStorage<{ children: { users: Many<User> } }>(
new EventStorageKV(new KVStorageInMemory()),
new DocumentStorageInMemory(),
)
expectTypeOf(docs.child('users').history('1')).toEqualTypeOf<AsyncIterable<StoredEvent<DocChange>>>()
expectTypeOf(docs.child('users').getAt('1', 'id')).toEqualTypeOf<Promise<User | undefined>>()
})
})