11import { sql } from "drizzle-orm" ;
22import {
3- boolean ,
43 check ,
4+ customType ,
55 index ,
66 integer ,
7- pgEnum ,
8- pgTable ,
97 primaryKey ,
10- serial ,
8+ sqliteTable ,
119 text ,
12- timestamp ,
13- } from "drizzle-orm/pg-core " ;
10+ } from "drizzle-orm/sqlite-core" ;
11+ import type { Hai } from "../hai/utils " ;
1412
15- export const haiyama = pgTable ( "haiyama" , {
16- id : text ( "id" ) . primaryKey ( ) ,
13+ export const haiArray = customType < { data : Hai [ ] ; driverData : string } > ( {
14+ dataType ( ) {
15+ return "text" ;
16+ } ,
17+ toDriver ( value : Hai [ ] ) {
18+ return JSON . stringify ( value ) ;
19+ } ,
20+ fromDriver ( value : string ) {
21+ return JSON . parse ( value ) as Hai [ ] ;
22+ } ,
1723} ) ;
1824
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
25+ export const haiyama = sqliteTable ( "haiyama" , {
26+ id : text ( "id" )
27+ . primaryKey ( )
28+ . $defaultFn ( ( ) => crypto . randomUUID ( ) ) ,
29+ // D1だと1クエリあたり100パラメータまでなので、あえて正規化していない
30+ tiles : haiArray ( "tiles" ) . notNull ( ) ,
3531} ) ;
3632
3733// relation between user and haiyama
38- export const kyoku = pgTable (
34+ export const kyoku = sqliteTable (
3935 "kyoku" ,
4036 {
4137 userId : text ( "user_id" )
@@ -44,7 +40,7 @@ export const kyoku = pgTable(
4440 haiyamaId : text ( "haiyama_id" )
4541 . notNull ( )
4642 . references ( ( ) => haiyama . id , { onDelete : "cascade" } ) ,
47- didAgari : boolean ( "did_agari" ) . notNull ( ) ,
43+ didAgari : integer ( "did_agari" , { mode : "boolean" } ) . notNull ( ) ,
4844 agariJunme : integer ( "agari_junme" ) ,
4945 } ,
5046 ( table ) => [
@@ -59,36 +55,43 @@ export const kyoku = pgTable(
5955) ;
6056
6157// better-auth
62- export const user = pgTable ( "user" , {
58+ export const user = sqliteTable ( "user" , {
6359 id : text ( "id" ) . primaryKey ( ) ,
6460 name : text ( "name" ) . notNull ( ) ,
6561 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 ( ) )
62+ emailVerified : integer ( "email_verified" , { mode : "boolean" } )
63+ . default ( false )
7264 . notNull ( ) ,
73- isAnonymous : boolean ( "is_anonymous" ) ,
65+ image : text ( "image" ) ,
66+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
67+ . notNull ( )
68+ . $defaultFn ( ( ) => new Date ( ) ) ,
69+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
70+ . notNull ( )
71+ . $defaultFn ( ( ) => new Date ( ) )
72+ . $onUpdate ( ( ) => new Date ( ) ) ,
73+ isAnonymous : integer ( "is_anonymous" , { mode : "boolean" } ) ,
7474} ) ;
7575
76- export const session = pgTable ( "session" , {
76+ export const session = sqliteTable ( "session" , {
7777 id : text ( "id" ) . primaryKey ( ) ,
78- expiresAt : timestamp ( "expires_at" ) . notNull ( ) ,
78+ expiresAt : integer ( "expires_at" , { mode : "timestamp" } ) . notNull ( ) ,
7979 token : text ( "token" ) . notNull ( ) . unique ( ) ,
80- createdAt : timestamp ( "created_at" ) . defaultNow ( ) . notNull ( ) ,
81- updatedAt : timestamp ( "updated_at" )
82- . $onUpdate ( ( ) => /* @__PURE__ */ new Date ( ) )
83- . notNull ( ) ,
80+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
81+ . notNull ( )
82+ . $defaultFn ( ( ) => new Date ( ) ) ,
83+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
84+ . notNull ( )
85+ . $defaultFn ( ( ) => new Date ( ) )
86+ . $onUpdate ( ( ) => new Date ( ) ) ,
8487 ipAddress : text ( "ip_address" ) ,
8588 userAgent : text ( "user_agent" ) ,
8689 userId : text ( "user_id" )
8790 . notNull ( )
8891 . references ( ( ) => user . id , { onDelete : "cascade" } ) ,
8992} ) ;
9093
91- export const account = pgTable ( "account" , {
94+ export const account = sqliteTable ( "account" , {
9295 id : text ( "id" ) . primaryKey ( ) ,
9396 accountId : text ( "account_id" ) . notNull ( ) ,
9497 providerId : text ( "provider_id" ) . notNull ( ) ,
@@ -98,24 +101,33 @@ export const account = pgTable("account", {
98101 accessToken : text ( "access_token" ) ,
99102 refreshToken : text ( "refresh_token" ) ,
100103 idToken : text ( "id_token" ) ,
101- accessTokenExpiresAt : timestamp ( "access_token_expires_at" ) ,
102- refreshTokenExpiresAt : timestamp ( "refresh_token_expires_at" ) ,
104+ accessTokenExpiresAt : integer ( "access_token_expires_at" , {
105+ mode : "timestamp" ,
106+ } ) ,
107+ refreshTokenExpiresAt : integer ( "refresh_token_expires_at" , {
108+ mode : "timestamp" ,
109+ } ) ,
103110 scope : text ( "scope" ) ,
104111 password : text ( "password" ) ,
105- createdAt : timestamp ( "created_at" ) . defaultNow ( ) . notNull ( ) ,
106- updatedAt : timestamp ( "updated_at" )
107- . $onUpdate ( ( ) => /* @__PURE__ */ new Date ( ) )
108- . notNull ( ) ,
112+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
113+ . notNull ( )
114+ . $defaultFn ( ( ) => new Date ( ) ) ,
115+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
116+ . notNull ( )
117+ . $defaultFn ( ( ) => new Date ( ) )
118+ . $onUpdate ( ( ) => new Date ( ) ) ,
109119} ) ;
110120
111- export const verification = pgTable ( "verification" , {
121+ export const verification = sqliteTable ( "verification" , {
112122 id : text ( "id" ) . primaryKey ( ) ,
113123 identifier : text ( "identifier" ) . notNull ( ) ,
114124 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 ( ) ,
125+ expiresAt : integer ( "expires_at" , { mode : "timestamp" } ) . notNull ( ) ,
126+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
127+ . notNull ( )
128+ . $defaultFn ( ( ) => new Date ( ) ) ,
129+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
130+ . notNull ( )
131+ . $defaultFn ( ( ) => new Date ( ) )
132+ . $onUpdate ( ( ) => new Date ( ) ) ,
121133} ) ;
0 commit comments