From dc850b23d353725963c821297acdef4bf86619fe Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Thu, 14 Mar 2024 21:48:34 +0000 Subject: [PATCH] feat(query,store-sync): pass through subject schema (#2441) --- packages/query/src/api.ts | 5 +- packages/query/src/findSubjects.ts | 2 + .../store-sync/src/query-cache/query.test.ts | 40 ++++ .../store-sync/src/query-cache/queryToWire.ts | 3 + .../src/query-cache/subscribeToQuery.test.ts | 179 ++++++++++++++++++ .../src/query-cache/subscribeToQuery.ts | 1 + 6 files changed, 229 insertions(+), 1 deletion(-) diff --git a/packages/query/src/api.ts b/packages/query/src/api.ts index 01818e866b..ecc5c37d5f 100644 --- a/packages/query/src/api.ts +++ b/packages/query/src/api.ts @@ -1,5 +1,5 @@ import { Hex } from "viem"; -import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type"; +import { StaticPrimitiveType, DynamicPrimitiveType, SchemaAbiType } from "@latticexyz/schema-type"; import { satisfy } from "@latticexyz/common/type-utils"; import { SchemaToPrimitives } from "@latticexyz/store"; import { Table } from "@latticexyz/store/config/v2"; @@ -58,15 +58,18 @@ export type ResultRecord = { }; export type Subject = readonly PrimitiveType[]; +export type SubjectSchema = readonly SchemaAbiType[]; export type SubjectRecords = { readonly subject: Subject; + readonly subjectSchema: SubjectSchema; readonly records: readonly ResultRecord[]; }; // TODO: consider flattening this to be more like `ResultRecord & { subject: Subject }` export type SubjectRecord = { readonly subject: Subject; + readonly subjectSchema: SubjectSchema; readonly record: ResultRecord; }; diff --git a/packages/query/src/findSubjects.ts b/packages/query/src/findSubjects.ts index 40e2ec4543..a060426c05 100644 --- a/packages/query/src/findSubjects.ts +++ b/packages/query/src/findSubjects.ts @@ -49,6 +49,7 @@ export function findSubjects({ .map((records) => ({ subjectId: records[0].subjectId, subject: records[0].subject, + subjectSchema: records[0].subjectSchema.map((abiType) => abiType.type), records, })) .filter(({ records }) => { @@ -75,6 +76,7 @@ export function findSubjects
({ const subjects = matchedSubjects.map((match) => ({ subject: match.subject, + subjectSchema: match.subjectSchema, records: match.records.map((record) => ({ tableId: record.table.tableId, primaryKey: record.primaryKey, diff --git a/packages/store-sync/src/query-cache/query.test.ts b/packages/store-sync/src/query-cache/query.test.ts index e5c332eec0..4f781426a0 100644 --- a/packages/store-sync/src/query-cache/query.test.ts +++ b/packages/store-sync/src/query-cache/query.test.ts @@ -41,6 +41,9 @@ describe("query", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -62,6 +65,9 @@ describe("query", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -83,6 +89,9 @@ describe("query", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -104,6 +113,9 @@ describe("query", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], }, ], } @@ -145,6 +157,9 @@ describe("query", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -166,6 +181,9 @@ describe("query", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], } @@ -209,6 +227,9 @@ describe("query", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -230,6 +251,9 @@ describe("query", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -251,6 +275,9 @@ describe("query", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], } @@ -289,6 +316,9 @@ describe("query", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -309,6 +339,9 @@ describe("query", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, ], } @@ -350,6 +383,10 @@ describe("query", async () => { 3, 5, ], + "subjectSchema": [ + "int32", + "int32", + ], }, ], } @@ -390,6 +427,9 @@ describe("query", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], }, ], } diff --git a/packages/store-sync/src/query-cache/queryToWire.ts b/packages/store-sync/src/query-cache/queryToWire.ts index 0bfd7bc7ad..5ac31030c6 100644 --- a/packages/store-sync/src/query-cache/queryToWire.ts +++ b/packages/store-sync/src/query-cache/queryToWire.ts @@ -24,6 +24,9 @@ export function queryToWire>( const left = leftTableField; const [tableName, fieldName] = left.split("."); const table = tables[tableName]; + if (op === "in") { + return { left: { tableId: table.tableId, field: fieldName }, op, right }; + } return { left: { tableId: table.tableId, field: fieldName }, op, right }; }); diff --git a/packages/store-sync/src/query-cache/subscribeToQuery.test.ts b/packages/store-sync/src/query-cache/subscribeToQuery.test.ts index 53c304d55b..8d57edb5a7 100644 --- a/packages/store-sync/src/query-cache/subscribeToQuery.test.ts +++ b/packages/store-sync/src/query-cache/subscribeToQuery.test.ts @@ -62,6 +62,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -82,6 +85,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -102,6 +108,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -122,6 +131,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -147,6 +159,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -166,6 +181,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -185,6 +203,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -204,6 +225,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -245,6 +269,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -270,6 +297,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -289,6 +319,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -308,6 +341,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -327,6 +363,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -346,6 +385,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -400,6 +442,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -420,6 +465,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -445,6 +493,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -464,6 +515,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -505,6 +559,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -530,6 +587,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -549,6 +609,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -568,6 +631,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -609,6 +675,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "exit", }, ], @@ -634,6 +703,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -653,6 +725,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -709,6 +784,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -729,6 +807,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -749,6 +830,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -774,6 +858,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -793,6 +880,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -812,6 +902,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -853,6 +946,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -878,6 +974,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -897,6 +996,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -916,6 +1018,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -935,6 +1040,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -976,6 +1084,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "exit", }, ], @@ -1001,6 +1112,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -1020,6 +1134,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -1039,6 +1156,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -1090,6 +1210,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, { @@ -1109,6 +1232,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -1133,6 +1259,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -1151,6 +1280,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -1205,6 +1337,10 @@ describe("subscribeToQuery", async () => { 3, 5, ], + "subjectSchema": [ + "int32", + "int32", + ], "type": "enter", }, ], @@ -1233,6 +1369,10 @@ describe("subscribeToQuery", async () => { 3, 5, ], + "subjectSchema": [ + "int32", + "int32", + ], }, ], }, @@ -1286,6 +1426,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -1311,6 +1454,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0xdBa86119a787422C593ceF119E40887f396024E2", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -1390,6 +1536,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -1415,6 +1564,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -1457,6 +1609,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "records": [ @@ -1478,6 +1633,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ] `); @@ -1533,6 +1691,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "enter", }, ], @@ -1558,6 +1719,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -1577,6 +1741,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -1596,6 +1763,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], }, ], }, @@ -1641,6 +1811,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x5f2cC8fb10299751348e1b10f5F1Ba47820B1cB8", ], + "subjectSchema": [ + "address", + ], "type": "exit", }, ], @@ -1666,6 +1839,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6", ], + "subjectSchema": [ + "address", + ], }, { "record": { @@ -1685,6 +1861,9 @@ describe("subscribeToQuery", async () => { "subject": [ "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb", ], + "subjectSchema": [ + "address", + ], }, ], }, diff --git a/packages/store-sync/src/query-cache/subscribeToQuery.ts b/packages/store-sync/src/query-cache/subscribeToQuery.ts index 53af633764..8841e51e9e 100644 --- a/packages/store-sync/src/query-cache/subscribeToQuery.ts +++ b/packages/store-sync/src/query-cache/subscribeToQuery.ts @@ -13,6 +13,7 @@ function flattenSubjectRecords(subjects: readonly SubjectRecords[]): readonly Su return subjects.flatMap((subject) => subject.records.map((record) => ({ subject: subject.subject, + subjectSchema: subject.subjectSchema, record, })), );