Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions specs/epics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace.

| Epic | Cluster | Status | What it is |
|------|---------|--------|------------|
| [`algolia-search/`](./algolia-search/) | **AL** | In progress (AL0) | Vercel's Algolia Integration Example, its `query()` on a pg client per request over Hyperdrive to Supabase Postgres, its Algolia keys the Worker's secrets and its table and demo products applied by db-migrate — and an Orun baseline that instantiates it in one landing (AL2) |

As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`.
11 changes: 11 additions & 0 deletions specs/epics/algolia-search/IMPLEMENTATION-STATUS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# algolia-search — implementation status

| Milestone | State | Landed |
|---|---|---|
| AL0 — the spec | ✅ | AL-1 |
| AL1 — the search on Workers and Supabase | planned | AL-2 |
| AL2 — a baseline | planned | AL-3 |

## Departures from the upstream template

Recorded as they land, from AL1 on.
33 changes: 33 additions & 0 deletions specs/epics/algolia-search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Epic: algolia-search (AL)

**Algolia's starter is search as a client concern: InstantSearch in the browser asks Algolia's index, which Algolia's own Supabase connector fills from a Postgres `products` table, and the one server read is the product page, which shows the fields the index never gets. On Cloudflare the socket goes through Hyperdrive to a Supabase Postgres project — postgres-drizzle-starter's data plane — and the code that changes is where the database client and the Algolia keys come from: upstream's lazy module-level `pg.Pool` becomes one `pg` client per request from the `HYPERDRIVE` binding, and the keys the template inlined into the browser bundle at build become the Worker's secrets, read when the page renders.**

The port keeps the page, the InstantSearch UI and its facets, the hit cards, the product page and its query, and the connector's transformation. Upstream's `demo/seed.sql`, pasted into Supabase's SQL Editor by hand, becomes two `packages/db` migrations — the table, and its twenty demo products byte for byte — applied by `db-migrate` with row-level security before the Worker serves; a local database gets the very same files through `pnpm db:migrate`. Until the owner connects Algolia the page keeps the template's notice and lists the catalogue from Postgres under it. It adds the Workers build, tests (InstantSearch rendered against an Algolia stand-in, in jsdom and in a headless Chrome), the Supabase and Hyperdrive Terraform roots and the deploy contract, and makes the repository an Orun baseline.

## Status

| Field | Value |
|-------|-------|
| Status | In progress — AL0 on main |
| Cluster | **AL** (AL0–AL2) |
| Owner(s) | `apps/web` (the pages, the search UI, `lib/db.ts`, `lib/algolia.ts`, `components/catalogue.tsx`, `scripts/migrate.mjs`) · `packages/db` (the migrations and their runner) · `tests/web`, `tests/db` · `infra/terraform/{supabase,cloudflare-hyperdrive}`, `infra/db-migrate` |
| Builds on | `postgres-kysely` (its scaffolding, Supabase and Hyperdrive roots, migrations, health check and baseline machinery), `prisma-nextjs-blog` (the Worker-secrets push), and through them `postgres-drizzle-starter` and `lumen` |
| Upstream | [algolia/algolia-supabase-vercel-starter](https://github.com/algolia/algolia-supabase-vercel-starter) at `da3f27b` (2026-09-04), the Vercel template "Algolia Integration Example" |
| Changes | The client resolution (module-level pool → a pg client per request over Hyperdrive, behind upstream's `query()`), the Algolia keys read per request from Worker secrets, the seed as migrations, the catalogue while not connected, the Workers build, tests, two Terraform roots, the deploy contract; page copy says Cloudflare |
| Decisions locked | (1) `query()` stays upstream's name and API; a WeakMap by the request's ExecutionContext keeps one connected client per request. (2) The Algolia keys are optional Worker secrets, never `NEXT_PUBLIC_`, never in a build. (3) The table, RLS and the demo rows are `db-migrate`'s. (4) The connector is the owner's step in Algolia's dashboard. (5) Infra before app on main: supabase → cloudflare-hyperdrive → db-migrate → web, enforced; advisory on pull requests. |
| Shipped as | `baseline-v1`; proven offline (Baseline CI: migrated into a real Postgres and served through the built Worker under `wrangler dev`, searched in a headless Chrome), deploy gated until a workspace is linked |

## Read order

1. `design.md` — what maps to what, the connection and key flow, what is out of scope
2. `implementation-plan.md` — the milestones and what "done" means for each
3. `risks-and-open-questions.md` — what could go wrong and what was decided
4. `IMPLEMENTATION-STATUS.md` — what actually shipped (kept distinct from intent)

## Milestones at a glance

| Milestone | What it lands | Done when |
|---|---|---|
| AL0 — the spec | this doc set and the task contracts | merged on `main` |
| AL1 — the search on Workers and Supabase | `apps/web`, `packages/db`, the two Terraform roots, `infra/db-migrate`, `tests/web`, `tests/db` | green; `wrangler dev` lists the catalogue over a migrated database, and the search runs against a stand-in |
| AL2 — a baseline | card, build document, rename, bootstrap tooling, the product's README and context pack, the baseline's tests | a new product goes from nothing to live on stage and prod in one landing; Baseline CI green |
82 changes: 82 additions & 0 deletions specs/epics/algolia-search/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# algolia-search — design

## 1. What the template is

[algolia/algolia-supabase-vercel-starter](https://github.com/algolia/algolia-supabase-vercel-starter)
at `da3f27b` (2026-09-04) — the Vercel template "Algolia Integration Example":
Next.js 16.3.0 (App Router), `pg` 8.22, `algoliasearch` 5 (the lite client)
and `react-instantsearch` 7, Tailwind 4, Geist. Three moving parts:

- **The home page** renders the template's hero and `SearchExperience`, a
client component: InstantSearch with a search box, category, price and
rating facets, a "hide out of stock" toggle, stats and highlighted hit
cards. It reads `NEXT_PUBLIC_ALGOLIA_APP_ID` and
`NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY`, which `next.config`'s `env` inlines
into the browser bundle at build (mapping the bare `ALGOLIA_*` names the
Vercel integration may inject); without them it shows a notice.
- **The product page** (`/products/[id]`) reads one row from Postgres with
`select * from products where id = $1` through a lazy module-level
`pg.Pool` (`lib/db.ts`, with an `ssl` override for Supabase's pooler) and
shows the fields only the database holds: cost price, supplier, stock
location, internal notes.
- **The data**: `demo/seed.sql` (a 12-column table and twenty upserted
products) pasted into the Supabase SQL Editor, and Algolia's Supabase
connector — configured in Algolia's dashboard with `demo/transform.js` —
which maps `id` to `objectID`, keeps the searchable fields, derives
`in_stock` and drops the internal ones.

## 2. The data plane

| Upstream | Here |
|---|---|
| Vercel's Supabase integration provisions a project and injects `POSTGRES_URL` | `infra/terraform/supabase` creates one project per environment; `infra/terraform/cloudflare-hyperdrive` one Hyperdrive config over it; their applies publish the credentials and the config id as orun secrets |
| A lazy module-level `pg.Pool` over `POSTGRES_URL`, `sslmode` stripped and `rejectUnauthorized: false` | `lib/db.ts`: `query(text, params)` unchanged, run on one `pg.Client` per request over `env.HYPERDRIVE.connectionString`, kept in a WeakMap keyed by the request's ExecutionContext and connected once (the Workers runtime refuses to reuse a socket across requests). No `ssl` override: the Worker talks to Hyperdrive, Hyperdrive talks TLS to Supabase. Without a Cloudflare context, upstream's own pool over `POSTGRES_URL` |
| `demo/seed.sql` pasted by hand | `packages/db/src/migrations/010_products` (its CREATE TABLE), `020_row_level_security` (RLS with no policy, behind a guard), `030_demo_products` (its twenty upserts, byte for byte), applied by `infra/db-migrate` per environment before the Worker deploys; `pnpm db:migrate` applies the same files to a local database (`apps/web/scripts/migrate.mjs`, ledger in schema `local_migrations`) |
| `pg` on Node | `pg` on workerd under `nodejs_compat`, its socket `pg-cloudflare`'s `workerd` export. `pg` is a Next server-external package, so the build traces it for Node and copies only pg-cloudflare's empty default export; `next.config.ts` ships the package whole (`outputFileTracingIncludes`) |

## 3. The keys

Orun never puts a SaaS key in a build. The application id and the
search-only key are browser-safe by Algolia's design — the template hands them
to every visitor — but inlining them at build would mean a bundle per key, a
rebuild per rotation, and a build that has to hold the product's secrets. So:

- They are the product's optional Orun secrets `ALGOLIA_APP_ID` and
`ALGOLIA_SEARCH_API_KEY` (Algolia's own bare names, which upstream's
next.config already read), resolved by the `web` component's
`optionalSecretEnv`.
- The build runs with them unset (`env -u`); after it,
`tooling/wire/worker-secrets.mjs` pushes the ones that are set to the
Worker with `wrangler secret bulk` (a verify lane, with no Cloudflare
credential, pushes nothing).
- The page (`force-dynamic`) reads them from `getCloudflareContext().env`
per request (`lib/algolia.ts`) and hands them to `SearchExperience` as
props; the component builds its lite client from them. An optional
`ALGOLIA_HOST` replaces Algolia's own hosts (the e2e's stand-in).
- Unset, the page renders the template's notice — naming the secrets — and
`components/catalogue.tsx` under it: the products read from Postgres in the
hits' own cards, only the fields the connector would index. The page wraps
both states in `data-search="connected" | "not-connected"`, which the
deploy's smoke reads.
- The write/admin key is never read: the connector indexes from Algolia's
dashboard, as upstream.

## 4. The deploy contract

postgres-kysely's, unchanged in shape: `web` (cloudflare-workers-assets-turbo)
depends on `cloudflare-hyperdrive` and `db-migrate`, which depend on
`supabase`; every edge `mode: enforced` on a push to main and advisory on a
pull request; `wrangler.jsonc` committed as the fixture render of
`wrangler.template.jsonc`, re-rendered by the stage and prod lanes from their
wiring document. The smoke reads the home page's `data-search=` and
`/api/health`'s `"ok":true`.

## 5. Out of scope

- Algolia's connector and index settings: the owner's, in Algolia's
dashboard (the product's README, "Connect Algolia"). There is no Orun
composition for Algolia, and the connector needs the Supabase project's
credentials, which it asks Supabase for itself.
- Server-side InstantSearch (`react-instantsearch-nextjs`): the template is
client-only, and so is this.
- Write paths: the template is read-only.
31 changes: 31 additions & 0 deletions specs/epics/algolia-search/epic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The epic's declaration. metadata.name equals the directory name; metadata.key
# is the task-key prefix this epic reserves (AL → AL-1, AL-2, …). Owner, target
# date and health are the platform's — never written here.
apiVersion: orun.io/v1
kind: Epic
metadata:
name: algolia-search
key: AL
spec:
title: "Vercel's Algolia Integration Example on Cloudflare Workers and Supabase"
summary: "Vercel's Algolia Integration Example — Algolia's InstantSearch over a Postgres products table, and a product page that reads the fields only the database holds — runs on Cloudflare Workers + Static Assets with its Postgres a Supabase project reached through Hyperdrive, the table and the template's demo products applied by db-migrate before the app serves and the Algolia keys the Worker's own secrets, inside the Cirrus baseline's repository conventions, and becomes an Orun baseline that takes a new product from nothing to live on stage and prod in one landing."
state: started
milestones:
- name: "AL0 — the spec"
exitCriteria:
- "the repository is born with its intent, CI, tooling and spec index, and this doc set and the task contracts are on main"
tasks: [AL-1]
- name: "AL1 — the search on Workers and Supabase"
exitCriteria:
- "apps/web is the upstream template on Next.js 16.3.8, built by @opennextjs/cloudflare into one Worker with Static Assets; lib/db.ts keeps upstream's `query()`, run on one pg client per request over the HYPERDRIVE binding"
- "the Algolia application id and search-only key are the Worker's secrets, read per request and handed to InstantSearch as props, never in a build; without them the page shows the template's notice and the catalogue from Postgres"
- "the products table, row-level security and upstream's twenty demo products are packages/db migrations that infra/db-migrate applies before the Worker deploys, after the Supabase project and the Hyperdrive config Terraform creates"
- "`pnpm typecheck`, `pnpm lint`, `pnpm test` and `pnpm build` are green; `wrangler dev` over the build lists the catalogue and serves the product pages from a migrated database, and with the Algolia secrets set a browser's InstantSearch renders hits from a stand-in"
- "the README says how to run, test, deploy and connect it, and how every Vercel piece maps to Cloudflare and Supabase"
tasks: [AL-2]
- name: "AL2 — a baseline"
exitCriteria:
- "blueprint.yaml (the console card) and repo-blueprint.yaml (the build) take a new product from nothing to live on stage and prod in one landing (supabase → cloudflare-hyperdrive → db-migrate → web), then record it; the card asks for no Algolia key"
- "a product built from it carries the product only, renamed throughout to its own name, repository, Worker, Supabase project and Hyperdrive config, with the upstream template's links and Algolia's own key names left alone"
- "Baseline CI is green: contract tests, generated component docs, the blueprint read by the pinned orun, the leak gate, the offline bootstrap with the product's own plans, and the end-to-end tier — migrated into a real Postgres, built with no Algolia key reaching the bundle, dry-deployed and served without Algolia and with it (a headless Chrome running the search against a stand-in), and its table shut to the Data API's roles"
tasks: [AL-3]
60 changes: 60 additions & 0 deletions specs/epics/algolia-search/implementation-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# algolia-search — implementation plan

Three milestones, one pull request each, squash-merged when green.

## AL0 — the spec (AL-1)

The repository is born (intent with the placeholder workspace, Orun CI byte
for byte turso-starter's, the shared TypeScript and ESLint configuration, the
wrangler renderer, the migrations manifest tool, the spec index), then this
doc set and the three task contracts land.

**Done when** the epic and the contracts are on `main`.

## AL1 — the search on Workers and Supabase (AL-2)

1. The template in `apps/web` on Next.js 16.3.8 with `@opennextjs/cloudflare`:
`output: standalone`, the monorepo tracing root, `initOpenNextCloudflareForDev()`,
pg-cloudflare shipped whole through `outputFileTracingIncludes`; Geist from
the `geist` package; `instantsearch.js` a direct dependency (pnpm does not
hoist it).
2. `lib/db.ts`: upstream's `query()`, on one `pg` client per request (a
WeakMap keyed by the request's ExecutionContext, Hyperdrive's connection
string, connected once), upstream's own pool over `POSTGRES_URL` without a
context.
3. `lib/algolia.ts` and the search UI: the application id and search-only key
(and an optional host) read from the Worker per request, handed to
`SearchExperience` as props; next.config's `env` inlining removed.
4. `components/catalogue.tsx`: the products from Postgres in the hits' cards
while search is not connected; the page marks its state (`data-search`).
5. The table, row-level security and the demo products as `packages/db`
migrations (010, 020, 030) with their manifest; `infra/db-migrate` applies
them; `pnpm db:migrate` applies the same files locally.
6. `/api/health`: 200 only with the table migrated and RLS on; it reports
whether search is connected.
7. The Algolia secrets as `optionalSecretEnv`, unset for the build and pushed
to the Worker after it (`tooling/wire/worker-secrets.mjs`).
8. `infra/terraform/supabase` and `infra/terraform/cloudflare-hyperdrive`, the
wiring template and its committed fixture render, `apps/web/component.yaml`.
9. `tests/web` (InstantSearch in jsdom against an in-process Algolia stand-in)
and `tests/db` (PGlite; the migrations held to upstream's seed).

**Done when** typecheck, lint, test and build are green and `wrangler dev`
over the build lists the catalogue and serves the product pages from a
migrated database, says search is not connected without the secrets, and with
them a headless Chrome's InstantSearch renders hits from a stand-in.

## AL2 — a baseline (AL-3)

postgres-kysely's machinery on this tree: `blueprint.yaml` (the console card:
Cloudflare and Supabase, five brokered keys, the subdomain read from the
connection, no domain and no Algolia key asked), `repo-blueprint.yaml` (one
landing, `01-product`, then the record, `02-docs`), `.baseline/` (identity,
the product's README with "Connect Algolia", task contracts), the rename
engine (the upstream template's links protected, Algolia's own key names
allowed by the leftover sweep), the bootstrap helpers, the deployment-record
renderer, the phase pages, the context pack, and the baseline's tests — the
e2e tier running the search in a headless Chrome against an Algolia stand-in.

**Done when** Baseline CI is green on the pull request and on `main`, then
`baseline-v1` is tagged.
Loading
Loading