|
| 1 | +// Copied into an isolated directory by check-package.ts and compiled with |
| 2 | +// `declaration: true`. A library that defines its tables and queries in one |
| 3 | +// package and runs them from another exports them, and tsc must then be able to |
| 4 | +// name every type they carry through the public entry points. Each export below |
| 5 | +// once failed with TS4023 or TS2742, pointing into a hashed internal chunk. |
| 6 | +import type { DateTime } from "effect" |
| 7 | +import * as CH from "@maple-dev/effect-orm/clickhouse" |
| 8 | +import * as PG from "@maple-dev/effect-orm/postgres" |
| 9 | + |
| 10 | +export const Events = CH.table("events", { external: true, columns: { appId: CH.uint32, name: CH.string } }) |
| 11 | + |
| 12 | +// A query with a param: its type carries `ParamEntry`. |
| 13 | +export const eventsByApp = CH.from(Events) |
| 14 | + .select(($) => ({ name: $.name })) |
| 15 | + .where(($) => [$.appId.eq(CH.param.int("appId"))]) |
| 16 | + |
| 17 | +// A custom function: its type carries `ParamsIn`. |
| 18 | +export const toStartOfDay = CH.defineFn<[CH.Expr<DateTime.Utc>], DateTime.Utc>("toStartOfDay", CH.dateTime) |
| 19 | + |
| 20 | +export const Accounts = PG.table("accounts", { columns: { id: PG.int4, org: PG.text }, primaryKey: ["id"] }) |
| 21 | +export const accountsByOrg = PG.from(Accounts) |
| 22 | + .select(($) => ({ id: $.id })) |
| 23 | + .where(($) => [$.org.eq(PG.param.string("org"))]) |
| 24 | + |
| 25 | +// An optional filter (`when`, `whenTrue`) and a param named by a union carry |
| 26 | +// `OptionalParamEntry` / `OptionalParams`. |
| 27 | +export const eventsMaybeByName = (name: string | undefined) => |
| 28 | + CH.from(Events) |
| 29 | + .select(($) => ({ appId: $.appId })) |
| 30 | + .where(($) => [CH.when(name, () => $.name.eq(CH.param.string("name")))]) |
| 31 | +export const eventsByAppOptional = CH.from(Events) |
| 32 | + .select(($) => ({ name: $.name })) |
| 33 | + .where(($) => [CH.whenTrue(true, () => $.appId.eq(CH.param.int("appId")))]) |
| 34 | + |
| 35 | +export const byUnionNamedParam = (byApp: boolean) => |
| 36 | + CH.from(Events) |
| 37 | + .select(($) => ({ name: $.name })) |
| 38 | + .where(($) => [$.appId.eq(CH.param.int(byApp ? "appId" : "fallbackAppId"))]) |
| 39 | + |
| 40 | +// Joins, CTEs, subqueries and unions over parameterized queries. |
| 41 | +export const Sessions = CH.table("sessions", { external: true, columns: { appId: CH.uint32, id: CH.string } }) |
| 42 | +export const joined = CH.from(Events) |
| 43 | + .innerJoin(Sessions, "s", (e, s) => e.appId.eq(s.appId)) |
| 44 | + .select(($) => ({ name: $.name })) |
| 45 | + .where(($) => [$.appId.eq(CH.param.int("appId"))]) |
| 46 | +export const Recent = CH.table("recent", { external: true, columns: { name: CH.string } }) |
| 47 | +export const withRecent = CH.from(Recent).withCTE("recent", eventsByApp).select(($) => ({ name: $.name })) |
| 48 | +export const fromSub = CH.fromQuery(eventsByApp, "sub").select(($) => ({ name: $.name })) |
| 49 | +export const both = CH.unionAll(eventsByApp, CH.from(Events).select(($) => ({ name: $.name }))) |
0 commit comments