Skip to content

Commit

Permalink
createTable -> buildTable
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 23, 2023
1 parent 7b93807 commit 83abbdf
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 32 deletions.
14 changes: 8 additions & 6 deletions packages/store-indexer/bin/postgres-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { isNotNull } from "@latticexyz/common/utils";
import { combineLatest, filter, first } from "rxjs";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { createInternalTables, schemaVersion, syncToPostgres } from "@latticexyz/store-sync/postgres";
import { postgresStorage, schemaVersion } from "@latticexyz/store-sync/postgres";
import { createStoreSync } from "@latticexyz/store-sync";

const possibleChains = Object.values({ ...mudChains, ...chains }) as Chain[];

Expand Down Expand Up @@ -66,13 +67,14 @@ const database = drizzle(postgres(env.DATABASE_URL), {

let startBlock = env.START_BLOCK;

const storage = await postgresStorage({ database, publicClient });

// Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error.
try {
const internalTables = createInternalTables();
const currentChainStates = await database
.select()
.from(internalTables.chain)
.where(eq(internalTables.chain.chainId, chainId))
.from(storage.internalTables.chain)
.where(eq(storage.internalTables.chain.chainId, chainId))
.execute();
// TODO: replace this type workaround with `noUncheckedIndexedAccess: true` when we can fix all the issues related (https://github.com/latticexyz/mud/issues/1212)
const currentChainState: (typeof currentChainStates)[number] | undefined = currentChainStates[0];
Expand All @@ -96,8 +98,8 @@ try {
// ignore errors, this is optional
}

const { latestBlockNumber$, blockStorageOperations$ } = await syncToPostgres({
database,
const { latestBlockNumber$, blockStorageOperations$ } = await createStoreSync({
storageAdapter: storage,
publicClient,
startBlock,
maxBlockRange: env.MAX_BLOCK_RANGE,
Expand Down
6 changes: 3 additions & 3 deletions packages/store-indexer/src/postgres/createStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { eq } from "drizzle-orm";
import { PgDatabase } from "drizzle-orm/pg-core";
import { createTable, createInternalTables, getTables } from "@latticexyz/store-sync/postgres";
import { buildTable, buildInternalTables, getTables } from "@latticexyz/store-sync/postgres";
import { StorageAdapter } from "@latticexyz/store-sync/trpc-indexer";
import { debug } from "../debug";
import { getAddress } from "viem";
Expand All @@ -14,14 +14,14 @@ import { getAddress } from "viem";
export async function createStorageAdapter(database: PgDatabase<any>): Promise<StorageAdapter> {
const adapter: StorageAdapter = {
async findAll(chainId, address) {
const internalTables = createInternalTables();
const internalTables = buildInternalTables();
const tables = (await getTables(database)).filter(
(table) => address != null && getAddress(address) === getAddress(table.address)
);

const tablesWithRecords = await Promise.all(
tables.map(async (table) => {
const sqliteTable = createTable(table);
const sqliteTable = buildTable(table);
const records = await database.select().from(sqliteTable).where(eq(sqliteTable.__isDeleted, false)).execute();
return {
...table,
Expand Down
1 change: 1 addition & 0 deletions packages/store-sync/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./blockLogsToStorage";
export * from "./common";
export * from "./createStoreSync";
export * from "./SyncStep";
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DynamicAbiType, StaticAbiType } from "@latticexyz/schema-type";
import { transformSchemaName } from "./transformSchemaName";

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createInternalTables() {
export function buildInternalTables() {
const schema = pgSchema(transformSchemaName("__mud_internal"));
return {
chain: schema.table("chain", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it, expect } from "vitest";
import { createTable } from "./createTable";
import { buildTable } from "./buildTable";

describe("createTable", () => {
describe("buildTable", () => {
it("should create table from schema", async () => {
const table = createTable({
const table = buildTable({
address: "0xffffffffffffffffffffffffffffffffffffffff",
namespace: "test",
name: "users",
Expand Down Expand Up @@ -250,7 +250,7 @@ describe("createTable", () => {
});

it("can create a singleton table", async () => {
const table = createTable({
const table = buildTable({
address: "0xffffffffffffffffffffffffffffffffffffffff",
namespace: "test",
name: "users",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type PgTableFromSchema<
};
}>;

type CreateTableOptions<
type BuildTableOptions<
TKeySchema extends Record<string, StaticAbiType>,
TValueSchema extends Record<string, SchemaAbiType>
> = {
Expand All @@ -41,12 +41,12 @@ type CreateTableOptions<
valueSchema: TValueSchema;
};

type CreateTableResult<
type BuildTableResult<
TKeySchema extends Record<string, StaticAbiType>,
TValueSchema extends Record<string, SchemaAbiType>
> = PgTableFromSchema<TKeySchema, TValueSchema>;

export function createTable<
export function buildTable<
TKeySchema extends Record<string, StaticAbiType>,
TValueSchema extends Record<string, SchemaAbiType>
>({
Expand All @@ -55,7 +55,7 @@ export function createTable<
name,
keySchema,
valueSchema,
}: CreateTableOptions<TKeySchema, TValueSchema>): CreateTableResult<TKeySchema, TValueSchema> {
}: BuildTableOptions<TKeySchema, TValueSchema>): BuildTableResult<TKeySchema, TValueSchema> {
const schemaName = transformSchemaName(`${getAddress(address)}__${namespace}`);

const keyColumns = Object.fromEntries(
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/getTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { PgDatabase } from "drizzle-orm/pg-core";
import { inArray } from "drizzle-orm";
import { Table } from "../common";
import { getTableKey } from "./getTableKey";
import { createInternalTables } from "./createInternalTables";
import { buildInternalTables } from "./buildInternalTables";
import { tableIdToHex } from "@latticexyz/common";

export async function getTables(db: PgDatabase<any>, ids: string[] = []): Promise<Table[]> {
const internalTables = createInternalTables();
const internalTables = buildInternalTables();

const tables = await db
.select()
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./createTable";
export * from "./buildTable";
export * from "./getTables";
export * from "./createInternalTables";
export * from "./buildInternalTables";
export * from "./schemaVersion";
export * from "./postgresStorage";
export * from "./setupTables";
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/postgres/postgresStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { blockLogsToStorage } from "../blockLogsToStorage";
import * as transformSchemaNameExports from "./transformSchemaName";
import { getTables } from "./getTables";
import { postgresStorage } from "./postgresStorage";
import { createTable } from "./createTable";
import { createTable } from "./buildTable";

vi.spyOn(transformSchemaNameExports, "transformSchemaName").mockImplementation(
(schemaName) => `${process.pid}_${process.env.VITEST_POOL_ID}__${schemaName}`
Expand Down
14 changes: 7 additions & 7 deletions packages/store-sync/src/postgres/postgresStorage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PublicClient, concatHex, encodeAbiParameters } from "viem";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { and, eq, inArray } from "drizzle-orm";
import { createTable } from "./createTable";
import { eq, inArray } from "drizzle-orm";
import { buildTable } from "./buildTable";
import { schemaToDefaults } from "../schemaToDefaults";
import { BlockLogsToStorageOptions } from "../blockLogsToStorage";
import { StoreConfig } from "@latticexyz/store";
import { debug } from "./debug";
import { createInternalTables } from "./createInternalTables";
import { buildInternalTables } from "./buildInternalTables";
import { getTables } from "./getTables";
import { schemaVersion } from "./schemaVersion";
import { tableIdToHex } from "@latticexyz/common";
Expand All @@ -16,7 +16,7 @@ import { getTableKey } from "./getTableKey";
// Currently assumes one DB per chain ID

type PostgresStorage<TConfig extends StoreConfig> = BlockLogsToStorageOptions<TConfig> & {
internalTables: ReturnType<typeof createInternalTables>;
internalTables: ReturnType<typeof buildInternalTables>;
cleanUp: () => Promise<void>;
};

Expand All @@ -32,13 +32,13 @@ export async function postgresStorage<TConfig extends StoreConfig = StoreConfig>

const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());

const internalTables = createInternalTables();
const internalTables = buildInternalTables();
cleanUp.push(await setupTables(database, Object.values(internalTables)));

const storage = {
async registerTables({ blockNumber, tables }) {
const sqlTables = tables.map((table) =>
createTable({
buildTable({
address: table.address,
namespace: table.namespace,
name: table.name,
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function postgresStorage<TConfig extends StoreConfig = StoreConfig>
continue;
}

const sqlTable = createTable(table);
const sqlTable = buildTable(table);
const key = concatHex(
Object.entries(table.keySchema).map(([keyName, type]) =>
encodeAbiParameters([{ type }], [operation.key[keyName]])
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/setupTables.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createInternalTables } from "./createInternalTables";
import { buildInternalTables } from "./buildInternalTables";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { DefaultLogger } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
Expand All @@ -21,7 +21,7 @@ describe("setupTables", async () => {
});

it("should set up tables with schemas and clean up", async () => {
const internalTables = createInternalTables();
const internalTables = buildInternalTables();

await expect(db.select().from(internalTables.chain)).rejects.toThrow(
/relation "\w+mud_internal.chain" does not exist/
Expand Down

0 comments on commit 83abbdf

Please sign in to comment.