Skip to content

Commit

Permalink
feat(store): add getKeySchema and getValueSchema helpers (#2475)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs authored Mar 19, 2024
1 parent 0a1fff9 commit fff2a53
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 491 deletions.
13 changes: 8 additions & 5 deletions packages/store/ts/config/v2/compat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { conform } from "@arktype/util";
import { Store, Table } from "./output";
import { Schema, Store, Table } from "./output";
import { mapObject } from "@latticexyz/common/utils";
import { getKeySchema, getValueSchema } from "./table";

export type storeToV1<store> = store extends Store
? {
Expand All @@ -20,13 +21,15 @@ export type storeToV1<store> = store extends Store
}
: never;

type schemaToV1<schema extends Schema> = { [key in keyof schema]: schema[key]["internalType"] };

export type tableToV1<table extends Table> = {
directory: table["codegen"]["directory"];
dataStruct: table["codegen"]["dataStruct"];
tableIdArgument: table["codegen"]["tableIdArgument"];
storeArgument: table["codegen"]["storeArgument"];
keySchema: { [key in keyof table["keySchema"]]: table["keySchema"][key]["internalType"] };
valueSchema: { [key in keyof table["valueSchema"]]: table["valueSchema"][key]["internalType"] };
keySchema: schemaToV1<getKeySchema<table>>;
valueSchema: schemaToV1<getValueSchema<table>>;
offchainOnly: Table extends table ? boolean : table["type"] extends "table" ? false : true;
name: table["name"];
};
Expand All @@ -42,8 +45,8 @@ export function storeToV1<store>(store: conform<store, Store>): storeToV1<store>
dataStruct: table.codegen.dataStruct,
tableIdArgument: table.codegen.tableIdArgument,
storeArgument: table.codegen.storeArgument,
keySchema: mapObject(table.keySchema, (field) => field.internalType),
valueSchema: mapObject(table.valueSchema, (field) => field.internalType),
keySchema: mapObject(getKeySchema(table), (field) => field.internalType),
valueSchema: mapObject(getValueSchema(table), (field) => field.internalType),
offchainOnly: table.type === "offchainTable",
name: table.name,
}));
Expand Down
13 changes: 1 addition & 12 deletions packages/store/ts/config/v2/output.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbiType, StaticAbiType, Schema, Table as BaseTable } from "@latticexyz/config";
import { AbiType, Schema, Table as BaseTable } from "@latticexyz/config";

export type { AbiType, Schema };

Expand All @@ -10,15 +10,6 @@ export type Enums = {
readonly [enumName: string]: readonly [string, ...string[]];
};

export type KeySchema = {
readonly [keyName: string]: {
/** the Solidity primitive ABI type */
readonly type: StaticAbiType;
/** the user defined type or Solidity primitive ABI type */
readonly internalType: string;
};
};

export type TableCodegen = {
readonly directory: string;
readonly tableIdArgument: boolean;
Expand All @@ -27,8 +18,6 @@ export type TableCodegen = {
};

export type Table = BaseTable & {
readonly keySchema: KeySchema;
readonly valueSchema: Schema;
readonly codegen: TableCodegen;
};

Expand Down
128 changes: 0 additions & 128 deletions packages/store/ts/config/v2/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@ describe("defineStore", () => {
internalType: "uint256",
},
},
keySchema: {
age: {
type: "uint256",
internalType: "uint256",
},
},
valueSchema: {
id: {
type: "address",
internalType: "address",
},
name: {
type: "string",
internalType: "string",
},
},
key: ["age"],
name: "Example",
namespace: "",
Expand Down Expand Up @@ -98,22 +82,6 @@ describe("defineStore", () => {
internalType: "static",
},
},
keySchema: {
age: {
type: "address",
internalType: "static",
},
},
valueSchema: {
id: {
type: "string",
internalType: "dynamic",
},
name: {
type: "string",
internalType: "string",
},
},
key: ["age"],
name: "Example",
namespace: "",
Expand Down Expand Up @@ -161,22 +129,6 @@ describe("defineStore", () => {
internalType: "uint256",
},
},
keySchema: {
age: {
type: "uint256",
internalType: "uint256",
},
id: {
type: "address",
internalType: "address",
},
},
valueSchema: {
name: {
type: "string",
internalType: "string",
},
},
key: ["age", "id"],
name: "Example",
namespace: "",
Expand Down Expand Up @@ -225,22 +177,6 @@ describe("defineStore", () => {
internalType: "uint256",
},
},
keySchema: {
firstKey: {
type: "address",
internalType: "address",
},
firstAge: {
type: "uint256",
internalType: "uint256",
},
},
valueSchema: {
firstName: {
type: "string",
internalType: "string",
},
},
key: ["firstKey", "firstAge"],
name: "First",
namespace: "",
Expand All @@ -263,22 +199,6 @@ describe("defineStore", () => {
internalType: "uint256",
},
},
keySchema: {
secondKey: {
type: "address",
internalType: "address",
},
secondAge: {
type: "uint256",
internalType: "uint256",
},
},
valueSchema: {
secondName: {
type: "string",
internalType: "string",
},
},
key: ["secondKey", "secondAge"],
name: "Second",
namespace: "",
Expand Down Expand Up @@ -331,22 +251,6 @@ describe("defineStore", () => {
internalType: "uint256",
},
},
keySchema: {
firstKey: {
type: "address",
internalType: "Static",
},
firstAge: {
type: "uint256",
internalType: "uint256",
},
},
valueSchema: {
firstName: {
type: "string",
internalType: "Dynamic",
},
},
key: ["firstKey", "firstAge"],
name: "First",
namespace: "",
Expand All @@ -369,22 +273,6 @@ describe("defineStore", () => {
internalType: "uint256",
},
},
keySchema: {
secondKey: {
type: "address",
internalType: "Static",
},
secondAge: {
type: "uint256",
internalType: "uint256",
},
},
valueSchema: {
secondName: {
type: "string",
internalType: "Dynamic",
},
},
key: ["secondKey", "secondAge"],
name: "Second",
namespace: "",
Expand Down Expand Up @@ -493,22 +381,6 @@ describe("defineStore", () => {
internalType: "static",
},
},
keySchema: {
name: {
type: "uint8",
internalType: "ValidNames",
},
},
valueSchema: {
age: {
type: "address",
internalType: "static",
},
id: {
type: "string",
internalType: "dynamic",
},
},
key: ["name"],
name: "Example",
namespace: "",
Expand Down
56 changes: 0 additions & 56 deletions packages/store/ts/config/v2/storeWithShorthands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ describe("defineStoreWithShorthands", () => {
internalType: "address",
},
},
keySchema: {
id: {
type: "bytes32",
internalType: "bytes32",
},
},
valueSchema: {
value: {
type: "address",
internalType: "address",
},
},
key: ["id"],
name: "Name",
namespace: "",
Expand Down Expand Up @@ -69,18 +57,6 @@ describe("defineStoreWithShorthands", () => {
internalType: "CustomType",
},
},
keySchema: {
id: {
type: "bytes32",
internalType: "bytes32",
},
},
valueSchema: {
value: {
type: "address",
internalType: "CustomType",
},
},
key: ["id"],
name: "Name",
namespace: "",
Expand Down Expand Up @@ -120,22 +96,6 @@ describe("defineStoreWithShorthands", () => {
internalType: "uint256",
},
},
keySchema: {
id: {
type: "address",
internalType: "address",
},
},
valueSchema: {
name: {
type: "string",
internalType: "string",
},
age: {
type: "uint256",
internalType: "uint256",
},
},
key: ["id"],
name: "Example",
namespace: "",
Expand Down Expand Up @@ -175,22 +135,6 @@ describe("defineStoreWithShorthands", () => {
internalType: "uint256",
},
},
keySchema: {
id: {
type: "address",
internalType: "address",
},
},
valueSchema: {
name: {
type: "string",
internalType: "string",
},
age: {
type: "uint256",
internalType: "uint256",
},
},
key: ["id"],
name: "Example",
namespace: "",
Expand Down
Loading

0 comments on commit fff2a53

Please sign in to comment.