alepha@docs:~/docs/reference/providers$
cat SqlExpressionProvider.md | pretty1 min read
Last commit:
#SqlExpressionProvider
#Import
typescript
1import { SqlExpressionProvider } from "alepha/orm";
#Overview
Dialect-neutral SQL expression builder.
Postgres stores timestamps as real timestamps; SQLite (and therefore Cloudflare D1) stores them as epoch-millisecond integers. Every date expression has to branch on that, which is why applications end up hand-writing the same aggregation twice. These helpers emit the correct fragment for the active dialect so the expression is written once.
ts
1class StatsController { 2 protected readonly sqlx = $inject(SqlExpressionProvider); 3 protected readonly database = $inject(DatabaseProvider); 4 protected readonly quests = $repository(quests); 5 6 perDay = () => this.database.run( 7 sql`SELECT ${this.sqlx.dateDay(this.quests.table.createdAt)} AS day, 8 COUNT(*) AS total 9 FROM ${this.quests.table} GROUP BY 1`,10 z.object({ day: z.text(), total: z.integer() }),11 );12}