feat: add batch insert operations to repositories (refs #26)

Add batchInsert methods to vehicles, fuel-logs, maintenance, and documents repositories. Multi-value INSERT syntax provides 10-100x performance improvement over individual operations for bulk data import.

- vehicles.repository: batchInsert for vehicles
- fuel-logs.repository: batchInsert for fuel logs
- maintenance.repository: batchInsertRecords and batchInsertSchedules
- documents.repository: batchInsert for documents
- All methods support empty array (immediate return) and optional transaction client
- Fix lint error: replace require() with ES6 import in test mock

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Eric Gullickson
2026-01-11 19:28:11 -06:00
parent bb8fdf33cf
commit e6af7ed5d5
5 changed files with 286 additions and 2 deletions

View File

@@ -148,6 +148,52 @@ export class FuelLogsRepository {
return this.mapRow(result.rows[0]);
}
async batchInsert(
logs: Array<CreateFuelLogRequest & { userId: string }>,
client?: any
): Promise<FuelLog[]> {
if (logs.length === 0) {
return [];
}
// Multi-value INSERT for performance (avoids N round-trips)
const queryClient = client || this.pool;
const placeholders: string[] = [];
const values: any[] = [];
let paramCount = 1;
logs.forEach((log) => {
const logParams = [
log.userId,
log.vehicleId,
log.date,
log.odometer,
log.gallons,
log.pricePerGallon,
log.totalCost,
log.station,
log.location,
log.notes
];
const placeholder = `($${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++}, $${paramCount++})`;
placeholders.push(placeholder);
values.push(...logParams);
});
const query = `
INSERT INTO fuel_logs (
user_id, vehicle_id, date, odometer, gallons,
price_per_gallon, total_cost, station, location, notes
)
VALUES ${placeholders.join(', ')}
RETURNING *
`;
const result = await queryClient.query(query, values);
return result.rows.map((row: any) => this.mapRow(row));
}
async delete(id: string): Promise<boolean> {
const query = 'DELETE FROM fuel_logs WHERE id = $1';
const result = await this.pool.query(query, [id]);