( sql: any, table: string, data: Record<string, unknown> )
| 133 | } |
| 134 | |
| 135 | export async function executeInsert( |
| 136 | sql: any, |
| 137 | table: string, |
| 138 | data: Record<string, unknown> |
| 139 | ): Promise<{ rows: unknown[]; rowCount: number }> { |
| 140 | const sanitizedTable = sanitizeIdentifier(table) |
| 141 | const columns = Object.keys(data) |
| 142 | const sanitizedColumns = columns.map((col) => sanitizeIdentifier(col)) |
| 143 | const placeholders = columns.map((_, index) => `$${index + 1}`) |
| 144 | const values = columns.map((col) => data[col]) |
| 145 | |
| 146 | const query = `INSERT INTO ${sanitizedTable} (${sanitizedColumns.join(', ')}) VALUES (${placeholders.join(', ')}) RETURNING *` |
| 147 | const result = await sql.unsafe(query, values) |
| 148 | |
| 149 | const rowCount = result.count ?? result.length ?? 0 |
| 150 | return { |
| 151 | rows: Array.isArray(result) ? result : [result], |
| 152 | rowCount, |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | export async function executeUpdate( |
| 157 | sql: any, |
no test coverage detected