Skip to content

Commit

Permalink
feat(query,store-sync): pass through subject schema (#2441)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Mar 14, 2024
1 parent 7346efb commit dc850b2
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/query/src/api.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
};

Expand Down
2 changes: 2 additions & 0 deletions packages/query/src/findSubjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function findSubjects<table extends Table>({
.map((records) => ({
subjectId: records[0].subjectId,
subject: records[0].subject,
subjectSchema: records[0].subjectSchema.map((abiType) => abiType.type),
records,
}))
.filter(({ records }) => {
Expand All @@ -75,6 +76,7 @@ export function findSubjects<table extends Table>({

const subjects = matchedSubjects.map((match) => ({
subject: match.subject,
subjectSchema: match.subjectSchema,
records: match.records.map((record) => ({
tableId: record.table.tableId,
primaryKey: record.primaryKey,
Expand Down
40 changes: 40 additions & 0 deletions packages/store-sync/src/query-cache/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ describe("query", async () => {
"subject": [
"0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -62,6 +65,9 @@ describe("query", async () => {
"subject": [
"0x328809Bc894f92807417D2dAD6b7C998c1aFdac6",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -83,6 +89,9 @@ describe("query", async () => {
"subject": [
"0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -104,6 +113,9 @@ describe("query", async () => {
"subject": [
"0xdBa86119a787422C593ceF119E40887f396024E2",
],
"subjectSchema": [
"address",
],
},
],
}
Expand Down Expand Up @@ -145,6 +157,9 @@ describe("query", async () => {
"subject": [
"0x328809Bc894f92807417D2dAD6b7C998c1aFdac6",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -166,6 +181,9 @@ describe("query", async () => {
"subject": [
"0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb",
],
"subjectSchema": [
"address",
],
},
],
}
Expand Down Expand Up @@ -209,6 +227,9 @@ describe("query", async () => {
"subject": [
"0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -230,6 +251,9 @@ describe("query", async () => {
"subject": [
"0x328809Bc894f92807417D2dAD6b7C998c1aFdac6",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -251,6 +275,9 @@ describe("query", async () => {
"subject": [
"0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb",
],
"subjectSchema": [
"address",
],
},
],
}
Expand Down Expand Up @@ -289,6 +316,9 @@ describe("query", async () => {
"subject": [
"0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e",
],
"subjectSchema": [
"address",
],
},
{
"records": [
Expand All @@ -309,6 +339,9 @@ describe("query", async () => {
"subject": [
"0x328809Bc894f92807417D2dAD6b7C998c1aFdac6",
],
"subjectSchema": [
"address",
],
},
],
}
Expand Down Expand Up @@ -350,6 +383,10 @@ describe("query", async () => {
3,
5,
],
"subjectSchema": [
"int32",
"int32",
],
},
],
}
Expand Down Expand Up @@ -390,6 +427,9 @@ describe("query", async () => {
"subject": [
"0xdBa86119a787422C593ceF119E40887f396024E2",
],
"subjectSchema": [
"address",
],
},
],
}
Expand Down
3 changes: 3 additions & 0 deletions packages/store-sync/src/query-cache/queryToWire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function queryToWire<tables extends Tables, query extends Query<tables>>(
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 };
});

Expand Down
Loading

0 comments on commit dc850b2

Please sign in to comment.