cds-dbml
v0.1.1
Published
CAP plugin for DBML (Database Markup Language) generation
Maintainers
Readme
cds-dbml
SAP Cloud Application Programming Model (CAP) plugin to generate Database Markup Language (DBML) schemas from CDS models.
DBML (Database Markup Language) is an open-source, readable DSL designed to define and document database schema designs. Tools like dbdocs and dbdiagram.io allow visualizing DBML schemas as interactive database documentation and diagrams.
There are also VS Code extensions to visualize ERD (Entity Relationship Diagram) database schemas from dbml files. For instance: DBML Entity-Relationship Diagrams visualizer
This plugin allows you to automatically export your CAP data models (.cds files) into DBML format via the CAP CLI (cds compile) or programmatically via Node.js.
Key Features
- Entities & Tables: Converts CDS entities into DBML
Tableblocks. - Data Types: Translates CDS built-in types (
cds.UUID,cds.String,cds.Integer,cds.Decimal,cds.Boolean,cds.DateTime, etc.) to standard SQL/DBML types. - Primary Keys & Constraints: Translates
keyproperties into DBML primary keys ([pk]) andnot nullconstraints. - Default Values: Preserves initial default values (
[default: ...]). - Enums: Maps CDS enum types to DBML
Enumdefinitions and typed columns. - Associations & Compositions: Translates CDS associations and compositions into DBML relationships (
Ref: TableA.col > TableB.col). - Documentation & Notes: Captures doc comments,
@title, and@descriptionannotations into DBMLNotefields. - Table Groups: Automatically groups entities by CDS namespace into DBML
TableGroupblocks. - Event Hooks: Synchronous lifecycle hooks (
compile.to.dbmlandafter:compile.to.dbml) for custom CSN or DBML transformations.
Installation
Install the plugin in your CAP project:
pnpm add --save-dev cds-dbmlNote: @sap/cds >= 8 is required as a peer dependency.
Usage
CLI Usage
Once installed, the DBML target is registered automatically with cds compile.
# Generate DBML to stdout
cds compile db --to dbml
# Save DBML to a file
cds compile db --to dbml > schema.dbml
# Output DBML to a target directory
cds compile db --to dbml --dest gen/dbmlProgrammatic Usage
You can call the compiler programmatically in Node.js (ES Modules):
import cds from "@sap/cds";
import "cds-dbml/cds-plugin"; // Ensures plugin target is registered
const csn = await cds.load("db/schema.cds");
// Generate DBML string
const dbml = cds.compile.to.dbml(csn);
console.log(dbml);Alternatively, import the compiler function directly:
import { compileToDBML } from "cds-dbml";
const dbml = compileToDBML(csn, { sort: true });Event Hooks
You can hook into the compilation lifecycle to inspect or modify the CSN model before conversion, or customize the generated DBML string after conversion:
import cds from "@sap/cds";
// Hook before conversion (modify CSN or options)
cds.on("compile.to.dbml", ({ csn, options }) => {
// Modify csn or options
});
// Hook after conversion (modify resulting DBML output)
cds.on("after:compile.to.dbml", ({ csn, options, result }) => {
// result is { dbml: string } or string
});Configuration Options
Options can be passed via CLI arguments or programmatically:
| Option | Type | Default | Description |
| ------------- | --------- | ----------- | ----------------------------------------------------------------------------------- |
| sort | boolean | false | Sorts entities, columns, and relationships alphabetically for deterministic output. |
| project | string | undefined | Project title for the DBML Project header block. |
| tableGroups | boolean | true | Group tables into DBML TableGroup blocks by CDS namespace. |
Example CLI usage with options:
cds compile db --to dbml --dbml:sort