login_info.ts (1686B)
1 import db from '@/lib/db' 2 import { modelBehaviour } from '@/lib/model_helpers' 3 import { OkPacket, RowDataPacket } from 'mysql2' 4 5 export interface LoginInfo { 6 id: number 7 email: string 8 passwordHash: string 9 createdAt: Date 10 updatedAt: Date 11 } 12 13 const SQL_GET_LOGIN_INFO = ` 14 SELECT id, email, password_hash, created_at, updated_at 15 FROM logins 16 WHERE id = ? 17 LIMIT 1 18 ` 19 20 export const getLoginInfo = modelBehaviour< 21 [id: number], 22 LoginInfo | null 23 >(async (conn, args) => { 24 const [rows] = await conn.query<RowDataPacket[]>( 25 SQL_GET_LOGIN_INFO, 26 args, 27 ) 28 if (rows.length === 0) { 29 return null 30 } 31 32 const row = rows[0] 33 34 return { 35 id: row[0], 36 email: row[1], 37 passwordHash: row[2], 38 createdAt: row[3], 39 updatedAt: row[4], 40 } 41 }) 42 43 const SQL_GET_LOGIN_INFO_VIA_EMAIL = ` 44 SELECT id, email, password_hash, created_at, updated_at 45 FROM logins 46 WHERE email = ? 47 LIMIT 1 48 ` 49 50 export const getLoginInfoViaEmail = modelBehaviour< 51 [email: string], 52 LoginInfo | null 53 >(async (conn, args) => { 54 const [rows] = await conn.query<RowDataPacket[]>({ 55 sql: SQL_GET_LOGIN_INFO_VIA_EMAIL, 56 }, args) 57 58 if (rows.length === 0) { 59 return null 60 } 61 62 const row = rows[0] 63 return { 64 id: row[0], 65 email: row[1], 66 passwordHash: row[2], 67 createdAt: row[3], 68 updatedAt: row[4], 69 } 70 }) 71 72 const SQL_CREATE_LOGIN_INFO = ` 73 INSERT INTO logins (email, password_hash) 74 VALUES (?, ?) 75 ` 76 77 export const createLoginInfo = modelBehaviour< 78 [email: string, passwordHash: string], 79 number 80 >(async (conn, args) => { 81 const [result] = await conn.query<OkPacket>({ 82 sql: SQL_CREATE_LOGIN_INFO, 83 }, args) 84 85 return result.insertId 86 })