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

types.ts

113 lines2.8 KB

1import type { Collection } from '@tanstack/db'
2​
3/**
4 * Test data schema types
5 */
6export interface User {
7 id: string // UUID
8 name: string // For collation testing
9 email: string | null
10 age: number
11 isActive: boolean
12 createdAt: Date
13 metadata: Record<string, unknown> | null // JSON field
14 deletedAt: Date | null // Soft delete
16​
17export interface Post {
18 id: string
19 userId: string // FK to User
20 title: string
21 content: string | null
22 viewCount: number
23 largeViewCount: bigint // BIGINT field for testing large number serialization
24 publishedAt: Date | null
25 deletedAt: Date | null
27​
28export interface Comment {
29 id: string
30 postId: string // FK to Post
31 userId: string // FK to User
32 text: string
33 createdAt: Date
34 deletedAt: Date | null
36​
37/**
38 * Seed data result
39 */
40export interface SeedDataResult {
41 users: Array<User>
42 posts: Array<Post>
43 comments: Array<Comment>
44 userIds: Array<string>
45 postIds: Array<string>
46 commentIds: Array<string>
48​
49/**
50 * Test configuration for e2e tests
51 */
52export interface E2ETestConfig {
53 // Exact initial test input, detached on every read; never collection output.
54 fixture: () => SeedDataResult
55 collections: {
56 eager: {
57 users: Collection<User>
58 posts: Collection<Post>
59 comments: Collection<Comment>
60 }
61 onDemand: {
62 users: Collection<User>
63 posts: Collection<Post>
64 comments: Collection<Comment>
65 }
66 progressive?: {
67 users: Collection<User>
68 posts: Collection<Post>
69 comments: Collection<Comment>
70 }
71 }
72​
73 // Mutation helpers using collection APIs (works for both Electric and Query)
74 // Note: Requires collections to have onInsert/onUpdate/onDelete handlers configured
75 mutations?: {
76 insertUser: (user: User) => Promise<void>
77 updateUser: (id: string, updates: Partial<User>) => Promise<void>
78 deleteUser: (id: string) => Promise<void>
79 insertPost: (post: Post) => Promise<void>
80 deletePost: (id: string) => Promise<void>
81 }
82​
83 // Helper to get txid for Electric txid tracking tests (Electric only)
84 getTxid?: () => Promise<number | null>
85​
86 // Indicates if the backend has replication lag (e.g., Electric sync)
87 // When true, tests will wait for mutations to propagate before proceeding
88 hasReplicationLag?: boolean
89​
90 // Test control for progressive mode (Electric only)
91 // Allows explicit control over when initial sync completes for deterministic testing
92 progressiveTestControl?: {
93 releaseInitialSync: () => void
94 }
95​
96 // Lifecycle hooks
97 setup: () => Promise<void>
98 teardown: () => Promise<void>
99 beforeEach?: () => Promise<void>
100 afterEach?: () => Promise<void>
102​
103/**
104 * Database client interface (pg Client)
105 */
106export interface DbClient {
107 connect: () => Promise<void>
108 end: () => Promise<void>
109 query: (
110 sql: string,
111 values?: Array<unknown>,
112 ) => Promise<{ rows: Array<unknown> }>