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
@@ -15,7 +15,7 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { db, ItemsBase } from "@/lib/db";
|
||||
import { db, ItemsBase, queryRows } from "@/lib/db";
|
||||
import { normalizeClassname } from "@/lib/furni/classname";
|
||||
import { indexLocalFurniture } from "@/lib/furni/local-presence";
|
||||
import { readFurniData } from "./furni-data";
|
||||
@@ -89,10 +89,10 @@ export async function getCatalogIdSet(): Promise<Set<number>> {
|
||||
}
|
||||
const set = new Set<number>();
|
||||
try {
|
||||
const rows = await db.execute(sql`SELECT item_ids FROM catalog_items`);
|
||||
for (const row of (
|
||||
rows as unknown as [Array<{ item_ids: string | null }>, unknown]
|
||||
)[0]) {
|
||||
const rows = await queryRows<{ item_ids: string | null }>(
|
||||
sql`SELECT item_ids FROM catalog_items`,
|
||||
);
|
||||
for (const row of rows) {
|
||||
for (const part of String(row.item_ids ?? "").split(/[;,]/)) {
|
||||
const n = Number(part.trim());
|
||||
if (Number.isFinite(n) && n > 0) set.add(n);
|
||||
@@ -138,48 +138,39 @@ export async function getFurniImportStats(
|
||||
return statsSnapshot.stats;
|
||||
}
|
||||
try {
|
||||
const [totalResult, catalogResult, allIdsResult, referencedResult] =
|
||||
const [totalInDbRaw, inCatalogRaw, allIds, referencedList] =
|
||||
await Promise.all([
|
||||
db.execute(sql`SELECT COUNT(*) as cnt FROM items_base`),
|
||||
db.execute(
|
||||
queryRows<{ cnt: bigint }>(sql`SELECT COUNT(*) as cnt FROM items_base`),
|
||||
queryRows<{ cnt: bigint }>(
|
||||
sql`SELECT COUNT(DISTINCT item_ids) as cnt FROM catalog_items`,
|
||||
),
|
||||
db.execute(sql`SELECT id FROM items_base`),
|
||||
db.execute(sql`SELECT item_ids FROM catalog_items`),
|
||||
queryRows<{ id: number }>(sql`SELECT id FROM items_base`),
|
||||
queryRows<{ item_ids: string | null }>(
|
||||
sql`SELECT item_ids FROM catalog_items`,
|
||||
),
|
||||
]);
|
||||
|
||||
const totalInDb = Number(
|
||||
(totalResult as unknown as [[{ cnt: bigint }], unknown])[0][0].cnt,
|
||||
);
|
||||
const inCatalog = Number(
|
||||
(catalogResult as unknown as [[{ cnt: bigint }], unknown])[0][0].cnt,
|
||||
);
|
||||
const totalInDb = Number(totalInDbRaw[0]?.cnt ?? 0);
|
||||
const inCatalog = Number(inCatalogRaw[0]?.cnt ?? 0);
|
||||
|
||||
const allIds = (
|
||||
allIdsResult as unknown as [Array<{ id: number }>, unknown]
|
||||
)[0].map((r) => Number(r.id));
|
||||
const allIdsNum = allIds.map((r) => Number(r.id));
|
||||
const referenced = new Set<number>();
|
||||
for (const row of (
|
||||
referencedResult as unknown as [
|
||||
Array<{ item_ids: string | null }>,
|
||||
unknown,
|
||||
]
|
||||
)[0]) {
|
||||
for (const row of referencedList) {
|
||||
for (const part of String(row.item_ids ?? "").split(/[;,]/)) {
|
||||
const n = Number(part.trim());
|
||||
if (Number.isFinite(n) && n > 0) referenced.add(n);
|
||||
}
|
||||
}
|
||||
const notInCatalog = allIds.filter((id) => !referenced.has(id)).length;
|
||||
const notInCatalog = allIdsNum.filter((id) => !referenced.has(id)).length;
|
||||
|
||||
const [importedItems] = (await db.execute(sql`
|
||||
const importedItems = await queryRows<{ item_name: string }>(sql`
|
||||
SELECT ib.item_name FROM items_base ib
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM catalog_items ci
|
||||
WHERE FIND_IN_SET(ib.id, REPLACE(ci.item_ids, ';', ',')) > 0
|
||||
)
|
||||
LIMIT 10000
|
||||
`)) as unknown as [Array<{ item_name: string }>, unknown];
|
||||
`);
|
||||
|
||||
let missingNitro = 0;
|
||||
for (const item of importedItems) {
|
||||
|
||||
Reference in new issue
Block a user