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

test-schema.ts

106 lines2.3 KB

1/**
2 * SQL schema definitions for test tables
3 */
4​
5export const USERS_TABLE_SCHEMA = `
6 id UUID PRIMARY KEY,
7 name TEXT NOT NULL,
8 email TEXT,
9 age INTEGER NOT NULL,
10 "isActive" BOOLEAN NOT NULL DEFAULT true,
11 "createdAt" TIMESTAMP NOT NULL DEFAULT NOW(),
12 metadata JSONB,
13 "deletedAt" TIMESTAMP
15​
16export const POSTS_TABLE_SCHEMA = `
17 id UUID PRIMARY KEY,
18 "userId" UUID NOT NULL,
19 title TEXT NOT NULL,
20 content TEXT,
21 "viewCount" INTEGER NOT NULL DEFAULT 0,
22 "publishedAt" TIMESTAMP,
23 "deletedAt" TIMESTAMP
25​
26export const COMMENTS_TABLE_SCHEMA = `
27 id UUID PRIMARY KEY,
28 "postId" UUID NOT NULL,
29 "userId" UUID NOT NULL,
30 text TEXT NOT NULL,
31 "createdAt" TIMESTAMP NOT NULL DEFAULT NOW(),
32 "deletedAt" TIMESTAMP
34​
35/**
36 * Helper to create all test tables
37 */
38export async function createTestTables(
39 dbClient: { query: (sql: string) => Promise<void> },
40 tableNames: {
41 users: string
42 posts: string
43 comments: string
44 },
45): Promise<void> {
46 await dbClient.query(`
47 CREATE TABLE IF NOT EXISTS ${tableNames.users} (${USERS_TABLE_SCHEMA});
48 `)
49​
50 await dbClient.query(`
51 CREATE TABLE IF NOT EXISTS ${tableNames.posts} (${POSTS_TABLE_SCHEMA});
52 `)
53​
54 await dbClient.query(`
55 CREATE TABLE IF NOT EXISTS ${tableNames.comments} (${COMMENTS_TABLE_SCHEMA});
56 `)
58​
59/**
60 * Helper to drop all test tables
61 */
62export async function dropTestTables(
63 dbClient: { query: (sql: string) => Promise<void> },
64 tableNames: {
65 users: string
66 posts: string
67 comments: string
68 },
69): Promise<void> {
70 await dbClient.query(`DROP TABLE IF EXISTS ${tableNames.comments}`)
71 await dbClient.query(`DROP TABLE IF EXISTS ${tableNames.posts}`)
72 await dbClient.query(`DROP TABLE IF EXISTS ${tableNames.users}`)
74​
75/**
76 * Type-safe table column mappings
77 */
78export const USER_COLUMNS = {
79 id: `id`,
80 name: `name`,
81 email: `email`,
82 age: `age`,
83 isActive: `isActive`,
84 createdAt: `createdAt`,
85 metadata: `metadata`,
86 deletedAt: `deletedAt`,
87} as const
88​
89export const POST_COLUMNS = {
90 id: `id`,
91 userId: `userId`,
92 title: `title`,
93 content: `content`,
94 viewCount: `viewCount`,
95 publishedAt: `publishedAt`,
96 deletedAt: `deletedAt`,
97} as const
98​
99export const COMMENT_COLUMNS = {
100 id: `id`,
101 postId: `postId`,
102 userId: `userId`,
103 text: `text`,
104 createdAt: `createdAt`,
105 deletedAt: `deletedAt`,
106} as const