Overview

Build a CRM from one file

You write a single Mermaid document describing a business: its data, the decisions it makes, and the processes it runs. One command turns that into a working full-stack application — Postgres schema, NestJS API, TanStack Start front end, an admin dictionary, an audit trail and a workflow engine — with your rules and workflows already wired in and running.

The finished application. Seventeen entities in seven groups, each group's name and description taken straight from the model. Nothing on this screen was hand-written — including the wording.
The generated CRM dashboard, showing seven groups of entities: Accounts and Contacts, Activities, Customer Service, Demand Generation, People and Teams, Quotes and Contracts, Sales Pipeline, plus the Application Dictionary.
1
model file
17
entities
8
business rules
5
state machines
5
multi-step sagas
403
generated files
Rather see it than read about it?

Chapter 09 runs the whole thing in your browser. Pick the CRM model below, press Generate, press Run, and a complete application starts in the page — PostgreSQL compiled to WebAssembly, the generated server on a worker thread, no install and no server anywhere. It opens with records already in it, so there is something to sort, open and look up rather than seventeen empty lists.

The one idea worth understanding first

Most code generators stop at the schema: you describe tables, you get CRUD screens, and every rule your business actually runs on is left for you to write by hand. This one takes the same document further, because the same document says more.

An EML file (AppWithAI Modeling Language) is valid Mermaid. It renders as diagrams in any Mermaid viewer, on GitHub, in your editor's preview. The generator reads three kinds of diagram out of it and treats each as a different layer of the application:

You drawThe generator emitsIn the running app
erDiagram Migrations, DTOs, services, REST routes, list and form screens Every entity, searchable, editable, audited
flowchart marked kind: rules GoRules decision graphs seeded into sys_rule_definitions Decisions evaluated inside the transaction that saves a record
stateDiagram-v2 BPMN definitions seeded into sys_workflow_definitions A status lifecycle the application applies and guards
flowchart marked kind: saga One BPMN service task per %%step Multi-entity processes that create, update and delete across tables

Semantics that Mermaid has no syntax for ride on %% comment directives — %%enum, %%rule, %%hook, %%step. Mermaid ignores comments, so the document still renders; the generator reads them, so the document still means something.

Why this matters

The diagram cannot drift from the application, because the diagram is the application. When someone asks "what happens when a lead is qualified?", the answer is a section of a file that a non-engineer can read and a renderer can draw — not a search through service code.

What you will build

An enterprise CRM covering the whole commercial lifecycle: marketing captures a lead, sales converts it into an account with a contact and an opportunity, the deal is quoted and contracted, and support handles cases against the account afterwards.

Sales pipeline. Opportunities with stage, amount and forecast category.
The Opportunities list in the generated CRM.
Service. Support cases with priority, SLA targets and origin.
The Support Cases list in the generated CRM.

Behind those screens the model also carries the behaviour:

  • Eight business rules — lead scoring and routing, a qualification gate, forecasting, a closed-won gate, discount policy, case triage, renewal risk.
  • Five state machines — the lifecycle of a lead, an opportunity, a quote, a support case and a contract, each with role guards on its transitions.
  • Seven hook workflows — the lifecycle handlers that normalize an email, mint an account number, block a delete, redact a phone number.
  • Five sagas — lead conversion, quote approval escalation, critical case escalation, closed-won handoff and the renewal playbook.

What arrives with it, unasked

The screens below are not part of the CRM's specification. Nobody described a workflow editor, an audit trail or a help system in the model — they come with every application the generator produces, wired to the entities you did describe. This is the difference between generating CRUD and generating a system.

A workflow editor, populated. The lead-conversion saga as the running application renders it: the trigger, the three rules that gate it, its six steps in order, and the runs it has performed — all compiled from %%step directives in the model. Chapter 03.
The LeadConversion workflow open in the editor, showing its trigger card, three rule gates, numbered steps, and a details sidebar listing the entity, trigger operation, run mode, step count, linked rules and recent runs.
An audit trail you cannot forget to switch on. One entry per insert, update and delete, written by the same transaction as the change, holding the before and after value of every column that moved. Append-only, and each entry independently verifiable. Chapter 06.
The audit log showing auth login, entity create and entity update events with timestamp, user, action, entity, the columns that changed, the source and a verify action.
Help on every screen, written from the model. It names the required fields, says which columns are lookups, names the entities that point back, and explains the draft-to-final save cycle — then lists every field with its own rule. Chapter 05.
The Lead help dialog with a window overview, a tabs section and a fields section listing each field and its help text.
Twenty-five decision tables, editable. The eight rules from the model plus a validation rule per entity — each one a table an operations person can change without a deploy. Chapter 02.
The Business Rules screen listing 25 active rules across 17 entities, each with its entity and the operation it fires on.
Roles, seeded and enforced. A transition your state machine guards with [Manager] is a permission check against this table at runtime. Chapter 06.
The Role Management screen listing Administrator as a master role and Analyst, Manager and User as standard roles, each active with a description.
The list nobody wrote a line of code for

A REST API with Swagger, an admin dictionary of windows, tabs and fields, a rules engine with a decision table editor, a workflow engine with a visual designer, role-based access control, an append-only audit log, generated per-screen help, and an end-to-end test suite. Every one of them present in the application above, and none of them mentioned in the model that produced it.

How the guide is arranged

Each chapter builds one layer and then shows it running. You can read straight through, or jump to the layer you care about — every chapter names the exact file and lines it is talking about.

Before you start

You need three things running locally. Nothing else.

RequirementWhy
bun ≥ 1.3.14The repository is Bun-only. Never npm or pnpm.
PostgreSQL 16+The generated backend targets Postgres. One database for the generator, one per generated app.
The repositorybun install at the root once.

The whole thing in five commands

shellfrom the repository root
# 1. check the model — the generator runs this for you, but it is fast and it is honest
bun language/checker.ts language/examples/crm.eml.mmd

# 2. generate the application
bun packages/generator/src/cli/generate.ts generate \
  --input language/examples/crm.eml.mmd \
  --output generated-projects/crm \
  --name crm --records-per-entity 25 --force --no-setup

# 3. point it at a database
cd generated-projects/crm
cp backend/.env.example backend/.env   # then set DATABASE_URL

# 4. create the schema and seed it
bun install && bun run db:setup

# 5. run it
bun run dev                            # API :4001, web :4000
What you get for that

Seventeen tables with their indexes and constraints, a REST API with Swagger, 192 front-end files, 39 seeded workflow definitions, 25 rule definitions, a seeded admin user, an audit log, and a generated end-to-end test suite. Roughly ninety seconds, most of it bun install.

If you would rather see the model first and the commands later, start with chapter 01. If you want to watch the whole chain fire before you read a line of EML, jump to chapter 07.

Writing a model of your own?

Chapter 11 is the procedure — how to turn a business description into a model a generator will accept, and how to prove it. The checker and fixer it runs are published as ES modules at /guide/checker.js and /guide/fixer.js, and the whole specification, written for language models, is one file at llms-full.txt.

Where this guide sits

The generator described here is the engine behind AppWithAI. Everything on these pages runs from the open source repository under the Apache License 2.0 — see Technology for the full stack the generator emits, or get in touch to have the same thing built for your own domain.