1import { defineRelationsPart } from "drizzle-orm";
2import { sqliteTable, text, integer, index } from "drizzle-orm/sqlite-core";
3
4export const user = sqliteTable("user", {
5 id: text("id").primaryKey(),
6 name: text("name").notNull(),
7 email: text("email").notNull().unique(),
8 emailVerified: integer("email_verified", { mode: "boolean" })
9 .default(false)
10 .notNull(),
11 image: text("image"),
12 createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
13 updatedAt: integer("updated_at", { mode: "timestamp_ms" })
14 .$onUpdate(() => new Date())
15 .notNull(),
16});
17
18export const session = sqliteTable(
19 "session",
20 {
21 id: text("id").primaryKey(),
22 expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
23 token: text("token").notNull().unique(),
24 createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
25 updatedAt: integer("updated_at", { mode: "timestamp_ms" })
26 .$onUpdate(() => new Date())
27 .notNull(),
28 ipAddress: text("ip_address"),
29 userAgent: text("user_agent"),
30 userId: text("user_id")
31 .notNull()
32 .references(() => user.id, { onDelete: "cascade" }),
33 },
34 (table) => [index("session_userId_idx").on(table.userId)],
35);
36
37export const account = sqliteTable(
38 "account",
39 {
40 id: text("id").primaryKey(),
41 accountId: text("account_id").notNull(),
42 providerId: text("provider_id").notNull(),
43 userId: text("user_id")
44 .notNull()
45 .references(() => user.id, { onDelete: "cascade" }),
46 accessToken: text("access_token"),
47 refreshToken: text("refresh_token"),
48 idToken: text("id_token"),
49 accessTokenExpiresAt: integer("access_token_expires_at", {
50 mode: "timestamp_ms",
51 }),
52 refreshTokenExpiresAt: integer("refresh_token_expires_at", {
53 mode: "timestamp_ms",
54 }),
55 scope: text("scope"),
56 password: text("password"),
57 createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
58 updatedAt: integer("updated_at", { mode: "timestamp_ms" })
59 .$onUpdate(() => new Date())
60 .notNull(),
61 },
62 (table) => [index("account_userId_idx").on(table.userId)],
63);
64
65export const verification = sqliteTable(
66 "verification",
67 {
68 id: text("id").primaryKey(),
69 identifier: text("identifier").notNull(),
70 value: text("value").notNull(),
71 expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
72 createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
73 updatedAt: integer("updated_at", { mode: "timestamp_ms" })
74 .$onUpdate(() => new Date())
75 .notNull(),
76 },
77 (table) => [index("verification_identifier_idx").on(table.identifier)],
78);
79
80export const authRelations = defineRelationsPart(
81 { user, session, account, verification },
82 (r) => ({
83 user: {
84 sessions: r.many.session({
85 from: r.user.id,
86 to: r.session.userId,
87 }),
88 accounts: r.many.account({
89 from: r.user.id,
90 to: r.account.userId,
91 }),
92 },
93 session: {
94 user: r.one.user({
95 from: r.session.userId,
96 to: r.user.id,
97 }),
98 },
99 account: {
100 user: r.one.user({
101 from: r.account.userId,
102 to: r.user.id,
103 }),
104 },
105 }),
106);