/6247b647461a3fd134
Created 2 weeks ago...
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
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<typeof sessions>;
export type UserInsertType = InferInsertModel<typeof users>;
export type UserSelectType = InferSelectModel<typeof users>;
export type AccountSelectType = InferSelectModel<typeof accounts>;