From 4e445a1abb764de970381f5c5570ce135b712c4c Mon Sep 17 00:00:00 2001 From: yonada Date: Tue, 20 Feb 2024 16:59:09 +0000 Subject: [PATCH] feat(store-sync): bool array column types for decoded indexer (#2283) Co-authored-by: Kevin Ingersoll --- .changeset/lemon-numbers-design.md | 5 +++++ .../src/postgres-decoded/buildColumn.ts | 15 +++++++++++++-- .../postgres-decoded/createStorageAdapter.test.ts | 2 +- packages/store-sync/src/postgres/columnTypes.ts | 14 ++++++++++++++ .../src/postgres/createStorageAdapter.test.ts | 2 +- packages/store-sync/src/postgres/version.ts | 2 +- 6 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 .changeset/lemon-numbers-design.md diff --git a/.changeset/lemon-numbers-design.md b/.changeset/lemon-numbers-design.md new file mode 100644 index 0000000000..4834070a8f --- /dev/null +++ b/.changeset/lemon-numbers-design.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store-sync": patch +--- + +Moved boolean array types to use array column types (instead of JSON columns) for the Postgres decoded indexer diff --git a/packages/store-sync/src/postgres-decoded/buildColumn.ts b/packages/store-sync/src/postgres-decoded/buildColumn.ts index f72c27443e..33a8925f55 100644 --- a/packages/store-sync/src/postgres-decoded/buildColumn.ts +++ b/packages/store-sync/src/postgres-decoded/buildColumn.ts @@ -1,7 +1,16 @@ import { boolean, text } from "drizzle-orm/pg-core"; import { SchemaAbiType } from "@latticexyz/schema-type"; import { assertExhaustive } from "@latticexyz/common/utils"; -import { asAddress, asBigInt, asBigIntArray, asHex, asJson, asNumber, asNumberArray } from "../postgres/columnTypes"; +import { + asAddress, + asBigInt, + asBigIntArray, + asBoolArray, + asHex, + asJson, + asNumber, + asNumberArray, +} from "../postgres/columnTypes"; // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export function buildColumn(name: string, schemaAbiType: SchemaAbiType) { @@ -124,6 +133,9 @@ export function buildColumn(name: string, schemaAbiType: SchemaAbiType) { case "address": return asAddress(name); + case "bool[]": + return asBoolArray(name); + case "uint8[]": case "uint16[]": case "uint24[]": @@ -233,7 +245,6 @@ export function buildColumn(name: string, schemaAbiType: SchemaAbiType) { case "bytes30[]": case "bytes31[]": case "bytes32[]": - case "bool[]": return asJson(name); // TODO: normalize like address column type diff --git a/packages/store-sync/src/postgres-decoded/createStorageAdapter.test.ts b/packages/store-sync/src/postgres-decoded/createStorageAdapter.test.ts index 9a37fe7036..184413b0ae 100644 --- a/packages/store-sync/src/postgres-decoded/createStorageAdapter.test.ts +++ b/packages/store-sync/src/postgres-decoded/createStorageAdapter.test.ts @@ -52,7 +52,7 @@ describe("createStorageAdapter", async () => { { "blockNumber": 20n, "chainId": 31337, - "version": "0.0.5", + "version": "0.0.6", }, ] `); diff --git a/packages/store-sync/src/postgres/columnTypes.ts b/packages/store-sync/src/postgres/columnTypes.ts index 6414172709..9d130da3aa 100644 --- a/packages/store-sync/src/postgres/columnTypes.ts +++ b/packages/store-sync/src/postgres/columnTypes.ts @@ -73,6 +73,20 @@ export const asAddress = (name: string) => }, })(name); +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type +export const asBoolArray = (name: string) => + customType<{ data: boolean[]; driverData: string[] }>({ + dataType() { + return "bool[]"; + }, + toDriver(data: boolean[]): string[] { + return data.map((datum) => String(datum)); + }, + fromDriver(driverData: string[]): boolean[] { + return driverData.map((datum) => Boolean(datum)); + }, + })(name); + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export const asNumberArray = (name: string, columnType: string) => customType<{ data: number[]; driverData: string[] }>({ diff --git a/packages/store-sync/src/postgres/createStorageAdapter.test.ts b/packages/store-sync/src/postgres/createStorageAdapter.test.ts index 0e3249f005..ae98d5163b 100644 --- a/packages/store-sync/src/postgres/createStorageAdapter.test.ts +++ b/packages/store-sync/src/postgres/createStorageAdapter.test.ts @@ -50,7 +50,7 @@ describe("createStorageAdapter", async () => { { "blockNumber": 20n, "chainId": 31337, - "version": "0.0.5", + "version": "0.0.6", }, ] `); diff --git a/packages/store-sync/src/postgres/version.ts b/packages/store-sync/src/postgres/version.ts index 631eadd03d..a7f2787858 100644 --- a/packages/store-sync/src/postgres/version.ts +++ b/packages/store-sync/src/postgres/version.ts @@ -1 +1 @@ -export const version = "0.0.5"; +export const version = "0.0.6";