diff --git a/.changeset/eight-bees-shake.md b/.changeset/eight-bees-shake.md new file mode 100644 index 000000000..ff55a89b6 --- /dev/null +++ b/.changeset/eight-bees-shake.md @@ -0,0 +1,5 @@ +--- +"@ponder/core": patch +--- + +Exported Drizzle [`alias`](https://orm.drizzle.team/docs/joins#aliases--selfjoins) function which can be used in API functions. diff --git a/packages/core/src/drizzle/runtime.ts b/packages/core/src/drizzle/runtime.ts index 33369d35a..ce9341f1b 100644 --- a/packages/core/src/drizzle/runtime.ts +++ b/packages/core/src/drizzle/runtime.ts @@ -10,6 +10,7 @@ import { isScalarColumn, } from "@/schema/utils.js"; import { getTables } from "@/schema/utils.js"; +import { type Table, TableAliasProxyHandler } from "drizzle-orm"; import { drizzle as drizzleSQLite } from "drizzle-orm/better-sqlite3"; import { drizzle as drizzlePg } from "drizzle-orm/node-postgres"; import { pgSchema, pgTable } from "drizzle-orm/pg-core"; @@ -20,6 +21,7 @@ import { numeric as PgNumeric, text as PgText, } from "drizzle-orm/pg-core"; +import type { View } from "drizzle-orm/sql"; import { integer as SQLiteInteger, real as SQLiteReal, @@ -30,6 +32,7 @@ import { SQLiteBigintBuilder } from "./bigint.js"; import { PgHexBuilder, SQLiteHexBuilder } from "./hex.js"; import { SQLiteJsonBuilder } from "./json.js"; import { PgListBuilder, SQLiteListBuilder } from "./list.js"; +import type { BuildAliasTable } from "./select.js"; export const createDrizzleDb = (database: DatabaseService) => { if (database.kind === "postgres") { @@ -70,6 +73,13 @@ export const createDrizzleDb = (database: DatabaseService) => { } }; +export function alias( + table: tableOrView, + alias: alias, +): BuildAliasTable { + return new Proxy(table, new TableAliasProxyHandler(alias, false)) as any; +} + type SQLiteTable = Parameters[1]; type PostgresTable = Parameters[1]; type DrizzleTable = { [tableName: string]: any }; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e198bd629..abbfcd890 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -58,3 +58,5 @@ export { max, min, } from "drizzle-orm"; + +export { alias } from "@/drizzle/runtime.js";