refactor(db): typed query helpers, shared test FormData helper
Replace raw db.execute tuple casts with queryRows/rowsFrom/execResult/ affectedRows helpers from lib/db, drop redundant mysql2 casts on typed query builders, and centralize per-test fakeForm into test/fake-form. Update db mocks in tests so helpers resolve against mocked execute.
This commit is contained in:
1 parent
2e25b39364
commit
8638e81444
116 files changed
+866
-897
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import type { SQL } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import type { ResultSetHeader } from "mysql2";
|
||||
import mysql from "mysql2/promise";
|
||||
import * as relations from "@/db/relations";
|
||||
import * as schema from "@/db/schema";
|
||||
@@ -62,6 +64,26 @@ export const db = globalForDb.db ?? createDb();
|
||||
|
||||
if (env.NODE_ENV !== "production") globalForDb.db = db;
|
||||
|
||||
export function rowsFrom<T>(result: unknown): T[] {
|
||||
const [first] = result as [unknown, unknown];
|
||||
return (Array.isArray(first) ? first : []) as T[];
|
||||
}
|
||||
|
||||
export async function queryRows<T>(query: SQL | string): Promise<T[]> {
|
||||
return rowsFrom<T>(await db.execute(query));
|
||||
}
|
||||
|
||||
export async function execResult(
|
||||
query: SQL | string,
|
||||
): Promise<ResultSetHeader> {
|
||||
const [first] = (await db.execute(query)) as unknown as [unknown, unknown];
|
||||
return first as ResultSetHeader;
|
||||
}
|
||||
|
||||
export async function affectedRows(query: SQL | string): Promise<number> {
|
||||
return (await execResult(query)).affectedRows;
|
||||
}
|
||||
|
||||
export type Db = ReturnType<typeof createDb>;
|
||||
|
||||
export * from "@/db/schema";
|
||||
Reference in new issue
Block a user