======================== CODE SNIPPETS ======================== TITLE: Install ANTLR4 Tools using pip (Bash) DESCRIPTION: Installs the ANTLR4 tools package using pip, the Python package installer. This is the first step in setting up the ANTLR4 environment. The comment suggests running `antlr4` after installation to verify and get help. SOURCE: https://github.com/holistics/dbml/blob/master/packages/dbml-core/src/parse/ANTLR/README.md#_snippet_0 LANGUAGE: bash CODE: ``` pip install antlr4-tools # Run antlr4 to check the version and help ``` ---------------------------------------- TITLE: Example: Running Tests for @dbml/cli DESCRIPTION: An example demonstrating how to run tests specifically for the @dbml/cli package using Lerna, including the command and its typical output. SOURCE: https://github.com/holistics/dbml/blob/master/CONTRIBUTING.md#_snippet_4 LANGUAGE: Shell CODE: ``` lerna run test --scope @dbml/cli info cli using local version of lerna lerna notice cli v3.16.4 lerna info filter [ '@dbml/cli' ] lerna info Executing command in 1 package: "yarn run test" lerna info run Ran npm script 'test' in '@dbml/cli' in 82.1s: yarn run v1.17.3 $ jest Done in 81.28s. lerna success run Ran npm script 'test' in 1 package in 82.1s: lerna success - @dbml/cli ``` ---------------------------------------- TITLE: Installing @dbml/core DESCRIPTION: Instructions for installing the @dbml/core library using npm or yarn package managers. SOURCE: https://github.com/holistics/dbml/blob/master/packages/dbml-core/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install @dbml/core ``` LANGUAGE: bash CODE: ``` yarn add @dbml/core ``` ---------------------------------------- TITLE: Example DBML Syntax DESCRIPTION: A basic example demonstrating table definitions for 'users' and 'posts' with columns, settings (primary key, note), and a many-to-one relationship defined using 'Ref'. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_0 LANGUAGE: text CODE: ``` Table users { id integer username varchar role varchar created_at timestamp } Table posts { id integer [primary key] title varchar body text [note: 'Content of the post'] user_id integer created_at timestamp } Ref: posts.user_id > users.id // many-to-one ``` ---------------------------------------- TITLE: Install @dbml/cli DESCRIPTION: These commands demonstrate different methods to install or run the @dbml/cli tool globally using npm or yarn, or directly execute it using npx. SOURCE: https://github.com/holistics/dbml/blob/master/packages/dbml-cli/README.md#_snippet_0 LANGUAGE: shell CODE: ``` npm install -g @dbml/cli ``` LANGUAGE: shell CODE: ``` yarn global add @dbml/cli ``` LANGUAGE: shell CODE: ``` npx -p @dbml/cli dbml2sql schema.dbml -o schema.sql ``` ---------------------------------------- TITLE: Install Dependencies with Yarn DESCRIPTION: Installs the necessary project dependencies using the Yarn package manager. This command should be run after cloning the repository. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/README.md#_snippet_0 LANGUAGE: Shell CODE: ``` yarn ``` ---------------------------------------- TITLE: Full DBML TablePartial Example DESCRIPTION: A comprehensive example demonstrating the definition of multiple TablePartials and their injection into a table definition. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_30 LANGUAGE: text CODE: ``` TablePartial base_template [headerColor: #ff0000] { id int [pk, not null] created_at timestamp [default: `now()`] updated_at timestamp [default: `now()`] } TablePartial soft_delete_template { delete_status boolean [not null] deleted_at timestamp [default: `now()`] } TablePartial email_index { email varchar [unique] indexes { email [unique] } } Table users { ~base_template ~email_index name varchar ~soft_delete_template } ``` ---------------------------------------- TITLE: Navigate to Parser Directory (Bash) DESCRIPTION: Changes the current directory to the specific location where the new ANTLR parser files for PostgreSQL are intended to be placed within the project structure. This is a prerequisite for running the ANTLR4 commands in the next step. SOURCE: https://github.com/holistics/dbml/blob/master/packages/dbml-core/src/parse/ANTLR/README.md#_snippet_1 LANGUAGE: bash CODE: ``` cd packages/dbml-core/src/parse/ANTLR/parsers/postgresql ``` ---------------------------------------- TITLE: Start Local Development Server with Yarn DESCRIPTION: Starts a local development server for the Docusaurus website. It automatically opens a browser window and reflects most changes live without requiring a server restart. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/README.md#_snippet_1 LANGUAGE: Shell CODE: ``` yarn dev ``` ---------------------------------------- TITLE: Installing @dbml/connector (bash) DESCRIPTION: Commands to install the @dbml/connector package using either npm or yarn package managers. SOURCE: https://github.com/holistics/dbml/blob/master/packages/dbml-connector/README.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install @dbml/connector # or if you're using yarn yarn add @dbml/connector ``` ---------------------------------------- TITLE: Install DBML CLI via npm DESCRIPTION: Installs the DBML Command Line Interface globally using npm. This makes the `dbml2sql`, `sql2dbml`, and `db2dbml` commands available in your terminal. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install -g @dbml/cli ``` ---------------------------------------- TITLE: Installing Yarn and Lerna (Global) DESCRIPTION: Instructions to install Yarn and Lerna globally on your system using either npm or yarn's global command. SOURCE: https://github.com/holistics/dbml/blob/master/CONTRIBUTING.md#_snippet_0 LANGUAGE: Shell CODE: ``` npm install -g yarn lerna ``` LANGUAGE: Shell CODE: ``` yarn global add lerna ``` ---------------------------------------- TITLE: Install @dbml/core with npm DESCRIPTION: This command installs the @dbml/core package using the npm package manager, making its functionalities available for use in your project. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install @dbml/core ``` ---------------------------------------- TITLE: Generate JavaScript Parser Files (Bash) DESCRIPTION: Runs the ANTLR4 tool to generate parser and lexer files from the specified grammar files (`` and ``). The `-Dlanguage=JavaScript` flag specifies the target language for the generated code, `-no-listener` disables listener generation, and `-visitor` enables visitor generation, which is required for writing the AST visitor. SOURCE: https://github.com/holistics/dbml/blob/master/packages/dbml-core/src/parse/ANTLR/README.md#_snippet_2 LANGUAGE: bash CODE: ``` antlr4 -Dlanguage=JavaScript -no-listener -visitor antlr4 -Dlanguage=JavaScript -no-listener -visitor ``` ---------------------------------------- TITLE: Cloning and Building DBML DESCRIPTION: Steps to clone the DBML repository from GitHub, navigate into the project directory, install dependencies using yarn, and build the project. SOURCE: https://github.com/holistics/dbml/blob/master/CONTRIBUTING.md#_snippet_1 LANGUAGE: Shell CODE: ``` git clone cd dbml yarn install yarn build ``` ---------------------------------------- TITLE: Installing @dbml/connector package - Bash DESCRIPTION: Installs the @dbml/connector package using npm, making it available for use in your project. Requires Node.js 18.0.0 or higher. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/connector.md#_snippet_0 LANGUAGE: bash CODE: ``` npm install @dbml/connector ``` ---------------------------------------- TITLE: Generate DBML from Database (PostgreSQL) DESCRIPTION: Connects directly to a database using a connection string and generates DBML based on its schema. This example connects to a PostgreSQL database and prints the output to the console. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_8 LANGUAGE: bash CODE: ``` $ db2dbml postgres 'postgresql://dbml_user:dbml_pass@localhost:5432/dbname?schemas=public' Table "staff" { "id" int4 [pk, not null] "name" varchar "age" int4 "email" varchar } ... ``` ---------------------------------------- TITLE: Defining Sticky Notes in DBML DESCRIPTION: Examples of defining standalone sticky notes on the diagram canvas, including single-line and multi-line formats. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_24 LANGUAGE: text CODE: ``` Table jobs { ... } Note single_line_note { 'This is a single line note' } Note multiple_lines_note { ''' This is a multiple lines note This string can spans over multiple lines. ''' } ``` ---------------------------------------- TITLE: Generate DBML from PostgreSQL SQL using importer.import DESCRIPTION: This example demonstrates how to read a PostgreSQL SQL file and use the `importer.import` method to convert its content into the DBML format. It requires the 'fs' module to read the file. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_2 LANGUAGE: javascript CODE: ``` const fs = require('fs'); const { importer } = require('@dbml/core'); // read PostgreSQL file script const postgreSQL = fs.readFileSync('./schema.sql', 'utf-8'); // generate DBML from PostgreSQL script const dbml = importer.import(postgreSQL, 'postgres'); ``` ---------------------------------------- TITLE: DBML TablePartial Injection Result DESCRIPTION: Shows the resulting table definition after injecting the TablePartials from the previous example, illustrating how partials are merged. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_31 LANGUAGE: text CODE: ``` Table users [headerColor: #ff0000] { id int [pk, not null] created_at timestamp [default: `now()`] updated_at timestamp [default: `now()`] email varchar [unique] name varchar delete_status boolean [not null] deleted_at timestamp [default: `now()`] indexes { email [unique] } } ``` ---------------------------------------- TITLE: Defining TableGroup in DBML DESCRIPTION: Provides the basic syntax and an example for grouping related tables using the TableGroup keyword. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_25 LANGUAGE: text CODE: ``` TableGroup tablegroup_name { // tablegroup is case-insensitive. table1 table2 table3 } // example TableGroup e_commerce1 { merchants countries } ``` ---------------------------------------- TITLE: Convert DBML to SQL (Output to File) DESCRIPTION: Converts a DBML file to SQL and saves the output to a specified file using the `-o` or `--out-file` option. This example saves the PostgreSQL output to `schema.sql`. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_3 LANGUAGE: bash CODE: ``` $ dbml2sql schema.dbml -o schema.sql ✔ Generated SQL dump file (PostgreSQL): schema.sql ``` ---------------------------------------- TITLE: Parse DBML to Database object using parser.parse DESCRIPTION: This example reads a DBML file and uses the `parser.parse` method to convert the DBML content into a structured `Database` object, which can then be further processed or exported. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_7 LANGUAGE: javascript CODE: ``` const fs = require('fs'); const { Parser } = require('@dbml/core'); const parser = new Parser(); // get DBML file content const dbml = fs.readFileSync('./schema.dbml', 'utf-8'); // parse DBML to Database object const database = parser.parse(dbml, 'dbml'); ``` ---------------------------------------- TITLE: Export DBML to MySQL SQL using exporter.export DESCRIPTION: This example demonstrates reading a DBML file and using the `exporter.export` method to convert its content into a MySQL SQL script. It requires the 'fs' module to read the file. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_5 LANGUAGE: javascript CODE: ``` const fs = require('fs'); const { exporter } = require('@dbml/core'); // get DBML file content const dbml = fs.readFileSync('./schema.dbml', 'utf-8'); // generate MySQL from DBML const mysql = exporter.export(dbml, 'mysql'); ``` ---------------------------------------- TITLE: Setting Default Values for DBML Table Columns DESCRIPTION: Illustrates the syntax for setting default values for columns in DBML, showing examples for number, string (using single quotes), expression (using backticks), and boolean/null values. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_8 LANGUAGE: text CODE: ``` Table users { id integer [primary key] username varchar(255) [not null, unique] full_name varchar(255) [not null] gender varchar(1) [not null] source varchar(255) [default: 'direct'] created_at timestamp [default: `now()`] rating integer [default: 10] } ``` ---------------------------------------- TITLE: DBML Multi-line String Output Example DESCRIPTION: Shows the resulting string value after parsing a multi-line string defined with triple single quotes ('''). The parser automatically removes the minimum number of leading spaces among all lines from the final output. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_33 LANGUAGE: DBML CODE: ``` This is a block string This string can spans over multiple lines. ``` ---------------------------------------- TITLE: Preview Local Build with Yarn DESCRIPTION: Serves the static content generated by the build command locally. This allows previewing the production version of the site before deployment. Ensure 'yarn build' is run first. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/README.md#_snippet_4 LANGUAGE: Shell CODE: ``` yarn serve ``` ---------------------------------------- TITLE: db2dbml Command Syntax Manual DESCRIPTION: Provides the general command-line syntax for the `db2dbml` tool, showing the required database type and connection string arguments, and the optional flag for specifying the output file. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_10 LANGUAGE: bash CODE: ``` $ db2dbml postgres|mysql|mssql|snowflake|bigquery [-o|--out-file ] ``` ---------------------------------------- TITLE: sql2dbml Command Syntax Manual DESCRIPTION: Provides the general command-line syntax for the `sql2dbml` tool, showing required arguments (input file) and optional flags for specifying the input SQL dialect and output file. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_7 LANGUAGE: bash CODE: ``` $ sql2dbml [--mysql|--postgres|--mssql|--postgres-legacy|--mysql-legacy|--mssql-legacy|--snowflake] [-o|--out-file ] ``` ---------------------------------------- TITLE: dbml2sql Command Syntax Manual DESCRIPTION: Provides the general command-line syntax for the `dbml2sql` tool, showing required arguments (input file) and optional flags for specifying the output dialect and output file. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_4 LANGUAGE: bash CODE: ``` $ dbml2sql [--mysql|--postgres|--mssql|--oracle] [-o|--out-file ] ``` ---------------------------------------- TITLE: Build Static Website with Yarn DESCRIPTION: Generates the static content for the website into the 'build' directory. The output can then be served using any static content hosting service. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/README.md#_snippet_3 LANGUAGE: Shell CODE: ``` yarn build ``` ---------------------------------------- TITLE: Format Code with Yarn DESCRIPTION: Runs a command to automatically format the project's code, ensuring consistent style before committing changes. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/README.md#_snippet_2 LANGUAGE: Shell CODE: ``` yarn format ``` ---------------------------------------- TITLE: Recommended DBML File Structure DESCRIPTION: This snippet illustrates the recommended file structure for including a `database.dbml` file within a project's root repository. It suggests placing the DBML schema definition alongside other configuration and boilerplate files like `README.md` for easy access and consistency. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/home.md#_snippet_1 LANGUAGE: text CODE: ``` . |_ ... |_ database.dbml |_ README.md ``` ---------------------------------------- TITLE: Generate DBML from Database (Output to File) DESCRIPTION: Connects to a database using a connection string and generates DBML, saving the output to a specified file using the `-o` option. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_9 LANGUAGE: bash CODE: ``` $ db2dbml postgres 'postgresql://dbml_user:dbml_pass@localhost:5432/dbname?schemas=public' -o schema.dbml ✔ Generated DBML file from database's connection: schema.dbml ``` ---------------------------------------- TITLE: Running Tests for All Packages DESCRIPTION: Command to execute the test script defined in the package.json for all packages within the monorepo using Lerna. SOURCE: https://github.com/holistics/dbml/blob/master/CONTRIBUTING.md#_snippet_2 LANGUAGE: Shell CODE: ``` lerna run test ``` ---------------------------------------- TITLE: Defining a DBML Project DESCRIPTION: Syntax for defining a project in DBML, including specifying the database type and adding a project-level note. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_1 LANGUAGE: text CODE: ``` Project project_name { database_type: 'PostgreSQL' Note: 'Description of the project' } ``` ---------------------------------------- TITLE: Applying Settings to a DBML Table DESCRIPTION: Shows how to add settings to a table definition using square brackets, such as changing the header color or applying column-level settings like 'primary key', 'not null', and 'unique'. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_6 LANGUAGE: text CODE: ``` Table users [headercolor: #3498DB] { id integer [primary key] username varchar(255) [not null, unique] } ``` ---------------------------------------- TITLE: Running Tests for a Specific Package DESCRIPTION: Command to execute the test script for a single, specified package within the monorepo using Lerna's --scope option. SOURCE: https://github.com/holistics/dbml/blob/master/CONTRIBUTING.md#_snippet_3 LANGUAGE: Shell CODE: ``` lerna run test --scope ``` ---------------------------------------- TITLE: Adding TableGroup Settings in DBML DESCRIPTION: Demonstrates how to apply settings like color to a TableGroup definition using square brackets. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_27 LANGUAGE: text CODE: ``` TableGroup e_commerce [color: #345] { merchants countries } ``` ---------------------------------------- TITLE: Defining Project Note in DBML DESCRIPTION: Demonstrates how to add a multi-line note to a DBML project definition, providing a general description and benefits. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_20 LANGUAGE: text CODE: ``` Project DBML { Note: ''' # DBML - Database Markup Language DBML (database markup language) is a simple, readable DSL language designed to define database structures. ## Benefits * It is simple, flexible and highly human-readable * It is database agnostic, focusing on the essential database structure definition without worrying about the detailed syntaxes of each database * Comes with a free, simple database visualiser at [dbdiagram.io](http://dbdiagram.io) ''' } ``` ---------------------------------------- TITLE: Defining a Schema and Table in DBML DESCRIPTION: Shows how to define a table within a specific schema by prefixing the table name with the schema name. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_2 LANGUAGE: text CODE: ``` Table core.user { ... } ``` ---------------------------------------- TITLE: Convert SQL to DBML (Output to File) DESCRIPTION: Converts a SQL file to DBML format for a specified dialect (MySQL in this case) and saves the output to a file using the `-o` option. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_6 LANGUAGE: bash CODE: ``` $ sql2dbml --mysql dump.sql -o mydatabase.dbml ✔ Generated DBML file from SQL file (MySQL): mydatabase.dbml ``` ---------------------------------------- TITLE: Adding Settings to DBML Relationships DESCRIPTION: Explains how to apply settings like referential actions (`delete`, `update`) and visual properties (`color`) to relationships defined using the short or long `Ref` syntax. Settings are enclosed in square brackets after the relationship definition. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_16 LANGUAGE: text CODE: ``` // short form Ref: products.merchant_id > merchants.id [delete: cascade, update: no action, color: #79AD51] // long form Ref { products.merchant_id > merchants.id [delete: cascade, update: no action, color: #79AD51] } ``` ---------------------------------------- TITLE: Defining DBML Table Columns with Settings DESCRIPTION: Demonstrates how to apply optional settings such as unique, not null, primary key (pk), default values, and notes to columns within a DBML table definition using square brackets. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_7 LANGUAGE: text CODE: ``` Table buildings { ... address varchar(255) [unique, not null, note: 'to include unit number'] id integer [ pk, unique, default: 123, note: 'Number' ] } ``` ---------------------------------------- TITLE: Convert SQL to DBML (PostgreSQL Default) DESCRIPTION: Converts a SQL dump file into DBML format. The `--postgres` flag specifies the input SQL dialect. The resulting DBML is printed to the console. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_5 LANGUAGE: bash CODE: ``` $ sql2dbml dump.sql --postgres Table staff { id int [pk] name varchar age int email varchar } ... ``` ---------------------------------------- TITLE: DBML TablePartial Usage Syntax DESCRIPTION: Illustrates how to inject a defined TablePartial into a table definition using the `~` prefix. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_29 LANGUAGE: text CODE: ``` Table table_name { ~partial_name field_name field_type ~another_partial } ``` ---------------------------------------- TITLE: Import and instantiate Parser from @dbml/core DESCRIPTION: This snippet imports the `Parser` class from @dbml/core and creates a new instance, which is used to parse various schema formats into a `Database` object representation. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_6 LANGUAGE: javascript CODE: ``` const { Parser } = require('@dbml/core'); const parser = new Parser(); ``` ---------------------------------------- TITLE: Defining Database Schema with DBML DESCRIPTION: This DBML snippet demonstrates how to define database tables, enums, and relationships. It includes definitions for 'users' and 'posts' tables with various column types and constraints, an 'post_status' enum, and a many-to-one reference between 'posts.user_id' and 'users.id'. This declarative syntax simplifies schema documentation and understanding. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/home.md#_snippet_0 LANGUAGE: DBML CODE: ``` Table users { id integer username varchar role varchar created_at timestamp } Table posts { id integer [primary key] title varchar body text [note: 'Content of the post'] user_id integer status post_status created_at timestamp } Enum post_status { draft published private [note: 'visible via URL only'] } Ref: posts.user_id > users.id // many-to-one ``` ---------------------------------------- TITLE: Defining Database Schema with DBML DESCRIPTION: This snippet demonstrates how to define a database schema using DBML. It includes the creation of two tables, 'users' and 'posts', specifying their columns, data types, and constraints (like primary key). It also illustrates how to define a many-to-one relationship between the 'posts' table (user_id) and the 'users' table (id). SOURCE: https://github.com/holistics/dbml/blob/master/README.md#_snippet_0 LANGUAGE: DBML CODE: ``` Table users { id integer username varchar role varchar created_at timestamp } Table posts { id integer [primary key] title varchar body text [note: 'Content of the post'] user_id integer created_at timestamp } Ref: posts.user_id > users.id // many-to-one ``` ---------------------------------------- TITLE: Convert DBML to SQL (Specify Dialect) DESCRIPTION: Converts a DBML file into SQL statements for a specific database dialect, such as MySQL, using the `--mysql` flag. The output is printed to the console. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_2 LANGUAGE: bash CODE: ``` $ dbml2sql schema.dbml --mysql CREATE TABLE `staff` ( `id` INT PRIMARY KEY, `name` VARCHAR(255), `age` INT, `email` VARCHAR(255) ); ... ``` ---------------------------------------- TITLE: DBML TablePartial Definition Syntax DESCRIPTION: Shows the syntax for defining a reusable TablePartial, including fields, settings, and indexes. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_28 LANGUAGE: text CODE: ``` TablePartial partial_name [table_settings] { field_name field_type [field_settings] indexes { (column_name) [index_settings] } } ``` ---------------------------------------- TITLE: Aliasing Tables in DBML DESCRIPTION: Demonstrates how to assign an alias to a table using the 'as' keyword, which can then be used in relationship definitions. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_4 LANGUAGE: text CODE: ``` Table very_long_user_table as U { ... } Ref: U.id < posts.user_id ``` ---------------------------------------- TITLE: Convert DBML to SQL (PostgreSQL Default) DESCRIPTION: Converts a DBML file into SQL statements. By default, it generates SQL compatible with PostgreSQL and prints the output to the console. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_1 LANGUAGE: bash CODE: ``` $ dbml2sql schema.dbml CREATE TABLE "staff" ( "id" INT PRIMARY KEY, "name" VARCHAR, "age" INT, "email" VARCHAR ); ... ``` ---------------------------------------- TITLE: Defining Composite Foreign Keys in DBML DESCRIPTION: Shows how to define a relationship based on multiple columns (composite foreign key) using the short form `Ref` syntax, listing the columns within parentheses. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_14 LANGUAGE: text CODE: ``` Ref: merchant_periods.(merchant_id, country_code) > merchants.(id, country_code) ``` ---------------------------------------- TITLE: Basic DBML Table Definition DESCRIPTION: Syntax for defining tables, both in the default 'public' schema and within a specified schema, including column definitions with types and optional settings. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_3 LANGUAGE: text CODE: ``` // table belonged to default "public" schema Table table_name { column_name column_type [column_settings] } // table belonged to a schema Table schema_name.table_name { column_name column_type [column_settings] } ``` ---------------------------------------- TITLE: Defining Cross-Schema Relationships in DBML DESCRIPTION: Illustrates how to define relationships between tables residing in different schemas by prefixing table names with their schema name (e.g., `schema.table`). Both inline and short `Ref` syntaxes are shown. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_15 LANGUAGE: text CODE: ``` Table core.users { id integer [pk] } Table blogging.posts { id integer [pk] user_id integer [ref: > core.users.id] } // or this Ref: blogging.posts.user_id > core.users.id ``` ---------------------------------------- TITLE: Generate DBML from DatabaseSchema object using importer.generateDbml DESCRIPTION: This snippet shows how to use the `importer.generateDbml` method to create a DBML string from a `DatabaseSchema` object, typically obtained using the @dbml/connector package. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_3 LANGUAGE: javascript CODE: ``` const { importer } = require('@dbml/core'); const { connector } = require('@dbml/connector'); // Use the dbml connector to get the DatabaseSchema object const connection = 'postgresql://dbml_user:dbml_pass@localhost:5432/dbname?schemas=public'; const databaseType = 'postgres'; const schemaJson = await connector.fetchSchemaJson(connection, databaseType); // Generate DBML from the DatabaseSchema object const dbml = importer.generateDbml(schemaJson); ``` ---------------------------------------- TITLE: Adding Notes to a DBML Table DESCRIPTION: Syntax for including a descriptive note directly within a table definition, providing context for the table's purpose. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_5 LANGUAGE: text CODE: ``` Table users { id integer status varchar [note: 'status'] Note: 'Stores user data' } ``` ---------------------------------------- TITLE: Defining Indexes in DBML Tables DESCRIPTION: Shows how to define various types of indexes within the 'indexes' block of a DBML table definition, including single-column, composite, and expression-based indexes, along with specifying index settings like primary key (pk), name, unique, and type. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_9 LANGUAGE: text CODE: ``` Table bookings { id integer country varchar booking_date date created_at timestamp indexes { (id, country) [pk] // composite primary key created_at [name: 'created_at_index', note: 'Date'] booking_date (country, booking_date) [unique] booking_date [type: hash] (`id*2`) (`id*3`,`getdate()`) (`id*3`,id) } } ``` ---------------------------------------- TITLE: Importing connector module - JavaScript DESCRIPTION: Imports the 'connector' object from the '@dbml/connector' package using the Node.js require syntax. This object provides methods for interacting with databases. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/connector.md#_snippet_1 LANGUAGE: javascript CODE: ``` const { connector } = require('@dbml/connector'); ``` ---------------------------------------- TITLE: Fetching database schema JSON - JavaScript DESCRIPTION: Uses the 'connector.fetchSchemaJson' method to connect to a specified database and retrieve its schema information as a DatabaseSchema object. Requires a connection string and database type. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/connector.md#_snippet_2 LANGUAGE: javascript CODE: ``` const { connector } = require('@dbml/connector'); const connection = 'postgresql://dbml_user:dbml_pass@localhost:5432/dbname?schemas=public'; const databaseType = 'postgres'; const schemaJson = await connector.fetchSchemaJson(connection, databaseType); ``` ---------------------------------------- TITLE: BigQuery JSON Credential File Format DESCRIPTION: Specifies the required JSON format for the credential file used when generating DBML from a Google BigQuery database via the `db2dbml` command. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/cli.md#_snippet_11 LANGUAGE: json CODE: ``` { "project_id": "your-project-id", "client_email": "your-client-email", "private_key": "your-private-key", "datasets": ["dataset_1", "dataset_2", ...] } ``` ---------------------------------------- TITLE: Export Database object to PostgreSQL SQL using ModelExporter.export DESCRIPTION: This snippet demonstrates how to use the static `ModelExporter.export` method to convert a `Database` object (obtained from parsing) into a PostgreSQL SQL string. It shows both exporting the raw object and a normalized version. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_8 LANGUAGE: javascript CODE: ``` const { ModelExporter, Parser } = require('@dbml/core'); // get DBML file content const dbml = fs.readFileSync('./schema.dbml', 'utf-8'); // parse DBML to Database object const parser = new Parser(); const database = parser.parse(dbml, 'dbml'); // Export Database object to PostgreSQL const postgreSQL = ModelExporter.export(database, 'postgres', false); // or const postgreSQL = ModelExporter.export(database.normalize(), 'postgres'); ``` ---------------------------------------- TITLE: Import importer from @dbml/core DESCRIPTION: This line imports the `importer` object from the @dbml/core package, which is used for generating DBML from various input formats like SQL or a DatabaseSchema object. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_1 LANGUAGE: javascript CODE: ``` const { importer } = require('@dbml/core'); ``` ---------------------------------------- TITLE: Defining Inline One-to-One Relationship in DBML DESCRIPTION: Demonstrates the inline syntax for defining a one-to-one relationship (`-`) where the column with the `ref` definition (`user_id` in `user_infos`) is treated as the foreign key. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_13 LANGUAGE: text CODE: ``` Table user_infos { user_id integer [ref: - users.id] } ``` ---------------------------------------- TITLE: Adding Single-Line Table Note in DBML DESCRIPTION: Shows how to add a single-line note directly within a table definition to describe its purpose. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_21 LANGUAGE: text CODE: ``` Table users { id int [pk] name varchar Note: 'Stores user data' } ``` ---------------------------------------- TITLE: Adding Notes to DBML Elements DESCRIPTION: Explains how to add descriptive notes to DBML elements like tables using either a short form with a colon or a long form with curly braces. Notes are string values. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_19 LANGUAGE: text CODE: ``` Table users { id int [pk] name varchar Note: 'This is a note of this table' // or Note { 'This is a note of this table' } } ``` ---------------------------------------- TITLE: Adding Multi-Line Column Note in DBML DESCRIPTION: Illustrates adding a multi-line note to a column definition, useful for enumerating possible values or detailed descriptions. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_23 LANGUAGE: text CODE: ``` Table orders { status varchar [ note: ''' 💸 1 = processing, ✔️ 2 = shipped, ❌ 3 = cancelled, 😔 4 = refunded '''] } ``` ---------------------------------------- TITLE: Adding TableGroup Note in DBML DESCRIPTION: Shows how to add a note to a TableGroup definition, either inline using the note setting or using the Note keyword within the group block. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_26 LANGUAGE: text CODE: ``` TableGroup e_commerce [note: 'Contains tables that are related to e-commerce system'] { merchants countries // or Note: 'Contains tables that are related to e-commerce system' } ``` ---------------------------------------- TITLE: Import exporter from @dbml/core DESCRIPTION: This line imports the `exporter` object from the @dbml/core package, which is used for converting DBML content into various SQL dialects. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/js-module/core.md#_snippet_4 LANGUAGE: javascript CODE: ``` const { exporter } = require('@dbml/core'); ``` ---------------------------------------- TITLE: Defining Zero-to Relationships in DBML DESCRIPTION: Shows how zero-to-one or zero-to-many relationships are implicitly defined by combining the relationship symbol (`>`) with the column's nullable constraint (`null` or `not null`). SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_11 LANGUAGE: text CODE: ``` Table follows { following_user_id int [ref: > users.id] // many-to-zero followed_user_id int [ref: > users.id, null] // many-to-zero } Table posts { id int [pk] user_id int [ref: > users.id, not null] // many-to-one } ``` ---------------------------------------- TITLE: Defining Inline Relationships in DBML DESCRIPTION: Illustrates how to define many-to-one and one-to-many relationships directly within table column definitions using the `ref` keyword and relationship symbols (`>`, `<`). Multiple relationships can be defined on a single column. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_10 LANGUAGE: text CODE: ``` Table posts { id integer [primary key] user_id integer [ref: > users.id] // many-to-one } // or this Table users { id integer [ref: < posts.user_id, ref: < reviews.user_id] // one to many } // The space after '<' is optional ``` ---------------------------------------- TITLE: Defining DBML Multi-line String DESCRIPTION: Demonstrates how to define a multi-line string value in DBML using triple single quotes ('''). The content between the quotes can span multiple lines, and leading indentation is handled automatically by the parser. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_32 LANGUAGE: DBML CODE: ``` Note: ''' This is a block string This string can spans over multiple lines. ''' ``` ---------------------------------------- TITLE: DBML Relationship Definition Syntaxes DESCRIPTION: Presents the three main syntaxes for defining relationships in DBML: the verbose long form using curly braces, the concise short form using a colon, and the inline form within a table column definition. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_12 LANGUAGE: text CODE: ``` // Long form Ref name_optional { schema1.table1.column1 < schema2.table2.column2 } // Short form Ref name_optional: schema1.table1.column1 < schema2.table2.column2 // Inline form Table schema2.table2 { id integer column2 integer [ref: > schema1.table1.column1] } ``` ---------------------------------------- TITLE: DBML Single-line Comment DESCRIPTION: Illustrates how to add a single-line comment in DBML using double forward slashes (//). Comments are used for code documentation and review, and they extend to the end of the line. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_34 LANGUAGE: DBML CODE: ``` // order_items refer to items from that order ``` ---------------------------------------- TITLE: Defining Enums in DBML DESCRIPTION: Shows how to define custom enum types with a list of allowed values. Enums can belong to the default schema or a specified schema and can include notes for individual values. Tables can then use these enums as column types. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_17 LANGUAGE: text CODE: ``` // enum belonged to default "public" schema enum job_status { created [note: 'Waiting to be processed'] running done failure } // enum belonged to a schema enum v2.job_status { ... } Table jobs { id integer status job_status status_v2 v2.job_status } ``` ---------------------------------------- TITLE: DBML Column Note Syntax DESCRIPTION: Provides the basic syntax for adding a note to a column definition in DBML. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_22 LANGUAGE: text CODE: ``` column_name column_type [note: 'replace text here'] ``` ---------------------------------------- TITLE: DBML Multi-line Comment DESCRIPTION: Shows how to define a multi-line comment block in DBML by enclosing the comment text within /* and */. This is useful for longer explanations or temporarily disabling blocks of code across multiple lines. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_35 LANGUAGE: DBML CODE: ``` /* This is a Multi-lines comment */ ``` ---------------------------------------- TITLE: Defining Enums with Special Characters in DBML DESCRIPTION: Illustrates how to define enum values that contain spaces or other special characters by enclosing the values in double quotes. SOURCE: https://github.com/holistics/dbml/blob/master/dbml-homepage/docs/docs.md#_snippet_18 LANGUAGE: text CODE: ``` enum grade { "A+" "A" "A-" "Not Yet Set" } ```