The best way to control Claude Code, Codex and more from any computer or phone

main
md 70 lines 2.17 KB
Rendered Raw
1 # @alchemy.run/sql
2
3 SQL for [Alchemy](https://alchemy.run). Typed Effect SQL clients for Postgres, MySQL, and SQLite (libSQL), deploy-time migrations for every database resource, and a Postgres state store.
4
5 ```sh
6 npm install alchemy @alchemy.run/sql @effect/sql-pg pg effect
7 ```
8
9 Query Postgres from any runtime. The connection opens on the first query of a request and closes when the request ends, so nothing connects at deploy time:
10
11 ```typescript
12 // src/worker.ts
13 import * as Cloudflare from "@alchemy.run/cloudflare";
14 import * as SQL from "@alchemy.run/sql/Postgres";
15 import * as Config from "effect/Config";
16 import * as Effect from "effect/Effect";
17 import * as HttpServerResponse from "effect/http/HttpServerResponse";
18
19 export default Cloudflare.Worker(
20 "Worker",
21 { main: import.meta.url },
22 Effect.gen(function* () {
23 const sql = yield* SQL.Postgres({ url: Config.Redacted("DATABASE_URL") });
24
25 return {
26 fetch: Effect.gen(function* () {
27 const users = yield* sql`SELECT * FROM users`;
28 return yield* HttpServerResponse.json(users);
29 }).pipe(Effect.orDie),
30 };
31 }),
32 );
33 ```
34
35 The same shape works for MySQL and SQLite:
36
37 | Database | Import | Driver |
38 | --- | --- | --- |
39 | Postgres | `@alchemy.run/sql/Postgres` | `@effect/sql-pg`, `pg` |
40 | MySQL | `@alchemy.run/sql/MySQL` | `@effect/sql-mysql2`, `mysql2` |
41 | SQLite (libSQL, Turso, local files) | `@alchemy.run/sql/SQLite` | `@effect/sql-libsql` |
42
43 Keep Alchemy's own state in a Postgres database you already run:
44
45 ```typescript
46 // alchemy.run.ts
47 import * as Alchemy from "alchemy";
48 import { postgresState } from "@alchemy.run/sql/PostgresState";
49 import * as Config from "effect/Config";
50 import * as Effect from "effect/Effect";
51 import * as Layer from "effect/Layer";
52
53 export default Alchemy.Stack(
54 "MyApp",
55 {
56 providers: Layer.empty,
57 state: postgresState({ url: Config.Redacted("STATE_DATABASE_URL") }),
58 },
59 Effect.gen(function* () {
60 return {};
61 }),
62 );
63 ```
64
65 ## Learn more
66
67 - [SQL on alchemy.run](https://alchemy.run/sql)
68 - [Effect SQL with Postgres](https://alchemy.run/sql/effect-sql/postgres)
69 - [Migrations](https://alchemy.run/sql/effect-sql/migrations)
70 - [State stores](https://alchemy.run/state-store)