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,6 +1,6 @@
|
||||
import "server-only";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import { queryRows } from "@/lib/db";
|
||||
import { type CatalogSearchResult, parseCatalogSearch } from "../domain/search";
|
||||
|
||||
const LIMIT = 25;
|
||||
@@ -29,18 +29,15 @@ export async function searchCatalog(
|
||||
);
|
||||
const number = input.numericId;
|
||||
const [categoryData, furnitureData] = await Promise.all([
|
||||
db.execute(
|
||||
queryRows<PageRow>(
|
||||
sql`SELECT id, caption, parent_id AS parentId FROM ${pages} WHERE caption LIKE ${input.pattern} ESCAPE '=' ${number === null ? sql`` : sql`OR id=${number}`} ORDER BY ${number === null ? sql`id` : sql`CASE WHEN id=${number} THEN 0 ELSE 1 END, id`} LIMIT 12`,
|
||||
),
|
||||
db.execute(
|
||||
queryRows<FurnitureRow>(
|
||||
sql`SELECT id, item_name AS classname, sprite_id AS spriteId FROM items_base WHERE item_name LIKE ${input.pattern} ESCAPE '=' OR public_name LIKE ${input.pattern} ESCAPE '=' ${number === null ? sql`` : sql`OR id=${number} OR sprite_id=${number}`} ORDER BY ${number === null ? sql`id` : sql`CASE WHEN id=${number} THEN 0 ELSE 1 END, id`} LIMIT 25`,
|
||||
),
|
||||
]);
|
||||
const categories = (categoryData[0] as unknown as PageRow[]).slice(0, 12);
|
||||
const furniture = (furnitureData[0] as unknown as FurnitureRow[]).slice(
|
||||
0,
|
||||
LIMIT,
|
||||
);
|
||||
const categories = categoryData.slice(0, 12);
|
||||
const furniture = furnitureData.slice(0, LIMIT);
|
||||
const predicates = [sql`i.catalog_name LIKE ${input.pattern} ESCAPE '='`];
|
||||
if (number !== null) {
|
||||
predicates.push(sql`i.id=${number}`, sql`i.page_id=${String(number)}`);
|
||||
@@ -51,13 +48,10 @@ export async function searchCatalog(
|
||||
predicates.push(
|
||||
sql`FIND_IN_SET(${String(base.id)}, REPLACE(i.item_ids, ';', ',')) > 0`,
|
||||
);
|
||||
const [offerData] = await db.execute(
|
||||
const offerData = await queryRows<OfferRow>(
|
||||
sql`SELECT i.id, i.catalog_name AS caption, i.page_id AS pageId, i.item_ids AS itemIds, p.caption AS pageCaption, p.parent_id AS parentId FROM ${offers} i INNER JOIN ${pages} p ON p.id=i.page_id WHERE ${sql.join(predicates, sql` OR `)} ORDER BY ${number === null ? sql`i.id` : sql`CASE WHEN i.id=${number} THEN 0 ELSE 1 END, i.id`} LIMIT ${LIMIT - categories.length}`,
|
||||
);
|
||||
const matches = (offerData as unknown as OfferRow[]).slice(
|
||||
0,
|
||||
LIMIT - categories.length,
|
||||
);
|
||||
const matches = offerData.slice(0, LIMIT - categories.length);
|
||||
const pageMap = new Map<number, PageRow>();
|
||||
for (const page of categories)
|
||||
pageMap.set(Number(page.id), {
|
||||
@@ -79,11 +73,11 @@ export async function searchCatalog(
|
||||
.slice(0, LIMIT);
|
||||
if (!ids.length) break;
|
||||
for (const id of ids) attempted.add(id);
|
||||
const [parents] = await db.execute(
|
||||
const parents = await queryRows<PageRow>(
|
||||
sql`SELECT id, caption, parent_id AS parentId FROM ${pages} WHERE id IN (${sql.join(ids, sql`, `)}) LIMIT 25`,
|
||||
);
|
||||
frontier = [];
|
||||
for (const raw of parents as unknown as PageRow[]) {
|
||||
for (const raw of parents) {
|
||||
const page = {
|
||||
...raw,
|
||||
id: Number(raw.id),
|
||||
|
||||
Reference in new issue
Block a user