()
| 484 | * `execute()` call lazily initializes the underlying driver. |
| 485 | */ |
| 486 | export function getDbExec(): DbExec { |
| 487 | if (_exec) return _exec; |
| 488 | |
| 489 | // Sanitize args: replace undefined with null (libsql rejects undefined) |
| 490 | function sanitize( |
| 491 | sql: string | { sql: string; args: any[] }, |
| 492 | ): string | { sql: string; args: any[] } { |
| 493 | if (typeof sql === "object" && sql.args) { |
| 494 | return { ...sql, args: sql.args.map((a: any) => a ?? null) }; |
| 495 | } |
| 496 | return sql; |
| 497 | } |
| 498 | |
| 499 | // Return a proxy that lazy-inits on first call |
| 500 | const proxy: DbExec = { |
| 501 | async execute(sql) { |
| 502 | if (!_initPromise) _initPromise = initClient(); |
| 503 | await _initPromise; |
| 504 | // After init, swap to a sanitizing wrapper around the real client |
| 505 | const wrapper: DbExec = { |
| 506 | execute: (s) => _exec!.execute(sanitize(s)), |
| 507 | }; |
| 508 | Object.assign(proxy, wrapper); |
| 509 | return _exec!.execute(sanitize(sql)); |
| 510 | }, |
| 511 | }; |
| 512 | return proxy; |
| 513 | } |
| 514 | |
| 515 | /** Close the database connection (for scripts that need cleanup). */ |
| 516 | export async function closeDbExec(): Promise<void> { |
no outgoing calls
no test coverage detected