Code Monkey home page Code Monkey logo

vscode-dbml's Introduction

DBML Language Support

Version 0.3.5 Badge MIT License Badge

This Visual Studio Code extension provides language support for the Database Markup Language (DBML).

Syntax Highlighting

Syntax highlighting is enabled for files with the .dbml extension.

Syntax Example

Commands

To SQL

When focusing a .dbml file, the DBML: To SQL command becomes available in the command palette. This command leverages the @dbml/core (source) package to generate an SQL script based on the provided schema.

Selecting this commands presents all available dialects. At this time the three available dialects are PostgreSQL, MySQL, and SQL Server. After choosing a dialect, enter an output filename. If a relative path is provided, the generated file will be placed relative to the focused .dbml file. Otherwise is will be placed at the provided absolute path.

From SQL

When focusing a .sql file, the DBML: From SQL command appears in the command palette. This command acts as the opposite of the DBML: To SQL command and generates a .dbml from from the provided .sql file. The dialect of the SQL must be provided. At this time, this command only works for PostgreSQL and MySQL.

Snippets

Snippets are provided for quickly creating tables, enums, and references. To use a snippet, begin typing any of the following keys.

  • table: Create a new table with an auto-incrementing int id as the primary key
  • enum: Create a new enum
  • oto: Create a one-to-one reference
  • otm: Create a one-to-many reference
  • mto: Create a many-to-one reference
  • mtm: Create a many-to-many join table

Issues

If you find an error or bug, please create an issue.

Contributing

If you want to add to this project, feel free to fork the repository and submit a pull request.

vscode-dbml's People

Contributors

dependabot[bot] avatar jaimeloeuf avatar mattmeyers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vscode-dbml's Issues

Highlight column type when enum

Currently the syntax parser only highlights core SQL types for the column type. It should use a pattern so that enums are also highlighted.

DBML: to SQL not working

I used the sample code from https://dbdiagram.io/d
then I tried "to SQL" with target MS SQL Server, but I only get errors:
image

VSC:

Version: 1.53.2 (user setup)
Commit: 622cb03f7e070a9670c94bae1a45d78d7181fbd4
Date: 2021-02-11T11:48:04.245Z
Electron: 11.2.1
Chrome: 87.0.4280.141
Node.js: 12.18.3
V8: 8.7.220.31-electron.0
OS: Windows_NT x64 10.0.19041

Update Syntax Parsing

Currently the syntax parser simply looks for keywords when performing the syntax highlighting. As such, certain words aren't always highlighted correctly. For example, column types are highlighted based off of a list of keywords. Any column types not in this list will not be highlighted.

The grammar should instead parse the structure of the document instead when assigning scope selectors to tokens.

Chart View

Could you add the ability to view the diagram directly in the IDE, without having to go to the site?

DBML: To SQL' resulted in an error

Command 'DBML: To SQL' resulted in an error (Expected Table Group, comment, end of input, enum, references, table, or whitespace but "P" found.)

sql export to SQL Server: Table "[schema_aaa].[table_bbb]" => CREATE TABLE [[schema_aaa].[table_bbb]]

dbml doesn't yet support schemas. The only way to use them is:
Table "[schema_aaa].[table_bbb]"

When creating the sql then it is not what it could be, it is created like CREATE TABLE [[schema_aaa].[table_bbb]]
I am not sure if this can be solved or if it should be implemented when dbml will support it. But without schema support only tables in schema dbo could be created.

I only want to report the issues. Currently, it is not so important for me to solve them, because dbml doesn't yet support calculated columns and some other required features, and anyway I can not use it to define a database in dbml and create sql from dbml without these features. But for tables without calculated columns and for simpler databases it could work if there would be a solution for the schemas.

Other issues:

  • "defaults"
    expressions are quoted with additional "()"
    it is OK, it is valid, so it could stay this way
  • index
    column names are OK in the table definition, but in the index definition they are quoted with "" instead of []
  • extended properties
    • when the target is MSSQL the name for exported descriptions should be "ms_description", but not "Column_description" or "Table_description"
    • issue with splitting into schema and table

example:
MS SQL Server

Table "[repo].[RepoObject]"{
"RepoObject_guid" uniqueidentifier [not null, pk, default: `(newsequentialid())`]
"has_execution_plan_issue" bit [ null]
"has_get_referenced_issue" bit [ null]
"Inheritance_StringAggSeparatorSql" nvarchar(4000) [ null]
"InheritanceDefinition" nvarchar(4000) [ null]
"InheritanceType" tinyint [ null]
"is_repo_managed" bit [ null]
"is_SysObject_missing" bit [ null]
"modify_dt" datetime [not null, default: `(getdate())`]
"pk_index_guid" uniqueidentifier [ null]
"Repo_history_table_guid" uniqueidentifier [ null]
"Repo_temporal_type" tinyint [ null, Note: '''
reference in [repo_sys].[type]
''']
"RepoObject_name" nvarchar(128) [not null, default: `(newid())`]
"RepoObject_Referencing_Count" int [ null]
"RepoObject_schema_name" nvarchar(128) [not null]
"RepoObject_type" char(2) [not null, Note: '''
reference in [repo_sys].[type]
''']
"SysObject_id" int [ null]
"SysObject_modify_date" datetime [ null]
"SysObject_name" nvarchar(128) [not null, default: `(newid())`]
"SysObject_parent_object_id" int [not null, default: `((0))`]
"SysObject_query_executed_dt" datetime [ null]
"SysObject_query_plan" xml [ null]
"SysObject_schema_name" nvarchar(128) [not null]
"SysObject_type" char(2) [ null, Note: '''
reference in [repo_sys].[type]
''']
"has_different_sys_names" bit [ null, Note: '''
(CONVERT([bit],case when [RepoObject_schema_name]<>[SysObject_schema_name] OR [RepoObject_name]<>[SysObject_name] OR [RepoObject_type]<>[SysObject_type] then (1) else (0) end))
''']
"is_RepoObject_name_uniqueidentifier" int [not null, Note: '''
(case when TRY_CAST([RepoObject_name] AS [uniqueidentifier]) IS NULL then (0) else (1) end)
''']
"is_SysObject_name_uniqueidentifier" int [not null, Note: '''
(case when TRY_CAST([SysObject_name] AS [uniqueidentifier]) IS NULL then (0) else (1) end)
''']
"node_id" bigint [ null, Note: '''
(CONVERT([bigint],[SysObject_id])*(10000))
''']
"RepoObject_fullname" nvarchar(261) [not null, Note: '''
(concat('[',[RepoObject_schema_name],'].[',[RepoObject_name],']'))
''']
"SysObject_fullname" nvarchar(261) [not null, Note: '''
(concat('[',[SysObject_schema_name],'].[',[SysObject_name],']'))
''']
"SysObject_query_sql" nvarchar(406) [not null, Note: '''
(concat('SELECT * FROM [',[repo].[fs_dwh_database_name](),'].[',[SysObject_schema_name],'].[',[SysObject_name],']'))
''']
"usp_persistence_name" nvarchar(140) [not null, Note: '''
('usp_PERSIST_'+[RepoObject_name])
''']

indexes {
 ( "SysObject_schema_name", "SysObject_name") [unique]
 ( "RepoObject_schema_name", "RepoObject_name") [unique]
}
}

this will create an mssql script

CREATE TABLE [[repo].[RepoObject]] (
  [RepoObject_guid] uniqueidentifier PRIMARY KEY NOT NULL DEFAULT ((newsequentialid())),
  [has_execution_plan_issue] bit,
  [has_get_referenced_issue] bit,
  [Inheritance_StringAggSeparatorSql] nvarchar(4000),
  [InheritanceDefinition] nvarchar(4000),
  [InheritanceType] tinyint,
  [is_repo_managed] bit,
  [is_SysObject_missing] bit,
  [modify_dt] datetime NOT NULL DEFAULT ((getdate())),
  [pk_index_guid] uniqueidentifier,
  [Repo_history_table_guid] uniqueidentifier,
  [Repo_temporal_type] tinyint,
  [RepoObject_name] nvarchar(128) NOT NULL DEFAULT ((newid())),
  [RepoObject_Referencing_Count] int,
  [RepoObject_schema_name] nvarchar(128) NOT NULL,
  [RepoObject_type] char(2) NOT NULL,
  [SysObject_id] int,
  [SysObject_modify_date] datetime,
  [SysObject_name] nvarchar(128) NOT NULL DEFAULT ((newid())),
  [SysObject_parent_object_id] int NOT NULL DEFAULT (((0))),
  [SysObject_query_executed_dt] datetime,
  [SysObject_query_plan] xml,
  [SysObject_schema_name] nvarchar(128) NOT NULL,
  [SysObject_type] char(2),
  [has_different_sys_names] bit,
  [is_RepoObject_name_uniqueidentifier] int NOT NULL,
  [is_SysObject_name_uniqueidentifier] int NOT NULL,
  [node_id] bigint,
  [RepoObject_fullname] nvarchar(261) NOT NULL,
  [SysObject_fullname] nvarchar(261) NOT NULL,
  [SysObject_query_sql] nvarchar(406) NOT NULL,
  [usp_persistence_name] nvarchar(140) NOT NULL
)
GO

CREATE UNIQUE INDEX [[repo].[RepoObject]_index_0] ON [[repo].[RepoObject]] ("SysObject_schema_name", "SysObject_name")
GO

CREATE UNIQUE INDEX [[repo].[RepoObject]_index_1] ON [[repo].[RepoObject]] ("RepoObject_schema_name", "RepoObject_name")
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'reference in [repo_sys].[type]',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'Repo_temporal_type';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'reference in [repo_sys].[type]',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'RepoObject_type';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'reference in [repo_sys].[type]',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'SysObject_type';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(CONVERT([bit],case when [RepoObject_schema_name]<>[SysObject_schema_name] OR [RepoObject_name]<>[SysObject_name] OR [RepoObject_type]<>[SysObject_type] then (1) else (0) end))',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'has_different_sys_names';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(case when TRY_CAST([RepoObject_name] AS [uniqueidentifier]) IS NULL then (0) else (1) end)',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'is_RepoObject_name_uniqueidentifier';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(case when TRY_CAST([SysObject_name] AS [uniqueidentifier]) IS NULL then (0) else (1) end)',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'is_SysObject_name_uniqueidentifier';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(CONVERT([bigint],[SysObject_id])*(10000))',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'node_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(concat("[",[RepoObject_schema_name],"].[",[RepoObject_name],"]"))',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'RepoObject_fullname';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(concat("[",[SysObject_schema_name],"].[",[SysObject_name],"]"))',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'SysObject_fullname';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '(concat("SELECT * FROM [",[repo].[fs_dwh_database_name](),"].[",[SysObject_schema_name],"].[",[SysObject_name],"]"))',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'SysObject_query_sql';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '("usp_PERSIST_"+[RepoObject_name])',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = '[repo].[RepoObject]',
@level2type = N'Column', @level2name = 'usp_persistence_name';
GO

Collapse code regions and support for VSCode Outline

Not a bug but more of an feature request, it would be really great if your code support for DBML included the ability to collapse sections or regions of code. Collapse tables and even allow for the ability to use something like the '#Region' directive to specify chunks of code one might want to collapse.

Also, the vscode feature Outline would be nice to be able to use.

Just a suggestion/request, otherwise great plugin...

k.

Command 'DBML: To SQL' resulted in an error (Expected "{", comment, or whitespace but "S" found.)

My DBML file
Project Enterprise Search {
database_type: 'mysql'
note: 'DB schema for Enterprise Search'
}

Table Org{
id varchar(36) [pk]
name varchar [not null]
}

Table OrgOptionsDetails{
id int [pk, increment]
name varchar(32) [not null]
description varchar(1024) [not null]
example varchar(256) [not null]
dataType varchar(24) [not null]
subType varchar(24)
dataList varchar
default varchar(256)
required boolean [default: false]
readOnly boolean [default: false]
enableOn int
enableValue varchar(256)
validation varchar(256)
}

Table OrgOptions{
id int [pk, increment]
org_id int [ref: > Org.id]
org_opt_id int [ref: > OrgOptionsDetails.id]
value varchar [not null]
}

Table Index{
id varchar(36) [pk]
name varchar [not null]
org_id int [ref: > Org.id]
crawler_type_id int [ref: > CrawlerTypes.id]
}

Table IndexOptionsDetails{
id int [pk, increment]
name varchar(32) [not null]
description varchar(1024) [not null]
example varchar(256) [not null]
dataType varchar(24) [not null]
subType varchar(24)
dataList varchar
default varchar(256)
required boolean [default: false]
readOnly boolean [default: false]
enableOn int
enableValue varchar(256)
validation varchar(256)
}

Table IndexOptions{
id int [pk, increment]
index_id varchar(36) [ref: > Index.id]
index_opt_id int [ref: > IndexOptionsDetails.id]
value varchar [not null]
}

Table SortOptions{
id int [pk, increment]
index_id varchar(36) [ref: > Index.id]
name varchar [not null]
}

Table SearchUser{
id varchar [not null, unique]
org_id varchar(36) [ref: > Org.id]
}

Table Query{
id varchar(36) [pk]
user_id int [not null, unique, ref: > SearchUser.id]
org_id int [not null, unique, ref: > Org.id]
queryType varchar [not null]
queryText carchar [not null]
created_at datetime [default: now()]
}

Table QueryIndexMapping{
query_id int [not null, unique, ref: > Query.id]
index_id int [not null, unique, ref: > Index.id]
}

Table Document{
id varchar(36) [pk]
fileDataType varchar [not null]
storageid varchar(36) [not null, unique]
}

Table DocumentIndexMapper{
doc_id varchar(36) [ref: > Document.id]
index_id varchar(36) [ref: > Index.id]
}

Table CrawlerOptionsDetails{
id int [pk, increment]
name varchar(32) [not null]
description varchar(1024) [not null]
example varchar(256) [not null]
data_type varchar(24) [not null]
sub_type varchar(24)
data_list varchar
default_value varchar(256)
required boolean [default: false]
readOnly boolean [default: false]
enableOn int
enableValue varchar(256)
validation varchar(256)
}

Table CrawlerOptions{
id int [pk, increment]
index_id varchar(36) [ref: > Index.id]
crawler_opt_detail_id int [ref: > CrawlerOptionsDetails.id]
crawler_type_id int [ref: > CrawlerTypes.id]
value varchar [not null]
}

Table CrawlerTypes{
id varchar(36) [pk]
type varchar(36) [not null]
}

Table CrawlerReg{
id varchar(16) [pk]
uri varchar [not null]
crawler_type_id int [ref: > CrawlerTypes.id]
crawler_polls boolean [default: false]
downLoad_enabled boolean [default: true]
}

Table CrawlerOrgs{
crawler_reg_id varchar(16) [ref: > CrawlerReg.id]
org_id int [ref: > Org.id]
}

Table CrawlerIndexes{
crawler_reg_id varchar(16) [ref: > CrawlerReg.id]
index_id int [ref: > Index.id]
}

Table CrawlerReq{
id varchar(16) [pk]
crawler_reg_id varchar(16) [ref: > CrawlerReg.id]
created_at datetime
comp_at datetime
timeout_ms int
completed boolean [default: false]
}

Table CrawlerReqIndexes{
crawler_req_id varchar(16) [ref: > CrawlerReq.id]
index_id int [ref: > Index.id]
index_update_id int [ref: > IndexUpdate.id]
}

Table CrawledDocs{
path varchar
index_id varchar(36) [ref: > Index.id]
doc_id varchar(36) [ref: > Document.id]
file_type_id int [ref: > FileType.id]
crawler_req_id varchar(16) [ref: > CrawlerReq.id]
user varchar(16)
last_crawled_at datetime
op_pending varchar(8) [note: 'add , update, delete, index, none']
error varchar(36)
}

Table FileType{
id int
name varchar(32)
}

enum index_op_status {
pending
doc_list_req
doc_list_rec
doc_list_failed
crawl_progress
crawl_partial_failed
crawl_failed
complete
}

Table IndexUpdate{
id int [pk, increment]
index_id int [ref: > Index.id]
operation int [note: '0 for Rebuild Index, 1 for Update Index']
completed boolean [default: false]
req_time datetime
comp_time datetime
status index_op_status
}

highlight not working

my only issue is that the highlight of the properties like int or null or which ever is it is not working atleast for me in vscode
noHighlight

MSSQL from SQL command broken

The importer from the @dbml/core library does not seem to work with SQL Server. This dialect should be removed for now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.