1+ import { sql } from "drizzle-orm" ;
12import {
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
1863export 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
3177export const session = pgTable ( "session" , {
0 commit comments