-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(query,store-sync): rework query types, refactor query client (…
- Loading branch information
Showing
15 changed files
with
860 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,86 @@ | ||
import { Hex } from "viem"; | ||
import { StaticPrimitiveType, DynamicPrimitiveType } from "@latticexyz/schema-type"; | ||
import { satisfy } from "@latticexyz/common/type-utils"; | ||
import { SchemaToPrimitives } from "@latticexyz/store"; | ||
import { Table } from "@latticexyz/store/config/v2"; | ||
|
||
/** | ||
* These types represent the "over the wire" protocol (i.e. JSON) for the query API. | ||
* | ||
* Currently always returns matching records for each subject. We may add separate endpoints and return types for just subjects later. | ||
*/ | ||
|
||
export type TableField = { | ||
// TODO: decide if we want to support stronger types here (e.g. table generic that constrains subjects, records, etc.) | ||
// TODO: decide if/how we want to add block number throughout (esp as it relates to instant sequencing) | ||
// TODO: separate set of types for querying just | ||
|
||
export type QueryTable = { | ||
readonly tableId: Hex; | ||
readonly field: string; | ||
}; | ||
|
||
export type TableSubject = { | ||
export type QuerySubject = { | ||
readonly tableId: Hex; | ||
readonly subject: readonly string[]; | ||
}; | ||
|
||
export type ConditionLiteral = boolean | number | bigint | string; | ||
// TODO: should we exclude arrays? might be hard to support array comparisons in SQL | ||
export type ConditionLiteral = StaticPrimitiveType | DynamicPrimitiveType; | ||
|
||
export type ComparisonCondition = { | ||
readonly left: TableField; | ||
readonly left: QueryTable; | ||
readonly op: "<" | "<=" | "=" | ">" | ">=" | "!="; | ||
// TODO: add support for TableField | ||
// TODO: add support for QueryTable | ||
readonly right: ConditionLiteral; | ||
}; | ||
|
||
export type InCondition = { | ||
readonly left: TableField; | ||
readonly left: QueryTable; | ||
readonly op: "in"; | ||
readonly right: readonly ConditionLiteral[]; | ||
}; | ||
|
||
export type QueryCondition = satisfy<{ readonly op: string }, ComparisonCondition | InCondition>; | ||
|
||
export type Query = { | ||
readonly from: readonly TableSubject[]; | ||
readonly except?: readonly TableSubject[]; | ||
readonly from: readonly QuerySubject[]; | ||
readonly except?: readonly QuerySubject[]; | ||
readonly where?: readonly QueryCondition[]; | ||
}; | ||
|
||
export type QueryResultSubject = readonly (StaticPrimitiveType | DynamicPrimitiveType)[]; | ||
export type PrimitiveType = StaticPrimitiveType | DynamicPrimitiveType; | ||
|
||
export type ResultRecord = { | ||
readonly tableId: Hex; | ||
readonly keyTuple: readonly Hex[]; | ||
readonly primaryKey: readonly StaticPrimitiveType[]; | ||
readonly fields: SchemaToPrimitives<Table["schema"]>; | ||
}; | ||
|
||
export type Subject = readonly PrimitiveType[]; | ||
|
||
export type SubjectRecords = { | ||
readonly subject: Subject; | ||
readonly records: readonly ResultRecord[]; | ||
}; | ||
|
||
// TODO: consider flattening this to be more like `ResultRecord & { subject: Subject }` | ||
export type SubjectRecord = { | ||
readonly subject: Subject; | ||
readonly record: ResultRecord; | ||
}; | ||
|
||
// TODO: for change event, should this include previous record? | ||
// TODO: use merge helper instead of `&` intersection? | ||
export type SubjectEvent = SubjectRecord & { | ||
/** | ||
* `enter` = a new subject+record pair matched | ||
* `exit` = a subject+record pair no longer matches | ||
* `change` = the record oft he subject+record pair changed | ||
*/ | ||
readonly type: "enter" | "exit" | "change"; | ||
}; | ||
|
||
export type QueryResult = { | ||
subjects: readonly QueryResultSubject[]; | ||
// TODO: matched records | ||
// TODO: block number | ||
export type Result = { | ||
readonly subjects: readonly SubjectRecords[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { Table, Schema } from "@latticexyz/store/config/v2"; | ||
import { StaticPrimitiveType } from "@latticexyz/schema-type"; | ||
import { SchemaToPrimitives } from "@latticexyz/store"; | ||
import { Table } from "@latticexyz/store/config/v2"; | ||
import { Hex } from "viem"; | ||
|
||
export type schemaAbiTypes<schema extends Schema> = { | ||
[key in keyof schema]: schema[key]["type"]; | ||
}; | ||
|
||
export type TableRecord<table extends Table = Table> = { | ||
export type TableRecord<table extends Table> = { | ||
readonly table: table; | ||
readonly fields: schemaAbiTypes<table["schema"]>; | ||
// TODO: refine to just static types | ||
// TODO: add helper to extract primary key of primitive types from table primary key + field values | ||
readonly primaryKey: readonly StaticPrimitiveType[]; | ||
readonly keyTuple: readonly Hex[]; | ||
readonly fields: SchemaToPrimitives<table["schema"]>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./api"; | ||
export * from "./findSubjects"; | ||
export * from "./matchesCondition"; | ||
export * from "./matchRecords"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.