dbml2sequelize
v1.0.8
Published
Converts [DBML](https://www.dbml.org/) files to Sequelize models.
Readme
dbml2sequelize
Converts DBML files to Sequelize models.
Table of Contents
Overview
dbml2sequelize is a Node.js library that parses .dbml files and automatically generates Sequelize models, allowing you to easily transition your database schema definitions into Sequelize-based projects.
Installation
npm install dbml2sequelizeUsage
Here's an example of how to use dbml2sequelize to convert a .dbml file into Sequelize models:
const path = require("path");
const Sequelize = require("sequelize");
const parseDbml = require("dbml2sequelize");
const opts = {
dialect: "postgres",
host: "localhost",
port: 5432,
};
const sequelize = new Sequelize("micro", "admin", "password", opts);
parseDbml(sequelize, Sequelize.DataTypes, path.join(__dirname, "./db.dbml"), {
timestamps: true,
createdAt: "created_dt",
updatedAt: "modified_dt",
});
sequelize.sync({ force: true }).then(() => {
console.log("done");
});API
parseDbml(sequelize, DataTypes, dbmlPath, options)
- sequelize: Sequelize instance.
- DataTypes: Sequelize DataTypes object.
- dbmlPath: Path to the
.dbmlfile. - options: (Optional) Additional model options (e.g., timestamps, createdAt, updatedAt).
Returns: Array of Sequelize models defined from the DBML file.
Example
See Usage above.
Development Guide
The main source code is in the src/ directory. The core logic is in src/index.js.
Main Flow
Reading the DBML file:
The file is read and split into definitions usinggrouping.js.Separating table settings:
separating.jsextracts table-level settings like indexes and notes.Parsing definitions:
parse-definition.jsandparse-definition-type.jsextract table, enum, and index structures.parse-table-content.jsandparse-indexes-content.jshandle fields and index attributes.
Converting to Sequelize Models:
convert-attributes.jsmaps DBML field types to Sequelize DataTypes.convert-indexes.jsandconvert-note.jsmap DBML indexes and notes to Sequelize options.
Model Generation:
Each table definition is used to create a Sequelize model viasequelize.define.
Key Files and Responsibilities
src/index.js: Main entry point; orchestrates parsing and model creation.src/utils/grouping.js: Groups DBML definitions.src/utils/separating.js: Separates table-level settings.src/parser/parse-definition.js,src/parser/parse-definition-type.js: Parse and identify DBML sections.src/converter/convert-attributes.js: Converts DBML fields to Sequelize attributes.src/converter/convert-indexes.js: Converts DBML indexes to Sequelize indexes.src/converter/convert-note.js: Handles table/field notes.
Extending or Modifying
- To add support for new DBML features, extend the relevant parser or converter modules.
- To adjust Sequelize options, modify how the options object is constructed in
src/index.js.
Error Handling
- Syntax errors or unrecognized DBML specs will throw errors with details in the parser modules.
- Ensure your
.dbmlfiles conform to DBML syntax for best results.
Contributing
Pull requests and issue reports are welcome! Please ensure code follows the style and structure of the project.
License
MIT
This documentation is generated based on the code and structure found in the repo. For more details, see the source code or open an issue.
