Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { Schema, Table, column } from '@powersync/node'
3import { createCollection } from '@tanstack/db'
4import { z } from 'zod'
5import { powerSyncCollectionOptions } from '../src'
6import type { PowerSyncDatabase } from '@powersync/node'
7import type { PowerSyncCollectionUtils } from '../src'
8
9const APP_SCHEMA = new Schema({
10 documents: new Table({
11 name: column.text,
12 author: column.text,
13 created_at: column.text,
14 }),
15})
16
17describe(`PowerSync collection type tests`, () => {
18 it(`should type collection.utils as PowerSyncCollectionUtils after createCollection`, () => {
19 const options = powerSyncCollectionOptions({
20 database: {} as PowerSyncDatabase,
21 table: APP_SCHEMA.props.documents,
22 })
23 const collection = createCollection(options)
24
25 // Verify that collection.utils is typed as PowerSyncCollectionUtils, not UtilsRecord
26 const utils: PowerSyncCollectionUtils<
27 (typeof APP_SCHEMA.props)['documents']
28 > = collection.utils
29 expectTypeOf(utils.getMeta).toBeFunction()
30 expectTypeOf(collection.utils.getMeta).toBeFunction()
31
32 type InsertParams = Parameters<NonNullable<typeof options.onInsert>>[0]
33 type InsertMutation = InsertParams[`transaction`][`mutations`][0]
34
35 expectTypeOf<InsertMutation[`key`]>().toEqualTypeOf<string>()
36 expectTypeOf<
37 InsertMutation[`collection`][`utils`][`getMeta`]
38 >().toBeFunction()
39
40 // @ts-expect-error PowerSync Collection does not expose Electric acknowledgement helpers
41 collection.utils.awaitTxId(1)
42 })
43
44 it(`types a no-schema comparator against the inferred SQLite row`, () => {
45 powerSyncCollectionOptions({
46 database: {} as PowerSyncDatabase,
47 table: APP_SCHEMA.props.documents,
48 compare: (left, right) => {
49 expectTypeOf(left.id).toEqualTypeOf<string>()
50 expectTypeOf(left.name).toEqualTypeOf<string | null>()
51 expectTypeOf(left.author).toEqualTypeOf<string | null>()
52 expectTypeOf(left.created_at).toEqualTypeOf<string | null>()
53 return (left.name ?? ``).localeCompare(right.name ?? ``)
54 },
55 })
56 })
57
58 it(`types compare against transformed schema output`, () => {
59 const schema = z.object({
60 id: z.string(),
61 name: z.string().nullable(),
62 author: z.string().nullable(),
63 created_at: z
64 .string()
65 .nullable()
66 .transform((value) => (value ? new Date(value) : null)),
67 })
68
69 powerSyncCollectionOptions({
70 database: {} as PowerSyncDatabase,
71 table: APP_SCHEMA.props.documents,
72 schema,
73 onDeserializationError: () => {},
74 compare: (left, right) => {
75 expectTypeOf(left).toEqualTypeOf<z.infer<typeof schema>>()
76 return (
77 (left.created_at?.getTime() ?? 0) - (right.created_at?.getTime() ?? 0)
78 )
79 },
80 })
81
82 powerSyncCollectionOptions({
83 database: {} as PowerSyncDatabase,
84 table: APP_SCHEMA.props.documents,
85 schema,
86 onDeserializationError: () => {},
87 compare: (left, right) => {
88 // @ts-expect-error transformed dates do not expose string methods
89 return left.created_at!.localeCompare(right.created_at!)
90 },
91 })
92 })
93})