-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
101 lines (92 loc) · 3.01 KB
/
schema.ts
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
import type { AdapterAccount } from "next-auth/adapters";
import Database from "better-sqlite3";
import { sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import {
integer,
primaryKey,
sqliteTable,
text,
uniqueIndex,
} from "drizzle-orm/sqlite-core";
const sqlite = new Database("./sqlite.db");
export const db = drizzle(sqlite);
export const users = sqliteTable("user", {
email: text("email").notNull(),
emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
id: text("id").notNull().primaryKey(),
image: text("image"),
name: text("name"),
});
export const accounts = sqliteTable(
"account",
{
access_token: text("access_token"),
expires_at: integer("expires_at"),
id_token: text("id_token"),
provider: text("provider").notNull(),
providerAccountId: text("providerAccountId").notNull(),
refresh_token: text("refresh_token"),
scope: text("scope"),
session_state: text("session_state"),
token_type: text("token_type"),
type: text("type").$type<AdapterAccount["type"]>().notNull(),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
},
(account) => ({
compoundKey: primaryKey(account.provider, account.providerAccountId),
}),
);
export const sessions = sqliteTable("session", {
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
sessionToken: text("sessionToken").notNull().primaryKey(),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
});
export const verificationTokens = sqliteTable(
"verificationToken",
{
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
identifier: text("identifier").notNull(),
token: text("token").notNull(),
},
(vt) => ({
compoundKey: primaryKey(vt.identifier, vt.token),
}),
);
export const reserveDateTimes = sqliteTable(
"reserveDateTime",
{
created_at: integer("created_at").default(
sql`(strftime('%s', 'now', 'localtime'))`,
),
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
reserved_at: integer("reserved_at", { mode: "timestamp_ms" }).notNull(),
updated_at: integer("updated_at").default(
sql`(strftime('%s', 'now', 'localtime'))`,
),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
},
(table) => ({
idx1: uniqueIndex("reserveDateTime_idx_1").on(table.reserved_at),
// idx2: uniqueIndex("reserveDateTime_idx_2").on(table.userId),
}),
);
export const reserveUserDetails = sqliteTable("reserveUserDetails", {
created_at: integer("created_at").default(
sql`(strftime('%s', 'now', 'localtime'))`,
),
id: text("id").notNull().primaryKey(),
realName: text("realName").notNull(),
tel: text("tel").notNull(),
updated_at: integer("updated_at").default(
sql`(strftime('%s', 'now', 'localtime'))`,
),
});
migrate(db, { migrationsFolder: "./drizzle" });