@dbml/connector 3.9.0 → 3.9.1

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.
@@ -500,7 +500,7 @@
500
500
  "not_null": false,
501
501
  "increment": false,
502
502
  "note": {
503
- "value": "first name of the individual"
503
+ "value": "first name's of the individual"
504
504
  }
505
505
  },
506
506
  {
@@ -513,7 +513,7 @@
513
513
  "not_null": false,
514
514
  "increment": false,
515
515
  "note": {
516
- "value": "last name of the individual"
516
+ "value": "last name's of the individual"
517
517
  }
518
518
  }
519
519
  ],
@@ -81,7 +81,7 @@
81
81
  "name": "table_with_comments",
82
82
  "schemaName": "public",
83
83
  "note": {
84
- "value": "This table stores information about various items."
84
+ "value": "This table stores information about various items. Such as: 'id', 'name', 'description'"
85
85
  }
86
86
  },
87
87
  {
@@ -1007,7 +1007,7 @@
1007
1007
  "not_null": false,
1008
1008
  "increment": false,
1009
1009
  "note": {
1010
- "value": "Name of the item."
1010
+ "value": "Item's name."
1011
1011
  }
1012
1012
  },
1013
1013
  {
@@ -1020,7 +1020,7 @@
1020
1020
  "not_null": false,
1021
1021
  "increment": false,
1022
1022
  "note": {
1023
- "value": "Detailed description of the item."
1023
+ "value": "Item's description"
1024
1024
  }
1025
1025
  },
1026
1026
  {
@@ -1,5 +1,19 @@
1
1
  {
2
2
  "tables": [
3
+ {
4
+ "name": "table1",
5
+ "schemaName": "dbml_test",
6
+ "note": {
7
+ "value": ""
8
+ }
9
+ },
10
+ {
11
+ "name": "table3",
12
+ "schemaName": "dbml_test",
13
+ "note": {
14
+ "value": ""
15
+ }
16
+ },
3
17
  {
4
18
  "name": "all_default_values",
5
19
  "schemaName": "public",
@@ -49,11 +63,25 @@
49
63
  "value": ""
50
64
  }
51
65
  },
66
+ {
67
+ "name": "table1",
68
+ "schemaName": "public",
69
+ "note": {
70
+ "value": ""
71
+ }
72
+ },
73
+ {
74
+ "name": "table2",
75
+ "schemaName": "public",
76
+ "note": {
77
+ "value": ""
78
+ }
79
+ },
52
80
  {
53
81
  "name": "table_with_comments",
54
82
  "schemaName": "public",
55
83
  "note": {
56
- "value": "This table stores information about various items."
84
+ "value": "This table stores information about various items. Such as: 'id', 'name', 'description'"
57
85
  }
58
86
  },
59
87
  {
@@ -69,9 +97,72 @@
69
97
  "note": {
70
98
  "value": ""
71
99
  }
100
+ },
101
+ {
102
+ "name": "employee",
103
+ "schemaName": "test",
104
+ "note": {
105
+ "value": ""
106
+ }
72
107
  }
73
108
  ],
74
109
  "fields": {
110
+ "dbml_test.table1": [
111
+ {
112
+ "name": "id",
113
+ "type": {
114
+ "type_name": "int4",
115
+ "schemaName": null
116
+ },
117
+ "dbdefault": null,
118
+ "not_null": true,
119
+ "increment": true,
120
+ "note": {
121
+ "value": ""
122
+ }
123
+ },
124
+ {
125
+ "name": "status",
126
+ "type": {
127
+ "type_name": "enum_type1",
128
+ "schemaName": "dbml_test"
129
+ },
130
+ "dbdefault": null,
131
+ "not_null": false,
132
+ "increment": false,
133
+ "note": {
134
+ "value": ""
135
+ }
136
+ }
137
+ ],
138
+ "dbml_test.table3": [
139
+ {
140
+ "name": "id",
141
+ "type": {
142
+ "type_name": "int4",
143
+ "schemaName": null
144
+ },
145
+ "dbdefault": null,
146
+ "not_null": true,
147
+ "increment": true,
148
+ "note": {
149
+ "value": ""
150
+ }
151
+ },
152
+ {
153
+ "name": "status",
154
+ "type": {
155
+ "type_name": "enum_type3",
156
+ "schemaName": "dbml_test"
157
+ },
158
+ "dbdefault": null,
159
+ "not_null": false,
160
+ "increment": false,
161
+ "note": {
162
+ "value": ""
163
+ }
164
+ }
165
+ ],
75
166
  "public.all_default_values": [
76
167
  {
77
168
  "name": "id",
@@ -843,6 +934,62 @@
843
934
  }
844
935
  }
845
936
  ],
937
+ "public.table1": [
938
+ {
939
+ "name": "id",
940
+ "type": {
941
+ "type_name": "int4",
942
+ "schemaName": null
943
+ },
944
+ "dbdefault": null,
945
+ "not_null": true,
946
+ "increment": true,
947
+ "note": {
948
+ "value": ""
949
+ }
950
+ },
951
+ {
952
+ "name": "status",
953
+ "type": {
954
+ "type_name": "enum_type1",
955
+ "schemaName": "public"
956
+ },
957
+ "dbdefault": null,
958
+ "not_null": false,
959
+ "increment": false,
960
+ "note": {
961
+ "value": ""
962
+ }
963
+ }
964
+ ],
965
+ "public.table2": [
966
+ {
967
+ "name": "id",
968
+ "type": {
969
+ "type_name": "int4",
970
+ "schemaName": null
971
+ },
972
+ "dbdefault": null,
973
+ "not_null": true,
974
+ "increment": true,
975
+ "note": {
976
+ "value": ""
977
+ }
978
+ },
979
+ {
980
+ "name": "status",
981
+ "type": {
982
+ "type_name": "enum_type2",
983
+ "schemaName": "public"
984
+ },
985
+ "dbdefault": null,
986
+ "not_null": false,
987
+ "increment": false,
988
+ "note": {
989
+ "value": ""
990
+ }
991
+ }
992
+ ],
846
993
  "public.table_with_comments": [
847
994
  {
848
995
  "name": "id",
@@ -867,7 +1014,7 @@
867
1014
  "not_null": false,
868
1015
  "increment": false,
869
1016
  "note": {
870
- "value": "Name of the item."
1017
+ "value": "Item's name."
871
1018
  }
872
1019
  },
873
1020
  {
@@ -880,7 +1027,7 @@
880
1027
  "not_null": false,
881
1028
  "increment": false,
882
1029
  "note": {
883
- "value": "Detailed description of the item."
1030
+ "value": "Item's description"
884
1031
  }
885
1032
  },
886
1033
  {
@@ -1130,6 +1277,34 @@
1130
1277
  "value": ""
1131
1278
  }
1132
1279
  }
1280
+ ],
1281
+ "test.employee": [
1282
+ {
1283
+ "name": "employeeid",
1284
+ "type": {
1285
+ "type_name": "int8",
1286
+ "schemaName": null
1287
+ },
1288
+ "dbdefault": null,
1289
+ "not_null": true,
1290
+ "increment": false,
1291
+ "note": {
1292
+ "value": ""
1293
+ }
1294
+ },
1295
+ {
1296
+ "name": "identifier",
1297
+ "type": {
1298
+ "type_name": "citext",
1299
+ "schemaName": "test"
1300
+ },
1301
+ "dbdefault": null,
1302
+ "not_null": false,
1303
+ "increment": false,
1304
+ "note": {
1305
+ "value": ""
1306
+ }
1307
+ }
1133
1308
  ]
1134
1309
  },
1135
1310
  "refs": [
@@ -1229,6 +1404,54 @@
1229
1404
  }
1230
1405
  ],
1231
1406
  "enums": [
1407
+ {
1408
+ "name": "enum_type1",
1409
+ "schemaName": "dbml_test",
1410
+ "values": [
1411
+ {
1412
+ "name": "value1"
1413
+ },
1414
+ {
1415
+ "name": "value2"
1416
+ }
1417
+ ]
1418
+ },
1419
+ {
1420
+ "name": "enum_type3",
1421
+ "schemaName": "dbml_test",
1422
+ "values": [
1423
+ {
1424
+ "name": "value4"
1425
+ },
1426
+ {
1427
+ "name": "value5"
1428
+ }
1429
+ ]
1430
+ },
1431
+ {
1432
+ "name": "enum_type1",
1433
+ "schemaName": "public",
1434
+ "values": [
1435
+ {
1436
+ "name": "value1"
1437
+ },
1438
+ {
1439
+ "name": "value2"
1440
+ }
1441
+ ]
1442
+ },
1443
+ {
1444
+ "name": "enum_type2",
1445
+ "schemaName": "public",
1446
+ "values": [
1447
+ {
1448
+ "name": "value1"
1449
+ },
1450
+ {
1451
+ "name": "value3"
1452
+ }
1453
+ ]
1454
+ },
1232
1455
  {
1233
1456
  "name": "gender_type",
1234
1457
  "schemaName": "public",
@@ -1386,6 +1609,26 @@
1386
1609
  "pk": true
1387
1610
  }
1388
1611
  },
1612
+ "public.table1": {
1613
+ "id": {
1614
+ "pk": true
1615
+ }
1616
+ },
1617
+ "dbml_test.table1": {
1618
+ "id": {
1619
+ "pk": true
1620
+ }
1621
+ },
1622
+ "public.table2": {
1623
+ "id": {
1624
+ "pk": true
1625
+ }
1626
+ },
1627
+ "dbml_test.table3": {
1628
+ "id": {
1629
+ "pk": true
1630
+ }
1631
+ },
1389
1632
  "public.table_with_comments": {
1390
1633
  "id": {
1391
1634
  "pk": true
@@ -7,6 +7,7 @@ exports.fetchSchemaJson = void 0;
7
7
  /* eslint-disable camelcase */
8
8
  const mssql_1 = __importDefault(require("mssql"));
9
9
  const parseSchema_1 = require("../utils/parseSchema");
10
+ // https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-and-time-types?view=sql-server-ver15
10
11
  const MSSQL_DATE_TYPES = [
11
12
  'date',
12
13
  'datetime',
@@ -36,18 +37,43 @@ const getValidatedClient = async (connection) => {
36
37
  }
37
38
  };
38
39
  const convertQueryBoolean = (val) => val === 'YES';
39
- const getFieldType = (data_type, default_type, character_maximum_length, numeric_precision, numeric_scale) => {
40
+ const getFieldType = (data_type, character_maximum_length, numeric_precision, numeric_scale) => {
40
41
  if (MSSQL_DATE_TYPES.includes(data_type)) {
41
42
  return data_type;
42
43
  }
44
+ // timestamp is a synonym of rowversion and we cannot specify the precision for it
45
+ // https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?view=sql-server-ver15
46
+ if (data_type === 'timestamp') {
47
+ return data_type;
48
+ }
49
+ // process numeric-based type
43
50
  if (data_type === 'bit') {
44
51
  return data_type;
45
52
  }
46
- if (numeric_precision && numeric_scale && default_type === 'number') {
47
- return `${data_type}(${numeric_precision},${numeric_scale})`;
53
+ // if precision != 0 => numeric-based column
54
+ if (numeric_precision) {
55
+ return numeric_scale
56
+ ? `${data_type}(${numeric_precision},${numeric_scale})`
57
+ : `${data_type}(${numeric_precision})`;
58
+ }
59
+ // process string-based type
60
+ // ntext, text & image
61
+ if (['ntext', 'text', 'image'].includes(data_type)) {
62
+ return data_type;
63
+ }
64
+ // Column data type is varchar(max), nvarchar(max), varbinary(max), or xml
65
+ if (character_maximum_length < 0) {
66
+ return data_type === 'xml' ? data_type : `${data_type}(MAX)`;
48
67
  }
49
- if (character_maximum_length && character_maximum_length > 0 && default_type === 'string') {
50
- return `${data_type}(${character_maximum_length})`;
68
+ // character_maximum_length is the lenght in bytes
69
+ // nchar and nvarchar store Unicode characters, each character needs 2 bytes
70
+ // so we have to divide it by 2 to get the correct maximum lenght in character.
71
+ // ref: https://learn.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql?view=sql-server-ver15
72
+ if (character_maximum_length > 0) {
73
+ const maximum_length_in_character = (data_type === 'nchar' || data_type === 'nvarchar')
74
+ ? character_maximum_length / 2
75
+ : character_maximum_length;
76
+ return `${data_type}(${maximum_length_in_character})`;
51
77
  }
52
78
  return data_type;
53
79
  };
@@ -90,7 +116,7 @@ const generateField = (row) => {
90
116
  const { column_name, data_type, character_maximum_length, numeric_precision, numeric_scale, identity_increment, is_nullable, column_default, default_type, column_comment, } = row;
91
117
  const dbdefault = column_default && default_type !== 'increment' ? getDbdefault(data_type, column_default, default_type) : null;
92
118
  const fieldType = {
93
- type_name: getFieldType(data_type, default_type, character_maximum_length, numeric_precision, numeric_scale),
119
+ type_name: getFieldType(data_type, character_maximum_length, numeric_precision, numeric_scale),
94
120
  schemaName: null,
95
121
  };
96
122
  return {
@@ -194,7 +220,7 @@ const generateTablesFieldsAndEnums = async (client, schemas) => {
194
220
  constraints_with_row_number
195
221
  WHERE
196
222
  rn = 1
197
- ${(0, parseSchema_1.buildSchemaQuery)('table_schema', schemas, 'WHERE')}
223
+ ${(0, parseSchema_1.buildSchemaQuery)('table_schema', schemas)}
198
224
  ORDER BY
199
225
  table_schema,
200
226
  table_name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/connector",
3
- "version": "3.9.0",
3
+ "version": "3.9.1",
4
4
  "description": "This package was created to fetch the schema JSON from many kind of databases.",
5
5
  "author": "huy.phung.sw@gmail.com",
6
6
  "license": "MIT",
@@ -40,5 +40,5 @@
40
40
  "engines": {
41
41
  "node": ">=18"
42
42
  },
43
- "gitHead": "d40aaa3827853d17fa385aa27ae1e099c9234e4b"
43
+ "gitHead": "8494c278b97347945cf3b71d6f75cd658dbae85d"
44
44
  }
@@ -16,6 +16,7 @@ import {
16
16
  TableConstraintsDictionary,
17
17
  } from './types';
18
18
 
19
+ // https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-and-time-types?view=sql-server-ver15
19
20
  const MSSQL_DATE_TYPES = [
20
21
  'date',
21
22
  'datetime',
@@ -49,19 +50,51 @@ const getValidatedClient = async (connection: string): Promise<sql.ConnectionPoo
49
50
 
50
51
  const convertQueryBoolean = (val: string | null): boolean => val === 'YES';
51
52
 
52
- const getFieldType = (data_type: string, default_type: DefaultType, character_maximum_length: number, numeric_precision: number, numeric_scale: number): string => {
53
+ const getFieldType = (data_type: string, character_maximum_length: number, numeric_precision: number, numeric_scale: number): string => {
53
54
  if (MSSQL_DATE_TYPES.includes(data_type)) {
54
55
  return data_type;
55
56
  }
57
+
58
+ // timestamp is a synonym of rowversion and we cannot specify the precision for it
59
+ // https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?view=sql-server-ver15
60
+ if (data_type === 'timestamp') {
61
+ return data_type;
62
+ }
63
+
64
+ // process numeric-based type
56
65
  if (data_type === 'bit') {
57
66
  return data_type;
58
67
  }
59
- if (numeric_precision && numeric_scale && default_type === 'number') {
60
- return `${data_type}(${numeric_precision},${numeric_scale})`;
68
+ // if precision != 0 => numeric-based column
69
+ if (numeric_precision) {
70
+ return numeric_scale
71
+ ? `${data_type}(${numeric_precision},${numeric_scale})`
72
+ : `${data_type}(${numeric_precision})`;
73
+ }
74
+
75
+ // process string-based type
76
+ // ntext, text & image
77
+ if (['ntext', 'text', 'image'].includes(data_type)) {
78
+ return data_type;
79
+ }
80
+
81
+ // Column data type is varchar(max), nvarchar(max), varbinary(max), or xml
82
+ if (character_maximum_length < 0) {
83
+ return data_type === 'xml' ? data_type : `${data_type}(MAX)`;
61
84
  }
62
- if (character_maximum_length && character_maximum_length > 0 && default_type === 'string') {
63
- return `${data_type}(${character_maximum_length})`;
85
+
86
+ // character_maximum_length is the lenght in bytes
87
+ // nchar and nvarchar store Unicode characters, each character needs 2 bytes
88
+ // so we have to divide it by 2 to get the correct maximum lenght in character.
89
+ // ref: https://learn.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql?view=sql-server-ver15
90
+ if (character_maximum_length > 0) {
91
+ const maximum_length_in_character = (data_type === 'nchar' || data_type === 'nvarchar')
92
+ ? character_maximum_length / 2
93
+ : character_maximum_length;
94
+
95
+ return `${data_type}(${maximum_length_in_character})`;
64
96
  }
97
+
65
98
  return data_type;
66
99
  };
67
100
 
@@ -119,7 +152,7 @@ const generateField = (row: Record<string, any>): Field => {
119
152
  const dbdefault = column_default && default_type !== 'increment' ? getDbdefault(data_type, column_default, default_type) : null;
120
153
 
121
154
  const fieldType = {
122
- type_name: getFieldType(data_type, default_type, character_maximum_length, numeric_precision, numeric_scale),
155
+ type_name: getFieldType(data_type, character_maximum_length, numeric_precision, numeric_scale),
123
156
  schemaName: null,
124
157
  };
125
158
 
@@ -229,7 +262,7 @@ const generateTablesFieldsAndEnums = async (client: sql.ConnectionPool, schemas:
229
262
  constraints_with_row_number
230
263
  WHERE
231
264
  rn = 1
232
- ${buildSchemaQuery('table_schema', schemas, 'WHERE')}
265
+ ${buildSchemaQuery('table_schema', schemas)}
233
266
  ORDER BY
234
267
  table_schema,
235
268
  table_name,