diff --git a/change/@apibara-indexer-eae11f64-2203-4964-a273-9c1536b8fcdd.json b/change/@apibara-indexer-eae11f64-2203-4964-a273-9c1536b8fcdd.json new file mode 100644 index 0000000..dfe3f9d --- /dev/null +++ b/change/@apibara-indexer-eae11f64-2203-4964-a273-9c1536b8fcdd.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "indexer: update drizzle and drizzle sink", + "packageName": "@apibara/indexer", + "email": "francesco@ceccon.me", + "dependentChangeType": "patch" +} diff --git a/examples/indexer/package.json b/examples/indexer/package.json index 3aa2940..275dbd8 100644 --- a/examples/indexer/package.json +++ b/examples/indexer/package.json @@ -23,7 +23,7 @@ "citty": "^0.1.6", "consola": "^3.2.3", "csv-stringify": "^6.5.0", - "drizzle-orm": "^0.33.0", + "drizzle-orm": "^0.35.0", "postgres": "^3.4.4", "viem": "^2.12.4" }, diff --git a/examples/starknet-indexer/package.json b/examples/starknet-indexer/package.json index 440d5c0..6241262 100644 --- a/examples/starknet-indexer/package.json +++ b/examples/starknet-indexer/package.json @@ -22,7 +22,7 @@ "citty": "^0.1.6", "consola": "^3.2.3", "csv-stringify": "^6.5.0", - "drizzle-orm": "^0.33.0", + "drizzle-orm": "^0.35.0", "postgres": "^3.4.4", "sqlite": "^5.1.1", "viem": "^2.12.4" diff --git a/package.json b/package.json index 92b24cb..5d5e595 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "description": "Apibara Typescript monorepo", "scripts": { "build": "turbo run build", - "typecheck": "turbo run typecheck --parallel", + "typecheck": "turbo run typecheck", "test": "turbo run test", "test:ci": "turbo run test:ci", "test:postgres": "docker run --rm -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres:16", diff --git a/packages/indexer/package.json b/packages/indexer/package.json index 0ea1389..2b7bccc 100644 --- a/packages/indexer/package.json +++ b/packages/indexer/package.json @@ -80,7 +80,7 @@ "@types/pg": "^8.11.10", "better-sqlite3": "^11.1.2", "csv-stringify": "^6.5.0", - "drizzle-orm": "^0.33.0", + "drizzle-orm": "^0.35.2", "pg": "^8.12.0", "postgres-range": "^1.1.4", "unbuild": "^2.0.0", @@ -98,7 +98,7 @@ "peerDependencies": { "better-sqlite3": "^11.1.2", "csv-stringify": "^6.5.0", - "drizzle-orm": "^0.33.0", + "drizzle-orm": "^0.35.2", "postgres-range": "^1.1.4", "vitest": "^1.6.0" } diff --git a/packages/indexer/src/sinks/drizzle/insert.ts b/packages/indexer/src/sinks/drizzle/insert.ts index 7d0f2f9..77a6b2b 100644 --- a/packages/indexer/src/sinks/drizzle/insert.ts +++ b/packages/indexer/src/sinks/drizzle/insert.ts @@ -4,12 +4,12 @@ import type { TablesRelationalConfig, } from "drizzle-orm"; import type { - PgInsertValue, + PgInsertValue as DrizzleInsertValue, PgQueryResultHKT, PgTable, PgTransaction, } from "drizzle-orm/pg-core"; -import { getDrizzleCursor } from "./utils"; +import { type PgInsertValue, getDrizzleCursor } from "./utils"; export class DrizzleSinkInsert< TTable extends PgTable, @@ -34,6 +34,9 @@ export class DrizzleSinkInsert< }; }, ); - return originalInsert.values(cursoredValues as PgInsertValue[]); + + return originalInsert.values( + cursoredValues as DrizzleInsertValue[], + ); } } diff --git a/packages/indexer/src/sinks/drizzle/utils.ts b/packages/indexer/src/sinks/drizzle/utils.ts index 7beee5d..a049f00 100644 --- a/packages/indexer/src/sinks/drizzle/utils.ts +++ b/packages/indexer/src/sinks/drizzle/utils.ts @@ -1,9 +1,72 @@ -import type { PgTableFn } from "drizzle-orm/pg-core"; +import type { + BuildColumns, + BuildExtraConfigColumns, + NotNull, + Placeholder, + SQL, +} from "drizzle-orm"; +import type { + PgColumnBuilderBase, + PgCustomColumnBuilder, + PgTable, + PgTableExtraConfig, + PgTableWithColumns, +} from "drizzle-orm/pg-core"; import { pgTable as drizzlePgTable } from "drizzle-orm/pg-core"; import range from "postgres-range"; import { Int8Range, int8range } from "./Int8Range"; -export const pgTable: PgTableFn = (name, columns, extraConfig?) => { +export type CursorColumnBuilder = NotNull< + PgCustomColumnBuilder<{ + name: "_cursor"; + dataType: "custom"; + columnType: "PgCustomColumn"; + data: Int8Range; + driverParam: undefined; + enumValues: undefined; + generated: undefined; + }> +>; + +// Redefining the type of `pgTable` to include the `_cursor` column. +export type PgTableWithCursorFn< + TSchema extends string | undefined = undefined, +> = < + TTableName extends string, + TColumnsMap extends Record, +>( + name: TTableName, + columns: TColumnsMap, + extraConfig?: ( + self: BuildExtraConfigColumns< + TTableName, + TColumnsMap & { _cursor: CursorColumnBuilder }, + "pg" + >, + ) => PgTableExtraConfig, +) => PgTableWithColumns<{ + name: TTableName; + schema: TSchema; + columns: BuildColumns< + TTableName, + TColumnsMap & { _cursor: CursorColumnBuilder }, + "pg" + >; + dialect: "pg"; +}>; + +// Same as the drizzle's `PgInsertValue` type, but without the `_cursor` column. +export type PgInsertValue = Omit< + { + [Key in keyof TTable["$inferInsert"]]: + | TTable["$inferInsert"][Key] + | SQL + | Placeholder; + } & {}, + "_cursor" +>; + +export const pgTable: PgTableWithCursorFn = (name, columns, extraConfig?) => { return drizzlePgTable( name, { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 540e7b4..8e34ab0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -141,8 +141,8 @@ importers: specifier: ^6.5.0 version: 6.5.0 drizzle-orm: - specifier: ^0.33.0 - version: 0.33.0(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4) + specifier: ^0.35.0 + version: 0.35.2(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4) postgres: specifier: ^3.4.4 version: 3.4.4 @@ -224,8 +224,8 @@ importers: specifier: ^6.5.0 version: 6.5.0 drizzle-orm: - specifier: ^0.33.0 - version: 0.33.0(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4) + specifier: ^0.35.0 + version: 0.35.2(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4) postgres: specifier: ^3.4.4 version: 3.4.4 @@ -460,8 +460,8 @@ importers: specifier: ^6.5.0 version: 6.5.0 drizzle-orm: - specifier: ^0.33.0 - version: 0.33.0(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4) + specifier: ^0.35.2 + version: 0.35.2(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4) pg: specifier: ^8.12.0 version: 8.13.0 @@ -2034,13 +2034,13 @@ packages: resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==} engines: {node: '>=12'} - drizzle-orm@0.33.0: - resolution: {integrity: sha512-SHy72R2Rdkz0LEq0PSG/IdvnT3nGiWuRk+2tXZQ90GVq/XQhpCzu/EFT3V2rox+w8MlkBQxifF8pCStNYnERfA==} + drizzle-orm@0.35.2: + resolution: {integrity: sha512-bLQtRchl8QvRo2MyG6kcZC90UDzR7Ubir4YwOHV3cZPdJbF+4jU/Yt0QOczsoXe25wLRt6CtCWLXtSDQKft3yg==} peerDependencies: '@aws-sdk/client-rds-data': '>=3' '@cloudflare/workers-types': '>=3' '@electric-sql/pglite': '>=0.1.1' - '@libsql/client': '*' + '@libsql/client': '>=0.10.0' '@neondatabase/serverless': '>=0.1' '@op-engineering/op-sqlite': '>=2' '@opentelemetry/api': ^1.4.1 @@ -4902,7 +4902,7 @@ snapshots: dotenv@16.4.5: {} - drizzle-orm@0.33.0(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4): + drizzle-orm@0.35.2(@opentelemetry/api@1.9.0)(@types/better-sqlite3@7.6.11)(@types/pg@8.11.10)(better-sqlite3@11.1.2)(pg@8.13.0)(postgres@3.4.4): optionalDependencies: '@opentelemetry/api': 1.9.0 '@types/better-sqlite3': 7.6.11