# MystBin ! - schema.ts import { createId as cuid } from "@paralleldrive/cuid2"; import type { InferInsertModel, InferSelectModel } from "drizzle-orm"; import { relations, sql } from "drizzle-orm"; import { customType, index, integer, primaryKey, sqliteTable, text } from "drizzle-orm/sqlite-core"; const dateTime = customType<{ data: Date; driverData: string }>({ dataType() { return "DATETIME"; }, fromDriver(value): Date { return new Date(value); }, }); export const users = sqliteTable("user", { id: text() .notNull() .primaryKey() .$defaultFn(() => cuid()), name: text().notNull(), username: text().unique(), email: text().notNull().unique(), dob: dateTime().notNull(), image: text(), createdAt: dateTime("created_at") .notNull() .default(sql`(CURRENT_TIMESTAMP)`), updatedAt: dateTime("updated_at") .notNull() .$defaultFn(() => new Date()), }); export const accounts = sqliteTable("account", { id: text() .notNull() .primaryKey() .$defaultFn(() => cuid()), providerId: text("provider_id").notNull().unique(), provider: text({ enum: ["crendetials", "google", "discord"] }).notNull(), password: text().unique(), userId: text("user_id") .notNull() .unique() .references(() => users.id, { onDelete: "cascade", onUpdate: "cascade" }), createdAt: dateTime("created_at") .notNull() .default(sql`(CURRENT_TIMESTAMP)`), updatedAt: dateTime("updated_at") .notNull() .$defaultFn(() => new Date()), }); export const verifications = sqliteTable( "verification", { target: text().notNull(), type: text({ enum: ["onboarding", "reset-password"] }).notNull(), charSet: text("char_set").notNull(), secret: text().notNull(), algorithm: text().notNull(), digits: integer().notNull(), period: integer().notNull(), createdAt: dateTime("created_at") .notNull() .default(sql`(CURRENT_TIMESTAMP)`), expiresAt: dateTime("expires_at").notNull(), }, (verification) => [ primaryKey({ name: "verification_target_type_pkey", columns: [verification.target, verification.type] }), ], ); export const sessions = sqliteTable( "session", { id: text() .notNull() .primaryKey() .$defaultFn(() => cuid()), userId: text("user_id") .notNull() .references(() => users.id, { onDelete: "cascade" }), token: text().notNull().unique(), userAgent: text("user_agent"), ipAddress: text("ip_address"), createdAt: dateTime("created_at") .notNull() .default(sql`(CURRENT_TIMESTAMP)`), updatedAt: dateTime("updated_at") .notNull() .$defaultFn(() => new Date()), expiresAt: dateTime("expires_at").notNull(), }, (session) => [index("session_userId_idx").on(session.userId)], ); export const usersRelations = relations(users, ({ many }) => ({ accounts: many(accounts), sessions: many(sessions), })); export const accountsRelations = relations(accounts, ({ one }) => ({ user: one(users, { relationName: "AccountToUser", fields: [accounts.userId], references: [users.id], }), })); export const SessionRelations = relations(sessions, ({ one }) => ({ user: one(users, { relationName: "SessionToUser", fields: [sessions.userId], references: [users.id], }), })); export type SessionSelectType = InferSelectModel; export type UserInsertType = InferInsertModel; export type UserSelectType = InferSelectModel; export type AccountSelectType = InferSelectModel;