Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ cp .env.example .env
docker compose up
bun dev
```

After running `bunx drizzle-kit generate --config=drizzle-do.config.ts`, you need to add `?raw` to SQL file names in drizzle-do/migraions.js`.
6 changes: 2 additions & 4 deletions app/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { anonymous } from "better-auth/plugins";
import * as schema from "../lib/db/schema";
import { getDB } from "./db";

export function getAuth(env?: Env) {
export function getAuth(env: Env) {
const auth = betterAuth({
database: drizzleAdapter(getDB(env), {
provider: "pg",
provider: "sqlite",
schema: schema,
}),
emailAndPassword: {
Expand All @@ -28,5 +28,3 @@ export function getAuth(env?: Env) {
});
return auth;
}
// This is for @better-auth/cli
// export const auth = getAuth();
15 changes: 3 additions & 12 deletions app/lib/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { drizzle as drizzleNeon } from "drizzle-orm/neon-http";
import { drizzle as drizzlePg } from "drizzle-orm/node-postgres";
import { drizzle } from "drizzle-orm/d1";

export function getDB(env?: Env) {
// better-auth/cli を実行するとき
if (!env) {
const db = drizzlePg(process.env.DATABASE_URL);
return db;
}
const db =
env.NODE_ENV === "development"
? drizzlePg(env.DATABASE_URL)
: drizzleNeon(env.DATABASE_URL);
export function getDB(env: Env) {
const db = drizzle(env.DB);
return db;
}
26 changes: 26 additions & 0 deletions app/lib/db/schema-do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { customType, int, sqliteTable } from "drizzle-orm/sqlite-core";
import type { Hai } from "../hai/utils";
import { haiArray } from "./schema";

export const hai = customType<{ data: Hai; driverData: string }>({
dataType() {
return "text";
},
toDriver(value: Hai) {
return JSON.stringify(value);
},
fromDriver(value: string) {
return JSON.parse(value) as Hai;
},
});

export const gameState = sqliteTable("game_state", {
// 1行しか使わないので、IDを固定
id: int("id").primaryKey().default(1),
kyoku: int("kyoku").notNull(),
junme: int("junme").notNull(),
haiyama: haiArray("haiyama").notNull(),
sutehai: haiArray("sutehai").notNull(),
tehai: haiArray("tehai").notNull(),
tsumohai: hai("tsumohai"),
});
120 changes: 66 additions & 54 deletions app/lib/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import { sql } from "drizzle-orm";
import {
boolean,
check,
customType,
index,
integer,
pgEnum,
pgTable,
primaryKey,
serial,
sqliteTable,
text,
timestamp,
} from "drizzle-orm/pg-core";
} from "drizzle-orm/sqlite-core";
import type { Hai } from "../hai/utils";

export const haiyama = pgTable("haiyama", {
id: text("id").primaryKey(),
export const haiArray = customType<{ data: Hai[]; driverData: string }>({
dataType() {
return "text";
},
toDriver(value: Hai[]) {
return JSON.stringify(value);
},
fromDriver(value: string) {
return JSON.parse(value) as Hai[];
},
});

export const haiKindEnum = pgEnum("hai_kind", [
"manzu",
"pinzu",
"souzu",
"jihai",
]);

export const hai = pgTable("hai", {
id: serial("id").primaryKey(),
haiyamaId: text("haiyama_id")
.notNull()
.references(() => haiyama.id, { onDelete: "cascade" }),
kind: haiKindEnum("kind").notNull(), // "manzu" | "pinzu" | "souzu" | "jihai"
value: text("value").notNull(), // 1~9 or "ton" ~ "tyun"
order: integer("order").notNull(), // 0~17
index: integer("index").notNull(), // haiToIndex
export const haiyama = sqliteTable("haiyama", {
id: text("id")
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
// D1だと1クエリあたり100パラメータまでなので、あえて正規化していない
tiles: haiArray("tiles").notNull(),
});

// relation between user and haiyama
export const kyoku = pgTable(
export const kyoku = sqliteTable(
"kyoku",
{
userId: text("user_id")
Expand All @@ -44,7 +40,7 @@ export const kyoku = pgTable(
haiyamaId: text("haiyama_id")
.notNull()
.references(() => haiyama.id, { onDelete: "cascade" }),
didAgari: boolean("did_agari").notNull(),
didAgari: integer("did_agari", { mode: "boolean" }).notNull(),
agariJunme: integer("agari_junme"),
},
(table) => [
Expand All @@ -59,36 +55,43 @@ export const kyoku = pgTable(
);

// better-auth
export const user = pgTable("user", {
export const user = sqliteTable("user", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").default(false).notNull(),
image: text("image"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
emailVerified: integer("email_verified", { mode: "boolean" })
.default(false)
.notNull(),
isAnonymous: boolean("is_anonymous"),
image: text("image"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date())
.$onUpdate(() => new Date()),
isAnonymous: integer("is_anonymous", { mode: "boolean" }),
});

export const session = pgTable("session", {
export const session = sqliteTable("session", {
id: text("id").primaryKey(),
expiresAt: timestamp("expires_at").notNull(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
token: text("token").notNull().unique(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date())
.$onUpdate(() => new Date()),
ipAddress: text("ip_address"),
userAgent: text("user_agent"),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
});

export const account = pgTable("account", {
export const account = sqliteTable("account", {
id: text("id").primaryKey(),
accountId: text("account_id").notNull(),
providerId: text("provider_id").notNull(),
Expand All @@ -98,24 +101,33 @@ export const account = pgTable("account", {
accessToken: text("access_token"),
refreshToken: text("refresh_token"),
idToken: text("id_token"),
accessTokenExpiresAt: timestamp("access_token_expires_at"),
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
accessTokenExpiresAt: integer("access_token_expires_at", {
mode: "timestamp",
}),
refreshTokenExpiresAt: integer("refresh_token_expires_at", {
mode: "timestamp",
}),
scope: text("scope"),
password: text("password"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date())
.$onUpdate(() => new Date()),
});

export const verification = pgTable("verification", {
export const verification = sqliteTable("verification", {
id: text("id").primaryKey(),
identifier: text("identifier").notNull(),
value: text("value").notNull(),
expiresAt: timestamp("expires_at").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date())
.$onUpdate(() => new Date()),
});
57 changes: 0 additions & 57 deletions app/lib/db/seed.ts

This file was deleted.

16 changes: 16 additions & 0 deletions app/lib/do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Hai } from "./hai/utils";

export interface GameState {
kyoku: number;
junme: number;
haiyama: Hai[];
sutehai: Hai[];
tehai: Hai[];
tsumohai: Hai | null;
}

export default function getDOStub(env: Env, userId: string) {
const id = env.DO.idFromName(userId);
const stub = env.DO.get(id);
return stub;
}
33 changes: 0 additions & 33 deletions app/lib/hai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,3 @@ export function constructHai(kind: HaiKind, value: number | JihaiValue): Hai {
export function sortTehai(haiArray: Hai[]): Hai[] {
return haiArray.sort((a, b) => haiToIndex(a) - haiToIndex(b));
}

// To store hai in DB
export type DBHai = {
haiyamaId: string;
kind: HaiKind;
value: string;
order: number;
index: number;
};

export function haiToDBHai(hai: Hai, haiyamaId: string, order: number): DBHai {
return {
haiyamaId,
kind: hai.kind,
value: String(hai.value),
order,
index: haiToIndex(hai),
};
}

export function dbHaiToHai(dbHai: DBHai): Hai {
if (dbHai.kind === "jihai") {
return {
kind: dbHai.kind,
value: dbHai.value as JihaiValue,
};
} else {
return {
kind: dbHai.kind as SuhaiKind,
value: Number(dbHai.value),
};
}
}
Loading