Skip to content

Commit

Permalink
rename getConfig to getTableConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Aug 30, 2024
1 parent 175ca92 commit 62f9cf0
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/stash/src/actions/getTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe("getTable", () => {
const table = stash.getTable({
table: config,
});
attest<typeof config, ReturnType<typeof table.getConfig>>();
attest<typeof config, ReturnType<typeof table.getTableConfig>>();
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/stash/src/actions/getTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Stash } from "../common";
import { DecodeKeyArgs, DecodeKeyResult, decodeKey } from "./decodeKey";
import { DeleteRecordArgs, DeleteRecordResult, deleteRecord } from "./deleteRecord";
import { EncodeKeyArgs, EncodeKeyResult, encodeKey } from "./encodeKey";
import { GetConfigResult, getConfig } from "./getConfig";
import { GetTableConfigResult, getTableConfig } from "./getTableConfig";
import { GetKeysResult, getKeys } from "./getKeys";
import { GetRecordArgs, GetRecordResult, getRecord } from "./getRecord";
import { GetRecordsArgs, GetRecordsResult, getRecords } from "./getRecords";
Expand All @@ -28,7 +28,7 @@ export type BoundTable<table extends Table = Table> = {
decodeKey: (args: TableBoundDecodeKeyArgs<table>) => DecodeKeyResult<table>;
deleteRecord: (args: TableBoundDeleteRecordArgs<table>) => DeleteRecordResult;
encodeKey: (args: TableBoundEncodeKeyArgs<table>) => EncodeKeyResult;
getConfig: () => GetConfigResult<table>;
getTableConfig: () => GetTableConfigResult<table>;
getKeys: () => GetKeysResult<table>;
getRecord: (args: TableBoundGetRecordArgs<table>) => GetRecordResult<table>;
getRecords: (args?: TableBoundGetRecordsArgs<table>) => GetRecordsResult<table>;
Expand All @@ -55,7 +55,7 @@ export function getTable<table extends Table>({ stash, table }: GetTableArgs<tab
decodeKey: (args: TableBoundDecodeKeyArgs<table>) => decodeKey({ stash, table, ...args }),
deleteRecord: (args: TableBoundDeleteRecordArgs<table>) => deleteRecord({ stash, table, ...args }),
encodeKey: (args: TableBoundEncodeKeyArgs<table>) => encodeKey({ table, ...args }),
getConfig: () => getConfig({ stash, table }) as table,
getTableConfig: () => getTableConfig({ stash, table }) as table,
getKeys: () => getKeys({ stash, table }),
getRecord: (args: TableBoundGetRecordArgs<table>) => getRecord({ stash, table, ...args }),
getRecords: (args?: TableBoundGetRecordsArgs<table>) => getRecords({ stash, table, ...args }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineTable } from "@latticexyz/store/config/v2";
import { describe, it } from "vitest";
import { createStash } from "../createStash";
import { attest } from "@ark/attest";
import { getConfig } from "./getConfig";
import { getTableConfig } from "./getTableConfig";
import { registerTable } from "./registerTable";

describe("getConfig", () => {
Expand Down Expand Up @@ -36,7 +36,9 @@ describe("getConfig", () => {
registerTable({ stash: stash, table: rootTable });
registerTable({ stash: stash, table: namespacedTable });

attest(getConfig({ stash: stash, table: { label: "test" } })).equals(rootTable);
attest(getConfig({ stash: stash, table: { label: "test", namespaceLabel: "namespace" } })).equals(namespacedTable);
attest(getTableConfig({ stash: stash, table: { label: "test" } })).equals(rootTable);
attest(getTableConfig({ stash: stash, table: { label: "test", namespaceLabel: "namespace" } })).equals(
namespacedTable,
);
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Table } from "@latticexyz/config";
import { Stash } from "../common";

export type GetConfigArgs = {
export type GetTableConfigArgs = {
stash: Stash;
table: { label: string; namespaceLabel?: string };
};

export type GetConfigResult<table extends Table = Table> = table;
export type GetTableConfigResult<table extends Table = Table> = table;

export function getConfig({ stash, table }: GetConfigArgs): GetConfigResult<Table> {
export function getTableConfig({ stash, table }: GetTableConfigArgs): GetTableConfigResult<Table> {
const { namespaceLabel, label } = table;
return stash.get().config[namespaceLabel ?? ""][label];
}
4 changes: 2 additions & 2 deletions packages/stash/src/actions/getTables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("getTables", () => {
decodeKey: "Function(decodeKey)",
deleteRecord: "Function(deleteRecord)",
encodeKey: "Function(encodeKey)",
getConfig: "Function(getConfig)",
getTableConfig: "Function(getTableConfig)",
getKeys: "Function(getKeys)",
getRecord: "Function(getRecord)",
getRecords: "Function(getRecords)",
Expand All @@ -53,7 +53,7 @@ describe("getTables", () => {
decodeKey: "Function(decodeKey1)",
deleteRecord: "Function(deleteRecord1)",
encodeKey: "Function(encodeKey1)",
getConfig: "Function(getConfig1)",
getTableConfig: "Function(getTableConfig1)",
getKeys: "Function(getKeys1)",
getRecord: "Function(getRecord1)",
getRecords: "Function(getRecords1)",
Expand Down
6 changes: 3 additions & 3 deletions packages/stash/src/actions/getTables.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Stash, StoreConfig, getNamespaces, getTableConfig, getNamespaceTables } from "../common";
import { Stash, StoreConfig, getNamespaces, getConfig, getNamespaceTables } from "../common";
import { BoundTable, getTable } from "./getTable";

type MutableBoundTables<config extends StoreConfig = StoreConfig> = {
-readonly [namespace in getNamespaces<config>]: {
-readonly [table in getNamespaceTables<config, namespace>]: BoundTable<getTableConfig<config, namespace, table>>;
-readonly [table in getNamespaceTables<config, namespace>]: BoundTable<getConfig<config, namespace, table>>;
};
};

export type BoundTables<config extends StoreConfig = StoreConfig> = {
[namespace in getNamespaces<config>]: {
[table in getNamespaceTables<config, namespace>]: BoundTable<getTableConfig<config, namespace, table>>;
[table in getNamespaceTables<config, namespace>]: BoundTable<getConfig<config, namespace, table>>;
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/stash/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from "./decodeKey";
export * from "./deleteRecord";
export * from "./encodeKey";
export * from "./extend";
export * from "./getConfig";
export * from "./getTableConfig";
export * from "./getKeys";
export * from "./getRecord";
export * from "./getRecords";
Expand Down
6 changes: 3 additions & 3 deletions packages/stash/src/actions/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
CommonQueryResult,
getQueryConfig,
} from "../common";
import { getConfig } from "./getConfig";
import { getTableConfig } from "./getTableConfig";
import { getRecords } from "./getRecords";

export type RunQueryOptions = CommonQueryOptions & {
Expand Down Expand Up @@ -41,11 +41,11 @@ export function runQuery<query extends Query, options extends RunQueryOptions>({
}: RunQueryArgs<query, options>): RunQueryResult<query, options> {
// Only allow fragments with matching table keys for now
// TODO: we might be able to enable this if we add something like a `keySelector`
const expectedKeySchema = getKeySchema(getConfig({ stash, table: query[0].table }));
const expectedKeySchema = getKeySchema(getTableConfig({ stash, table: query[0].table }));
for (const fragment of query) {
if (
Object.values(expectedKeySchema).join("|") !==
Object.values(getKeySchema(getConfig({ stash, table: fragment.table }))).join("|")
Object.values(getKeySchema(getTableConfig({ stash, table: fragment.table }))).join("|")
) {
throw new Error(
"All tables in a query must share the same key schema. Found mismatch when comparing tables: " +
Expand Down
4 changes: 2 additions & 2 deletions packages/stash/src/actions/subscribeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
StoreConfig,
getNamespaces,
getNamespaceTables,
getTableConfig,
getConfig,
getQueryConfig,
} from "../common";
import { decodeKey } from "./decodeKey";
Expand All @@ -26,7 +26,7 @@ export type SubscribeQueryOptions<config extends StoreConfig = StoreConfig> = Co

type QueryTableUpdates<config extends StoreConfig = StoreConfig> = {
[namespace in getNamespaces<config>]: {
[table in getNamespaceTables<config, namespace>]: TableUpdates<getTableConfig<config, namespace, table>>;
[table in getNamespaceTables<config, namespace>]: TableUpdates<getConfig<config, namespace, table>>;
};
};

Expand Down
12 changes: 6 additions & 6 deletions packages/stash/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type getNamespaceTables<
namespace extends keyof config["namespaces"],
> = keyof config["namespaces"][namespace]["tables"];

export type getTableConfig<
export type getConfig<
config extends StoreConfig,
namespace extends keyof config["namespaces"] | undefined,
table extends keyof config["namespaces"][namespace extends undefined ? "" : namespace]["tables"],
Expand Down Expand Up @@ -81,22 +81,22 @@ export type MutableTableRecords<table extends Table = Table> = { [key: string]:

export type StoreRecords<config extends StoreConfig = StoreConfig> = {
readonly [namespace in getNamespaces<config>]: {
readonly [table in getNamespaceTables<config, namespace>]: TableRecords<getTableConfig<config, namespace, table>>;
readonly [table in getNamespaceTables<config, namespace>]: TableRecords<getConfig<config, namespace, table>>;
};
};

export type MutableStoreRecords<config extends StoreConfig = StoreConfig> = {
-readonly [namespace in getNamespaces<config>]: {
-readonly [table in getNamespaceTables<config, namespace>]: MutableTableRecords<
getTableConfig<config, namespace, table>
getConfig<config, namespace, table>
>;
};
};

export type State<config extends StoreConfig = StoreConfig> = {
readonly config: {
readonly [namespace in getNamespaces<config>]: {
readonly [table in getNamespaceTables<config, namespace>]: getTableConfig<config, namespace, table>;
readonly [table in getNamespaceTables<config, namespace>]: getConfig<config, namespace, table>;
};
};
readonly records: StoreRecords<config>;
Expand All @@ -105,7 +105,7 @@ export type State<config extends StoreConfig = StoreConfig> = {
export type MutableState<config extends StoreConfig = StoreConfig> = {
config: {
-readonly [namespace in getNamespaces<config>]: {
-readonly [table in getNamespaceTables<config, namespace>]: getTableConfig<config, namespace, table>;
-readonly [table in getNamespaceTables<config, namespace>]: getConfig<config, namespace, table>;
};
};
records: MutableStoreRecords<config>;
Expand Down Expand Up @@ -136,7 +136,7 @@ export type StoreUpdates<config extends StoreConfig = StoreConfig> = {
};
records: {
[namespace in getNamespaces<config>]: {
[table in getNamespaceTables<config, namespace>]: TableUpdates<getTableConfig<config, namespace, table>>;
[table in getNamespaceTables<config, namespace>]: TableUpdates<getConfig<config, namespace, table>>;
};
} & {
[namespace: string]: {
Expand Down
6 changes: 3 additions & 3 deletions packages/stash/src/decorators/defaultActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("stash with default actions", () => {
const stash = createStash();
stash.registerTable({ table });

attest(stash.getConfig({ table: { label: "test", namespaceLabel: "namespace" } })).equals(table);
attest(stash.getTableConfig({ table: { label: "test", namespaceLabel: "namespace" } })).equals(table);
});
});

Expand Down Expand Up @@ -299,7 +299,7 @@ describe("stash with default actions", () => {
decodeKey: "Function(decodeKey)",
deleteRecord: "Function(deleteRecord)",
encodeKey: "Function(encodeKey)",
getConfig: "Function(getConfig)",
getTableConfig: "Function(getTableConfig)",
getKeys: "Function(getKeys)",
getRecord: "Function(getRecord)",
getRecords: "Function(getRecords)",
Expand All @@ -313,7 +313,7 @@ describe("stash with default actions", () => {
decodeKey: "Function(decodeKey1)",
deleteRecord: "Function(deleteRecord1)",
encodeKey: "Function(encodeKey1)",
getConfig: "Function(getConfig1)",
getTableConfig: "Function(getTableConfig1)",
getKeys: "Function(getKeys1)",
getRecord: "Function(getRecord1)",
getRecords: "Function(getRecords1)",
Expand Down
8 changes: 4 additions & 4 deletions packages/stash/src/decorators/defaultActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Query, Stash, StoreConfig } from "../common";
import { DecodeKeyArgs, DecodeKeyResult, decodeKey } from "../actions/decodeKey";
import { DeleteRecordArgs, DeleteRecordResult, deleteRecord } from "../actions/deleteRecord";
import { EncodeKeyArgs, EncodeKeyResult, encodeKey } from "../actions/encodeKey";
import { GetConfigArgs, GetConfigResult, getConfig } from "../actions/getConfig";
import { GetTableConfigArgs, GetTableConfigResult, getTableConfig } from "../actions/getTableConfig";
import { GetKeysArgs, GetKeysResult, getKeys } from "../actions/getKeys";
import { GetRecordArgs, GetRecordResult, getRecord } from "../actions/getRecord";
import { GetRecordsArgs, GetRecordsResult, getRecords } from "../actions/getRecords";
Expand All @@ -20,7 +20,7 @@ import { Table } from "@latticexyz/config";
export type StashBoundDecodeKeyArgs<table extends Table = Table> = Omit<DecodeKeyArgs<table>, "stash">;
export type StashBoundDeleteRecordArgs<table extends Table> = Omit<DeleteRecordArgs<table>, "stash">;
export type StashBoundEncodeKeyArgs<table extends Table = Table> = EncodeKeyArgs<table>;
export type StashBoundGetConfigArgs = Omit<GetConfigArgs, "stash">;
export type StashBoundGetTableConfigArgs = Omit<GetTableConfigArgs, "stash">;
export type StashBoundGetKeysArgs<table extends Table = Table> = Omit<GetKeysArgs<table>, "stash">;
export type StashBoundGetRecordArgs<table extends Table = Table> = Omit<GetRecordArgs<table>, "stash">;
export type StashBoundGetRecordsArgs<table extends Table = Table> = Omit<GetRecordsArgs<table>, "stash">;
Expand All @@ -43,7 +43,7 @@ export type DefaultActions<config extends StoreConfig = StoreConfig> = {
decodeKey: <table extends Table>(args: StashBoundDecodeKeyArgs<table>) => DecodeKeyResult<table>;
deleteRecord: <table extends Table>(args: StashBoundDeleteRecordArgs<table>) => DeleteRecordResult;
encodeKey: <table extends Table>(args: StashBoundEncodeKeyArgs<table>) => EncodeKeyResult;
getConfig: (args: StashBoundGetConfigArgs) => GetConfigResult;
getTableConfig: (args: StashBoundGetTableConfigArgs) => GetTableConfigResult;
getKeys: <table extends Table>(args: StashBoundGetKeysArgs<table>) => GetKeysResult<table>;
getRecord: <table extends Table>(args: StashBoundGetRecordArgs<table>) => GetRecordResult<table>;
getRecords: <table extends Table>(args: StashBoundGetRecordsArgs<table>) => GetRecordsResult<table>;
Expand All @@ -65,7 +65,7 @@ export function defaultActions<config extends StoreConfig>(stash: Stash<config>)
decodeKey: <table extends Table>(args: StashBoundDecodeKeyArgs<table>) => decodeKey({ stash, ...args }),
deleteRecord: <table extends Table>(args: StashBoundDeleteRecordArgs<table>) => deleteRecord({ stash, ...args }),
encodeKey: <table extends Table>(args: StashBoundEncodeKeyArgs<table>) => encodeKey(args),
getConfig: (args: StashBoundGetConfigArgs) => getConfig({ stash, ...args }),
getTableConfig: (args: StashBoundGetTableConfigArgs) => getTableConfig({ stash, ...args }),
getKeys: <table extends Table>(args: StashBoundGetKeysArgs<table>) => getKeys({ stash, ...args }),
getRecord: <table extends Table>(args: StashBoundGetRecordArgs<table>) => getRecord({ stash, ...args }),
getRecords: <table extends Table>(args: StashBoundGetRecordsArgs<table>) => getRecords({ stash, ...args }),
Expand Down

0 comments on commit 62f9cf0

Please sign in to comment.