Skip to content

Commit

Permalink
Export new alias helper function for select API (#1048)
Browse files Browse the repository at this point in the history
* alias

* chore: changeset

* nits

* Update eight-bees-shake.md
  • Loading branch information
kyscott18 authored Aug 19, 2024
1 parent 69e563d commit c88b78a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-bees-shake.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions packages/core/src/drizzle/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -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") {
Expand Down Expand Up @@ -70,6 +73,13 @@ export const createDrizzleDb = (database: DatabaseService) => {
}
};

export function alias<tableOrView extends Table | View, alias extends string>(
table: tableOrView,
alias: alias,
): BuildAliasTable<tableOrView, alias> {
return new Proxy(table, new TableAliasProxyHandler(alias, false)) as any;
}

type SQLiteTable = Parameters<typeof sqliteTable>[1];
type PostgresTable = Parameters<typeof pgTable>[1];
type DrizzleTable = { [tableName: string]: any };
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ export {
max,
min,
} from "drizzle-orm";

export { alias } from "@/drizzle/runtime.js";

0 comments on commit c88b78a

Please sign in to comment.