|
1 | 1 | import { sql } from "drizzle-orm"; |
2 | 2 | import { |
3 | | - boolean, |
4 | | - check, |
5 | | - index, |
6 | | - integer, |
7 | | - pgEnum, |
8 | | - pgTable, |
9 | | - primaryKey, |
10 | | - serial, |
11 | | - text, |
12 | | - timestamp, |
| 3 | + boolean, |
| 4 | + check, |
| 5 | + index, |
| 6 | + integer, |
| 7 | + pgEnum, |
| 8 | + pgTable, |
| 9 | + primaryKey, |
| 10 | + serial, |
| 11 | + text, |
| 12 | + timestamp, |
13 | 13 | } from "drizzle-orm/pg-core"; |
14 | 14 |
|
15 | 15 | export const haiyama = pgTable("haiyama", { |
16 | | - id: text("id").primaryKey(), |
| 16 | + id: text("id").primaryKey(), |
17 | 17 | }); |
18 | 18 |
|
19 | 19 | export const haiKindEnum = pgEnum("hai_kind", [ |
20 | | - "manzu", |
21 | | - "pinzu", |
22 | | - "souzu", |
23 | | - "jihai", |
| 20 | + "manzu", |
| 21 | + "pinzu", |
| 22 | + "souzu", |
| 23 | + "jihai", |
24 | 24 | ]); |
25 | 25 |
|
26 | 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 |
| 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 |
35 | 35 | }); |
36 | 36 |
|
37 | 37 | // relation between user and haiyama |
38 | 38 | export const kyoku = pgTable( |
39 | | - "kyoku", |
40 | | - { |
41 | | - userId: text("user_id") |
42 | | - .notNull() |
43 | | - .references(() => user.id, { onDelete: "cascade" }), |
44 | | - haiyamaId: text("haiyama_id") |
45 | | - .notNull() |
46 | | - .references(() => haiyama.id, { onDelete: "cascade" }), |
47 | | - didAgari: boolean("did_agari").notNull(), |
48 | | - agariJunme: integer("agari_junme"), |
49 | | - }, |
50 | | - (table) => [ |
51 | | - primaryKey({ columns: [table.userId, table.haiyamaId] }), |
52 | | - index("kyoku_user_id_idx").on(table.userId), |
53 | | - index("kyoku_haiyama_id_idx").on(table.haiyamaId), |
54 | | - check( |
55 | | - "agari_consistency", |
56 | | - sql`(${table.didAgari} = false) OR (${table.didAgari} = true AND ${table.agariJunme} IS NOT NULL)`, |
57 | | - ), |
58 | | - ], |
| 39 | + "kyoku", |
| 40 | + { |
| 41 | + userId: text("user_id") |
| 42 | + .notNull() |
| 43 | + .references(() => user.id, { onDelete: "cascade" }), |
| 44 | + haiyamaId: text("haiyama_id") |
| 45 | + .notNull() |
| 46 | + .references(() => haiyama.id, { onDelete: "cascade" }), |
| 47 | + didAgari: boolean("did_agari").notNull(), |
| 48 | + agariJunme: integer("agari_junme"), |
| 49 | + }, |
| 50 | + (table) => [ |
| 51 | + primaryKey({ columns: [table.userId, table.haiyamaId] }), |
| 52 | + index("kyoku_user_id_idx").on(table.userId), |
| 53 | + index("kyoku_haiyama_id_idx").on(table.haiyamaId), |
| 54 | + check( |
| 55 | + "agari_consistency", |
| 56 | + sql`(${table.didAgari} = false) OR (${table.didAgari} = true AND ${table.agariJunme} IS NOT NULL)`, |
| 57 | + ), |
| 58 | + ], |
59 | 59 | ); |
60 | 60 |
|
61 | 61 | // better-auth |
62 | 62 | export const user = pgTable("user", { |
63 | | - id: text("id").primaryKey(), |
64 | | - name: text("name").notNull(), |
65 | | - email: text("email").notNull().unique(), |
66 | | - emailVerified: boolean("email_verified").default(false).notNull(), |
67 | | - image: text("image"), |
68 | | - createdAt: timestamp("created_at").defaultNow().notNull(), |
69 | | - updatedAt: timestamp("updated_at") |
70 | | - .defaultNow() |
71 | | - .$onUpdate(() => /* @__PURE__ */ new Date()) |
72 | | - .notNull(), |
73 | | - isAnonymous: boolean("is_anonymous"), |
| 63 | + id: text("id").primaryKey(), |
| 64 | + name: text("name").notNull(), |
| 65 | + email: text("email").notNull().unique(), |
| 66 | + emailVerified: boolean("email_verified").default(false).notNull(), |
| 67 | + image: text("image"), |
| 68 | + createdAt: timestamp("created_at").defaultNow().notNull(), |
| 69 | + updatedAt: timestamp("updated_at") |
| 70 | + .defaultNow() |
| 71 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 72 | + .notNull(), |
| 73 | + isAnonymous: boolean("is_anonymous"), |
74 | 74 | }); |
75 | 75 |
|
76 | 76 | export const session = pgTable("session", { |
77 | | - id: text("id").primaryKey(), |
78 | | - expiresAt: timestamp("expires_at").notNull(), |
79 | | - token: text("token").notNull().unique(), |
80 | | - createdAt: timestamp("created_at").defaultNow().notNull(), |
81 | | - updatedAt: timestamp("updated_at") |
82 | | - .$onUpdate(() => /* @__PURE__ */ new Date()) |
83 | | - .notNull(), |
84 | | - ipAddress: text("ip_address"), |
85 | | - userAgent: text("user_agent"), |
86 | | - userId: text("user_id") |
87 | | - .notNull() |
88 | | - .references(() => user.id, { onDelete: "cascade" }), |
| 77 | + id: text("id").primaryKey(), |
| 78 | + expiresAt: timestamp("expires_at").notNull(), |
| 79 | + token: text("token").notNull().unique(), |
| 80 | + createdAt: timestamp("created_at").defaultNow().notNull(), |
| 81 | + updatedAt: timestamp("updated_at") |
| 82 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 83 | + .notNull(), |
| 84 | + ipAddress: text("ip_address"), |
| 85 | + userAgent: text("user_agent"), |
| 86 | + userId: text("user_id") |
| 87 | + .notNull() |
| 88 | + .references(() => user.id, { onDelete: "cascade" }), |
89 | 89 | }); |
90 | 90 |
|
91 | 91 | export const account = pgTable("account", { |
92 | | - id: text("id").primaryKey(), |
93 | | - accountId: text("account_id").notNull(), |
94 | | - providerId: text("provider_id").notNull(), |
95 | | - userId: text("user_id") |
96 | | - .notNull() |
97 | | - .references(() => user.id, { onDelete: "cascade" }), |
98 | | - accessToken: text("access_token"), |
99 | | - refreshToken: text("refresh_token"), |
100 | | - idToken: text("id_token"), |
101 | | - accessTokenExpiresAt: timestamp("access_token_expires_at"), |
102 | | - refreshTokenExpiresAt: timestamp("refresh_token_expires_at"), |
103 | | - scope: text("scope"), |
104 | | - password: text("password"), |
105 | | - createdAt: timestamp("created_at").defaultNow().notNull(), |
106 | | - updatedAt: timestamp("updated_at") |
107 | | - .$onUpdate(() => /* @__PURE__ */ new Date()) |
108 | | - .notNull(), |
| 92 | + id: text("id").primaryKey(), |
| 93 | + accountId: text("account_id").notNull(), |
| 94 | + providerId: text("provider_id").notNull(), |
| 95 | + userId: text("user_id") |
| 96 | + .notNull() |
| 97 | + .references(() => user.id, { onDelete: "cascade" }), |
| 98 | + accessToken: text("access_token"), |
| 99 | + refreshToken: text("refresh_token"), |
| 100 | + idToken: text("id_token"), |
| 101 | + accessTokenExpiresAt: timestamp("access_token_expires_at"), |
| 102 | + refreshTokenExpiresAt: timestamp("refresh_token_expires_at"), |
| 103 | + scope: text("scope"), |
| 104 | + password: text("password"), |
| 105 | + createdAt: timestamp("created_at").defaultNow().notNull(), |
| 106 | + updatedAt: timestamp("updated_at") |
| 107 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 108 | + .notNull(), |
109 | 109 | }); |
110 | 110 |
|
111 | 111 | export const verification = pgTable("verification", { |
112 | | - id: text("id").primaryKey(), |
113 | | - identifier: text("identifier").notNull(), |
114 | | - value: text("value").notNull(), |
115 | | - expiresAt: timestamp("expires_at").notNull(), |
116 | | - createdAt: timestamp("created_at").defaultNow().notNull(), |
117 | | - updatedAt: timestamp("updated_at") |
118 | | - .defaultNow() |
119 | | - .$onUpdate(() => /* @__PURE__ */ new Date()) |
120 | | - .notNull(), |
| 112 | + id: text("id").primaryKey(), |
| 113 | + identifier: text("identifier").notNull(), |
| 114 | + value: text("value").notNull(), |
| 115 | + expiresAt: timestamp("expires_at").notNull(), |
| 116 | + createdAt: timestamp("created_at").defaultNow().notNull(), |
| 117 | + updatedAt: timestamp("updated_at") |
| 118 | + .defaultNow() |
| 119 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 120 | + .notNull(), |
121 | 121 | }); |
0 commit comments