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

seed-data.ts

249 lines7.1 KB

1import { reportFixtureCapture } from './fixture-artifact'
2import type { FixtureDiagnostics } from './fixture-artifact'
3import type { Comment, Post, SeedDataResult, User } from '../types'
4​
5/** Capture once before driver delivery; each reader owns a detached copy. */
6export function captureSeedData(
7 seedData: SeedDataResult,
8 diagnostics?: FixtureDiagnostics,
9): () => SeedDataResult {
10 const authority = structuredClone(seedData)
11 if (diagnostics) reportFixtureCapture(authority, diagnostics)
12 return () => structuredClone(authority)
14​
15// Cache UUIDs for deterministic behavior across test runs
16const uuidCache = new Map<string, string>()
17​
18/**
19 * Generate deterministic valid UUIDs for testing
20 */
21function generateId(prefix: string, index: number): string {
22 const key = `${prefix}-${index}`
23 if (!uuidCache.has(key)) {
24 // Generate a real UUID but make it deterministic for the same prefix+index
25 const hex = index.toString(16).padStart(8, `0`)
26 // Create a valid UUID v4 format
27 uuidCache.set(
28 key,
29 `${hex.slice(0, 8)}-0000-4000-8000-${hex.padStart(12, `0`)}`,
30 )
31 }
32 return uuidCache.get(key)!
34​
35/**
36 * Generate seed data with proper distributions for testing
37 *
38 * Data characteristics:
39 * - ~100 users with varied attributes
40 * - ~100 posts distributed across users
41 * - ~100 comments distributed across posts
42 * - Mix of null/non-null values
43 * - Various string cases (uppercase, lowercase, special chars)
44 * - Date ranges (past, present, future)
45 * - Numeric ranges (negative, zero, positive)
46 * - Some soft-deleted records
47 */
48export function generateSeedData(): SeedDataResult {
49 const users: Array<User> = []
50 const posts: Array<Post> = []
51 const comments: Array<Comment> = []
52​
53 const now = new Date()
54 const oneDay = 24 * 60 * 60 * 1000
55 const oneYear = 365 * oneDay
56​
57 // Generate 100 users with varied distributions
58 for (let i = 0; i < 100; i++) {
59 const id = generateId(`user`, i)
60​
61 // Name variations for collation testing
62 const names = [
63 `Alice ${i}`,
64 `bob ${i}`,
65 `Charlie ${i}`,
66 `DIANA ${i}`,
67 `Eve ${i}`,
68 `Frank ${i}`,
69 `Grace ${i}`,
70 `henry ${i}`,
71 `Ivy ${i}`,
72 `Jack ${i}`,
73 `Kate ${i}`,
74 `liam ${i}`,
75 `Mia ${i}`,
76 `Noah ${i}`,
77 `Olivia ${i}`,
78 `PAUL ${i}`,
79 `Quinn ${i}`,
80 `Rose ${i}`,
81 `sam ${i}`,
82 `Tina ${i}`,
83 `Ursula ${i}\nNewline`,
84 ]
85 const name = names[i % names.length]
86​
87 // Email: 70% have emails, 30% null
88 const email = i % 10 < 7 ? `user${i}@example.com` : null
89​
90 // Age distribution: 18-80, with some edge cases
91 const age = i === 0 ? 0 : i === 1 ? -5 : i === 2 ? 150 : 18 + (i % 63)
92​
93 // IsActive: 80% true, 20% false
94 const isActive = i % 5 !== 0
95​
96 // CreatedAt: distributed over past year
97 const createdAt = new Date(now.getTime() - Math.random() * oneYear)
98​
99 // Metadata: 40% have metadata, 60% null
100 const metadata =
101 i % 5 < 2 ? { score: i * 10, level: Math.floor(i / 10) } : null
102​
103 // DeletedAt: 10% soft deleted
104 const deletedAt =
105 i % 10 === 0
106 ? new Date(now.getTime() - Math.random() * oneDay * 30)
107 : null
108​
109 users.push({
110 id: id,
111 name: name!,
112 email,
113 age,
114 isActive,
115 createdAt,
116 metadata,
117 deletedAt,
118 })
119 }
120​
121 // Generate 100 posts distributed across users
122 for (let i = 0; i < 100; i++) {
123 const id = generateId(`post`, i)
124​
125 // Distribute posts across users (some users have multiple posts)
126 const userId = users[i % users.length]!.id
127​
128 // Title variations
129 const titles = [
130 `Introduction to ${i}`,
131 `Deep Dive: Topic ${i}`,
132 `Quick Guide ${i}`,
133 `ANNOUNCEMENT: ${i}`,
134 `tutorial ${i}`,
135 `Best Practices ${i}`,
136 ]
137 const title = titles[i % titles.length]!
138​
139 // Content: 70% have content, 30% null
140 const content =
141 i % 10 < 7
142 ? `This is the content for post ${i}. Lorem ipsum dolor sit amet.`
143 : null
144​
145 // ViewCount: varied distribution
146 const viewCount = i === 0 ? 0 : i === 1 ? -10 : i * 42
147​
148 // LargeViewCount: BigInt field with values exceeding MAX_SAFE_INTEGER for some posts
149 // This tests BigInt serialization in predicates
150 // Base value: 9007199254740992n (Number.MAX_SAFE_INTEGER + 1)
151 const largeViewCount =
152 i < 10
153 ? BigInt(`9007199254740992`) + BigInt(i) // First 10 posts have very large values
154 : BigInt(i * 1000) // Other posts have smaller bigint values
155​
156 // PublishedAt: 80% published, 20% null (drafts)
157 const publishedAt =
158 i % 5 !== 0 ? new Date(now.getTime() - Math.random() * oneYear) : null
159​
160 // DeletedAt: 5% soft deleted
161 const deletedAt =
162 i % 20 === 0
163 ? new Date(now.getTime() - Math.random() * oneDay * 10)
164 : null
165​
166 posts.push({
167 id,
168 userId,
169 title,
170 content,
171 viewCount,
172 largeViewCount,
173 publishedAt,
174 deletedAt,
175 })
176 }
177​
178 // Generate 100 comments distributed across posts
179 for (let i = 0; i < 100; i++) {
180 const id = generateId(`comment`, i)
181​
182 // Distribute comments across posts (some posts have multiple comments)
183 const postId = posts[i % posts.length]!.id
184 const userId = users[(i * 3) % users.length]!.id
185​
186 // Text variations
187 const texts = [
188 `Great post! Comment ${i}`,
189 `I disagree with comment ${i}`,
190 `question about ${i}`,
191 `AMAZING WORK ${i}`,
192 `thanks for sharing ${i}`,
193 `Very helpful comment ${i}`,
194 ]
195 const text = texts[i % texts.length]!
196​
197 // CreatedAt: distributed over past 6 months
198 const createdAt = new Date(now.getTime() - Math.random() * (oneYear / 2))
199​
200 // DeletedAt: 8% soft deleted
201 const deletedAt =
202 i % 13 === 0 ? new Date(now.getTime() - Math.random() * oneDay * 5) : null
203​
204 comments.push({
205 id,
206 postId,
207 userId,
208 text,
209 createdAt,
210 deletedAt,
211 })
212 }
213​
214 return {
215 users,
216 posts,
217 comments,
218 userIds: users.map((u) => u.id),
219 postIds: posts.map((p) => p.id),
220 commentIds: comments.map((c) => c.id),
221 }
223​
224/**
225 * Get expected counts for different predicate scenarios
226 */
227export function getExpectedCounts(seedData: SeedDataResult) {
228 return {
229 // Users
230 totalUsers: seedData.users.length,
231 activeUsers: seedData.users.filter((u) => u.isActive).length,
232 deletedUsers: seedData.users.filter((u) => u.deletedAt !== null).length,
233 usersWithEmail: seedData.users.filter((u) => u.email !== null).length,
234 usersWithoutEmail: seedData.users.filter((u) => u.email === null).length,
235 usersWithMetadata: seedData.users.filter((u) => u.metadata !== null).length,
236​
237 // Posts
238 totalPosts: seedData.posts.length,
239 publishedPosts: seedData.posts.filter((p) => p.publishedAt !== null).length,
240 draftPosts: seedData.posts.filter((p) => p.publishedAt === null).length,
241 deletedPosts: seedData.posts.filter((p) => p.deletedAt !== null).length,
242 postsWithContent: seedData.posts.filter((p) => p.content !== null).length,
243​
244 // Comments
245 totalComments: seedData.comments.length,
246 deletedComments: seedData.comments.filter((c) => c.deletedAt !== null)
247 .length,
248 }