import type { AdapterAccount } from "@auth/core/adapters"; import { createId } from "@paralleldrive/cuid2"; import { relations } from "drizzle-orm"; import { AnySQLiteColumn, index, integer, primaryKey, sqliteTable, text, unique, } from "drizzle-orm/sqlite-core"; function createdAtField() { return integer("createdAt", { mode: "timestamp" }) .notNull() .$defaultFn(() => new Date()); } export const apiKeys = sqliteTable( "apiKey", { id: text("id") .notNull() .primaryKey() .$defaultFn(() => createId()), name: text("name").notNull(), createdAt: createdAtField(), keyId: text("keyId").notNull().unique(), keyHash: text("keyHash").notNull(), userId: text("userId") .notNull() .references(() => users.id, { onDelete: "cascade" }), }, (ak) => ({ unq: unique().on(ak.name, ak.userId), }), ); export const users = sqliteTable("user", { id: text("id") .notNull() .primaryKey() .$defaultFn(() => createId()), name: text("name").notNull(), email: text("email").notNull().unique(), emailVerified: integer("emailVerified", { mode: "timestamp_ms" }), image: text("image"), password: text("password"), role: text("role", { enum: ["admin", "user"] }).default("user"), }); export const accounts = sqliteTable( "account", { userId: text("userId") .notNull() .references(() => users.id, { onDelete: "cascade" }), type: text("type").$type().notNull(), provider: text("provider").notNull(), providerAccountId: text("providerAccountId").notNull(), refresh_token: text("refresh_token"), access_token: text("access_token"), expires_at: integer("expires_at"), token_type: text("token_type"), scope: text("scope"), id_token: text("id_token"), session_state: text("session_state"), }, (account) => ({ compoundKey: primaryKey({ columns: [account.provider, account.providerAccountId], }), }), ); export const sessions = sqliteTable("session", { sessionToken: text("sessionToken") .notNull() .primaryKey() .$defaultFn(() => createId()), userId: text("userId") .notNull() .references(() => users.id, { onDelete: "cascade" }), expires: integer("expires", { mode: "timestamp_ms" }).notNull(), }); export const verificationTokens = sqliteTable( "verificationToken", { identifier: text("identifier").notNull(), token: text("token").notNull(), expires: integer("expires", { mode: "timestamp_ms" }).notNull(), }, (vt) => ({ compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }), }), );