Skip to content

Commit

Permalink
refactor: primaryKey -> key (#2449)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Mar 15, 2024
1 parent 0a61cfa commit 2a4b289
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 162 deletions.
2 changes: 1 addition & 1 deletion packages/config/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};
6 changes: 3 additions & 3 deletions packages/store/ts/config/v2/compat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,22 @@ describe("configToV1", () => {
x: "int32",
y: "int32",
},
primaryKey: ["player"],
key: ["player"],
},
Health: {
schema: {
player: "CustomAddress",
health: "uint256",
},
primaryKey: ["player"],
key: ["player"],
},
Terrain: {
schema: {
x: "int32",
y: "int32",
terrainType: "TerrainType",
},
primaryKey: ["x", "y"],
key: ["x", "y"],
},
},
});
Expand Down
70 changes: 35 additions & 35 deletions packages/store/ts/config/v2/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("resolveStoreConfig", () => {
internalType: "address",
},
},
primaryKey: ["id"],
key: ["id"],
name: "Name",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("resolveStoreConfig", () => {
internalType: "CustomType",
},
},
primaryKey: ["id"],
key: ["id"],
name: "Name",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
Expand Down Expand Up @@ -133,7 +133,7 @@ describe("resolveStoreConfig", () => {
internalType: "uint256",
},
},
primaryKey: ["id"],
key: ["id"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("resolveStoreConfig", () => {
internalType: "uint256",
},
},
primaryKey: ["id"],
key: ["id"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand All @@ -205,39 +205,39 @@ 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",
},
},
}),
).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" },
static: { type: "address", filePath: "path/to/file" },
},
}),
).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.",
);
});

Expand All @@ -246,7 +246,7 @@ describe("resolveStoreConfig", () => {
tables: {
Example: {
schema: { id: "address", name: "string", age: "uint256" },
primaryKey: ["age"],
key: ["age"],
},
},
});
Expand Down Expand Up @@ -284,7 +284,7 @@ describe("resolveStoreConfig", () => {
internalType: "string",
},
},
primaryKey: ["age"],
key: ["age"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand All @@ -305,7 +305,7 @@ describe("resolveStoreConfig", () => {
tables: {
Example: {
schema: { id: "dynamic", name: "string", age: "static" },
primaryKey: ["age"],
key: ["age"],
},
},
userTypes: {
Expand Down Expand Up @@ -347,7 +347,7 @@ describe("resolveStoreConfig", () => {
internalType: "string",
},
},
primaryKey: ["age"],
key: ["age"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand All @@ -365,12 +365,12 @@ describe("resolveStoreConfig", () => {

attest<typeof expected>(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"],
},
},
});
Expand Down Expand Up @@ -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 },
Expand All @@ -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"],
},
},
});
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand All @@ -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: {
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand All @@ -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"'`);
});

Expand All @@ -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"'`);
});

Expand All @@ -676,15 +676,15 @@ 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: {
Dynamic: { type: "string", filePath: "path/to/file" },
},
}),
)
.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"'`);
});

Expand All @@ -693,7 +693,7 @@ describe("resolveStoreConfig", () => {
tables: {
Example: {
schema: { id: "dynamic", name: "ValidNames", age: "static" },
primaryKey: ["name"],
key: ["name"],
},
},
userTypes: {
Expand Down Expand Up @@ -738,7 +738,7 @@ describe("resolveStoreConfig", () => {
internalType: "dynamic",
},
},
primaryKey: ["name"],
key: ["name"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand Down Expand Up @@ -785,7 +785,7 @@ describe("resolveStoreConfig", () => {
tables: {
Example: {
schema: { id: "address", name: "string", age: "uint256" },
primaryKey: ["age"],
key: ["age"],
},
},
});
Expand Down
Loading

0 comments on commit 2a4b289

Please sign in to comment.