Skip to content

Commit

Permalink
refactor(store): default to id field instead of key field as prim…
Browse files Browse the repository at this point in the history
…ary key (#2447)
  • Loading branch information
holic authored Mar 15, 2024
1 parent e4c0dea commit 694568b
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 227 deletions.
94 changes: 47 additions & 47 deletions packages/store/ts/config/v2/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("resolveStoreConfig", () => {
Name: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Name" }),
schema: {
key: {
id: {
type: "bytes32",
internalType: "bytes32",
},
Expand All @@ -23,7 +23,7 @@ describe("resolveStoreConfig", () => {
},
},
keySchema: {
key: {
id: {
type: "bytes32",
internalType: "bytes32",
},
Expand All @@ -34,7 +34,7 @@ describe("resolveStoreConfig", () => {
internalType: "address",
},
},
primaryKey: ["key"],
primaryKey: ["id"],
name: "Name",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
Expand All @@ -60,7 +60,7 @@ describe("resolveStoreConfig", () => {
Name: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Name" }),
schema: {
key: {
id: {
type: "bytes32",
internalType: "bytes32",
},
Expand All @@ -70,7 +70,7 @@ describe("resolveStoreConfig", () => {
},
},
keySchema: {
key: {
id: {
type: "bytes32",
internalType: "bytes32",
},
Expand All @@ -81,7 +81,7 @@ describe("resolveStoreConfig", () => {
internalType: "CustomType",
},
},
primaryKey: ["key"],
primaryKey: ["id"],
name: "Name",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
Expand All @@ -97,14 +97,14 @@ describe("resolveStoreConfig", () => {
attest<typeof expected>(config).equals(expected);
});

it("given a schema with a key field with static ABI type, it should use `key` as single key", () => {
const config = resolveStoreConfig({ tables: { Example: { key: "address", name: "string", age: "uint256" } } });
it("given a schema with a key field with static ABI type, it should use `id` as single key", () => {
const config = resolveStoreConfig({ tables: { Example: { id: "address", name: "string", age: "uint256" } } });
const expected = {
tables: {
Example: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Example" }),
schema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -118,7 +118,7 @@ describe("resolveStoreConfig", () => {
},
},
keySchema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -133,7 +133,7 @@ describe("resolveStoreConfig", () => {
internalType: "uint256",
},
},
primaryKey: ["key"],
primaryKey: ["id"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand All @@ -149,14 +149,14 @@ describe("resolveStoreConfig", () => {
attest<typeof expected>(config).equals(expected);
});

it("given a schema with a key field with static custom type, it should use `key` as single key", () => {
const config = resolveStoreConfig({ tables: { Example: { key: "address", name: "string", age: "uint256" } } });
it("given a schema with a key field with static custom type, it should use `id` as single key", () => {
const config = resolveStoreConfig({ tables: { Example: { id: "address", name: "string", age: "uint256" } } });
const expected = {
tables: {
Example: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Example" }),
schema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -170,7 +170,7 @@ describe("resolveStoreConfig", () => {
},
},
keySchema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -185,7 +185,7 @@ describe("resolveStoreConfig", () => {
internalType: "uint256",
},
},
primaryKey: ["key"],
primaryKey: ["id"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
Expand All @@ -205,47 +205,47 @@ describe("resolveStoreConfig", () => {
attest(() =>
resolveStoreConfig({
tables: {
// @ts-expect-error Invalid schema. Expected a `key` 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 `primaryKey` option.
Example: {
name: "string",
age: "uint256",
},
},
}),
).throwsAndHasTypeError(
"Invalid schema. Expected a `key` 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 `primaryKey` option.",
);
});

it("throw an error if the shorthand config includes a non-static key field", () => {
attest(() =>
// @ts-expect-error Invalid schema. Expected a `key` field with a static ABI type or an explicit `primaryKey` option.
resolveStoreConfig({ tables: { Example: { key: "string", name: "string", age: "uint256" } } }),
// @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` option.
resolveStoreConfig({ tables: { Example: { id: "string", name: "string", age: "uint256" } } }),
).throwsAndHasTypeError(
"Invalid schema. Expected a `key` 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 `primaryKey` 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 a `key` field with a static ABI type or an explicit `primaryKey` option.
tables: { Example: { key: "dynamic", name: "string", age: "uint256" } },
// @ts-expect-error Invalid schema. Expected an `id` field with a static ABI type or an explicit `primaryKey` 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 a `key` 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 `primaryKey` option.",
);
});

it("should return the full config given a full config with one key", () => {
const config = resolveStoreConfig({
tables: {
Example: {
schema: { key: "address", name: "string", age: "uint256" },
schema: { id: "address", name: "string", age: "uint256" },
primaryKey: ["age"],
},
},
Expand All @@ -255,7 +255,7 @@ describe("resolveStoreConfig", () => {
Example: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Example" }),
schema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -275,7 +275,7 @@ describe("resolveStoreConfig", () => {
},
},
valueSchema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand Down Expand Up @@ -304,7 +304,7 @@ describe("resolveStoreConfig", () => {
const config = resolveStoreConfig({
tables: {
Example: {
schema: { key: "dynamic", name: "string", age: "static" },
schema: { id: "dynamic", name: "string", age: "static" },
primaryKey: ["age"],
},
},
Expand All @@ -318,7 +318,7 @@ describe("resolveStoreConfig", () => {
Example: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Example" }),
schema: {
key: {
id: {
type: "string",
internalType: "dynamic",
},
Expand All @@ -338,7 +338,7 @@ describe("resolveStoreConfig", () => {
},
},
valueSchema: {
key: {
id: {
type: "string",
internalType: "dynamic",
},
Expand Down Expand Up @@ -369,8 +369,8 @@ describe("resolveStoreConfig", () => {
const config = resolveStoreConfig({
tables: {
Example: {
schema: { key: "address", name: "string", age: "uint256" },
primaryKey: ["age", "key"],
schema: { id: "address", name: "string", age: "uint256" },
primaryKey: ["age", "id"],
},
},
});
Expand All @@ -379,7 +379,7 @@ describe("resolveStoreConfig", () => {
Example: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Example" }),
schema: {
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -397,7 +397,7 @@ describe("resolveStoreConfig", () => {
type: "uint256",
internalType: "uint256",
},
key: {
id: {
type: "address",
internalType: "address",
},
Expand All @@ -408,7 +408,7 @@ describe("resolveStoreConfig", () => {
internalType: "string",
},
},
primaryKey: ["age", "key"],
primaryKey: ["age", "id"],
name: "Example",
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
Expand Down Expand Up @@ -658,24 +658,24 @@ describe("resolveStoreConfig", () => {
resolveStoreConfig({
tables: {
Example: {
schema: { key: "address", name: "string", age: "uint256" },
// @ts-expect-error Type '"name"' is not assignable to type '"key" | "age"'.
schema: { id: "address", name: "string", age: "uint256" },
// @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"'.
primaryKey: ["name"],
},
},
}),
)
.throws('Invalid primary key. Expected `("key" | "age")[]`, received `["name"]`')
.type.errors(`Type '"name"' is not assignable to type '"key" | "age"'`);
.throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`')
.type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`);
});

it("should throw an error if the provided key is not a static field with user types", () => {
attest(() =>
resolveStoreConfig({
tables: {
Example: {
schema: { key: "address", name: "Dynamic", age: "uint256" },
// @ts-expect-error Type '"name"' is not assignable to type '"key" | "age"'.
schema: { id: "address", name: "Dynamic", age: "uint256" },
// @ts-expect-error Type '"name"' is not assignable to type '"id" | "age"'.
primaryKey: ["name"],
},
},
Expand All @@ -684,15 +684,15 @@ describe("resolveStoreConfig", () => {
},
}),
)
.throws('Invalid primary key. Expected `("key" | "age")[]`, received `["name"]`')
.type.errors(`Type '"name"' is not assignable to type '"key" | "age"'`);
.throws('Invalid primary key. Expected `("id" | "age")[]`, received `["name"]`')
.type.errors(`Type '"name"' is not assignable to type '"id" | "age"'`);
});

it("should return the full config given a full config with enums and user types", () => {
const config = resolveStoreConfig({
tables: {
Example: {
schema: { key: "dynamic", name: "ValidNames", age: "static" },
schema: { id: "dynamic", name: "ValidNames", age: "static" },
primaryKey: ["name"],
},
},
Expand All @@ -709,7 +709,7 @@ describe("resolveStoreConfig", () => {
Example: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Example" }),
schema: {
key: {
id: {
type: "string",
internalType: "dynamic",
},
Expand All @@ -733,7 +733,7 @@ describe("resolveStoreConfig", () => {
type: "address",
internalType: "static",
},
key: {
id: {
type: "string",
internalType: "dynamic",
},
Expand Down Expand Up @@ -784,7 +784,7 @@ describe("resolveStoreConfig", () => {
namespace: "namespace",
tables: {
Example: {
schema: { key: "address", name: "string", age: "uint256" },
schema: { id: "address", name: "string", age: "uint256" },
primaryKey: ["age"],
},
},
Expand Down
Loading

0 comments on commit 694568b

Please sign in to comment.