Skip to content

Instantly share code, notes, and snippets.

@joshsmith
Created January 28, 2024 18:19
Show Gist options
  • Save joshsmith/10d8fe13ca09c464db782fab01a15acd to your computer and use it in GitHub Desktop.
Save joshsmith/10d8fe13ca09c464db782fab01a15acd to your computer and use it in GitHub Desktop.
Zod schema, Prisma schema, and TypeORM PowerSync schema
// PowerSync TypeORM schema
export const TASK_TABLE = 'task';
export interface TaskRecord {
id: string;
title: string;
description?: string;
completed: boolean;
due_date?: string;
start_time?: string;
due_time?: string;
completed_at?: string;
created_at: string;
updated_at: string;
task_recurring_rule_id: string;
user_id: string;
}
export const schema = new Schema([
new Table({
name: TASK_TABLE,
columns: [
new Column({ name: 'id', type: ColumnType.TEXT }),
new Column({ name: 'title', type: ColumnType.TEXT }),
new Column({ name: 'description', type: ColumnType.TEXT }),
new Column({ name: 'completed', type: ColumnType.INTEGER }),
new Column({ name: 'due_date', type: ColumnType.TEXT }),
new Column({ name: 'start_time', type: ColumnType.TEXT }),
new Column({ name: 'due_time', type: ColumnType.TEXT }),
new Column({ name: 'completed_at', type: ColumnType.TEXT }),
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'updated_at', type: ColumnType.TEXT }),
new Column({ name: 'task_recurring_rule_id', type: ColumnType.TEXT }),
new Column({ name: 'user_id', type: ColumnType.TEXT }),
],
indexes: [
new Index({
name: 'task_recurring_rule',
columns: [new IndexedColumn({ name: 'task_recurring_rule_id' })]
}),
new Index({
name: 'user',
columns: [new IndexedColumn({ name: 'user_id' })]
})
]
}),
... omitting recurring rule for brevity
]);
// Prisma schema
model Task {
id String @id @default(uuid()) @db.Uuid
title String?
description String?
completed Boolean @default(false)
dueDate DateTime? @map("due_date") @db.Timestamptz(3)
startTime DateTime? @map("start_time") @db.Timetz(3)
dueTime DateTime? @map("due_time") @db.Timetz(3)
completedAt DateTime? @map("completed_at") @db.Timestamptz(3)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
taskRecurringRule TaskRecurringRule? @relation(fields: [taskRecurringRuleId], references: [id], onDelete: Cascade)
taskRecurringRuleId String? @map("task_recurring_rule_id") @db.Uuid
user Profile @relation(fields: [userId], references: [id])
userId String @map("user_id") @db.Uuid
@@map("task")
}
// Zod schema and types
import { z } from 'zod'
import { Task as PrismaTask } from '@it/db'
export const taskSchema = z.object({
id: z.string(),
title: z.string(),
description: z.string().nullable(),
completed: z.boolean(),
dueDate: z.date().nullable(),
startTime: z.date().nullable(),
dueTime: z.date().nullable(),
completedAt: z.date().nullable(),
createdAt: z.date(),
updatedAt: z.date(),
taskRecurringRuleId: z.string().nullable(),
userId: z.string(),
}) satisfies z.ZodType<PrismaTask>
export const taskInputSchema = taskSchema.omit({ id: true, taskRecurringRuleId: true, userId: true})
export type Task = z.infer<typeof taskSchema>
export type TaskInput = z.infer<typeof taskInputSchema>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment