1+ import type { string } from "better-auth" ;
12import { sql } from "drizzle-orm" ;
23import {
3- boolean ,
44 check ,
5+ customType ,
56 index ,
67 integer ,
7- pgEnum ,
8- pgTable ,
98 primaryKey ,
10- serial ,
9+ sqliteTable ,
1110 text ,
12- timestamp ,
13- } from "drizzle-orm/pg-core " ;
11+ } from "drizzle-orm/sqlite-core" ;
12+ import type { Hai } from "../hai/utils " ;
1413
15- export const haiyama = pgTable ( "haiyama" , {
16- id : text ( "id" ) . primaryKey ( ) ,
14+ const haiArray = customType < { data : Hai [ ] ; driverData : string } > ( {
15+ dataType ( ) {
16+ return "text" ;
17+ } ,
18+ toDriver ( value : Hai [ ] ) {
19+ return JSON . stringify ( value ) ;
20+ } ,
21+ fromDriver ( value : string ) {
22+ return JSON . parse ( value ) as Hai [ ] ;
23+ } ,
1724} ) ;
1825
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
26+ export const haiyama = sqliteTable ( "haiyama" , {
27+ id : text ( "id" )
28+ . primaryKey ( )
29+ . $defaultFn ( ( ) => crypto . randomUUID ( ) ) ,
30+ // D1だと1クエリあたり100パラメータまでなので、あえて正規化していない
31+ tiles : haiArray ( "tiles" ) . notNull ( ) ,
3532} ) ;
3633
3734// relation between user and haiyama
38- export const kyoku = pgTable (
35+ export const kyoku = sqliteTable (
3936 "kyoku" ,
4037 {
4138 userId : text ( "user_id" )
@@ -44,7 +41,7 @@ export const kyoku = pgTable(
4441 haiyamaId : text ( "haiyama_id" )
4542 . notNull ( )
4643 . references ( ( ) => haiyama . id , { onDelete : "cascade" } ) ,
47- didAgari : boolean ( "did_agari" ) . notNull ( ) ,
44+ didAgari : integer ( "did_agari" , { mode : "boolean" } ) . notNull ( ) ,
4845 agariJunme : integer ( "agari_junme" ) ,
4946 } ,
5047 ( table ) => [
@@ -59,36 +56,43 @@ export const kyoku = pgTable(
5956) ;
6057
6158// better-auth
62- export const user = pgTable ( "user" , {
59+ export const user = sqliteTable ( "user" , {
6360 id : text ( "id" ) . primaryKey ( ) ,
6461 name : text ( "name" ) . notNull ( ) ,
6562 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 ( ) )
63+ emailVerified : integer ( "email_verified" , { mode : "boolean" } )
64+ . default ( false )
7265 . notNull ( ) ,
73- isAnonymous : boolean ( "is_anonymous" ) ,
66+ image : text ( "image" ) ,
67+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
68+ . notNull ( )
69+ . $defaultFn ( ( ) => new Date ( ) ) ,
70+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
71+ . notNull ( )
72+ . $defaultFn ( ( ) => new Date ( ) )
73+ . $onUpdate ( ( ) => new Date ( ) ) ,
74+ isAnonymous : integer ( "is_anonymous" , { mode : "boolean" } ) ,
7475} ) ;
7576
76- export const session = pgTable ( "session" , {
77+ export const session = sqliteTable ( "session" , {
7778 id : text ( "id" ) . primaryKey ( ) ,
78- expiresAt : timestamp ( "expires_at" ) . notNull ( ) ,
79+ expiresAt : integer ( "expires_at" , { mode : "timestamp" } ) . notNull ( ) ,
7980 token : text ( "token" ) . notNull ( ) . unique ( ) ,
80- createdAt : timestamp ( "created_at" ) . defaultNow ( ) . notNull ( ) ,
81- updatedAt : timestamp ( "updated_at" )
82- . $onUpdate ( ( ) => /* @__PURE__ */ new Date ( ) )
83- . notNull ( ) ,
81+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
82+ . notNull ( )
83+ . $defaultFn ( ( ) => new Date ( ) ) ,
84+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
85+ . notNull ( )
86+ . $defaultFn ( ( ) => new Date ( ) )
87+ . $onUpdate ( ( ) => new Date ( ) ) ,
8488 ipAddress : text ( "ip_address" ) ,
8589 userAgent : text ( "user_agent" ) ,
8690 userId : text ( "user_id" )
8791 . notNull ( )
8892 . references ( ( ) => user . id , { onDelete : "cascade" } ) ,
8993} ) ;
9094
91- export const account = pgTable ( "account" , {
95+ export const account = sqliteTable ( "account" , {
9296 id : text ( "id" ) . primaryKey ( ) ,
9397 accountId : text ( "account_id" ) . notNull ( ) ,
9498 providerId : text ( "provider_id" ) . notNull ( ) ,
@@ -98,24 +102,33 @@ export const account = pgTable("account", {
98102 accessToken : text ( "access_token" ) ,
99103 refreshToken : text ( "refresh_token" ) ,
100104 idToken : text ( "id_token" ) ,
101- accessTokenExpiresAt : timestamp ( "access_token_expires_at" ) ,
102- refreshTokenExpiresAt : timestamp ( "refresh_token_expires_at" ) ,
105+ accessTokenExpiresAt : integer ( "access_token_expires_at" , {
106+ mode : "timestamp" ,
107+ } ) ,
108+ refreshTokenExpiresAt : integer ( "refresh_token_expires_at" , {
109+ mode : "timestamp" ,
110+ } ) ,
103111 scope : text ( "scope" ) ,
104112 password : text ( "password" ) ,
105- createdAt : timestamp ( "created_at" ) . defaultNow ( ) . notNull ( ) ,
106- updatedAt : timestamp ( "updated_at" )
107- . $onUpdate ( ( ) => /* @__PURE__ */ new Date ( ) )
108- . notNull ( ) ,
113+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
114+ . notNull ( )
115+ . $defaultFn ( ( ) => new Date ( ) ) ,
116+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
117+ . notNull ( )
118+ . $defaultFn ( ( ) => new Date ( ) )
119+ . $onUpdate ( ( ) => new Date ( ) ) ,
109120} ) ;
110121
111- export const verification = pgTable ( "verification" , {
122+ export const verification = sqliteTable ( "verification" , {
112123 id : text ( "id" ) . primaryKey ( ) ,
113124 identifier : text ( "identifier" ) . notNull ( ) ,
114125 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 ( ) ,
126+ expiresAt : integer ( "expires_at" , { mode : "timestamp" } ) . notNull ( ) ,
127+ createdAt : integer ( "created_at" , { mode : "timestamp" } )
128+ . notNull ( )
129+ . $defaultFn ( ( ) => new Date ( ) ) ,
130+ updatedAt : integer ( "updated_at" , { mode : "timestamp" } )
131+ . notNull ( )
132+ . $defaultFn ( ( ) => new Date ( ) )
133+ . $onUpdate ( ( ) => new Date ( ) ) ,
121134} ) ;
0 commit comments