ddl-emitter
v0.1.0
Published
Simple, Hibernate-style SQL DDL emitter for TypeSpec.
Readme
ddl-emitter
A small, self-contained TypeSpec emitter that turns your models into SQL DDL, using the same "simple ORM" conventions Hibernate applies by default.
Install
npm install --save-dev ddl-emitterUsage
Via the config (tspconfig.yaml):
emit:
- "ddl-emitter"
options:
"ddl-emitter":
drop-tables: true # default; set false to omit the leading `drop table ...` statementsOr via the command line:
tsp compile . --emit ddl-emitterEach TypeSpec namespace is treated as one "package": every model declared
directly in it lands in its own <packagename>.ddl file (namespace name,
snake_cased). Models declared with no wrapping namespace fall back to
schema.ddl.
Mapping rules
- model → table (name converted to snake_case)
@keyproperty → primary key column- model with no
@key→ a surrogateididentity column is synthesized - scalar / enum property → plain column (enums become
varchar+ acheckconstraint, i.e. the@Enumerated(STRING)style) other: Otherproperty → many-to-one: another_idforeign key column on this tableothers: Other[]property → one-to-many: a<table>_idforeign key column on the referenced table (a unidirectional collection with a join column, not a join table)tags: string[]property → element collection: a separate<table>_<column>join table, like@ElementCollectionextends→ merged into one table (SINGLE_TABLEinheritance, Hibernate's default)
Anything else (unions, tuples, anonymous nested objects, ...) is skipped with a warning diagnostic rather than failing the whole emit.
@comment decorator
Attach a real, persisted column comment (emitted as comment on column ...),
the DDL equivalent of Hibernate's @Comment annotation:
import "ddl-emitter";
using DdlEmitter;
model BusinessPartner {
@key
@comment("Primary identifier of the business partner.")
id: string;
}Emitter options
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| drop-tables | boolean | true | Prefix the script with drop table if exists ... cascade; for every table, so it can be re-run against an existing database. |
Dialect: generic SQL, close to PostgreSQL. Adjust the generated output as needed for your target database.
