|
| 1 | +# Data recipe (Drizzle) |
| 2 | + |
| 3 | +> Status: **doc-level, not CI-verified** — integration shape, not a tested |
| 4 | +> artifact. It will move to verified status when a consumer reproduces it |
| 5 | +> (0.42.x recipe follow-up). |
| 6 | +
|
| 7 | +openElement does not ship an ORM or a data layer. Drizzle (or any query |
| 8 | +builder) runs inside loaders and actions, which are ordinary server |
| 9 | +functions on a `rendering: 'dynamic'` route: |
| 10 | + |
| 11 | +1. **Reads**: run queries in `loader`; return plain serializable data. The |
| 12 | + page renders it in DSD at request time; pure-static routes keep using |
| 13 | + build-time data instead. |
| 14 | +2. **Writes**: run mutations in `action` after validation (see the |
| 15 | + validation recipe). On conflict or constraint failure, |
| 16 | + `return fail(422, { error })` to re-render with the echo; on success, |
| 17 | + `redirect()` to the changed resource (303 PRG, revalidation re-runs the |
| 18 | + loader for you). |
| 19 | +3. **Connection**: create the client once at module scope of a small |
| 20 | + `db.ts` and import it from routes. Connection secrets belong to the |
| 21 | + deployment environment (`ctx.env`), never to the client bundle — |
| 22 | + loaders/actions never ship to the browser. |
| 23 | + |
| 24 | +```ts |
| 25 | +// app/db.ts |
| 26 | +export const db = drizzle(process.env.DATABASE_URL!); |
| 27 | + |
| 28 | +// app/routes/posts.tsx |
| 29 | +export async function loader() { |
| 30 | + return { posts: await db.select().from(postsTable).limit(20) }; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +Transactions, pooling and migrations stay with Drizzle; the framework |
| 35 | +owns only the route-to-interaction loop around them. |
0 commit comments