(
session: D1DatabaseSession,
stmt: string | { readonly sql: string; readonly args: readonly unknown[] },
)
| 22 | const firstWord = (sql: string): string => sql.trimStart().split(/\s+/, 1)[0]?.toUpperCase() ?? ""; |
| 23 | |
| 24 | const queryRows = <T extends Record<string, unknown>>( |
| 25 | session: D1DatabaseSession, |
| 26 | stmt: string | { readonly sql: string; readonly args: readonly unknown[] }, |
| 27 | ): Promise<readonly T[]> => { |
| 28 | const sql = typeof stmt === "string" ? stmt : stmt.sql; |
| 29 | const args = typeof stmt === "string" ? [] : stmt.args; |
| 30 | const prepared = session.prepare(sql).bind(...args); |
| 31 | if (firstWord(sql) === "SELECT" || firstWord(sql) === "PRAGMA") { |
| 32 | return prepared.all<T>().then((result) => result.results); |
| 33 | } |
| 34 | return prepared.run<T>().then(() => []); |
| 35 | }; |
| 36 | |
| 37 | export const d1DataMigrationClient = (db: D1Database): SqliteDataMigrationClient => { |
| 38 | const session = db.withSession("first-primary"); |
no test coverage detected