Skip to content

Commit 49b48ab

Browse files
authored
Merge pull request #142 from ut-code/fix-better-auth-1.4
better-authのjoinを有効化した後schemaを再生成するのを忘れていた
2 parents 957d470 + bf28685 commit 49b48ab

File tree

4 files changed

+560
-46
lines changed

4 files changed

+560
-46
lines changed

app/schema/auth.ts

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { pgTable, text, timestamp, boolean } from "drizzle-orm/pg-core";
1+
import { relations } from "drizzle-orm";
2+
import { pgTable, text, timestamp, boolean, index } from "drizzle-orm/pg-core";
23

34
export const user = pgTable("user", {
45
id: text("id").primaryKey(),
@@ -11,52 +12,83 @@ export const user = pgTable("user", {
1112
.defaultNow()
1213
.$onUpdate(() => /* @__PURE__ */ new Date())
1314
.notNull(),
14-
isAnonymous: boolean("is_anonymous"),
15+
isAnonymous: boolean("is_anonymous").default(false),
1516
});
1617

17-
export const session = pgTable("session", {
18-
id: text("id").primaryKey(),
19-
expiresAt: timestamp("expires_at").notNull(),
20-
token: text("token").notNull().unique(),
21-
createdAt: timestamp("created_at").defaultNow().notNull(),
22-
updatedAt: timestamp("updated_at")
23-
.$onUpdate(() => /* @__PURE__ */ new Date())
24-
.notNull(),
25-
ipAddress: text("ip_address"),
26-
userAgent: text("user_agent"),
27-
userId: text("user_id")
28-
.notNull()
29-
.references(() => user.id, { onDelete: "cascade" }),
30-
});
18+
export const session = pgTable(
19+
"session",
20+
{
21+
id: text("id").primaryKey(),
22+
expiresAt: timestamp("expires_at").notNull(),
23+
token: text("token").notNull().unique(),
24+
createdAt: timestamp("created_at").defaultNow().notNull(),
25+
updatedAt: timestamp("updated_at")
26+
.$onUpdate(() => /* @__PURE__ */ 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+
);
3136

32-
export const account = pgTable("account", {
33-
id: text("id").primaryKey(),
34-
accountId: text("account_id").notNull(),
35-
providerId: text("provider_id").notNull(),
36-
userId: text("user_id")
37-
.notNull()
38-
.references(() => user.id, { onDelete: "cascade" }),
39-
accessToken: text("access_token"),
40-
refreshToken: text("refresh_token"),
41-
idToken: text("id_token"),
42-
accessTokenExpiresAt: timestamp("access_token_expires_at"),
43-
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
44-
scope: text("scope"),
45-
password: text("password"),
46-
createdAt: timestamp("created_at").defaultNow().notNull(),
47-
updatedAt: timestamp("updated_at")
48-
.$onUpdate(() => /* @__PURE__ */ new Date())
49-
.notNull(),
50-
});
37+
export const account = pgTable(
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: timestamp("access_token_expires_at"),
50+
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
51+
scope: text("scope"),
52+
password: text("password"),
53+
createdAt: timestamp("created_at").defaultNow().notNull(),
54+
updatedAt: timestamp("updated_at")
55+
.$onUpdate(() => /* @__PURE__ */ new Date())
56+
.notNull(),
57+
},
58+
(table) => [index("account_userId_idx").on(table.userId)],
59+
);
5160

52-
export const verification = pgTable("verification", {
53-
id: text("id").primaryKey(),
54-
identifier: text("identifier").notNull(),
55-
value: text("value").notNull(),
56-
expiresAt: timestamp("expires_at").notNull(),
57-
createdAt: timestamp("created_at").defaultNow().notNull(),
58-
updatedAt: timestamp("updated_at")
59-
.defaultNow()
60-
.$onUpdate(() => /* @__PURE__ */ new Date())
61-
.notNull(),
62-
});
61+
export const verification = pgTable(
62+
"verification",
63+
{
64+
id: text("id").primaryKey(),
65+
identifier: text("identifier").notNull(),
66+
value: text("value").notNull(),
67+
expiresAt: timestamp("expires_at").notNull(),
68+
createdAt: timestamp("created_at").defaultNow().notNull(),
69+
updatedAt: timestamp("updated_at")
70+
.defaultNow()
71+
.$onUpdate(() => /* @__PURE__ */ new Date())
72+
.notNull(),
73+
},
74+
(table) => [index("verification_identifier_idx").on(table.identifier)],
75+
);
76+
77+
export const userRelations = relations(user, ({ many }) => ({
78+
sessions: many(session),
79+
accounts: many(account),
80+
}));
81+
82+
export const sessionRelations = relations(session, ({ one }) => ({
83+
user: one(user, {
84+
fields: [session.userId],
85+
references: [user.id],
86+
}),
87+
}));
88+
89+
export const accountRelations = relations(account, ({ one }) => ({
90+
user: one(user, {
91+
fields: [account.userId],
92+
references: [user.id],
93+
}),
94+
}));

drizzle/0002_sad_mindworm.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE "user" ALTER COLUMN "is_anonymous" SET DEFAULT false;--> statement-breakpoint
2+
CREATE INDEX "account_userId_idx" ON "account" USING btree ("user_id");--> statement-breakpoint
3+
CREATE INDEX "session_userId_idx" ON "session" USING btree ("user_id");--> statement-breakpoint
4+
CREATE INDEX "verification_identifier_idx" ON "verification" USING btree ("identifier");

0 commit comments

Comments
 (0)