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

main
mdx 102 lines 2.86 KB
Raw
1 ---
2 title: "Part 1: Your First Database"
3 description: Create a Turso group and a template database, migrated from a Drizzle schema.
4 sidebar:
5 order: 1
6 ---
7
8 Build a notes API where every customer (tenant) gets their own Turso database. A Worker creates each database by copying a template, then reads and writes notes with Drizzle.
9
10 The finished code is in [`examples/cloudflare-turso-drizzle`](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-turso-drizzle).
11
12 ## Prerequisites
13
14 Finish [Turso setup](/turso/setup) and [Cloudflare setup](/cloudflare/setup). Turso's free plan is enough.
15
16 Start in `src/Db.ts`. Every tenant's database will be a copy of the template you make here.
17
18 ## Create a group
19
20 ```diff lang="typescript"
21 +export const Tenants = Turso.Group("Tenants", { location: "aws-us-east-1" });
22 ```
23
24 A group decides where its databases run. All tenant databases go in this group.
25
26 ## Define the notes table
27
28 ```diff lang="typescript"
29 +// src/schema.ts
30 +import { sql } from "drizzle-orm";
31 +import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
32 +
33 +export const Notes = sqliteTable("notes", {
34 + id: integer("id").primaryKey({ autoIncrement: true }),
35 + body: text("body").notNull(),
36 + createdAt: integer("created_at", { mode: "timestamp" })
37 + .notNull()
38 + .default(sql`(unixepoch())`),
39 +});
40 ```
41
42 Turso databases are SQLite, so use Drizzle's `sqlite-core` columns.
43
44 ## Turn the schema into migrations
45
46 ```diff lang="typescript"
47 +export const Template = Effect.gen(function* () {
48 + const schema = yield* Drizzle.Schema("NotesSchema", {
49 + schema: "./src/schema.ts",
50 + out: "./migrations",
51 + dialect: "sqlite",
52 + });
53 +});
54 ```
55
56 When `src/schema.ts` changes, `Drizzle.Schema` writes a new migration to `./migrations`. Commit that folder.
57
58 ## Create the template database
59
60 ```diff lang="typescript"
61 export const Template = Effect.gen(function* () {
62 + const group = yield* Tenants;
63 const schema = yield* Drizzle.Schema("NotesSchema", {
64 schema: "./src/schema.ts",
65 out: "./migrations",
66 dialect: "sqlite",
67 });
68 + return yield* Turso.Database("Template", {
69 + group: group.name,
70 + migrations: schema,
71 + });
72 });
73 ```
74
75 Each deploy runs new migrations on the template. The template has tables but no rows, so each tenant starts empty.
76
77 ## Add the providers
78
79 ```diff lang="typescript"
80 // alchemy.run.ts
81 export default Alchemy.Stack(
82 "CloudflareTursoDrizzleExample",
83 {
84 + providers: Layer.mergeAll(Cloudflare.providers(), Turso.providers(), Drizzle.providers()),
85 state: Alchemy.localState(),
86 },
87 Effect.gen(function* () {
88 + const template = yield* Template;
89 + return { template: template.name };
90 }),
91 );
92 ```
93
94 ## Deploy
95
96 ```diff lang="sh"
97 +pnpm alchemy deploy
98 ```
99
100 Alchemy prints the template database's name. It has an empty `notes` table.
101
102 In [Part 2](/turso/tutorial/part-2), you'll add a Worker that creates a database for each tenant.