Skip to content

Commit

Permalink
refactor(store-cache): use table-specific namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a committed May 31, 2023
1 parent d4b3cee commit d8c11b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/store-cache/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ export type FilterOptions<C extends StoreConfig = StoreConfig, T extends keyof C

export type Update<C extends StoreConfig = StoreConfig, Table extends keyof C["tables"] = keyof C["tables"]> = {
[key in Table]: {
namespace: C["namespace"];
namespace: C["tables"][Table]["namespace"];
table: key;
set: KeyValue<C, key>[];
remove: { key: Key<C, key> }[];
};
}[Table];

export type ScanResult<C extends StoreConfig = StoreConfig, T extends keyof C["tables"] = keyof C["tables"]> = Array<
KeyValue<C, T> & { namespace: C["namespace"]; table: T }
KeyValue<C, T> & { namespace: C["tables"][T]["namespace"]; table: T }
>;
18 changes: 15 additions & 3 deletions packages/store-cache/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ describe("utils", () => {

expect(rows.length).toBe(positionUpdates.length + multiKeyUpdates.length);
expect(rows).toEqual([
...multiKeyUpdates.map((row) => ({ ...row, namespace: config["namespace"], table: "MultiKey" })),
...positionUpdates.map((row) => ({ ...row, namespace: config["namespace"], table: "Position" })),
...multiKeyUpdates.map((row) => ({
...row,
namespace: config.tables["MultiKey"].namespace,
table: "MultiKey",
})),
...positionUpdates.map((row) => ({
...row,
namespace: config.tables["Position"].namespace,
table: "Position",
})),
]);
});

Expand All @@ -72,7 +80,11 @@ describe("utils", () => {

expect(rows.length).toBe(positionUpdates.length);
expect(rows).toEqual([
...positionUpdates.map((row) => ({ ...row, namespace: config["namespace"], table: "Position" })),
...positionUpdates.map((row) => ({
...row,
namespace: config.tables["Position"].namespace,
table: "Position",
})),
]);
});

Expand Down
13 changes: 7 additions & 6 deletions packages/store-cache/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { getAbiTypeDefaultValue } from "@latticexyz/schema-type";
export function set<C extends StoreConfig, T extends keyof C["tables"] = keyof C["tables"]>(
config: C,
client: TupleDatabaseClient,
namespace: C["namespace"],
namespace: C["tables"][T]["namespace"],
table: T & string,
key: Key<C, T>,
value: Partial<Value<C, T>>,
Expand All @@ -53,7 +53,7 @@ export function set<C extends StoreConfig, T extends keyof C["tables"] = keyof C
export function get<C extends StoreConfig, T extends keyof C["tables"] = keyof C["tables"]>(
config: C,
client: TupleDatabaseClient,
namespace: C["namespace"],
namespace: C["tables"][T]["namespace"],
table: T & string,
key: Key<C, T>
): Value<C, T> {
Expand All @@ -74,7 +74,7 @@ export function get<C extends StoreConfig, T extends keyof C["tables"] = keyof C
export function remove<C extends StoreConfig, T extends keyof C["tables"] = keyof C["tables"]>(
config: C,
client: TupleDatabaseClient,
namespace: C["namespace"],
namespace: C["tables"][T]["namespace"],
table: T & string,
key: Key<C, T>,
options?: RemoveOptions
Expand All @@ -96,7 +96,7 @@ export function scan<C extends StoreConfig = StoreConfig, T extends keyof C["tab
return results.map(
({ key, value }) =>
({ namespace: key[0], table: key[1], key: tupleToRecord(key), value } as KeyValue<C, T> & {
namespace: C["namespace"];
namespace: C["tables"][T]["namespace"];
table: T;
})
);
Expand Down Expand Up @@ -181,7 +181,8 @@ function getScanArgsFromFilter<C extends StoreConfig, T extends keyof C["tables"
) {
const { table, key } = filter || {};
// Default to the config namespace if a filter without namespace is provided
const namespace = filter ? filter.namespace ?? config.namespace : undefined;
const configNamespace = table && config.tables[table] ? config.tables[table].namespace : config.namespace;
const namespace = filter ? filter.namespace ?? configNamespace : undefined;

const prefix = table != null && namespace != null ? [namespace, table] : undefined;
const scanArgs: ScanArgs<Tuple, Tuple> = {};
Expand All @@ -208,7 +209,7 @@ function getScanArgsFromFilter<C extends StoreConfig, T extends keyof C["tables"
*/
function databaseKey<C extends StoreConfig, T extends keyof C["tables"] = keyof C["tables"]>(
config: C,
namespace: C["namespace"],
namespace: C["tables"][T]["namespace"],
table: T & string,
key: Key<C, T>
) {
Expand Down

0 comments on commit d8c11b5

Please sign in to comment.