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

seed-data.ts

237 lines6.6 KB

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