@ymmy/dbml-relationalizer is a CLI tool that generates DBML (Database Markup Language) representations of table relationships based on database schema information and relationship definition files.
Key Features:
- Automatically generate DBML by fetching schema information from an existing database
- Allow user-defined relationship additions through a
relations.ymlfile - Provide an inference feature that infers relationships from table and column names
npm install -g @ymmy/dbml-relationalizergit clone https://github.com/Ymmy833y/dbml-relationalizer.git
cd dbml-relationalizer
npm install
npm run build
npm link # Global installationAfter installation, you can use the relation2dbml command in your terminal or command line.
Follow the steps below to generate DBML from a database schema and a custom relationship definition file:
Refer to relations.sample.yml for an example. For more details on how to write your own definitions, see Relationship Definition File.
For example:
relation2dbml mysql mysql://user:pass@localhost:3306/dbname -o schema.dbmlThis connects to the specified MySQL database and processes the schema information along with any relationships defined in relations.yml.
- If you specify an output file (e.g.,
-o schema.dbml), the resulting DBML is saved to that file. - If you do not specify an output file, the DBML is printed to
stdout(the console).
-o, --out-file <pathspec>: Specify the output file path for the generated DBML. If omitted, the result is printed to stdout.-v, --verbose: Set log level todebugfor more detailed logs.-i, --input-file <pathspec>: Specify the path to the relationship definition file (e.g.,relations.yml). Defaults to./relations.yml.
If you want to define custom relationships, create a relations.yml file with the following structure:
inference:
enabled: true
strategy: default
relations:
- parentQualifiedColumn: "users.id"
childQualifiedColumns:
- "orders.user_id"
ignoreChildQualifiedColumns:
- "some_other_table.user_id"
- parentQualifiedColumn: "products.id"
childQualifiedColumns:
- "orders.product_id"
ignoreSelfReferences: falseThe inference feature looks at primary keys (PK) and unique keys to guess which tables and columns may be related, and generates inferred relationships in the DBML output. You can configure this in relations.yml under the inference section:
inference:
enabled: true
strategy: default # 'default' (e.g., users.id), or 'identical' (e.g., users.user_id)-
enabled(boolean):
Set totrueto enable inference. -
strategy(string):default: Usespluralizeto get the singular form of table names and looks for<singularTableName>_<primaryKey>columns (e.g., forusers.id, look foruser_id).identical: Assumes child columns share the same name as the parent column (e.g., if the parent isusers.user_id, the child is also%.user_id).
Within the relations block, you can define relationships manually:
parentQualifiedColumn(Required): Specifies the parent table’s column (wildcards not supported).childQualifiedColumns(Optional): Specifies the child table’s columns (% wildcard supported).ignoreChildQualifiedColumns(Optional): Specifies any child columns to exclude (% wildcard supported).
When entering values, please use the tableName.columnName format.
%.item_id: Matches anyitem_idcolumn in all tables (e.g.,foo.item_id)%.order_%date: Matches any column in all tables that starts withorder_and ends withdate(e.g.,foo.order_created_date,foo.order_date)
Although rare, self-referencing relationships are also supported.
- By default,
ignoreSelfReferencesis set totrue, so self-referencing relationships are excluded without additional configuration. - To include self-referencing relationships, set
ignoreSelfReferences: false.
When ignoreSelfReferences: false is specified, a self-referencing relationship such as the following may be generated:
Ref "infer_fk_user_user_user_id":"user"."user_id" < "user"."user_id"
This tool is released under the MIT License. See the LICENSE file for details.