From 2a4b2891482bcba1668064e70ee8b3b4691d0969 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Fri, 15 Mar 2024 21:45:32 +0000 Subject: [PATCH] refactor: primaryKey -> key (#2449) --- packages/config/src/common.ts | 2 +- packages/store/ts/config/v2/compat.test.ts | 6 +- packages/store/ts/config/v2/store.test.ts | 70 +++++++-------- packages/store/ts/config/v2/table.test.ts | 50 +++++------ packages/store/ts/config/v2/table.ts | 8 +- packages/store/ts/config/v2/tableFull.ts | 44 +++++----- .../store/ts/config/v2/tableShorthand.test.ts | 28 +++--- packages/store/ts/config/v2/tableShorthand.ts | 12 ++- packages/world/ts/config/v2/world.test.ts | 86 +++++++++---------- test/mock-game-contracts/mud.config.ts | 12 +-- 10 files changed, 156 insertions(+), 162 deletions(-) diff --git a/packages/config/src/common.ts b/packages/config/src/common.ts index b2aeae528c..639fe84fee 100644 --- a/packages/config/src/common.ts +++ b/packages/config/src/common.ts @@ -28,6 +28,6 @@ export type Table = { readonly name: string; readonly namespace: string; readonly tableId: Hex; - readonly primaryKey: readonly string[]; readonly schema: Schema; + readonly key: readonly string[]; }; diff --git a/packages/store/ts/config/v2/compat.test.ts b/packages/store/ts/config/v2/compat.test.ts index f7cb8ad97e..7895f20946 100644 --- a/packages/store/ts/config/v2/compat.test.ts +++ b/packages/store/ts/config/v2/compat.test.ts @@ -67,14 +67,14 @@ describe("configToV1", () => { x: "int32", y: "int32", }, - primaryKey: ["player"], + key: ["player"], }, Health: { schema: { player: "CustomAddress", health: "uint256", }, - primaryKey: ["player"], + key: ["player"], }, Terrain: { schema: { @@ -82,7 +82,7 @@ describe("configToV1", () => { y: "int32", terrainType: "TerrainType", }, - primaryKey: ["x", "y"], + key: ["x", "y"], }, }, }); diff --git a/packages/store/ts/config/v2/store.test.ts b/packages/store/ts/config/v2/store.test.ts index 93c83c32a7..68b6baba70 100644 --- a/packages/store/ts/config/v2/store.test.ts +++ b/packages/store/ts/config/v2/store.test.ts @@ -34,7 +34,7 @@ describe("resolveStoreConfig", () => { internalType: "address", }, }, - primaryKey: ["id"], + key: ["id"], name: "Name", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -81,7 +81,7 @@ describe("resolveStoreConfig", () => { internalType: "CustomType", }, }, - primaryKey: ["id"], + key: ["id"], name: "Name", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -133,7 +133,7 @@ describe("resolveStoreConfig", () => { internalType: "uint256", }, }, - primaryKey: ["id"], + key: ["id"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -185,7 +185,7 @@ describe("resolveStoreConfig", () => { internalType: "uint256", }, }, - primaryKey: ["id"], + key: ["id"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -205,7 +205,7 @@ describe("resolveStoreConfig", () => { attest(() => resolveStoreConfig({ tables: { - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. Example: { name: "string", age: "uint256", @@ -213,23 +213,23 @@ describe("resolveStoreConfig", () => { }, }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("throw an error if the shorthand config includes a non-static key field", () => { attest(() => - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. resolveStoreConfig({ tables: { Example: { id: "string", name: "string", age: "uint256" } } }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("throw an error if the shorthand config includes a non-static user type as key field", () => { attest(() => resolveStoreConfig({ - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. tables: { Example: { id: "dynamic", name: "string", age: "uint256" } }, userTypes: { dynamic: { type: "string", filePath: "path/to/file" }, @@ -237,7 +237,7 @@ describe("resolveStoreConfig", () => { }, }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); @@ -246,7 +246,7 @@ describe("resolveStoreConfig", () => { tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age"], + key: ["age"], }, }, }); @@ -284,7 +284,7 @@ describe("resolveStoreConfig", () => { internalType: "string", }, }, - primaryKey: ["age"], + key: ["age"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -305,7 +305,7 @@ describe("resolveStoreConfig", () => { tables: { Example: { schema: { id: "dynamic", name: "string", age: "static" }, - primaryKey: ["age"], + key: ["age"], }, }, userTypes: { @@ -347,7 +347,7 @@ describe("resolveStoreConfig", () => { internalType: "string", }, }, - primaryKey: ["age"], + key: ["age"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -365,12 +365,12 @@ describe("resolveStoreConfig", () => { attest(config).equals(expected); }), - it("should return the full config given a full config with two primaryKey", () => { + it("should return the full config given a full config with two key", () => { const config = resolveStoreConfig({ tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age", "id"], + key: ["age", "id"], }, }, }); @@ -408,7 +408,7 @@ describe("resolveStoreConfig", () => { internalType: "string", }, }, - primaryKey: ["age", "id"], + key: ["age", "id"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -429,11 +429,11 @@ describe("resolveStoreConfig", () => { tables: { First: { schema: { firstKey: "address", firstName: "string", firstAge: "uint256" }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], }, Second: { schema: { secondKey: "address", secondName: "string", secondAge: "uint256" }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], }, }, }); @@ -471,7 +471,7 @@ describe("resolveStoreConfig", () => { internalType: "string", }, }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], name: "First", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -509,7 +509,7 @@ describe("resolveStoreConfig", () => { internalType: "string", }, }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], name: "Second", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -530,11 +530,11 @@ describe("resolveStoreConfig", () => { tables: { First: { schema: { firstKey: "Static", firstName: "Dynamic", firstAge: "uint256" }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], }, Second: { schema: { secondKey: "Static", secondName: "Dynamic", secondAge: "uint256" }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], }, }, userTypes: { @@ -576,7 +576,7 @@ describe("resolveStoreConfig", () => { internalType: "Dynamic", }, }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], name: "First", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -614,7 +614,7 @@ describe("resolveStoreConfig", () => { internalType: "Dynamic", }, }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], name: "Second", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -639,17 +639,17 @@ describe("resolveStoreConfig", () => { tables: { First: { schema: { firstKey: "address", firstName: "string", firstAge: "uint256" }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], }, Second: { schema: { secondKey: "address", secondName: "string", secondAge: "uint256" }, // @ts-expect-error Type '"firstKey"' is not assignable to type '"secondKey" | "secondAge"' - primaryKey: ["firstKey", "secondAge"], + key: ["firstKey", "secondAge"], }, }, }), ) - .throws('Invalid primary key. Expected `("secondKey" | "secondAge")[]`, received `["firstKey", "secondAge"]`') + .throws('Invalid key. Expected `("secondKey" | "secondAge")[]`, received `["firstKey", "secondAge"]`') .type.errors(`Type '"firstKey"' is not assignable to type '"secondKey" | "secondAge"'`); }); @@ -660,12 +660,12 @@ describe("resolveStoreConfig", () => { Example: { schema: { id: "address", name: "string", age: "uint256" }, // @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"'. - primaryKey: ["name"], + key: ["name"], }, }, }), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["name"]`') .type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`); }); @@ -676,7 +676,7 @@ describe("resolveStoreConfig", () => { Example: { schema: { id: "address", name: "Dynamic", age: "uint256" }, // @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"'. - primaryKey: ["name"], + key: ["name"], }, }, userTypes: { @@ -684,7 +684,7 @@ describe("resolveStoreConfig", () => { }, }), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["name"]`') .type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`); }); @@ -693,7 +693,7 @@ describe("resolveStoreConfig", () => { tables: { Example: { schema: { id: "dynamic", name: "ValidNames", age: "static" }, - primaryKey: ["name"], + key: ["name"], }, }, userTypes: { @@ -738,7 +738,7 @@ describe("resolveStoreConfig", () => { internalType: "dynamic", }, }, - primaryKey: ["name"], + key: ["name"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -785,7 +785,7 @@ describe("resolveStoreConfig", () => { tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age"], + key: ["age"], }, }, }); diff --git a/packages/store/ts/config/v2/table.test.ts b/packages/store/ts/config/v2/table.test.ts index cd3adb2a34..6c9de595ef 100644 --- a/packages/store/ts/config/v2/table.test.ts +++ b/packages/store/ts/config/v2/table.test.ts @@ -33,7 +33,7 @@ describe("resolveTableConfig", () => { internalType: "address", }, }, - primaryKey: ["id"], + key: ["id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -70,7 +70,7 @@ describe("resolveTableConfig", () => { internalType: "CustomType", }, }, - primaryKey: ["id"], + key: ["id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -114,7 +114,7 @@ describe("resolveTableConfig", () => { internalType: "uint256", }, }, - primaryKey: ["id"], + key: ["id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -159,7 +159,7 @@ describe("resolveTableConfig", () => { internalType: "uint256", }, }, - primaryKey: ["id"], + key: ["id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -170,17 +170,17 @@ describe("resolveTableConfig", () => { }); it("should throw if the shorthand key is a dynamic ABI type", () => { - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. attest(() => resolveTableConfig({ id: "string", name: "string", age: "uint256" })).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("should throw if the shorthand key is a dyamic custom type", () => { const scope = extendScope(AbiTypeScope, { CustomType: "bytes" }); - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. attest(() => resolveTableConfig({ id: "CustomType" }, scope)).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); @@ -192,16 +192,16 @@ describe("resolveTableConfig", () => { }); it("should throw if the shorthand doesn't include a key field", () => { - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. attest(() => resolveTableConfig({ name: "string", age: "uint256" })).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("should return the full config given a full config with one key", () => { const table = resolveTableConfig({ schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age"], + key: ["age"], }); const expected = { tableId: "0x" as Hex, @@ -217,7 +217,7 @@ describe("resolveTableConfig", () => { id: { type: "address", internalType: "address" }, name: { type: "string", internalType: "string" }, }, - primaryKey: ["age"], + key: ["age"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -227,10 +227,10 @@ describe("resolveTableConfig", () => { attest(table).equals(expected); }); - it("should return the full config given a full config with two primaryKey", () => { + it("should return the full config given a full config with two key", () => { const table = resolveTableConfig({ schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age", "id"], + key: ["age", "id"], }); const expected = { tableId: "0x" as Hex, @@ -246,7 +246,7 @@ describe("resolveTableConfig", () => { valueSchema: { name: { type: "string", internalType: "string" }, }, - primaryKey: ["age", "id"], + key: ["age", "id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -291,7 +291,7 @@ describe("resolveTableConfig", () => { internalType: "CustomNumber", }, }, - primaryKey: ["id"], + key: ["id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -336,7 +336,7 @@ describe("resolveTableConfig", () => { internalType: "CustomNumber", }, }, - primaryKey: ["id"], + key: ["id"], name: "", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -351,10 +351,10 @@ describe("resolveTableConfig", () => { resolveTableConfig({ schema: { id: "address", name: "string", age: "uint256" }, // @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"' - primaryKey: ["name"], + key: ["name"], }), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["name"]`') .type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`); }); @@ -365,12 +365,12 @@ describe("resolveTableConfig", () => { { schema: { id: "address", name: "string", age: "uint256" }, // @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"' - primaryKey: ["name"], + key: ["name"], }, scope, ), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["name"]`') .type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`); }); @@ -381,12 +381,12 @@ describe("resolveTableConfig", () => { { schema: { id: "CustomType", name: "string", age: "uint256" }, // @ts-expect-error Type '"id"' is not assignable to type '"age"' - primaryKey: ["id"], + key: ["id"], }, scope, ), ) - .throws('Invalid primary key. Expected `("age")[]`, received `["id"]`') + .throws('Invalid key. Expected `("age")[]`, received `["id"]`') .type.errors(`Type '"id"' is not assignable to type '"age"'`); }); @@ -397,12 +397,12 @@ describe("resolveTableConfig", () => { { schema: { id: "address", name: "string", age: "uint256" }, // @ts-expect-error Type '"NotAKey"' is not assignable to type '"id" | "age"' - primaryKey: ["NotAKey"], + key: ["NotAKey"], }, scope, ), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["NotAKey"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["NotAKey"]`') .type.errors(`Type '"NotAKey"' is not assignable to type '"id" | "age"'`); }); diff --git a/packages/store/ts/config/v2/table.ts b/packages/store/ts/config/v2/table.ts index e27426d4b8..6bb7fb815b 100644 --- a/packages/store/ts/config/v2/table.ts +++ b/packages/store/ts/config/v2/table.ts @@ -20,8 +20,8 @@ import { CONFIG_DEFAULTS } from "./defaults"; export type TableInput< schema extends SchemaInput = SchemaInput, scope extends AbiTypeScope = AbiTypeScope, - primaryKey extends ValidKeys = ValidKeys, -> = TableFullInput | TableShorthandInput; + key extends ValidKeys = ValidKeys, +> = TableFullInput | TableShorthandInput; export type validateTableConfig = input extends TableShorthandInput @@ -58,8 +58,8 @@ export type resolveTableConfig< /** * If a shorthand table config is passed we expand it with sane defaults: - * - A single ABI type is turned into { schema: { id: "bytes32", value: INPUT }, primaryKey: ["id"] }. - * - A schema with a `id` field with static ABI type is turned into { schema: INPUT, primaryKey: ["id"] }. + * - A single ABI type is turned into { schema: { id: "bytes32", value: INPUT }, key: ["id"] }. + * - A schema with a `id` field with static ABI type is turned into { schema: INPUT, key: ["id"] }. * - A schema without a `id` field is invalid. */ export function resolveTableConfig< diff --git a/packages/store/ts/config/v2/tableFull.ts b/packages/store/ts/config/v2/tableFull.ts index b032572daf..7ae5ddf248 100644 --- a/packages/store/ts/config/v2/tableFull.ts +++ b/packages/store/ts/config/v2/tableFull.ts @@ -11,10 +11,10 @@ import { resourceToHex } from "@latticexyz/common"; export type TableFullInput< schema extends SchemaInput = SchemaInput, scope extends AbiTypeScope = AbiTypeScope, - primaryKey extends ValidKeys = ValidKeys, + key extends ValidKeys = ValidKeys, > = { schema: schema; - primaryKey: primaryKey; + key: key; tableId?: Hex; type?: "table" | "offchainTable"; name?: string; @@ -37,13 +37,13 @@ function getValidKeys, scope extends AbiTypeSc } export function isValidPrimaryKey, scope extends AbiTypeScope>( - primaryKey: unknown, + key: unknown, schema: schema, scope: scope = AbiTypeScope as scope, -): primaryKey is ValidKeys { +): key is ValidKeys { return ( - Array.isArray(primaryKey) && - primaryKey.every( + Array.isArray(key) && + key.every( (key) => hasOwnKey(schema, key) && hasOwnKey(scope.types, schema[key]) && isStaticAbiType(scope.types[schema[key]]), ) @@ -55,8 +55,8 @@ export function isTableFullInput(input: unknown): input is TableFullInput { typeof input === "object" && input !== null && hasOwnKey(input, "schema") && - hasOwnKey(input, "primaryKey") && - Array.isArray(input["primaryKey"]) + hasOwnKey(input, "key") && + Array.isArray(input["key"]) ); } @@ -65,7 +65,7 @@ export type validateKeys = { }; export type validateTableFull = { - [key in keyof input]: key extends "primaryKey" + [key in keyof input]: key extends "key" ? validateKeys, SchemaInput>, scope>, input[key]> : key extends "schema" ? conform> @@ -86,13 +86,13 @@ export function validateTableFull `"${String(item)}"`) .join(" | ")})[]\`, received \`${ - hasOwnKey(input, "primaryKey") && Array.isArray(input.primaryKey) - ? `[${input.primaryKey.map((item) => `"${item}"`).join(", ")}]` + hasOwnKey(input, "key") && Array.isArray(input.key) + ? `[${input.key.map((item) => `"${item}"`).join(", ")}]` : "undefined" }\``, ); @@ -129,7 +129,7 @@ export function resolveTableCodegen< tableIdArgument: get(options, "tableIdArgument") ?? TABLE_CODEGEN_DEFAULTS.tableIdArgument, storeArgument: get(options, "storeArgument") ?? TABLE_CODEGEN_DEFAULTS.storeArgument, // dataStruct is true if there are at least 2 value fields - dataStruct: get(options, "dataStruct") ?? Object.keys(input.schema).length - input.primaryKey.length > 1, + dataStruct: get(options, "dataStruct") ?? Object.keys(input.schema).length - input.key.length > 1, } satisfies TableCodegenOptions as resolveTableCodegen; } @@ -180,17 +180,17 @@ export type resolveTableFullConfig< readonly name: input["name"] extends undefined ? "" : input["name"]; readonly namespace: input["namespace"] extends undefined ? "" : input["namespace"]; readonly type: input["type"] extends undefined ? "table" : input["type"]; - readonly primaryKey: Readonly; + readonly key: Readonly; readonly schema: resolveSchema; readonly keySchema: resolveSchema< { - readonly [key in input["primaryKey"][number]]: input["schema"][key]; + readonly [key in input["key"][number]]: input["schema"][key]; }, scope >; readonly valueSchema: resolveSchema< { - readonly [key in Exclude]: input["schema"][key]; + readonly [key in Exclude]: input["schema"][key]; }, scope >; @@ -209,21 +209,17 @@ export function resolveTableFullConfig< name: input.name ?? ("" as const), namespace: input.namespace ?? ("" as const), type: input.type ?? ("table" as const), - primaryKey: input["primaryKey"], + key: input["key"], schema: resolveSchema(input["schema"], scope), keySchema: resolveSchema( Object.fromEntries( - Object.entries(input["schema"]).filter(([key]) => - input["primaryKey"].includes(key as input["primaryKey"][number]), - ), + Object.entries(input["schema"]).filter(([key]) => input["key"].includes(key as input["key"][number])), ), scope, ), valueSchema: resolveSchema( Object.fromEntries( - Object.entries(input["schema"]).filter( - ([key]) => !input["primaryKey"].includes(key as input["primaryKey"][number]), - ), + Object.entries(input["schema"]).filter(([key]) => !input["key"].includes(key as input["key"][number])), ), scope, ), diff --git a/packages/store/ts/config/v2/tableShorthand.test.ts b/packages/store/ts/config/v2/tableShorthand.test.ts index 2443fe30b7..cf086e6139 100644 --- a/packages/store/ts/config/v2/tableShorthand.test.ts +++ b/packages/store/ts/config/v2/tableShorthand.test.ts @@ -12,13 +12,13 @@ describe("resolveTableShorthand", () => { id: "bytes32"; value: "address"; }; - primaryKey: ["id"]; + key: ["id"]; }>(table).equals({ schema: { id: "bytes32", value: "address", }, - primaryKey: ["id"], + key: ["id"], }); }); @@ -31,13 +31,13 @@ describe("resolveTableShorthand", () => { id: "bytes32"; value: "CustomType"; }; - primaryKey: ["id"]; + key: ["id"]; }>(table).equals({ schema: { id: "bytes32", value: "CustomType", }, - primaryKey: ["id"], + key: ["id"], }); }); @@ -72,32 +72,32 @@ describe("resolveTableShorthand", () => { name: "string"; age: "uint256"; }; - primaryKey: ["id"]; + key: ["id"]; }>(table).equals({ schema: { id: "address", name: "string", age: "uint256", }, - primaryKey: ["id"], + key: ["id"], }); }); it("should throw an error if the shorthand doesn't include an `id` field", () => { attest(() => - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. resolveTableShorthand({ name: "string", age: "uint256" }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("should throw an error if the shorthand config includes a non-static `id` field", () => { attest(() => - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. resolveTableShorthand({ id: "string", name: "string", age: "uint256" }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); @@ -116,20 +116,20 @@ describe("resolveTableShorthand", () => { attest<{ schema: { id: "CustomType"; name: "string"; age: "uint256" }; - primaryKey: ["id"]; + key: ["id"]; }>(table).equals({ schema: { id: "CustomType", name: "string", age: "uint256" }, - primaryKey: ["id"], + key: ["id"], }); }); it("should throw an error if `id` is not a custom static type", () => { const scope = extendScope(AbiTypeScope, { CustomType: "bytes" }); attest(() => - // @ts-expect-error "Error: Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option." + // @ts-expect-error "Error: Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option." resolveTableShorthand({ id: "CustomType", name: "string", age: "uint256" }, scope), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); }); diff --git a/packages/store/ts/config/v2/tableShorthand.ts b/packages/store/ts/config/v2/tableShorthand.ts index 1d445587eb..405e71096e 100644 --- a/packages/store/ts/config/v2/tableShorthand.ts +++ b/packages/store/ts/config/v2/tableShorthand.ts @@ -6,7 +6,7 @@ import { AbiTypeScope, getStaticAbiTypeKeys } from "./scope"; import { TableFullInput } from "./tableFull"; export type NoStaticKeyFieldError = - ErrorMessage<"Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.">; + ErrorMessage<"Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.">; export type TableShorthandInput = SchemaInput | keyof scope["types"]; @@ -45,9 +45,7 @@ export function validateTableShorthand : input extends SchemaInput ? "id" extends getStaticAbiTypeKeys - ? // If the shorthand includes a static field called `id`, use it as `primaryKey` + ? // If the shorthand includes a static field called `id`, use it as `key` evaluate> : never : never; @@ -78,7 +76,7 @@ export function resolveTableShorthand; } @@ -87,6 +85,6 @@ export function resolveTableShorthand; } diff --git a/packages/world/ts/config/v2/world.test.ts b/packages/world/ts/config/v2/world.test.ts index b9e94f7626..f0cf872fe9 100644 --- a/packages/world/ts/config/v2/world.test.ts +++ b/packages/world/ts/config/v2/world.test.ts @@ -17,7 +17,7 @@ describe("resolveWorldConfig", () => { value: "uint256", dynamic: "string", }, - primaryKey: ["id"], + key: ["id"], }, }, }, @@ -58,7 +58,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["id"], + key: ["id"], name: "ExampleTable", namespace: "ExampleNamespace", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -100,7 +100,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["id"], + key: ["id"], name: "ExampleTable", namespace: "ExampleNamespace", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -129,7 +129,7 @@ describe("resolveWorldConfig", () => { value: "MyEnum", dynamic: "Dynamic", }, - primaryKey: ["id"], + key: ["id"], }, }, }, @@ -177,7 +177,7 @@ describe("resolveWorldConfig", () => { internalType: "Dynamic", }, }, - primaryKey: ["id"], + key: ["id"], name: "ExampleTable", namespace: "ExampleNamespace", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -219,7 +219,7 @@ describe("resolveWorldConfig", () => { internalType: "Dynamic", }, }, - primaryKey: ["id"], + key: ["id"], name: "ExampleTable", namespace: "ExampleNamespace", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -253,7 +253,7 @@ describe("resolveWorldConfig", () => { value: "MyEnum", dynamic: "Dynamic", }, - primaryKey: ["id"], + key: ["id"], }, }, }, @@ -278,7 +278,7 @@ describe("resolveWorldConfig", () => { tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age"], + key: ["age"], }, }, }, @@ -321,7 +321,7 @@ describe("resolveWorldConfig", () => { internalType: "address", }, }, - primaryKey: ["id"], + key: ["id"], name: "Name", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -370,7 +370,7 @@ describe("resolveWorldConfig", () => { internalType: "CustomType", }, }, - primaryKey: ["id"], + key: ["id"], name: "Name", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -423,7 +423,7 @@ describe("resolveWorldConfig", () => { internalType: "uint256", }, }, - primaryKey: ["id"], + key: ["id"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -476,7 +476,7 @@ describe("resolveWorldConfig", () => { internalType: "uint256", }, }, - primaryKey: ["id"], + key: ["id"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -497,7 +497,7 @@ describe("resolveWorldConfig", () => { attest(() => resolveWorldConfig({ tables: { - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. Example: { name: "string", age: "uint256", @@ -505,23 +505,23 @@ describe("resolveWorldConfig", () => { }, }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("throw an error if the shorthand config includes a non-static key field", () => { attest(() => - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. resolveWorldConfig({ tables: { Example: { id: "string", name: "string", age: "uint256" } } }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); it("throw an error if the shorthand config includes a non-static user type as key field", () => { attest(() => resolveWorldConfig({ - // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option. + // @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option. tables: { Example: { id: "dynamic", name: "string", age: "uint256" } }, userTypes: { dynamic: { type: "string", filePath: "path/to/file" }, @@ -529,7 +529,7 @@ describe("resolveWorldConfig", () => { }, }), ).throwsAndHasTypeError( - "Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.", + "Invalid schema. Expected an `id` field with a static ABI type or an explicit `key` option.", ); }); @@ -538,7 +538,7 @@ describe("resolveWorldConfig", () => { tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age"], + key: ["age"], }, }, }); @@ -576,7 +576,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["age"], + key: ["age"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -598,7 +598,7 @@ describe("resolveWorldConfig", () => { tables: { Example: { schema: { id: "dynamic", name: "string", age: "static" }, - primaryKey: ["age"], + key: ["age"], }, }, userTypes: { @@ -640,7 +640,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["age"], + key: ["age"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -659,12 +659,12 @@ describe("resolveWorldConfig", () => { attest(config).equals(expected); }), - it("should return the full config given a full config with two primaryKey", () => { + it("should return the full config given a full config with two key", () => { const config = resolveWorldConfig({ tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age", "id"], + key: ["age", "id"], }, }, }); @@ -702,7 +702,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["age", "id"], + key: ["age", "id"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -724,11 +724,11 @@ describe("resolveWorldConfig", () => { tables: { First: { schema: { firstKey: "address", firstName: "string", firstAge: "uint256" }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], }, Second: { schema: { secondKey: "address", secondName: "string", secondAge: "uint256" }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], }, }, }); @@ -766,7 +766,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], name: "First", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -804,7 +804,7 @@ describe("resolveWorldConfig", () => { internalType: "string", }, }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], name: "Second", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -826,11 +826,11 @@ describe("resolveWorldConfig", () => { tables: { First: { schema: { firstKey: "Static", firstName: "Dynamic", firstAge: "uint256" }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], }, Second: { schema: { secondKey: "Static", secondName: "Dynamic", secondAge: "uint256" }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], }, }, userTypes: { @@ -872,7 +872,7 @@ describe("resolveWorldConfig", () => { internalType: "Dynamic", }, }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], name: "First", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -910,7 +910,7 @@ describe("resolveWorldConfig", () => { internalType: "Dynamic", }, }, - primaryKey: ["secondKey", "secondAge"], + key: ["secondKey", "secondAge"], name: "Second", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean }, @@ -936,17 +936,17 @@ describe("resolveWorldConfig", () => { tables: { First: { schema: { firstKey: "address", firstName: "string", firstAge: "uint256" }, - primaryKey: ["firstKey", "firstAge"], + key: ["firstKey", "firstAge"], }, Second: { schema: { secondKey: "address", secondName: "string", secondAge: "uint256" }, // @ts-expect-error Type '"firstKey"' is not assignable to type '"secondKey" | "secondAge"' - primaryKey: ["firstKey", "secondAge"], + key: ["firstKey", "secondAge"], }, }, }), ) - .throws('Invalid primary key. Expected `("secondKey" | "secondAge")[]`, received `["firstKey", "secondAge"]`') + .throws('Invalid key. Expected `("secondKey" | "secondAge")[]`, received `["firstKey", "secondAge"]`') .type.errors(`Type '"firstKey"' is not assignable to type '"secondKey" | "secondAge"'`); }); @@ -957,12 +957,12 @@ describe("resolveWorldConfig", () => { Example: { schema: { id: "address", name: "string", age: "uint256" }, // @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"'. - primaryKey: ["name"], + key: ["name"], }, }, }), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["name"]`') .type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`); }); @@ -973,7 +973,7 @@ describe("resolveWorldConfig", () => { Example: { schema: { id: "address", name: "Dynamic", age: "uint256" }, // @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"'. - primaryKey: ["name"], + key: ["name"], }, }, userTypes: { @@ -981,7 +981,7 @@ describe("resolveWorldConfig", () => { }, }), ) - .throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`') + .throws('Invalid key. Expected `("id" | "age")[]`, received `["name"]`') .type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`); }); @@ -990,7 +990,7 @@ describe("resolveWorldConfig", () => { tables: { Example: { schema: { id: "dynamic", name: "ValidNames", age: "static" }, - primaryKey: ["name"], + key: ["name"], }, }, userTypes: { @@ -1035,7 +1035,7 @@ describe("resolveWorldConfig", () => { internalType: "dynamic", }, }, - primaryKey: ["name"], + key: ["name"], name: "Example", namespace: "", codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean }, @@ -1083,7 +1083,7 @@ describe("resolveWorldConfig", () => { tables: { Example: { schema: { id: "address", name: "string", age: "uint256" }, - primaryKey: ["age"], + key: ["age"], }, }, }); diff --git a/test/mock-game-contracts/mud.config.ts b/test/mock-game-contracts/mud.config.ts index 4a7848008b..a2e2dfe1cb 100644 --- a/test/mock-game-contracts/mud.config.ts +++ b/test/mock-game-contracts/mud.config.ts @@ -72,14 +72,14 @@ export const configV2 = resolveStoreConfig({ x: "int32", y: "int32", }, - primaryKey: ["player"], + key: ["player"], }, Health: { schema: { player: "address", health: "uint256", }, - primaryKey: ["player"], + key: ["player"], }, Inventory: { schema: { @@ -87,7 +87,7 @@ export const configV2 = resolveStoreConfig({ item: "uint8", amount: "uint32", }, - primaryKey: ["player", "item"], + key: ["player", "item"], }, Score: { schema: { @@ -95,14 +95,14 @@ export const configV2 = resolveStoreConfig({ game: "uint256", score: "uint256", }, - primaryKey: ["player", "game"], + key: ["player", "game"], }, Winner: { schema: { game: "uint256", player: "address", }, - primaryKey: ["game"], + key: ["game"], }, Terrain: { schema: { @@ -110,7 +110,7 @@ export const configV2 = resolveStoreConfig({ y: "int32", terrainType: "TerrainType", }, - primaryKey: ["x", "y"], + key: ["x", "y"], }, }, });