Skip to content

Commit

Permalink
indexer: update drizzle and drizzle sink (#107)
Browse files Browse the repository at this point in the history
Update to the latest drizzle version to solve type issues on new
projects.
  • Loading branch information
fracek authored Oct 20, 2024
2 parents 0ba1f8b + 68700fe commit 5cd8d6f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "indexer: update drizzle and drizzle sink",
"packageName": "@apibara/indexer",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion examples/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/starknet-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
Expand Down
9 changes: 6 additions & 3 deletions packages/indexer/src/sinks/drizzle/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,6 +34,9 @@ export class DrizzleSinkInsert<
};
},
);
return originalInsert.values(cursoredValues as PgInsertValue<TTable>[]);

return originalInsert.values(
cursoredValues as DrizzleInsertValue<TTable>[],
);
}
}
67 changes: 65 additions & 2 deletions packages/indexer/src/sinks/drizzle/utils.ts
Original file line number Diff line number Diff line change
@@ -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<string, PgColumnBuilderBase>,
>(
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<TTable extends PgTable> = Omit<
{
[Key in keyof TTable["$inferInsert"]]:
| TTable["$inferInsert"][Key]
| SQL
| Placeholder;
} & {},
"_cursor"
>;

export const pgTable: PgTableWithCursorFn = (name, columns, extraConfig?) => {
return drizzlePgTable(
name,
{
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cd8d6f

Please sign in to comment.