diff --git a/.changeset/nine-windows-brake.md b/.changeset/nine-windows-brake.md new file mode 100644 index 0000000000..bf2689f2ac --- /dev/null +++ b/.changeset/nine-windows-brake.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/explorer": patch +--- + +Fixed table name construction in the explorer query for root tables for SQLite. diff --git a/packages/explorer/src/app/(explorer)/api/sqlite-indexer/route.ts b/packages/explorer/src/app/(explorer)/api/sqlite-indexer/route.ts index 591c7a16eb..8fd38407d6 100644 --- a/packages/explorer/src/app/(explorer)/api/sqlite-indexer/route.ts +++ b/packages/explorer/src/app/(explorer)/api/sqlite-indexer/route.ts @@ -24,7 +24,7 @@ export async function POST(request: Request) { } const columns = Object.keys(data[0]).map((key) => key.replaceAll("_", "").toLowerCase()); - const rows = data.map((row) => Object.values(row).map((value) => value.toString())); + const rows = data.map((row) => Object.values(row).map((value) => value?.toString() ?? "")); result.push([columns, ...rows]); } diff --git a/packages/explorer/src/app/(explorer)/utils/constructTableName.ts b/packages/explorer/src/app/(explorer)/utils/constructTableName.ts index e706c56726..a7775afb1a 100644 --- a/packages/explorer/src/app/(explorer)/utils/constructTableName.ts +++ b/packages/explorer/src/app/(explorer)/utils/constructTableName.ts @@ -5,9 +5,13 @@ import { indexerForChainId } from "./indexerForChainId"; export function constructTableName(table: Table, worldAddress: Hex, chainId: number) { const indexer = indexerForChainId(chainId); - let tableId = table.name; - if (table.namespace) { - tableId = `${table.namespace}${indexer.type === "sqlite" ? "_" : "__"}${tableId}`; - } - return indexer.type === "sqlite" ? `${worldAddress}__${snakeCase(tableId)}`.toLowerCase() : tableId; + return indexer.type === "sqlite" ? constructSqliteTableName(table, worldAddress) : constructDozerTableName(table); +} + +function constructSqliteTableName(table: Table, worldAddress: Hex) { + return `${worldAddress}__${snakeCase(table.namespace)}__${snakeCase(table.name)}`; +} + +function constructDozerTableName(table: Table) { + return table.namespace ? `${table.namespace}__${table.name}` : table.name; } diff --git a/packages/explorer/src/utils.ts b/packages/explorer/src/utils.ts index cded49550a..0a4a7acdfc 100644 --- a/packages/explorer/src/utils.ts +++ b/packages/explorer/src/utils.ts @@ -7,7 +7,7 @@ export function cn(...inputs: ClassValue[]) { } export function snakeCase(str: string) { - return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); + return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`).replace(/^_/, ""); } export function formatBalance(wei: bigint) {