@dbml/connector 3.9.0 → 3.9.2-alpha.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.
Files changed (52) hide show
  1. package/.babelrc +0 -0
  2. package/LICENSE +0 -0
  3. package/README.md +0 -0
  4. package/__tests__/connector.test.ts +0 -0
  5. package/__tests__/connectors/mssql/connection.json +0 -0
  6. package/__tests__/connectors/mssql/expect-out-files/schema.json +203 -59
  7. package/__tests__/connectors/mysql/connection.json +0 -0
  8. package/__tests__/connectors/mysql/expect-out-files/schema.json +2 -2
  9. package/__tests__/connectors/postgres/connection.json +0 -0
  10. package/__tests__/connectors/postgres/expect-out-files/schema.json +3 -3
  11. package/dist/connectors/bigqueryConnector.d.ts +0 -0
  12. package/dist/connectors/bigqueryConnector.js +0 -0
  13. package/dist/connectors/connector.d.ts +0 -0
  14. package/dist/connectors/connector.js +0 -0
  15. package/dist/connectors/mssqlConnector.d.ts +0 -0
  16. package/dist/connectors/mssqlConnector.js +33 -7
  17. package/dist/connectors/mysqlConnector.d.ts +0 -0
  18. package/dist/connectors/mysqlConnector.js +0 -0
  19. package/dist/connectors/postgresConnector.d.ts +0 -0
  20. package/dist/connectors/postgresConnector.js +0 -0
  21. package/dist/connectors/snowflakeConnector.d.ts +0 -0
  22. package/dist/connectors/snowflakeConnector.js +0 -0
  23. package/dist/connectors/types.d.ts +0 -0
  24. package/dist/connectors/types.js +0 -0
  25. package/dist/index.d.ts +0 -0
  26. package/dist/index.js +0 -0
  27. package/dist/utils/credential-loader.d.ts +0 -0
  28. package/dist/utils/credential-loader.js +0 -0
  29. package/dist/utils/helpers.d.ts +0 -0
  30. package/dist/utils/helpers.js +0 -0
  31. package/dist/utils/parseSchema.d.ts +0 -0
  32. package/dist/utils/parseSchema.js +0 -0
  33. package/jest.config.ts +0 -0
  34. package/jestHelpers.ts +0 -0
  35. package/package.json +2 -2
  36. package/src/connectors/bigqueryConnector.ts +0 -0
  37. package/src/connectors/connector.ts +0 -0
  38. package/src/connectors/mssqlConnector.ts +40 -7
  39. package/src/connectors/mysqlConnector.ts +0 -0
  40. package/src/connectors/postgresConnector.ts +0 -0
  41. package/src/connectors/snowflakeConnector.ts +0 -0
  42. package/src/connectors/types.ts +0 -0
  43. package/src/ddl_samples/bigquery.sql +0 -0
  44. package/src/ddl_samples/snowflake.sql +0 -0
  45. package/src/index.ts +0 -0
  46. package/src/utils/credential-loader.ts +0 -0
  47. package/src/utils/helpers.ts +0 -0
  48. package/src/utils/parseSchema.ts +0 -0
  49. package/tsconfig.json +0 -0
  50. package/__tests__/connectors/bigquery/out-files/schema.json +0 -565
  51. package/__tests__/connectors/mysql/out-files/schema.json +0 -1390
  52. package/__tests__/connectors/postgres/out-files/schema.json +0 -1411
@@ -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
  {
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -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,
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/dist/index.d.ts CHANGED
File without changes
package/dist/index.js CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/jest.config.ts CHANGED
File without changes
package/jestHelpers.ts CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/connector",
3
- "version": "3.9.0",
3
+ "version": "3.9.2-alpha.0",
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": "269532f01f0475328ef08c72ec2ca901a91b0846"
44
44
  }
File without changes
File without changes
@@ -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,
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/src/index.ts CHANGED
File without changes
File without changes
File without changes
File without changes
package/tsconfig.json CHANGED
File without changes