Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

schema-transform-conformance.test-d.ts

124 lines4.0 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { z } from 'zod'
3import { createCollection } from '@tanstack/db'
4import { rxdbCollectionOptions } from '../src/rxdb'
5import type {
6 ChangeMessageOrDeleteKeyMessage,
7 WithVirtualProps,
8} from '@tanstack/db'
9import type { RxCollection } from 'rxdb/plugins/core'
10​
11const rowSchema = z.object({
12 id: z.string().brand<`RxDBRowId`>(),
13 createdAt: z
14 .union([z.string(), z.date()])
15 .transform((value) =>
16 typeof value === `string` ? new Date(value) : value,
17 ),
18 score: z
19 .union([z.string(), z.number()])
20 .transform((value) => (typeof value === `string` ? Number(value) : value)),
21 label: z.string().default(`untitled`),
22 note: z.string().nullish(),
23})
24​
25type RowInput = z.input<typeof rowSchema>
26type RowOutput = z.output<typeof rowSchema>
27type RowId = RowOutput[`id`]
28type ItemOf<T> = T extends Array<infer U> ? U : T
29​
30const rawInput = {
31 id: `row-1`,
32 createdAt: `2026-09-18T12:00:00.000Z`,
33 score: `42`,
34 note: null,
35} satisfies RowInput
36​
37const synchronizedOutput = {
38 id: `row-1` as RowId,
39 createdAt: new Date(`2026-09-18T12:00:00.000Z`),
40 score: 42,
41 label: `untitled`,
42 note: null,
43} satisfies RowOutput
44​
45/**
46 * Adapter mapping for the shared schema input/output law:
47 * `rxdbCollectionOptions` requires the RxDB document type to equal schema
48 * output because RxDB supplies sync rows. `getKey`, `compare`, Collection rows,
49 * and sync change messages observe that output. Public insert and update entry
50 * points accept schema input. RxDB keys remain strings because the runtime
51 * reads its string-only `primaryPath`; schema branding does not narrow them.
52 *
53 * The assertions observe those production type paths after option and
54 * Collection inference. Hostile controls reject an input row at the sync
55 * boundary and an input-shaped `RxCollection`. This partial oracle does not
56 * execute RxDB subscriptions, provider I/O, or schema parsing.
57 */
58describe(`RxDB schema transform conformance`, () => {
59 it(`requires output-shaped RxDB documents`, () => {
60 const rxCollection = {} as RxCollection<
61 RowOutput,
62 unknown,
63 unknown,
64 unknown
65 >
66 const options = rxdbCollectionOptions({
67 rxCollection,
68 schema: rowSchema,
69 compare: (left, right) => {
70 expectTypeOf(left).toEqualTypeOf<RowOutput>()
71 expectTypeOf(right).toEqualTypeOf<RowOutput>()
72 return left.score - right.score
73 },
74 })
75 const collection = createCollection(options)
76​
77 expectTypeOf(options.getKey).parameters.toEqualTypeOf<[RowOutput]>()
78 expectTypeOf(options.getKey).returns.toEqualTypeOf<string>()
79 expectTypeOf(collection.toArray).toEqualTypeOf<
80 Array<WithVirtualProps<RowOutput, string>>
81 >()
82​
83 type Insert = ItemOf<Parameters<typeof collection.insert>[0]>
84 expectTypeOf<Insert>().toEqualTypeOf<RowInput>()
85 collection.update(`row-1`, (draft) => {
86 expectTypeOf(draft).toEqualTypeOf<RowInput>()
87 })
88​
89 type SyncParams = Parameters<(typeof options)[`sync`][`sync`]>[0]
90 type SyncMessage = Parameters<SyncParams[`write`]>[0]
91 expectTypeOf<SyncMessage>().toEqualTypeOf<
92 ChangeMessageOrDeleteKeyMessage<RowOutput, string>
93 >()
94​
95 const assertRxDBBoundary = (write: SyncParams[`write`]) => {
96 const wrongDate = {
97 ...synchronizedOutput,
98 createdAt: rawInput.createdAt,
99 }
100​
101 write({ type: `insert`, value: synchronizedOutput })
102 // @ts-expect-error one untransformed field cannot cross the sync boundary
103 write({ type: `insert`, value: wrongDate })
104 // @ts-expect-error RxDB sync must not publish schema input rows
105 write({ type: `insert`, value: rawInput })
106 }
107 expectTypeOf(assertRxDBBoundary).toBeFunction()
108 })
109​
110 it(`rejects an input-shaped RxDB collection`, () => {
111 const inputCollection = {} as RxCollection<
112 RowInput,
113 unknown,
114 unknown,
115 unknown
116 >
117​
118 // @ts-expect-error provider documents must match schema output
119 rxdbCollectionOptions({
120 rxCollection: inputCollection,
121 schema: rowSchema,
122 })
123 })
124})