style: clean up code formatting and remove trailing whitespace
refactor: reorganize imports and fix type imports
This commit is contained in:
1 parent
d3704602fd
commit
47c04de6fe
232 files changed
+7140
-4798
No files matched your search
+42
-32
@@ -4,11 +4,11 @@
|
||||
* usamos $queryRaw para queries dinâmicas
|
||||
*/
|
||||
|
||||
import { Prisma } from "@prisma/client";
|
||||
// Nota: itemsPrisma não está disponível ainda - usando better-sqlite3 diretamente
|
||||
// TODO: Implementar quando migrar SQLite para Prisma
|
||||
// import { itemsPrisma } from "./items";
|
||||
import { enToPtCategoryMap } from "../item-images";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
// Stub temporário - não está sendo usado no momento
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -50,7 +50,6 @@ function getOrderBy(sort: SearchParams["sort"]): string {
|
||||
return "weight DESC, name ASC";
|
||||
case "category":
|
||||
return "category ASC, name ASC";
|
||||
case "name":
|
||||
default:
|
||||
return "name ASC";
|
||||
}
|
||||
@@ -60,7 +59,7 @@ function getOrderBy(sort: SearchParams["sort"]): string {
|
||||
* Busca itens no banco SQLite usando Prisma $queryRaw
|
||||
*/
|
||||
export async function searchItems<T = any>(
|
||||
params: SearchParams
|
||||
params: SearchParams,
|
||||
): Promise<SearchResult<T>> {
|
||||
const {
|
||||
q = "",
|
||||
@@ -80,27 +79,37 @@ export async function searchItems<T = any>(
|
||||
// Log básico do schema na primeira chamada
|
||||
if (!schemaLogged) {
|
||||
try {
|
||||
const tables = await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(`SELECT name FROM sqlite_master WHERE type='table' ORDER BY name`)
|
||||
) as Array<{ name: string }>;
|
||||
console.info("[items-db] Tabelas:", tables.map((t: { name: string }) => t.name));
|
||||
const tables = (await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(
|
||||
`SELECT name FROM sqlite_master WHERE type='table' ORDER BY name`,
|
||||
),
|
||||
)) as Array<{ name: string }>;
|
||||
console.info(
|
||||
"[items-db] Tabelas:",
|
||||
tables.map((t: { name: string }) => t.name),
|
||||
);
|
||||
schemaLogged = true;
|
||||
} catch (intErr) {
|
||||
console.warn("[items-db] Falha ao ler schema:", (intErr as any)?.message);
|
||||
console.warn(
|
||||
"[items-db] Falha ao ler schema:",
|
||||
(intErr as any)?.message,
|
||||
);
|
||||
schemaLogged = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Buscar todas as tabelas disponíveis
|
||||
const allTables = await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(`SELECT name FROM sqlite_master WHERE type='table' AND name <> 'sqlite_sequence'`)
|
||||
) as Array<{ name: string }>;
|
||||
const allTables = (await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(
|
||||
`SELECT name FROM sqlite_master WHERE type='table' AND name <> 'sqlite_sequence'`,
|
||||
),
|
||||
)) as Array<{ name: string }>;
|
||||
|
||||
// Filtra por categorias solicitadas (EN → PT)
|
||||
const allowedPt = new Set(
|
||||
categories.length > 0
|
||||
? categories.map((en) => enToPtCategoryMap[en] || en)
|
||||
: allTables.map((t: { name: string }) => t.name)
|
||||
: allTables.map((t: { name: string }) => t.name),
|
||||
);
|
||||
const candidateTables = allTables
|
||||
.map((t: { name: string }) => t.name)
|
||||
@@ -112,11 +121,13 @@ export async function searchItems<T = any>(
|
||||
for (const tableName of candidateTables) {
|
||||
try {
|
||||
// Verificar colunas da tabela
|
||||
const cols = await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(`PRAGMA table_info(${tableName})`)
|
||||
) as Array<{ name: string; type: string }>;
|
||||
const cols = (await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(`PRAGMA table_info(${tableName})`),
|
||||
)) as Array<{ name: string; type: string }>;
|
||||
|
||||
const colNames = new Set(cols.map((c: { name?: string }) => (c?.name || "").toLowerCase()));
|
||||
const colNames = new Set(
|
||||
cols.map((c: { name?: string }) => (c?.name || "").toLowerCase()),
|
||||
);
|
||||
|
||||
// Requer colunas mínimas
|
||||
if (
|
||||
@@ -184,13 +195,16 @@ export async function searchItems<T = any>(
|
||||
${levelCol} AS level,
|
||||
${weightCol} AS weight,
|
||||
${slotsCol} AS slots,
|
||||
${skillCol} AS skill${extraSelect ? ", " + extraSelect : ""}
|
||||
${skillCol} AS skill${extraSelect ? `, ${extraSelect}` : ""}
|
||||
FROM ${tableName} ${whereSqlTbl}`;
|
||||
|
||||
selects.push(selectSql);
|
||||
bindings.push(...tableBindings);
|
||||
} catch (e) {
|
||||
console.warn(`[items-db] Ignorando tabela ${tableName}:`, (e as any)?.message);
|
||||
console.warn(
|
||||
`[items-db] Ignorando tabela ${tableName}:`,
|
||||
(e as any)?.message,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,20 +218,20 @@ export async function searchItems<T = any>(
|
||||
|
||||
// Contagem total
|
||||
const countSql = `SELECT COUNT(1) as cnt FROM (${unionSql}) AS u`;
|
||||
const countResult = await (itemsPrisma.$queryRaw as any)(
|
||||
const countResult = (await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(countSql),
|
||||
...bindings
|
||||
) as Array<{ cnt: number }>;
|
||||
...bindings,
|
||||
)) as Array<{ cnt: number }>;
|
||||
const total = countResult[0]?.cnt ?? 0;
|
||||
|
||||
// Buscar itens com paginação
|
||||
const selectSql = `SELECT * FROM (${unionSql}) AS u ORDER BY ${orderBy} LIMIT ? OFFSET ?`;
|
||||
const items = await (itemsPrisma.$queryRaw as any)(
|
||||
const items = (await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(selectSql),
|
||||
...bindings,
|
||||
safePageSize,
|
||||
offset
|
||||
) as T[];
|
||||
offset,
|
||||
)) as T[];
|
||||
|
||||
return { items, total };
|
||||
} catch (error) {
|
||||
@@ -239,18 +253,15 @@ export async function getItemById(itemId: number): Promise<any | null> {
|
||||
|
||||
for (const table of tables) {
|
||||
try {
|
||||
const item = await (itemsPrisma.$queryRaw as any)(
|
||||
const item = (await (itemsPrisma.$queryRaw as any)(
|
||||
Prisma.raw(`SELECT * FROM ${table.name} WHERE id = ?`),
|
||||
itemId
|
||||
) as any[];
|
||||
itemId,
|
||||
)) as any[];
|
||||
|
||||
if (item && item.length > 0) {
|
||||
return item[0];
|
||||
}
|
||||
} catch (err) {
|
||||
// Ignorar erros de tabelas que não têm coluna 'id' ou outros problemas
|
||||
continue;
|
||||
}
|
||||
} catch (_err) {}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -259,4 +270,3 @@ export async function getItemById(itemId: number): Promise<any | null> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user