@dbml/cli 3.6.1 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__test__/cli.test.js +7 -1
- package/__test__/db2dbml/mssql/dbml-error.log +467 -0
- package/__test__/db2dbml/mssql/expect-out-files/schema.dbml +190 -0
- package/__test__/db2dbml/mssql/options.json +8 -0
- package/__test__/db2dbml/mssql/schema.sql +296 -0
- package/__test__/db2dbml/mssql/stdout.txt +1 -0
- package/__test__/db2dbml/mysql/dbml-error.log +281 -0
- package/__test__/db2dbml/mysql/expect-out-files/schema.dbml +180 -0
- package/__test__/db2dbml/mysql/options.json +8 -0
- package/__test__/db2dbml/mysql/out-files/schema.dbml +180 -0
- package/__test__/db2dbml/mysql/schema.sql +141 -0
- package/__test__/db2dbml/mysql/stdout.txt +1 -0
- package/__test__/db2dbml/postgres/dbml-error.log +252 -0
- package/__test__/db2dbml/postgres/expect-out-files/schema.dbml +140 -0
- package/__test__/db2dbml/postgres/options.json +8 -0
- package/__test__/db2dbml/postgres/out-files/schema.dbml +140 -0
- package/__test__/db2dbml/postgres/schema.sql +156 -0
- package/__test__/db2dbml/postgres/stdout.txt +1 -0
- package/__test__/db2dbml_bin.js +5 -0
- package/__test__/dbml2sql/filename --mysql --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filename --oracle --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filename --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filename --postgres --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filenames --mysql --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filenames --oracle --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filenames --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/filenames --postgres --out-file/out-files/schema.sql +1 -1
- package/__test__/dbml2sql/multiple_schema_mssql/out-files/multiple_schema.out.sql +1 -1
- package/__test__/dbml2sql/multiple_schema_mysql/out-files/multiple_schema.out.sql +1 -1
- package/__test__/dbml2sql/multiple_schema_oracle/out-files/multiple_schema.out.sql +1 -1
- package/__test__/dbml2sql/multiple_schema_pg/out-files/multiple_schema.out.sql +1 -1
- package/__test__/dbml2sql/syntax-error/dbml-error.log +9 -0
- package/__test__/sql2dbml/filename --snowflake stdout/dbml-error.log +0 -0
- package/__test__/sql2dbml/filename --snowflake stdout/stdout.txt +6 -6
- package/__test__/sql2dbml/syntax-error/dbml-error.log +9 -0
- package/__test__/sql2dbml/syntax-error-duplicate-endpoints --mssql/dbml-error.log +9 -0
- package/__test__/sql2dbml/syntax-error-duplicate-endpoints --mysql/dbml-error.log +9 -0
- package/bin/db2dbml.js +4 -0
- package/dbml-error.log +53 -0
- package/lib/cli/connector.js +36 -0
- package/lib/cli/index.js +28 -0
- package/lib/cli/utils.js +20 -0
- package/lib/connectors/Connector.js +19 -0
- package/lib/connectors/MssqlConnector.js +483 -0
- package/lib/connectors/PostgresConnector.js +450 -0
- package/lib/index.js +6 -0
- package/package.json +5 -4
- package/src/cli/connector.js +31 -0
- package/src/cli/index.js +35 -0
- package/src/cli/utils.js +23 -0
- package/src/index.js +2 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
-- Create users table
|
|
2
|
+
CREATE TABLE users (
|
|
3
|
+
user_id SERIAL PRIMARY KEY,
|
|
4
|
+
username VARCHAR(50) UNIQUE NOT NULL,
|
|
5
|
+
email VARCHAR(100) UNIQUE NOT NULL,
|
|
6
|
+
password_hash VARCHAR(255) NOT NULL,
|
|
7
|
+
first_name VARCHAR(50),
|
|
8
|
+
last_name VARCHAR(50),
|
|
9
|
+
full_name VARCHAR(100),
|
|
10
|
+
date_of_birth DATE,
|
|
11
|
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
12
|
+
last_login TIMESTAMP WITH TIME ZONE,
|
|
13
|
+
is_active BOOLEAN DEFAULT TRUE,
|
|
14
|
+
CONSTRAINT chk_email_format CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$'),
|
|
15
|
+
CONSTRAINT chk_age CHECK (date_of_birth <= CURRENT_DATE - INTERVAL '13 years')
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
-- Create an index on the email column for faster lookups
|
|
19
|
+
CREATE INDEX idx_users_email ON users (email);
|
|
20
|
+
|
|
21
|
+
CREATE UNIQUE INDEX ON "users" ("user_id");
|
|
22
|
+
|
|
23
|
+
CREATE INDEX "User Name" ON "users" ("full_name");
|
|
24
|
+
|
|
25
|
+
CREATE INDEX ON "users" ("is_active", ((lower(full_name))));
|
|
26
|
+
|
|
27
|
+
-- Create products table
|
|
28
|
+
CREATE TABLE products (
|
|
29
|
+
product_id SERIAL PRIMARY KEY,
|
|
30
|
+
name VARCHAR(100) NOT NULL,
|
|
31
|
+
description TEXT,
|
|
32
|
+
price DECIMAL(10, 2) NOT NULL,
|
|
33
|
+
stock_quantity INTEGER NOT NULL DEFAULT 0,
|
|
34
|
+
category VARCHAR(50),
|
|
35
|
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
36
|
+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
37
|
+
is_available BOOLEAN DEFAULT TRUE,
|
|
38
|
+
CONSTRAINT chk_price_positive CHECK (price > 0),
|
|
39
|
+
CONSTRAINT chk_stock_non_negative CHECK (stock_quantity >= 0)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
-- Create an index on the category column for faster filtering
|
|
43
|
+
CREATE INDEX idx_products_category ON products (category);
|
|
44
|
+
|
|
45
|
+
-- Create orders table
|
|
46
|
+
CREATE TABLE orders (
|
|
47
|
+
order_id SERIAL PRIMARY KEY,
|
|
48
|
+
user_id INTEGER NOT NULL,
|
|
49
|
+
order_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
50
|
+
total_amount DECIMAL(12, 2) NOT NULL,
|
|
51
|
+
status VARCHAR(20) DEFAULT 'pending',
|
|
52
|
+
shipping_address TEXT NOT NULL,
|
|
53
|
+
billing_address TEXT NOT NULL,
|
|
54
|
+
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
|
|
55
|
+
CONSTRAINT chk_total_amount_positive CHECK (total_amount > 0),
|
|
56
|
+
CONSTRAINT chk_status CHECK (status IN ('pending', 'processing', 'shipped', 'delivered', 'cancelled'))
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
-- Create an index on the user_id and order_date columns for faster querying
|
|
60
|
+
CREATE INDEX idx_orders_user_date ON orders (user_id, order_date);
|
|
61
|
+
|
|
62
|
+
-- Create order_items table
|
|
63
|
+
CREATE TABLE order_items (
|
|
64
|
+
order_item_id SERIAL PRIMARY KEY,
|
|
65
|
+
order_id INTEGER NOT NULL,
|
|
66
|
+
product_id INTEGER NOT NULL,
|
|
67
|
+
quantity INTEGER NOT NULL,
|
|
68
|
+
unit_price DECIMAL(10, 2) NOT NULL,
|
|
69
|
+
CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES orders(order_id) ON DELETE CASCADE,
|
|
70
|
+
CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE,
|
|
71
|
+
CONSTRAINT chk_quantity_positive CHECK (quantity > 0),
|
|
72
|
+
CONSTRAINT chk_unit_price_positive CHECK (unit_price > 0),
|
|
73
|
+
CONSTRAINT uq_order_product UNIQUE (order_id, product_id)
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
-- Create a composite index on order_id and product_id for faster joins
|
|
77
|
+
CREATE INDEX idx_order_items_order_product ON order_items (order_id, product_id);
|
|
78
|
+
|
|
79
|
+
-- Create a table with default values for string types
|
|
80
|
+
CREATE TABLE all_string_types (
|
|
81
|
+
text_col TEXT DEFAULT 'default_text',
|
|
82
|
+
varchar_col VARCHAR(100) DEFAULT 'default_varchar',
|
|
83
|
+
char_col CHAR(10) DEFAULT 'default_char',
|
|
84
|
+
character_varying_col CHARACTER VARYING(50) DEFAULT 'default_character_varying',
|
|
85
|
+
character_col CHARACTER(5) DEFAULT 'default_character',
|
|
86
|
+
name_col NAME DEFAULT 'default_name',
|
|
87
|
+
bpchar_col BPCHAR(15) DEFAULT 'default_bpchar',
|
|
88
|
+
text_array_col TEXT[] DEFAULT ARRAY['default_text1', 'default_text2'],
|
|
89
|
+
json_col JSON DEFAULT '{"default_key": "default_value"}',
|
|
90
|
+
jsonb_col JSONB DEFAULT '{"default_key": "default_value"}'
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
-- Create a table with default value for number type, boolean type, date type
|
|
94
|
+
CREATE TABLE all_default_values (
|
|
95
|
+
id SERIAL PRIMARY KEY, -- A primary key column
|
|
96
|
+
boolean_col BOOLEAN DEFAULT TRUE, -- BOOLEAN type with default value
|
|
97
|
+
integer_col INTEGER DEFAULT 42, -- INTEGER type with default value
|
|
98
|
+
numeric_col NUMERIC(10, 2) DEFAULT 99.99, -- NUMERIC type with default value
|
|
99
|
+
date_col DATE DEFAULT CURRENT_DATE, -- DATE type with default value
|
|
100
|
+
date_col_specific DATE DEFAULT '2024-01-01', -- DATE with a specific default value
|
|
101
|
+
timestamp_col TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- TIMESTAMP with default value
|
|
102
|
+
timestamp_col_specific TIMESTAMP DEFAULT '2024-01-01 12:00:00', -- TIMESTAMP with a specific default value
|
|
103
|
+
-- New columns that calculate default values based on formulas
|
|
104
|
+
date_plus_7_days DATE DEFAULT (CURRENT_DATE + INTERVAL '7 days'), -- 7 days from current date
|
|
105
|
+
date_minus_30_days DATE DEFAULT (CURRENT_DATE - INTERVAL '30 days'), -- 30 days before current date
|
|
106
|
+
timestamp_plus_1_hour TIMESTAMP DEFAULT (CURRENT_TIMESTAMP + INTERVAL '1 hour'), -- 1 hour from current timestamp
|
|
107
|
+
timestamp_minus_15_minutes TIMESTAMP DEFAULT (CURRENT_TIMESTAMP - INTERVAL '15 minutes') -- 15 minutes before current timestamp
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
-- Create a table with user-defined data types
|
|
111
|
+
CREATE TYPE gender_type AS ENUM ('Male', 'Female', 'Other');
|
|
112
|
+
|
|
113
|
+
CREATE TABLE user_define_data_types (
|
|
114
|
+
id SERIAL PRIMARY KEY,
|
|
115
|
+
name VARCHAR(50),
|
|
116
|
+
gender gender_type,
|
|
117
|
+
age int4range, -- Using built-in int4range for age range
|
|
118
|
+
height FLOAT,
|
|
119
|
+
weight FLOAT
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
-- Create table with comments
|
|
123
|
+
CREATE TABLE table_with_comments (
|
|
124
|
+
id SERIAL PRIMARY KEY,
|
|
125
|
+
name VARCHAR(100),
|
|
126
|
+
description TEXT,
|
|
127
|
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
-- Add comments
|
|
131
|
+
COMMENT ON TABLE table_with_comments IS 'This table stores information about various items.';
|
|
132
|
+
COMMENT ON COLUMN table_with_comments.id IS 'Unique identifier for each item.';
|
|
133
|
+
COMMENT ON COLUMN table_with_comments.name IS 'Name of the item.';
|
|
134
|
+
COMMENT ON COLUMN table_with_comments.description IS 'Detailed description of the item.';
|
|
135
|
+
COMMENT ON COLUMN table_with_comments.created_at IS 'Timestamp when the item was created.';
|
|
136
|
+
|
|
137
|
+
-- Create Authors table
|
|
138
|
+
CREATE TABLE Authors (
|
|
139
|
+
AuthorID SERIAL,
|
|
140
|
+
NationalityID INT,
|
|
141
|
+
AuthorName VARCHAR(100),
|
|
142
|
+
BirthYear INT,
|
|
143
|
+
PRIMARY KEY (AuthorID, NationalityID) -- Use composite primary key
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
-- Create Books table
|
|
147
|
+
CREATE TABLE Books (
|
|
148
|
+
BookID SERIAL PRIMARY KEY,
|
|
149
|
+
AuthorID INT,
|
|
150
|
+
NationalityID INT,
|
|
151
|
+
ISBN VARCHAR(20),
|
|
152
|
+
Title VARCHAR(200),
|
|
153
|
+
UNIQUE (ISBN),
|
|
154
|
+
CONSTRAINT FK_AuthorNationality FOREIGN KEY (AuthorID, NationalityID)
|
|
155
|
+
REFERENCES Authors (AuthorID, NationalityID) ON DELETE CASCADE
|
|
156
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
✔ Generated DBML file from database's connection: schema.dbml
|
|
File without changes
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Table "HP_TEST"."CUSTOMERS" {
|
|
2
2
|
"CUSTOMER_ID" "NUMBER(38, 0)" [pk, not null, increment]
|
|
3
|
-
"FIRST_NAME" VARCHAR(50) [not null, default:
|
|
4
|
-
"LAST_NAME" VARCHAR(50) [not null, default:
|
|
3
|
+
"FIRST_NAME" VARCHAR(50) [not null, default: 'John']
|
|
4
|
+
"LAST_NAME" VARCHAR(50) [not null, default: 'Doe']
|
|
5
5
|
"EMAIL" VARCHAR(100)
|
|
6
6
|
"SECOND_EMAIL" VARCHAR(100)
|
|
7
7
|
"UNIQUE_NAME" VARCHAR(100) [unique, not null, default: `CONCAT(FIRST_NAME, LAST_NAME, EMAIL)`]
|
|
@@ -196,19 +196,19 @@ Table "t_default_14" {
|
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
Table "t_default_15" {
|
|
199
|
-
"v" varchar(32) [default:
|
|
199
|
+
"v" varchar(32) [default: 'hey']
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
Table "t_default_16" {
|
|
203
|
-
"v" varchar(32) [unique, default:
|
|
203
|
+
"v" varchar(32) [unique, default: 'hey']
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
Table "t_default_17" {
|
|
207
|
-
"v" varchar(32) [unique, default:
|
|
207
|
+
"v" varchar(32) [unique, default: 'hey']
|
|
208
208
|
}
|
|
209
209
|
|
|
210
210
|
Table "t_default_18" {
|
|
211
|
-
"v" varchar(32) [pk, default:
|
|
211
|
+
"v" varchar(32) [pk, default: 'hey']
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
Table "t_masking_1" {
|
package/bin/db2dbml.js
ADDED
package/dbml-error.log
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
2024-08-07T02:44:57.506Z
|
|
2
|
+
Error: Error: connect ECONNREFUSED 127.0.0.1:3306
|
|
3
|
+
at _callee8$ (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:474:17)
|
|
4
|
+
at tryCatch (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:1357)
|
|
5
|
+
at Generator.<anonymous> (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:4174)
|
|
6
|
+
at Generator.throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:2208)
|
|
7
|
+
at asyncGeneratorStep (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:17:103)
|
|
8
|
+
at _throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:18:291)
|
|
9
|
+
at processTicksAndRejections (node:internal/process/task_queues:96:5)
|
|
10
|
+
|
|
11
|
+
2024-08-07T02:51:24.263Z
|
|
12
|
+
Error: Error: MySQL connection error:
|
|
13
|
+
at _callee8$ (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:485:17)
|
|
14
|
+
at tryCatch (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:1357)
|
|
15
|
+
at Generator.<anonymous> (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:4174)
|
|
16
|
+
at Generator.throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:2208)
|
|
17
|
+
at asyncGeneratorStep (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:17:103)
|
|
18
|
+
at _throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:18:291)
|
|
19
|
+
at processTicksAndRejections (node:internal/process/task_queues:96:5)
|
|
20
|
+
|
|
21
|
+
2024-08-07T02:52:34.901Z
|
|
22
|
+
Error: Error: MySQL connection error: Error: connect ECONNREFUSED 127.0.0.1:3306
|
|
23
|
+
at _callee8$ (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:485:17)
|
|
24
|
+
at tryCatch (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:1357)
|
|
25
|
+
at Generator.<anonymous> (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:4174)
|
|
26
|
+
at Generator.throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:2208)
|
|
27
|
+
at asyncGeneratorStep (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:17:103)
|
|
28
|
+
at _throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:18:291)
|
|
29
|
+
at processTicksAndRejections (node:internal/process/task_queues:96:5)
|
|
30
|
+
|
|
31
|
+
2024-08-07T02:53:35.536Z
|
|
32
|
+
Error: Error: MySQL connection error: Error: connect ECONNREFUSED 127.0.0.1:3306
|
|
33
|
+
at _callee8$ (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:485:17)
|
|
34
|
+
at tryCatch (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:1357)
|
|
35
|
+
at Generator.<anonymous> (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:4174)
|
|
36
|
+
at Generator.throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:16:2208)
|
|
37
|
+
at asyncGeneratorStep (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:17:103)
|
|
38
|
+
at _throw (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/parse/connection/mysql/utils/sql.js:18:291)
|
|
39
|
+
at processTicksAndRejections (node:internal/process/task_queues:96:5)
|
|
40
|
+
|
|
41
|
+
2024-08-12T04:39:12.767Z
|
|
42
|
+
TypeError [ERR_INVALID_URL]: Invalid URL
|
|
43
|
+
at new NodeError (node:internal/errors:387:5)
|
|
44
|
+
at URL.onParseError (node:internal/url:565:9)
|
|
45
|
+
at new URL (node:internal/url:641:5)
|
|
46
|
+
at Function.parseUrl (/Users/huylm/code/dbx/dbml/node_modules/mysql2/lib/connection_config.js:263:23)
|
|
47
|
+
at new ConnectionConfig (/Users/huylm/code/dbx/dbml/node_modules/mysql2/lib/connection_config.js:75:34)
|
|
48
|
+
at Object.exports.createConnection (/Users/huylm/code/dbx/dbml/node_modules/mysql2/index.js:10:35)
|
|
49
|
+
at createConnection (/Users/huylm/code/dbx/dbml/node_modules/mysql2/promise.js:252:31)
|
|
50
|
+
at _callee$ (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/connectors/mysqlConnector.js:41:48)
|
|
51
|
+
at tryCatch (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/connectors/mysqlConnector.js:14:1357)
|
|
52
|
+
at Generator.<anonymous> (/Users/huylm/code/dbx/dbml/packages/dbml-core/lib/connectors/mysqlConnector.js:14:4174)
|
|
53
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = connectionHandler;
|
|
7
|
+
var _core = require("@dbml/core");
|
|
8
|
+
var _figures = _interopRequireDefault(require("figures"));
|
|
9
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
var _utils = require("./utils");
|
|
12
|
+
var _outputConsolePlugin = _interopRequireDefault(require("./outputPlugins/outputConsolePlugin"));
|
|
13
|
+
var _outputFilePlugin = _interopRequireDefault(require("./outputPlugins/outputFilePlugin"));
|
|
14
|
+
var _logger = _interopRequireDefault(require("../helpers/logger"));
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
+
async function connectionHandler(program) {
|
|
17
|
+
try {
|
|
18
|
+
const {
|
|
19
|
+
connection,
|
|
20
|
+
format
|
|
21
|
+
} = (0, _utils.getConnectionOpt)(program.args);
|
|
22
|
+
const opts = program.opts();
|
|
23
|
+
if (!opts.outFile && !opts.outDir) {
|
|
24
|
+
const res = await _core.importer.generateDbml(connection, format);
|
|
25
|
+
_outputConsolePlugin.default.write(res);
|
|
26
|
+
} else if (opts.outFile) {
|
|
27
|
+
const res = await _core.importer.generateDbml(connection, format);
|
|
28
|
+
new _outputFilePlugin.default((0, _utils.resolvePaths)(opts.outFile)).write(res);
|
|
29
|
+
|
|
30
|
+
// bearer:disable javascript_lang_logger
|
|
31
|
+
console.log(` ${_chalk.default.green(_figures.default.main.tick)} Generated DBML file from database's connection: ${_path.default.basename(opts.outFile)}`);
|
|
32
|
+
}
|
|
33
|
+
} catch (error) {
|
|
34
|
+
_logger.default.error(error);
|
|
35
|
+
}
|
|
36
|
+
}
|
package/lib/cli/index.js
CHANGED
|
@@ -3,18 +3,24 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.db2dbml = db2dbml;
|
|
6
7
|
exports.dbml2sql = dbml2sql;
|
|
7
8
|
exports.sql2dbml = sql2dbml;
|
|
8
9
|
var _commander = _interopRequireDefault(require("commander"));
|
|
9
10
|
var _import = _interopRequireDefault(require("./import"));
|
|
10
11
|
var _export = _interopRequireDefault(require("./export"));
|
|
12
|
+
var _connector = _interopRequireDefault(require("./connector"));
|
|
11
13
|
var _package = _interopRequireDefault(require("../../package.json"));
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
function showHelp(args) {
|
|
16
|
+
if (args.length < 3) _commander.default.help();
|
|
17
|
+
}
|
|
13
18
|
function dbml2sql(args) {
|
|
14
19
|
_commander.default.version(_package.default.version);
|
|
15
20
|
_commander.default.usage('[options] <files...>').option('--mysql').option('--postgres').option('--mssql').option('--oracle').option('-o, --out-file <pathspec>', 'compile all input files into a single files');
|
|
16
21
|
// .option('-d, --out-dir <pathspec>', 'compile an input directory of dbml files into an output directory');
|
|
17
22
|
|
|
23
|
+
showHelp(args);
|
|
18
24
|
_commander.default.parse(args);
|
|
19
25
|
(0, _export.default)(_commander.default);
|
|
20
26
|
}
|
|
@@ -23,6 +29,28 @@ function sql2dbml(args) {
|
|
|
23
29
|
_commander.default.usage('[options] <files...>').option('--mysql').option('--mysql-legacy').option('--postgres').option('--postgres-legacy').option('--mssql').option('--snowflake').option('-o, --out-file <pathspec>', 'compile all input files into a single files');
|
|
24
30
|
// .option('-d, --out-dir <pathspec>', 'compile an input directory of sql files into an output directory');
|
|
25
31
|
|
|
32
|
+
showHelp(args);
|
|
26
33
|
_commander.default.parse(args);
|
|
27
34
|
(0, _import.default)(_commander.default);
|
|
35
|
+
}
|
|
36
|
+
function db2dbml(args) {
|
|
37
|
+
_commander.default.version(_package.default.version);
|
|
38
|
+
|
|
39
|
+
// Q: How to write the arguments description for the below usage?
|
|
40
|
+
// A: The usage description is written in the following way:
|
|
41
|
+
// - <format> your database format (postgres, mysql, mssql)
|
|
42
|
+
// - <connection-string> your database connection string
|
|
43
|
+
// - postgres: postgresql://user:password@localhost:5432/dbname
|
|
44
|
+
// - mssql: 'Server=localhost,1433;Database=master;User Id=sa;Password=your_password;Encrypt=true;TrustServerCertificate
|
|
45
|
+
const description = `
|
|
46
|
+
<format> your database format (postgres, mysql, mssql)
|
|
47
|
+
<connection-string> your database connection string:
|
|
48
|
+
- postgres: postgresql://user:password@localhost:5432/dbname
|
|
49
|
+
- mysql: mysql://user:password@localhost:3306/dbname
|
|
50
|
+
- mssql: 'Server=localhost,1433;Database=master;User Id=sa;Password=your_password;Encrypt=true;TrustServerCertificate=true;'
|
|
51
|
+
`;
|
|
52
|
+
_commander.default.usage('<format> <connection-string> [options]').description(description).option('-o, --out-file <pathspec>', 'compile all input files into a single files');
|
|
53
|
+
showHelp(args);
|
|
54
|
+
_commander.default.parse(args);
|
|
55
|
+
(0, _connector.default)(_commander.default);
|
|
28
56
|
}
|
package/lib/cli/utils.js
CHANGED
|
@@ -4,12 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.generate = generate;
|
|
7
|
+
exports.getConnectionOpt = getConnectionOpt;
|
|
7
8
|
exports.getFormatOpt = getFormatOpt;
|
|
8
9
|
exports.resolvePaths = resolvePaths;
|
|
9
10
|
exports.validateInputFilePaths = validateInputFilePaths;
|
|
10
11
|
var _path2 = _interopRequireDefault(require("path"));
|
|
11
12
|
var _fs = _interopRequireDefault(require("fs"));
|
|
12
13
|
var _core = require("@dbml/core");
|
|
14
|
+
var _lodash = require("lodash");
|
|
13
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
16
|
function resolvePaths(paths) {
|
|
15
17
|
if (!Array.isArray(paths)) {
|
|
@@ -35,6 +37,24 @@ function getFormatOpt(opts) {
|
|
|
35
37
|
});
|
|
36
38
|
return format;
|
|
37
39
|
}
|
|
40
|
+
function getConnectionOpt(args) {
|
|
41
|
+
const supportedDatabases = ['postgres', 'mysql', 'mssql'];
|
|
42
|
+
const defaultConnectionOpt = {
|
|
43
|
+
connection: args[0],
|
|
44
|
+
format: 'unknown'
|
|
45
|
+
};
|
|
46
|
+
return (0, _lodash.reduce)(args, (connectionOpt, arg) => {
|
|
47
|
+
if (supportedDatabases.includes(arg)) connectionOpt.format = arg;
|
|
48
|
+
// Check if the arg is a connection string using regex
|
|
49
|
+
const connectionStringRegex = /^.*[:;]/;
|
|
50
|
+
if (connectionStringRegex.test(arg)) {
|
|
51
|
+
// Example: jdbc:mysql://localhost:3306/mydatabase
|
|
52
|
+
// Example: odbc:Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
|
|
53
|
+
connectionOpt.connection = arg;
|
|
54
|
+
}
|
|
55
|
+
return connectionOpt;
|
|
56
|
+
}, defaultConnectionOpt);
|
|
57
|
+
}
|
|
38
58
|
function generate(inputPaths, transform, outputPlugin) {
|
|
39
59
|
inputPaths.forEach(_path => {
|
|
40
60
|
const source = _fs.default.readFileSync(_path, 'utf-8');
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.fetchSchemaJson = void 0;
|
|
7
|
+
var _PostgresConnector = require("./PostgresConnector");
|
|
8
|
+
var _MssqlConnector = require("./MssqlConnector");
|
|
9
|
+
const fetchSchemaJson = async (connection, format) => {
|
|
10
|
+
switch (format) {
|
|
11
|
+
case 'postgres':
|
|
12
|
+
return (0, _PostgresConnector.fetchSchemaJson)(connection);
|
|
13
|
+
case 'mssql':
|
|
14
|
+
return (0, _MssqlConnector.fetchSchemaJson)(connection);
|
|
15
|
+
default:
|
|
16
|
+
throw new Error(`Unsupported connection format: ${format}`);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
exports.fetchSchemaJson = fetchSchemaJson;
|