Skip to content

Commit

Permalink
fix: store-sync uses getKeySchema helper (#2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaaa authored Mar 20, 2024
1 parent fff2a53 commit e3f0abe
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/store-sync/src/query-cache/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, Table } from "@latticexyz/store/config/v2";
import { Store, Table } from "@latticexyz/store/config/v2";
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type/internal";
import { ComparisonCondition, InCondition } from "@latticexyz/query";

Expand All @@ -10,7 +10,7 @@ export type subjectSchemaToPrimitive<tuple> = {
[key in keyof tuple]: tuple[key] extends SchemaAbiType ? SchemaAbiTypeToPrimitiveType<tuple[key]> : never;
};

export type Tables = Config["tables"];
export type Tables = Store["tables"];

export type TableSubjectItem<table extends Table = Table> = keyof table["schema"];

Expand Down
5 changes: 3 additions & 2 deletions packages/store-sync/src/query-cache/createStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser/internal
import { flattenSchema } from "../flattenSchema";
import debug from "debug";
import { Tables } from "./common";
import { getKeySchema, getValueSchema } from "@latticexyz/store/config/v2";

function getRecordId({ tableId, keyTuple }: { tableId: Hex; keyTuple: readonly Hex[] }): string {
return `${tableId}:${concatHex(keyTuple)}`;
Expand Down Expand Up @@ -109,8 +110,8 @@ export function createStorageAdapter({ store }: CreateStorageAdapterOptions<Quer
const records: readonly TableRecord[] = [
...previousRecords.filter((record) => !touchedIds.has(record.id)),
...Object.values(updatedRawRecords).map((rawRecord): TableRecord => {
const key = decodeKey(flattenSchema(rawRecord.table.keySchema), rawRecord.keyTuple);
const value = decodeValueArgs(flattenSchema(rawRecord.table.valueSchema), rawRecord);
const key = decodeKey(flattenSchema(getKeySchema(rawRecord.table)), rawRecord.keyTuple);
const value = decodeValueArgs(flattenSchema(getValueSchema(rawRecord.table)), rawRecord);

return {
table: rawRecord.table,
Expand Down
6 changes: 3 additions & 3 deletions packages/store-sync/src/query-cache/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StoreApi, UseBoundStore, create } from "zustand";
import { Table } from "@latticexyz/store/config/v2";
import { Table, getKeySchema, getValueSchema } from "@latticexyz/store/config/v2";
import { Tables } from "./common";
import { Hex } from "viem";
import { StaticPrimitiveType } from "@latticexyz/schema-type/internal";
Expand All @@ -21,8 +21,8 @@ export type TableRecord<table extends Table = Table> = {
readonly id: string;
readonly keyTuple: readonly Hex[];
readonly primaryKey: readonly StaticPrimitiveType[];
readonly key: SchemaToPrimitives<table["keySchema"]>;
readonly value: SchemaToPrimitives<table["valueSchema"]>;
readonly key: SchemaToPrimitives<getKeySchema<table>>;
readonly value: SchemaToPrimitives<getValueSchema<table>>;
readonly fields: SchemaToPrimitives<table["schema"]>;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/query-cache/getTables.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { resourceToHex } from "@latticexyz/common";
import { Config } from "@latticexyz/store/config/v2";
import { Store } from "@latticexyz/store/config/v2";

// TODO(alvrs): table resolver doesn't yet provide `tableId` so we'll use this helper to inject it for now

export function getTables<config extends Config>(config: config): config["tables"] {
export function getTables<config extends Store>(config: config): config["tables"] {
const tables = Object.fromEntries(
Object.entries(config.tables).map(([tableName, table]) => [
tableName,
Expand Down
6 changes: 3 additions & 3 deletions packages/store-sync/src/query-cache/syncToQueryCache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SyncOptions, SyncResult } from "../common";
import { createStoreSync } from "../createStoreSync";
import { Address } from "viem";
import { Config } from "@latticexyz/store/config/v2";
import { Store } from "@latticexyz/store/config/v2";
import { createStore } from "./createStore";
import { createStorageAdapter } from "./createStorageAdapter";
import { getTables } from "./getTables";

type SyncToQueryCacheOptions<config extends Config> = Omit<SyncOptions, "config"> & {
type SyncToQueryCacheOptions<config extends Store> = Omit<SyncOptions, "config"> & {
// require address for now to keep the data model + retrieval simpler
address: Address;
config: config;
Expand All @@ -17,7 +17,7 @@ type SyncToQueryCacheResult = SyncResult & {
stopSync: () => void;
};

export async function syncToQueryCache<config extends Config>({
export async function syncToQueryCache<config extends Store>({
config,
startSync = true,
...syncOptions
Expand Down

0 comments on commit e3f0abe

Please sign in to comment.