|
| 1 | +import type pg from 'pg'; |
| 2 | +import type { SQL } from '../sql'; |
| 3 | + |
| 4 | +export const execute = async <Result = void>( |
| 5 | + pool: pg.Pool, |
| 6 | + handle: (client: pg.PoolClient) => Promise<Result>, |
| 7 | +) => { |
| 8 | + const client = await pool.connect(); |
| 9 | + try { |
| 10 | + return await handle(client); |
| 11 | + } finally { |
| 12 | + client.release(); |
| 13 | + } |
| 14 | +}; |
| 15 | + |
| 16 | +export const executeInTransaction = async <Result = void>( |
| 17 | + pool: pg.Pool, |
| 18 | + handle: ( |
| 19 | + client: pg.PoolClient, |
| 20 | + ) => Promise<{ success: boolean; result: Result }>, |
| 21 | +): Promise<Result> => |
| 22 | + execute(pool, async (client) => { |
| 23 | + try { |
| 24 | + await client.query('BEGIN'); |
| 25 | + |
| 26 | + const { success, result } = await handle(client); |
| 27 | + |
| 28 | + if (success) await client.query('COMMIT'); |
| 29 | + else await client.query('ROLLBACK'); |
| 30 | + |
| 31 | + return result; |
| 32 | + } catch (e) { |
| 33 | + await client.query('ROLLBACK'); |
| 34 | + throw e; |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | +export const executeSQL = async < |
| 39 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 40 | +>( |
| 41 | + poolOrClient: pg.Pool | pg.PoolClient, |
| 42 | + sql: SQL, |
| 43 | +): Promise<pg.QueryResult<Result>> => |
| 44 | + 'totalCount' in poolOrClient |
| 45 | + ? execute(poolOrClient, (client) => client.query<Result>(sql)) |
| 46 | + : poolOrClient.query<Result>(sql); |
| 47 | + |
| 48 | +export const executeSQLInTransaction = async < |
| 49 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 50 | +>( |
| 51 | + pool: pg.Pool, |
| 52 | + sql: SQL, |
| 53 | +) => { |
| 54 | + console.log(sql); |
| 55 | + return executeInTransaction(pool, async (client) => ({ |
| 56 | + success: true, |
| 57 | + result: await client.query<Result>(sql), |
| 58 | + })); |
| 59 | +}; |
| 60 | + |
| 61 | +export const executeSQLBatchInTransaction = async < |
| 62 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 63 | +>( |
| 64 | + pool: pg.Pool, |
| 65 | + ...sqls: SQL[] |
| 66 | +) => |
| 67 | + executeInTransaction(pool, async (client) => { |
| 68 | + for (const sql of sqls) { |
| 69 | + await client.query<Result>(sql); |
| 70 | + } |
| 71 | + |
| 72 | + return { success: true, result: undefined }; |
| 73 | + }); |
| 74 | + |
| 75 | +export const firstOrNull = async < |
| 76 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 77 | +>( |
| 78 | + getResult: Promise<pg.QueryResult<Result>>, |
| 79 | +): Promise<Result | null> => { |
| 80 | + const result = await getResult; |
| 81 | + |
| 82 | + return result.rows.length > 0 ? result.rows[0] ?? null : null; |
| 83 | +}; |
| 84 | + |
| 85 | +export const first = async < |
| 86 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 87 | +>( |
| 88 | + getResult: Promise<pg.QueryResult<Result>>, |
| 89 | +): Promise<Result> => { |
| 90 | + const result = await getResult; |
| 91 | + |
| 92 | + if (result.rows.length === 0) |
| 93 | + throw new Error("Query didn't return any result"); |
| 94 | + |
| 95 | + return result.rows[0]!; |
| 96 | +}; |
| 97 | + |
| 98 | +export const singleOrNull = async < |
| 99 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 100 | +>( |
| 101 | + getResult: Promise<pg.QueryResult<Result>>, |
| 102 | +): Promise<Result | null> => { |
| 103 | + const result = await getResult; |
| 104 | + |
| 105 | + if (result.rows.length > 1) throw new Error('Query had more than one result'); |
| 106 | + |
| 107 | + return result.rows.length > 0 ? result.rows[0] ?? null : null; |
| 108 | +}; |
| 109 | + |
| 110 | +export const single = async < |
| 111 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 112 | +>( |
| 113 | + getResult: Promise<pg.QueryResult<Result>>, |
| 114 | +): Promise<Result> => { |
| 115 | + const result = await getResult; |
| 116 | + |
| 117 | + if (result.rows.length === 0) |
| 118 | + throw new Error("Query didn't return any result"); |
| 119 | + |
| 120 | + if (result.rows.length > 1) throw new Error('Query had more than one result'); |
| 121 | + |
| 122 | + return result.rows[0]!; |
| 123 | +}; |
| 124 | + |
| 125 | +export const mapRows = async < |
| 126 | + Result extends pg.QueryResultRow = pg.QueryResultRow, |
| 127 | + Mapped = unknown, |
| 128 | +>( |
| 129 | + getResult: Promise<pg.QueryResult<Result>>, |
| 130 | + map: (row: Result) => Mapped, |
| 131 | +): Promise<Mapped[]> => { |
| 132 | + const result = await getResult; |
| 133 | + |
| 134 | + return result.rows.map(map); |
| 135 | +}; |
| 136 | + |
| 137 | +export const toCamelCase = (snakeStr: string): string => |
| 138 | + snakeStr.replace(/_([a-z])/g, (g) => g[1]?.toUpperCase() ?? ''); |
| 139 | + |
| 140 | +export const mapToCamelCase = <T extends Record<string, unknown>>( |
| 141 | + obj: T, |
| 142 | +): T => { |
| 143 | + const newObj: Record<string, unknown> = {}; |
| 144 | + for (const key in obj) { |
| 145 | + if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| 146 | + newObj[toCamelCase(key)] = obj[key]; |
| 147 | + } |
| 148 | + } |
| 149 | + return newObj as T; |
| 150 | +}; |
| 151 | + |
| 152 | +export type ExistsSQLQueryResult = { exists: boolean }; |
| 153 | + |
| 154 | +export const exists = async (pool: pg.Pool, sql: SQL): Promise<boolean> => { |
| 155 | + const result = await single(executeSQL<ExistsSQLQueryResult>(pool, sql)); |
| 156 | + |
| 157 | + return result.exists === true; |
| 158 | +}; |
0 commit comments