- BaseKVStorage / BaseEventStorage / BaseDocumentStorage define the API once; implementations provide only raw primitives (client and server share an API) - Drop KV indexes, query planner, unique indexes; document stores take index fields - Escape every char <= '/' so child keys stay contiguous; skip-scan is now simple - Event-sourced documents = event storage + any document store for projections - New folder layout (kv-storage, event-storage, document-storage, utils); tests only in test/ Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
90 lines
4.0 KiB
TypeScript
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', 3)).toEqualTypeOf<Promise<User | undefined>>()
|
|
})
|
|
})
|