1- import process from "node:process"
21import type { Database } from "db0"
32import type { UserInfo } from "#/types"
43
@@ -9,58 +8,28 @@ export class UserTable {
98 }
109
1110 async init ( ) {
12- // Check if we're using MySQL by looking for MySQL environment variables
13- const isMySQL = process . env . MYSQL_HOST && process . env . MYSQL_USER && process . env . MYSQL_PASSWORD && process . env . MYSQL_DATABASE
14-
15- if ( isMySQL ) {
16- // MySQL syntax
17- await this . db . prepare ( `
18- CREATE TABLE IF NOT EXISTS user (
19- id VARCHAR(255) PRIMARY KEY,
20- email VARCHAR(255),
21- data TEXT,
22- type VARCHAR(50),
23- created BIGINT,
24- updated BIGINT
25- );
26- ` ) . run ( )
27- await this . db . prepare ( `
28- CREATE INDEX IF NOT EXISTS idx_user_id ON user(id);
29- ` ) . run ( )
30- } else {
31- // SQLite syntax
32- await this . db . prepare ( `
33- CREATE TABLE IF NOT EXISTS user (
34- id TEXT PRIMARY KEY,
35- email TEXT,
36- data TEXT,
37- type TEXT,
38- created INTEGER,
39- updated INTEGER
40- );
41- ` ) . run ( )
42- await this . db . prepare ( `
43- CREATE INDEX IF NOT EXISTS idx_user_id ON user(id);
44- ` ) . run ( )
45- }
11+ await this . db . prepare ( `
12+ CREATE TABLE IF NOT EXISTS user (
13+ id TEXT PRIMARY KEY,
14+ email TEXT,
15+ data TEXT,
16+ type TEXT,
17+ created INTEGER,
18+ updated INTEGER
19+ );
20+ ` ) . run ( )
21+ await this . db . prepare ( `
22+ CREATE INDEX IF NOT EXISTS idx_user_id ON user(id);
23+ ` ) . run ( )
4624 logger . success ( `init user table` )
4725 }
4826
4927 async addUser ( id : string , email : string , type : "github" ) {
5028 const u = await this . getUser ( id )
5129 const now = Date . now ( )
52- const isMySQL = process . env . MYSQL_HOST && process . env . MYSQL_USER && process . env . MYSQL_PASSWORD && process . env . MYSQL_DATABASE
53-
5430 if ( ! u ) {
55- if ( isMySQL ) {
56- // MySQL syntax - use ON DUPLICATE KEY UPDATE
57- await this . db . prepare ( `INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE email = VALUES(email), updated = VALUES(updated)` )
58- . run ( id , email , "" , type , now , now )
59- } else {
60- // SQLite syntax
61- await this . db . prepare ( `INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?)` )
62- . run ( id , email , "" , type , now , now )
63- }
31+ await this . db . prepare ( `INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?)` )
32+ . run ( id , email , "" , type , now , now )
6433 logger . success ( `add user ${ id } ` )
6534 } else if ( u . email !== email && u . type !== type ) {
6635 await this . db . prepare ( `UPDATE user SET email = ?, updated = ? WHERE id = ?` ) . run ( email , now , id )
@@ -97,4 +66,4 @@ export class UserTable {
9766 if ( ! state . success ) throw new Error ( `delete user ${ key } failed` )
9867 logger . success ( `delete user ${ key } ` )
9968 }
100- }
69+ }
0 commit comments