user_profile.ts (1114B)
1 import db from '@/lib/db' 2 import { modelBehaviour } from '@/lib/model_helpers' 3 import { OkPacket, RowDataPacket } from 'mysql2' 4 5 export interface UserProfile { 6 loginId: number 7 nickname: string 8 bio: string 9 updatedAt: Date 10 } 11 12 const SQL_CREATE_USER_PROFILE = ` 13 INSERT INTO user_profiles (login_id, nickname, bio) 14 VALUES (?, ?, ?) 15 ` 16 17 export const createUserProfile = modelBehaviour< 18 [ loginId: number, nickname: string, bio: string | null ], 19 void 20 >(async (conn, args) => { 21 await conn.query<OkPacket>({ 22 sql: SQL_CREATE_USER_PROFILE, 23 }, args) 24 }) 25 26 const SQL_GET_USER_PROFILE = ` 27 SELECT login_id, nickname, bio, updated_at 28 FROM user_profiles 29 WHERE login_id = ? 30 LIMIT 1 31 ` 32 33 export const getUserProfile = modelBehaviour< 34 [ loginId: number ], 35 UserProfile | null 36 >(async (conn, args) => { 37 const [rows] = await conn.query<RowDataPacket[]>({ 38 sql: SQL_GET_USER_PROFILE, 39 rowsAsArray: true, 40 }, args) 41 42 if (rows.length === 0) { 43 return null 44 } 45 46 const row = rows[0] 47 return { 48 loginId: row[0], 49 nickname: row[1], 50 bio: row[2], 51 updatedAt: row[3], 52 } 53 })