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
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ soft-delete / status / type view it models.
and are NOT in the cross-port registry — `view.text`, `view.textarea`, `view.date`,
`view.month`, `view.hotlink`, `view.image`, `view.dropdown`, `view.radio`, `view.checkbox`,
`view.number`, `view.password`, `view.hidden`, `view.web`. Every port LOADS them, so shared metadata that
carries them is valid in a backend port; nothing outside TS/web reads them. **Audit these
only for TS adopters.** Only
carries them is valid in a backend port. The one exception is `view.password`: every port's generated
routes read it as a credential, so it is never returned and never written (see
"Credentials and excluded fields" in `docs/features/api-contract.md`).
Audit the other widgets only for TS adopters. Only
`view.base` / `view.currency` are cross-port-gated.

## Template — `template.*` (prompt pillar)
Expand Down
4 changes: 2 additions & 2 deletions .claude/skills/metaobjects-audit/references/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ meta gen # render → 3-way merge → write (overwrites @
meta gen --dry-run # preview without writing
meta gen Author Post # scope to named entities
meta verify --codegen # regenerate to temp, diff vs committed output
meta verify --templates # check {{field}} refs vs payload VO (bare `meta verify` default)
meta verify --templates # check {{field}} refs vs payload VO (a bare `meta verify` runs this and `--codegen`)
meta verify --db # schema drift — live DB vs metadata (requires DB connection)
meta verify --codegen --templates --db # all three drift checks in one pass
```
Expand Down Expand Up @@ -199,4 +199,4 @@ TypeScript is the reference implementation — it ships the full feature set:
- Output-parser codegen: **complete** (`outputParser()` generator; `parse*`/`safeParse*`/`extract*` emitted as `<Name>.response.ts` per **responding `template.prompt`** — one carrying `@responseRef`; ADR-0052).
- ObjectManager: **complete** (`@metaobjectsdev/runtime-ts`; `kyselyDriver` / `inMemoryDriver`).
- Schema migrate / `meta verify --db`: **complete** (TS owns the shared migrate engine — ADR-0015).
- D1 dialect: **TS-only by design** — `dialect: "d1"` in `metaobjects.config.ts` targets Cloudflare D1 via the Wrangler CLI layout. Do NOT recommend porting D1 support to other languages.
- D1 dialect: **TS-only by design** — `migrate.dialect: "d1"` (and `meta migrate --dialect d1`) targets Cloudflare D1 via the Wrangler CLI layout; codegen's `dialect` in `metaobjects.config.ts` is `"sqlite"` for D1 and `"d1"` there is refused. Do NOT recommend porting D1 support to other languages.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ build on a shared model.** A project may declare `dependencies` in
`.metaobjects/config.json` and `meta deps sync` a publisher's metadata into a
committed snapshot — that snapshot loads BEFORE your own files, so a foreign
abstract resolves via `extends` and a foreign node re-opens via `overlay: true`
exactly like a local one. Two rules are specific to that boundary:
exactly like a local one. A dependency names one transport: `path` (a directory
relative to the directory holding `.metaobjects/`), `npm` (an installed npm
package, e.g. `{ "name": "acme-model", "npm": "@acme/model", "dir": "metaobjects" }`)
or `python` (an installed Python package by its import name, e.g.
`{ "name": "acme-model", "python": "acme_model", "dir": "metaobjects" }`). For
`npm`/`python`, install the package with the project's package manager first;
`meta deps sync` locates it, copies its shared model into the snapshot, and says
what to install when it is missing. Prefer the package transport over a `path` into
someone else's checkout when the publisher releases a package. Two rules are
specific to that boundary:

- **Say `overlay: true` on every amendment to a node you don't own.** Within one
project the parser merges a same-`(type, package::name)` redeclaration whether
Expand Down
7 changes: 4 additions & 3 deletions .claude/skills/metaobjects-codegen/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ exact config, generator names and commands for this project; read it. The other
| `references/dependencies-and-shared-models.md` | the project consumes or publishes metadata across projects |
| `references/typescript-document-store.md` | a TypeScript project on MongoDB, or any store MetaObjects does not manage, wanting a tested repository generator |
| `references/typescript-mysql.md` | a TypeScript project on a MySQL database |
| `references/typescript-prisma.md` | a TypeScript project that keeps Prisma as its ORM |
| `references/typescript-retargeting.md` | a TypeScript project on a framework other than Fastify, Hono or React |
| `references/typescript-templates.md` | declarative Mustache template-codegen in a TypeScript project |
| `references/typescript-docs.md` | the docs generators (`docsFile`, `apiDocsFile`, `agentDocsFile`, `requirementsFile`, `metamodelDocsFile`, `docsSiteFile`) in a TypeScript project |

## Upgrading a project from 1.0 to 1.1

Read the one upgrade guide, https://github.com/metaobjectsdev/metaobjects/blob/v1.1.4/docs/features/migrations/upgrading-1.0-to-1.1.md, before the first `gen`, for every
Read the one upgrade guide, https://github.com/metaobjectsdev/metaobjects/blob/v1.2.0/docs/features/migrations/upgrading-1.0-to-1.1.md, before the first `gen`, for every
port the project uses; the build can stay green while the result is wrong. It lists the steps
in order and every change that needs action.

Expand All @@ -71,7 +72,7 @@ in order and every change that needs action.
`eject --list`; `dotnet meta gen --list`; `mvn metaobjects:eject -Dlist=true` in Java and Kotlin)
reports each copy that differs from the reference;
`eject <name> --force` takes the new reference for a copy you never edited, and a copy you
edited is three-way merged instead. Steps for 1.0.x: https://github.com/metaobjectsdev/metaobjects/blob/v1.1.4/docs/features/migrations/upgrading-within-1.0.md.
edited is three-way merged instead. Steps for 1.0.x: https://github.com/metaobjectsdev/metaobjects/blob/v1.2.0/docs/features/migrations/upgrading-within-1.0.md.

## Need an output? Write the generator — this is the primary path

Expand Down Expand Up @@ -147,7 +148,7 @@ without typechecking, so a wrong accessor runs silently.
Every port's minimal generator (each run through gen, verify and a convicted model
change) and two worked examples to copy — JSON Schema and OpenAPI 3.1, examples and not a
product surface — are in the guide "Write your own generator":
<https://github.com/metaobjectsdev/metaobjects/blob/v1.1.4/docs/recipes/write-your-own-generator.md>.
<https://github.com/metaobjectsdev/metaobjects/blob/v1.2.0/docs/recipes/write-your-own-generator.md>.

**Use a stock generator as shipped** when one already emits what you need:
`meta gen --list --probe` is the catalog, and each row says how to wire it with nothing
Expand Down
8 changes: 7 additions & 1 deletion .claude/skills/metaobjects-codegen/references/any-stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ reference generators target React and TanStack. Anything else is this page.
| Layer | Always available | When your target is not built in |
|---|---|---|
| **model** — types, create/PATCH validation, filter and sort allowlists | yes, in every port, for every store | — |
| **persistence** — how rows are read and written | Postgres, SQLite, D1; MySQL (TypeScript, Java) | §3 |
| **persistence** — how rows are read and written | Postgres, SQLite, D1; MySQL (TypeScript, Java); a Prisma schema (TypeScript) | §3 |
| **api** — the HTTP surface | Fastify, Hono | §4 |
| **client** — hooks, forms, grids | React, TanStack | §5 |

Expand All @@ -52,6 +52,12 @@ the column-type table and the behaviour differences are in `typescript-mysql.md`
OMDB's `MySQLDriver` reads and writes MySQL; the generated DTOs and controllers are
dialect-neutral.

**Prisma as the ORM (TypeScript).** Set `orm: "prisma"` and wire `prismaSchemaFile()`. The
model then writes `schema.prisma` (Postgres, MySQL, SQLite) beside Zod schemas and types, and
Prisma runs the database. `meta verify --codegen` and `--db` gate it. Setup, the mapping and
what it refuses are in `typescript-prisma.md`. Any other ORM (TypeORM, Sequelize, MikroORM) is
a generator you write: the Prisma one is a model to copy (`meta eject prisma-schema`).

**A SQL database that speaks another dialect's protocol.** Use that dialect: CockroachDB,
Neon, Supabase, AlloyDB and Aurora PostgreSQL are `postgres`; Turso and libSQL are `sqlite`
(or `d1` on Cloudflare); MariaDB and PlanetScale are `mysql`. Test the generated queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ artifact is a contract whose bytes a cross-port corpus pins and whose hash
consumers verify, so everything a consumer checks stays in the package, in the two
functions the copy calls (`buildSharedModel`, `renderSharedModelFiles`). Do not
replace those calls: an unmodified copy publishes the same bytes as the packaged
generator. Only TypeScript can run it in Phase 1a. **Phase 1a is TypeScript and Python
only, full stop** — TypeScript and Python are the only two ports that can
*consume* a dependency at all today, and only the TypeScript toolchain can
*publish* one; Java, Kotlin, and C# do not read `dependencies` yet (Phase 2).
generator. To publish through a package, point its `target` inside the package
(an npm package's directory listed in `files`; a Python package's directory shipped
as package data), run `meta gen`, then release the package as usual; consumers
declare an `npm` or `python` dependency on it. With no `version` option the
manifest takes the nearest `package.json` version, else a `pyproject.toml`
`[project]` version — pass `version` when neither is the package consumers install.
Only TypeScript can run it in Phase 1a. TypeScript, Python, Java, Kotlin (through
Java's loader and Maven plugin) and C# can *consume* a dependency, and only the
TypeScript toolchain can *publish* one.
Full detail: `docs/features/metadata-dependencies.md`.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ directory each port's `verify --codegen` reports the other ports' pages as drift
name (`meta gen --list` prints them); it never adds one, and naming an entry the config
does not declare is refused. A config still carrying a 1.0 `docs: { ... }` block fails
`meta gen` and `meta verify --codegen` before writing anything, naming the block. Full
before/after and the flag-to-option table: <https://github.com/metaobjectsdev/metaobjects/blob/v1.1.4/docs/features/migrations/docs-are-generators.md>,
before/after and the flag-to-option table: <https://github.com/metaobjectsdev/metaobjects/blob/v1.2.0/docs/features/migrations/docs-are-generators.md>,
section "TypeScript".

**An owned docs generator goes in `generators`, like any other owned copy.** `meta eject
Expand Down
160 changes: 160 additions & 0 deletions .claude/skills/metaobjects-codegen/references/typescript-prisma.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Prisma on TypeScript

> Part of the `metaobjects-codegen` skill's TypeScript reference. Read it when the app keeps
> Prisma as its ORM.

Under `orm: "prisma"`, `meta gen` writes `schema.prisma` from the model, along with Zod schemas
and types that have no Drizzle code. Prisma still creates the tables (`prisma db push` or
`prisma migrate`) and runs the queries. `meta verify --codegen` fails when someone edits
`schema.prisma` or it is out of date. `meta verify --db` fails when the database Prisma created
differs from the model (Postgres and SQLite). Do not hand-write a Prisma model for a table the
MetaObjects model declares.

## Contents
- TypeScript setup
- The workflow
- How the model maps
- What it refuses
- SQLite and MySQL
- Eject it

## TypeScript setup

```ts
// metaobjects.config.ts
import { defineConfig } from "@metaobjectsdev/cli";
import { prismaSchemaFile } from "@metaobjectsdev/codegen-ts/generators";

export default defineConfig({
orm: "prisma",
dialect: "postgres", // "postgres" | "mysql" | "sqlite": picks the Prisma provider
outDir: "src/generated", // Zod schemas and types
targets: { prisma: { outDir: "prisma" } },
generators: ["entity", prismaSchemaFile({ target: "prisma" })],
});
```

`orm: "prisma"` does two things:

- **Every target defaults to `runtime: false`.** The `entity` generator then writes Zod schemas
and types with no `drizzle-orm` import. A target that sets `runtime: true` is refused.
- **Generators that write Drizzle code are refused** by name: `queries`, `routes`,
`routes-hono` and `callable`. Your routes call the Prisma Client instead. `meta gen` exits
with `orm: "prisma" refuses "queries", …` and makes no changes.

The default `orm` is `"drizzle"`, and it changes nothing.

Prisma 7 reads the connection URL from `prisma.config.ts`, not from the schema:

```ts
// prisma.config.ts
import { defineConfig } from "prisma/config";
export default defineConfig({ schema: "prisma/schema.prisma", datasource: { url: process.env.DATABASE_URL! } });
```

The generated schema names the `prisma-client` generator, writing to `prisma/generated`. To
choose your own generator and datasource blocks, pass `header` as a string. Pass
`header: false` for a multi-file schema (`prisma/schema/`) whose blocks live in a file you own.
Other options:

- `provider` overrides the provider that `dialect` picks.
- `fileName` renames the file (default `schema.prisma`).
- `filter` narrows which objects get a model.
- `checks: "omit"` drops the CHECK file.

## The workflow

```bash
meta gen # writes prisma/schema.prisma (+ schema.checks.sql)
npx prisma db push # or: npx prisma migrate dev
npx prisma db execute --file prisma/schema.checks.sql
npx prisma generate # the Prisma Client, as usual
meta verify --codegen --db "$DATABASE_URL" # in CI: fails on an edited schema or a drifted database
```

To change a field, edit the model and run `meta gen`. **Do not edit `schema.prisma` by hand.**
The generator overwrites hand edits instead of merging them. Prisma treats the whole file as
the schema, so a merged edit would change the database without the model knowing.
`meta verify --codegen` fails on the edit until you run `meta gen` again. `prisma format`
leaves the file unchanged, so formatting it does not count as an edit.

A Prisma schema cannot declare a CHECK constraint, so the constraints the model implies go in
`schema.checks.sql`. These are enum membership checks plus the `@min`/`@max` and length
validators. Apply the file after every `db push` or migration. On Postgres the file drops each
constraint before adding it, so you can apply it more than once. On MySQL it only adds them,
so apply it once, when you create the table. When a check changes, drop the old one in a Prisma
migration. When the model has no checks left, `meta gen` deletes the file.

With `prisma migrate`, keep the Prisma migration chain: `meta gen` changes `schema.prisma`, and
`prisma migrate dev` writes the SQL. To make the checks part of the chain, put the contents of
`schema.checks.sql` into a migration (`prisma migrate dev --create-only`, then add the SQL).

## How the model maps

The schema is built from the same expected schema that `meta migrate` writes and
`meta verify --db` compares against. A database that Prisma creates from it therefore passes
the gate.

| Model | Prisma |
|---|---|
| object with a writable `source.rdb` | `model <Name>` with `@@map("<table>")` when the names differ |
| field | a scalar field with `@map("<column>")` when the names differ; `?` when the field is not `@required` |
| `field.string @maxLength` | `String @db.VarChar(n)` (Postgres, MySQL) |
| `field.int` / `field.long` / `field.currency` | `Int` / `BigInt` / `BigInt` |
| `field.double` / `field.float` | `Float` / `Float @db.Real` (Postgres) |
| `field.decimal` | `Decimal @db.Decimal(p, s)` |
| `field.boolean` | `Boolean` |
| `field.timestamp` / `field.date` / `field.time` | `DateTime` with `@db.Timestamptz`/`@db.Timestamp`, `@db.Date`, `@db.Time` (Postgres); `String` on SQLite, where the column holds ISO text |
| `field.uuid` | `String @db.Uuid` (Postgres), `@db.VarChar(36)` (MySQL) |
| `field.enum` | `String` plus a CHECK in `schema.checks.sql` (not a Prisma `enum`, so the column matches `meta migrate`'s) |
| `field.object`, `field.map`, `@storage: jsonb` | `Json` |
| `field.object @storage: flattened` | one field per field of the value object, `<field><Nested>` with `@map("<column>_<nested_column>")` |
| `isArray` scalar | a list (`String[]`) on Postgres; `Json` on MySQL; `String` holding JSON text on SQLite |
| `identity.primary` | `@id` or `@@id([...])`; `@generation: increment` is `@default(autoincrement())`; `@generation: uuid` is `gen_random_uuid()` on Postgres, `uuid()` elsewhere |
| `@default` | `@default(...)` for a literal; `"now"` and other expressions are `dbgenerated(...)`, so the default text matches what `verify --db` expects (a MySQL `DateTime` takes `now()`) |
| `identity.secondary` | `@unique` or `@@unique` (the index name in `map:`) |
| `index.lookup` | `@@index` (`@orders` becomes `sort: Desc`) |
| `identity.reference` | a relation field on both models, with `fields`, `references`, `onDelete`, `onUpdate` and the constraint name in `map:` |
| `@schema` on `source.rdb` | `@@schema("<schema>")` and the datasource's `schemas` list (Postgres) |

## What it refuses

The generator stops with an error code rather than write a schema that would push a database
the model does not describe. Each message names the object or index, says why, and says what
to change.

| Code | When | What to do |
|---|---|---|
| `ERR_PRISMA_READ_ONLY_SOURCE` | an object declares a view that `meta migrate` creates and `meta verify --db` expects (a projection, a write-through entity's read view, or a served report) | create the view in a Prisma migration and mark its read `source.rdb` `@unmanaged: true`, or leave the object out with `filter` |
| `ERR_PRISMA_UNSUPPORTED_INDEX` | an index has `@expr` or `@where`, or a `@using` method other than `btree` or, on Postgres, `hash`, `gin`, `gist`, `brin` or `spgist` | create it in a Prisma migration, or drop the attribute |
| `ERR_PRISMA_UNSUPPORTED_SCHEMA` | a `source.rdb` declares `@schema` and the provider is not `postgresql`, where Prisma has no `@@schema` | remove the `@schema`, or use the `postgresql` provider |
| `ERR_PRISMA_UNSUPPORTED_CHECK` | the provider is SQLite and the model implies a CHECK | pass `checks: "omit"` |
| `ERR_PRISMA_NO_PRIMARY_KEY` | a table has no primary key | declare an `identity.primary` |
| `ERR_PRISMA_UNMAPPABLE_RELATION` | a foreign key points at a table with no model, at a non-unique key, or at a column of another type | include the table, reference a key, or match the subtype |
| `ERR_PRISMA_NAME_COLLISION` | two objects, or two fields, would get the same Prisma name | rename one, or narrow `filter` |
| `ERR_PRISMA_UNSUPPORTED_PROVIDER` | the dialect or `provider` is not `postgresql`, `mysql` or `sqlite` | pass `provider` |

A model with projections, write-through read views or served reports therefore needs one of
those two fixes before the generator runs. A stored procedure, table function or materialized
view is not refused: `meta migrate` does not create it, so neither does Prisma. Read the view's rows with Prisma's `$queryRaw`, and validate them with the
generated Zod schema.

## SQLite and MySQL

- **SQLite** cannot add a CHECK to an existing table. Pass `checks: "omit"`. `meta verify --db`
then reports each missing CHECK as an `add-check` row and nothing else. Only the Zod schemas
enforce those rules. Timestamps, dates and times are `String` in the Prisma Client, because the
column holds ISO text, as it does under `meta migrate`.
- **MySQL** gets the enum CHECKs only. The other checks are written in Postgres SQL. The schema
matches the column types in the MySQL recipe, but `meta verify --db` does not support MySQL,
so on MySQL the drift gate is `verify --codegen` alone.

## Eject it

```bash
meta eject prisma-schema
```

This copies the generator to `codegen/generators/prisma-schema.ts`, and the mapping and
renderers to `codegen/templates/prisma/`. To change a type mapping, edit your copy. A mapping
that no longer matches the expected schema makes `meta verify --db` report drift.
Loading
Loading