Skip to content

Commit 0d721e0

Browse files
authored
スキーマを定義 (#103)
* add anonymous plugin * anonymous user to new user though this may not be used... * define schema * format
1 parent f7c4cb3 commit 0d721e0

File tree

7 files changed

+92
-10
lines changed

7 files changed

+92
-10
lines changed

lefthook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ pre-commit:
22
commands:
33
check:
44
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
5-
run: bunx biome format --write {staged_files}
5+
run: bunx @biomejs/biome check --write {staged_files} && git add {staged_files}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build": "bun vite build",
1212
"lint": "eslint .",
1313
"preview": "bun vite preview",
14-
"format": "bunx biome check . --write"
14+
"format": "bunx @biomejs/biome check . --write"
1515
},
1616
"dependencies": {},
1717
"devDependencies": {

rr/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
NODE_ENV=development
22
DATABASE_URL=postgres://user:postgres@localhost:5432/
33
BETTER_AUTH_SECRET=
4-
BETTER_AUTH_URL=http://localhost:3000
4+
BETTER_AUTH_URL=http://localhost:5173

rr/app/lib/auth-client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
import { anonymousClient } from "better-auth/client/plugins";
12
import { createAuthClient } from "better-auth/react";
2-
export const authClient = createAuthClient({});
3+
export const authClient = createAuthClient({
4+
plugins: [anonymousClient()],
5+
});

rr/app/lib/auth.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { betterAuth } from "better-auth";
22
import { drizzleAdapter } from "better-auth/adapters/drizzle";
3+
import { anonymous } from "better-auth/plugins";
4+
import { eq } from "drizzle-orm";
35
import { getDB } from "./db";
46

57
export function getAuth(env?: Env) {
@@ -10,6 +12,18 @@ export function getAuth(env?: Env) {
1012
emailAndPassword: {
1113
enabled: true,
1214
},
15+
plugins: [
16+
anonymous({
17+
onLinkAccount: async ({ anonymousUser, newUser }) => {
18+
const db = getDB(env);
19+
// migrate like this
20+
// await db
21+
// .update(playHistory)
22+
// .set({ userId: newUser.user.id })
23+
// .where(eq(playHistory.userId, anonymousUser.user.id));
24+
},
25+
}),
26+
],
1327
});
1428
return auth;
1529
}

rr/app/lib/db/schema.ts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
1+
import { sql } from "drizzle-orm";
12
import {
23
boolean,
4+
check,
5+
index,
36
integer,
7+
pgEnum,
48
pgTable,
9+
primaryKey,
10+
serial,
511
text,
612
timestamp,
7-
varchar,
813
} from "drizzle-orm/pg-core";
914

10-
export const usersTable = pgTable("users", {
11-
id: integer().primaryKey().generatedAlwaysAsIdentity(),
12-
name: varchar({ length: 255 }).notNull(),
13-
age: integer().notNull(),
14-
email: varchar({ length: 255 }).notNull().unique(),
15+
export const haiyama = pgTable("haiyama", {
16+
id: text("id").primaryKey(),
17+
});
18+
19+
export const haiKindEnum = pgEnum("hai_kind", [
20+
"manzu",
21+
"pinzu",
22+
"souzu",
23+
"jihai",
24+
]);
25+
26+
export const hai = pgTable("hai", {
27+
id: serial("id").primaryKey(),
28+
haiyamaId: text("haiyama_id")
29+
.notNull()
30+
.references(() => haiyama.id, { onDelete: "cascade" }),
31+
kind: haiKindEnum("kind").notNull(), // "manzu" | "pinzu" | "souzu" | "jihai"
32+
value: text("value").notNull(), // 1~9 or "ton" ~ "tyun"
33+
order: integer("order").notNull(), // 0~17
34+
index: integer("index").notNull(), // haiToIndex
1535
});
1636

37+
// relation between user and haiyama
38+
// TODO: index
39+
export const kyoku = pgTable(
40+
"kyoku",
41+
{
42+
userId: text("user_id")
43+
.notNull()
44+
.references(() => user.id, { onDelete: "cascade" }),
45+
haiyamaId: text("haiyama_id")
46+
.notNull()
47+
.references(() => haiyama.id, { onDelete: "cascade" }),
48+
didAgari: boolean("did_agari").notNull(),
49+
agariJunme: integer("agari_junme"),
50+
},
51+
(table) => [
52+
primaryKey({ columns: [table.userId, table.haiyamaId] }),
53+
index("kyoku_user_id_idx").on(table.userId),
54+
index("kyoku_haiyama_id_idx").on(table.haiyamaId),
55+
check(
56+
"agari_consistency",
57+
sql`(${table.didAgari} = false) OR (${table.didAgari} = true AND ${table.agariJunme} IS NOT NULL)`,
58+
),
59+
],
60+
);
61+
1762
// better-auth
1863
export const user = pgTable("user", {
1964
id: text("id").primaryKey(),
@@ -26,6 +71,7 @@ export const user = pgTable("user", {
2671
.defaultNow()
2772
.$onUpdate(() => /* @__PURE__ */ new Date())
2873
.notNull(),
74+
isAnonymous: boolean("is_anonymous"),
2975
});
3076

3177
export const session = pgTable("session", {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,22 @@ export function constructHai(kind: HaiKind, value: number | JihaiValue): Hai {
7878
export function sortTehai(haiArray: Hai[]): Hai[] {
7979
return haiArray.sort((a, b) => haiToIndex(a) - haiToIndex(b));
8080
}
81+
82+
// To store hai in DB
83+
export type DBHai = {
84+
haiyamaId: string;
85+
kind: HaiKind;
86+
value: string;
87+
order: number;
88+
index: number;
89+
};
90+
91+
export function haiToDBHai(hai: Hai, haiyamaId: string, order: number): DBHai {
92+
return {
93+
haiyamaId,
94+
kind: hai.kind,
95+
value: String(hai.value),
96+
order,
97+
index: haiToIndex(hai),
98+
};
99+
}

0 commit comments

Comments
 (0)