Skip to content

Commit

Permalink
fix(explorer): construct sqlite table names (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs authored Sep 25, 2024
1 parent 9d7fc85 commit e39afda
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-windows-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/explorer": patch
---

Fixed table name construction in the explorer query for root tables for SQLite.
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
14 changes: 9 additions & 5 deletions packages/explorer/src/app/(explorer)/utils/constructTableName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion packages/explorer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e39afda

Please sign in to comment.