universal-ddl
Universal DDL —
Parse DDL scripts in a universal format, then generates DDL scripts for several DBMS.
This package provides a CLI and a programmatic API for Node.js.
How to use the Command Line Interface
Example:
npx universal-ddl --autofix --postgresql --sqlite path/to/ddl-file.sql
This command will generate two new files path/to/ddl-file.postgresql.sql
and path/to/ddl-file.sqlite.sql
.
Available options:
-h, --help Print this help message.
-a, --autofix Enable autofix.
-o, --output-dir directory The output directory (optional).
-p, --postgresql Generate a DDL for Postgresql.
-s, --sqlite Generate a DDL for SQLite.
-m, --mariadb Generate a DDL for Mariadb or MySQL.
-u, --universal-ddl Generate a DDL using the Universal DDL syntax.
-d, --generate-drop Generate drop statements (except for the Universal DDL output).
-e, --encoding string Encoding for input and output file(s) (default is utf8).
-f, --force Overwrite output files.
--src file The source file (by default at last position).
How to use the API from Node.js
Install as a dependency:
npm install universal-ddl
Then, use it:
const parseDdl generateDdl createRds parseDdlToRds = ; const input = ` create table t1 ( a integer not null primary key autoincrement ); `; // Parse the input DDL and create an ASTconst ast = ; // The AST is a pure JSON format, it can be stringifiedconsole; // How to generate a specific DDL for your DBMSconsole; // Create a RDS (Relational Database Structure). It is a POJO object, higher// level than an AST. It is recursive, so it can't be stringified.const rds = ; // Or, create the same RDS using a shortcutconst rds2 = ; // Use the RDSconsole; // true
Contribute
Install and build
We need a JVM (Java Virtual Machine) to build the parser because we use ANTLR, which is a Java program. So, at first, install a JVM on your system.
In a terminal, open the cloned universal-ddl/
repository. Then:
# Download once the ANTLR JAR file in the project's root directory wget https://www.antlr.org/download/antlr-4.7.2-complete.jar # Install once all Node.js dependencies npm install # Build npm run build # Run tests npm run test
Development environment
With VS Code, our recommanded plugins are:
- ANTLR4 grammar syntax support from Mike Lischke (
mike-lischke.vscode-antlr4
) - TSLint from Microsoft (
ms-vscode.vscode-typescript-tslint-plugin
)