MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / queryRows

Function queryRows

apps/host-cloudflare/src/db/data-migrations.ts:24–35  ·  view source on GitHub ↗
(
  session: D1DatabaseSession,
  stmt: string | { readonly sql: string; readonly args: readonly unknown[] },
)

Source from the content-addressed store, hash-verified

22const firstWord = (sql: string): string => sql.trimStart().split(/\s+/, 1)[0]?.toUpperCase() ?? "";
23
24const 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
37export const d1DataMigrationClient = (db: D1Database): SqliteDataMigrationClient => {
38 const session = db.withSession("first-primary");

Callers 1

d1DataMigrationClientFunction · 0.70

Calls 1

firstWordFunction · 0.85

Tested by

no test coverage detected