@dbml/connector 5.3.1 → 5.4.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.
@@ -1,13 +1,9 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
 
4
- const scanDirNames = (dirname: string, subpath: string): string[] => {
4
+ export function scanDirNames (dirname: string, subpath: string): string[] {
5
5
  const dirPath = path.join(dirname, subpath);
6
6
  const sanitizedDirPath = path.normalize(dirPath);
7
7
  const dirs = fs.readdirSync(sanitizedDirPath);
8
8
  return dirs;
9
- };
10
-
11
- export {
12
- scanDirNames,
13
- };
9
+ }
@@ -456,26 +456,31 @@ const generateIndexesAndConstraints = async (client, schemas) => {
456
456
  const fetchSchemaJson = async (connection) => {
457
457
  const { connectionString, schemas } = (0, parseSchema_1.parseConnectionString)(connection, 'odbc');
458
458
  const client = await getValidatedClient(connectionString);
459
- const tablesRes = generateTablesFields(client, schemas);
460
- const indexesAndConstraintsRes = generateIndexesAndConstraints(client, schemas);
461
- const refsRes = generateRefs(client, schemas);
462
- const res = await Promise.all([
463
- tablesRes,
464
- indexesAndConstraintsRes,
465
- refsRes,
466
- ]);
467
- const { tables, fields } = res[0];
468
- const { indexes, tableConstraints, checks } = res[1];
469
- const refs = res[2];
470
- return {
471
- tables,
472
- fields,
473
- // MSSQL doesn't support the notion of enums
474
- enums: [],
475
- refs,
476
- indexes,
477
- tableConstraints,
478
- checks,
479
- };
459
+ try {
460
+ const tablesRes = generateTablesFields(client, schemas);
461
+ const indexesAndConstraintsRes = generateIndexesAndConstraints(client, schemas);
462
+ const refsRes = generateRefs(client, schemas);
463
+ const res = await Promise.all([
464
+ tablesRes,
465
+ indexesAndConstraintsRes,
466
+ refsRes,
467
+ ]);
468
+ const { tables, fields } = res[0];
469
+ const { indexes, tableConstraints, checks } = res[1];
470
+ const refs = res[2];
471
+ return {
472
+ tables,
473
+ fields,
474
+ // MSSQL doesn't support the notion of enums
475
+ enums: [],
476
+ refs,
477
+ indexes,
478
+ tableConstraints,
479
+ checks,
480
+ };
481
+ }
482
+ finally {
483
+ await client.close();
484
+ }
480
485
  };
481
486
  exports.fetchSchemaJson = fetchSchemaJson;
@@ -416,35 +416,39 @@ async function fetchSchemaJson(connection) {
416
416
  if (!schemaName) {
417
417
  throw new Error('Cannot get schema name from the connection');
418
418
  }
419
- const result = await Promise.all([
420
- generateTablesAndFields(client, schemaName),
421
- generateEnums(client, schemaName),
422
- generateIndexes(client, schemaName),
423
- generatePrimaryAndUniqueConstraint(client, schemaName),
424
- generateForeignKeys(client, schemaName),
425
- generateCheckConstraints(client, schemaName),
426
- ]);
427
- client.end();
428
- const [{ tableList, fieldMap }, enumList, rawIndexMap, { primaryAndUniqueConstraintMap, compositeConstraintMap }, foreignKeyList, { checks: tableCheckConstraintMap, tableConstraints: columnCheckConstraintMap },] = result;
429
- // combine normal index and composite key
430
- const indexMap = combineIndexAndCompositeConstraint(rawIndexMap, compositeConstraintMap);
431
- const tableConstraints = { ...primaryAndUniqueConstraintMap };
432
- Object.keys(columnCheckConstraintMap).forEach((tableName) => {
433
- if (!tableConstraints[tableName])
434
- tableConstraints[tableName] = {};
435
- Object.keys(columnCheckConstraintMap[tableName]).forEach((colName) => {
436
- if (!tableConstraints[tableName][colName])
437
- tableConstraints[tableName][colName] = { checks: [] };
438
- tableConstraints[tableName][colName].checks.push(...columnCheckConstraintMap[tableName][colName].checks);
419
+ try {
420
+ const result = await Promise.all([
421
+ generateTablesAndFields(client, schemaName),
422
+ generateEnums(client, schemaName),
423
+ generateIndexes(client, schemaName),
424
+ generatePrimaryAndUniqueConstraint(client, schemaName),
425
+ generateForeignKeys(client, schemaName),
426
+ generateCheckConstraints(client, schemaName),
427
+ ]);
428
+ const [{ tableList, fieldMap }, enumList, rawIndexMap, { primaryAndUniqueConstraintMap, compositeConstraintMap }, foreignKeyList, { checks: tableCheckConstraintMap, tableConstraints: columnCheckConstraintMap },] = result;
429
+ // combine normal index and composite key
430
+ const indexMap = combineIndexAndCompositeConstraint(rawIndexMap, compositeConstraintMap);
431
+ const tableConstraints = { ...primaryAndUniqueConstraintMap };
432
+ Object.keys(columnCheckConstraintMap).forEach((tableName) => {
433
+ if (!tableConstraints[tableName])
434
+ tableConstraints[tableName] = {};
435
+ Object.keys(columnCheckConstraintMap[tableName]).forEach((colName) => {
436
+ if (!tableConstraints[tableName][colName])
437
+ tableConstraints[tableName][colName] = { checks: [] };
438
+ tableConstraints[tableName][colName].checks.push(...columnCheckConstraintMap[tableName][colName].checks);
439
+ });
439
440
  });
440
- });
441
- return {
442
- tables: tableList,
443
- fields: fieldMap,
444
- refs: foreignKeyList,
445
- enums: enumList,
446
- indexes: indexMap,
447
- tableConstraints,
448
- checks: tableCheckConstraintMap,
449
- };
441
+ return {
442
+ tables: tableList,
443
+ fields: fieldMap,
444
+ refs: foreignKeyList,
445
+ enums: enumList,
446
+ indexes: indexMap,
447
+ tableConstraints,
448
+ checks: tableCheckConstraintMap,
449
+ };
450
+ }
451
+ finally {
452
+ await client.end();
453
+ }
450
454
  }
@@ -485,29 +485,33 @@ const generateRawEnums = async (client, schemas) => {
485
485
  const fetchSchemaJson = async (connection) => {
486
486
  const { connectionString, schemas } = (0, parseSchema_1.parseConnectionString)(connection, 'jdbc');
487
487
  const client = await getValidatedClient(connectionString);
488
- const tablesAndFieldsRes = generateTablesAndFields(client, schemas);
489
- const indexesRes = generateIndexesAndConstraints(client, schemas);
490
- const refsRes = generateRawRefs(client, schemas);
491
- const enumsRes = generateRawEnums(client, schemas);
492
- const res = await Promise.all([
493
- tablesAndFieldsRes,
494
- indexesRes,
495
- refsRes,
496
- enumsRes,
497
- ]);
498
- client.end();
499
- const { tables, fields } = res[0];
500
- const { indexes, tableConstraints, checks } = res[1];
501
- const refs = res[2];
502
- const enums = res[3];
503
- return {
504
- tables,
505
- fields,
506
- refs,
507
- enums,
508
- indexes,
509
- tableConstraints,
510
- checks,
511
- };
488
+ try {
489
+ const tablesAndFieldsRes = generateTablesAndFields(client, schemas);
490
+ const indexesRes = generateIndexesAndConstraints(client, schemas);
491
+ const refsRes = generateRawRefs(client, schemas);
492
+ const enumsRes = generateRawEnums(client, schemas);
493
+ const res = await Promise.all([
494
+ tablesAndFieldsRes,
495
+ indexesRes,
496
+ refsRes,
497
+ enumsRes,
498
+ ]);
499
+ const { tables, fields } = res[0];
500
+ const { indexes, tableConstraints, checks } = res[1];
501
+ const refs = res[2];
502
+ const enums = res[3];
503
+ return {
504
+ tables,
505
+ fields,
506
+ refs,
507
+ enums,
508
+ indexes,
509
+ tableConstraints,
510
+ checks,
511
+ };
512
+ }
513
+ finally {
514
+ await client.end();
515
+ }
512
516
  };
513
517
  exports.fetchSchemaJson = fetchSchemaJson;
package/eslint.config.ts CHANGED
@@ -19,9 +19,9 @@ export default defineConfig(
19
19
  'node_modules/*',
20
20
  'dist/*',
21
21
  'eslint.config.ts',
22
- 'jestHelpers.ts',
23
22
  'jest.config.ts',
24
23
  '**/*.test.ts',
24
+ '__tests__/**/*',
25
25
  ],
26
26
  },
27
27
  {
package/jest.config.ts CHANGED
@@ -1,4 +1,7 @@
1
- module.exports = {
1
+ import { type Config } from 'jest';
2
+
3
+ const config: Config = {
4
+ testMatch: ["**/?(*.)+(spec|test).?([mc])[jt]s?(x)"],
2
5
  preset: 'ts-jest',
3
6
  testEnvironment: 'node',
4
7
  transform: {
@@ -7,4 +10,9 @@ module.exports = {
7
10
  moduleNameMapper: {
8
11
  '\\.(css|less|scss)$': 'identity-obj-proxy',
9
12
  },
13
+ collectCoverage: true,
14
+ coverageReporters: ['json', 'json-summary', 'html', 'text'],
15
+ coverageDirectory: 'coverage',
10
16
  };
17
+
18
+ export default config;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@dbml/connector",
4
- "version": "5.3.1",
4
+ "version": "5.4.1",
5
5
  "description": "This package was created to fetch the schema JSON from many kind of databases.",
6
6
  "author": "huy.phung.sw@gmail.com",
7
7
  "license": "MIT",
@@ -36,7 +36,8 @@
36
36
  "scripts": {
37
37
  "build": "tsc",
38
38
  "start": "node dist/index.js",
39
- "test": "jest",
39
+ "test": "jest --coverage=false",
40
+ "coverage": "jest --coverage",
40
41
  "lint": "eslint .",
41
42
  "lint:fix": "eslint --fix ."
42
43
  },
@@ -55,5 +56,5 @@
55
56
  "engines": {
56
57
  "node": ">=18"
57
58
  },
58
- "gitHead": "54ca4861d608adbfc17c86d840c2e663e9c428f9"
59
+ "gitHead": "f5c8bf0c9d95948e3874cfb6cc777f65c4180891"
59
60
  }
@@ -553,30 +553,34 @@ const fetchSchemaJson = async (connection: string): Promise<DatabaseSchema> => {
553
553
  const { connectionString, schemas } = parseConnectionString(connection, 'odbc');
554
554
  const client = await getValidatedClient(connectionString);
555
555
 
556
- const tablesRes = generateTablesFields(client, schemas);
557
- const indexesAndConstraintsRes = generateIndexesAndConstraints(client, schemas);
558
- const refsRes = generateRefs(client, schemas);
559
-
560
- const res = await Promise.all([
561
- tablesRes,
562
- indexesAndConstraintsRes,
563
- refsRes,
564
- ]);
565
-
566
- const { tables, fields } = res[0];
567
- const { indexes, tableConstraints, checks } = res[1];
568
- const refs = res[2];
569
-
570
- return {
571
- tables,
572
- fields,
573
- // MSSQL doesn't support the notion of enums
574
- enums: [],
575
- refs,
576
- indexes,
577
- tableConstraints,
578
- checks,
579
- };
556
+ try {
557
+ const tablesRes = generateTablesFields(client, schemas);
558
+ const indexesAndConstraintsRes = generateIndexesAndConstraints(client, schemas);
559
+ const refsRes = generateRefs(client, schemas);
560
+
561
+ const res = await Promise.all([
562
+ tablesRes,
563
+ indexesAndConstraintsRes,
564
+ refsRes,
565
+ ]);
566
+
567
+ const { tables, fields } = res[0];
568
+ const { indexes, tableConstraints, checks } = res[1];
569
+ const refs = res[2];
570
+
571
+ return {
572
+ tables,
573
+ fields,
574
+ // MSSQL doesn't support the notion of enums
575
+ enums: [],
576
+ refs,
577
+ indexes,
578
+ tableConstraints,
579
+ checks,
580
+ };
581
+ } finally {
582
+ await client.close();
583
+ }
580
584
  };
581
585
 
582
586
  export {
@@ -531,46 +531,49 @@ async function fetchSchemaJson (connection: string): Promise<DatabaseSchema> {
531
531
  throw new Error('Cannot get schema name from the connection');
532
532
  }
533
533
 
534
- const result = await Promise.all([
535
- generateTablesAndFields(client, schemaName),
536
- generateEnums(client, schemaName),
537
- generateIndexes(client, schemaName),
538
- generatePrimaryAndUniqueConstraint(client, schemaName),
539
- generateForeignKeys(client, schemaName),
540
- generateCheckConstraints(client, schemaName),
541
- ]);
542
- client.end();
543
-
544
- const [
545
- { tableList, fieldMap },
546
- enumList,
547
- rawIndexMap,
548
- { primaryAndUniqueConstraintMap, compositeConstraintMap },
549
- foreignKeyList,
550
- { checks: tableCheckConstraintMap, tableConstraints: columnCheckConstraintMap },
551
- ] = result;
552
-
553
- // combine normal index and composite key
554
- const indexMap = combineIndexAndCompositeConstraint(rawIndexMap, compositeConstraintMap);
555
-
556
- const tableConstraints: TableConstraintsDictionary = { ...primaryAndUniqueConstraintMap };
557
- Object.keys(columnCheckConstraintMap).forEach((tableName) => {
558
- if (!tableConstraints[tableName]) tableConstraints[tableName] = {};
559
- Object.keys(columnCheckConstraintMap[tableName]).forEach((colName) => {
560
- if (!tableConstraints[tableName][colName]) tableConstraints[tableName][colName] = { checks: [] };
561
- tableConstraints[tableName][colName].checks.push(...columnCheckConstraintMap[tableName][colName].checks);
534
+ try {
535
+ const result = await Promise.all([
536
+ generateTablesAndFields(client, schemaName),
537
+ generateEnums(client, schemaName),
538
+ generateIndexes(client, schemaName),
539
+ generatePrimaryAndUniqueConstraint(client, schemaName),
540
+ generateForeignKeys(client, schemaName),
541
+ generateCheckConstraints(client, schemaName),
542
+ ]);
543
+
544
+ const [
545
+ { tableList, fieldMap },
546
+ enumList,
547
+ rawIndexMap,
548
+ { primaryAndUniqueConstraintMap, compositeConstraintMap },
549
+ foreignKeyList,
550
+ { checks: tableCheckConstraintMap, tableConstraints: columnCheckConstraintMap },
551
+ ] = result;
552
+
553
+ // combine normal index and composite key
554
+ const indexMap = combineIndexAndCompositeConstraint(rawIndexMap, compositeConstraintMap);
555
+
556
+ const tableConstraints: TableConstraintsDictionary = { ...primaryAndUniqueConstraintMap };
557
+ Object.keys(columnCheckConstraintMap).forEach((tableName) => {
558
+ if (!tableConstraints[tableName]) tableConstraints[tableName] = {};
559
+ Object.keys(columnCheckConstraintMap[tableName]).forEach((colName) => {
560
+ if (!tableConstraints[tableName][colName]) tableConstraints[tableName][colName] = { checks: [] };
561
+ tableConstraints[tableName][colName].checks.push(...columnCheckConstraintMap[tableName][colName].checks);
562
+ });
562
563
  });
563
- });
564
564
 
565
- return {
566
- tables: tableList,
567
- fields: fieldMap,
568
- refs: foreignKeyList,
569
- enums: enumList,
570
- indexes: indexMap,
571
- tableConstraints,
572
- checks: tableCheckConstraintMap,
573
- };
565
+ return {
566
+ tables: tableList,
567
+ fields: fieldMap,
568
+ refs: foreignKeyList,
569
+ enums: enumList,
570
+ indexes: indexMap,
571
+ tableConstraints,
572
+ checks: tableCheckConstraintMap,
573
+ };
574
+ } finally {
575
+ await client.end();
576
+ }
574
577
  }
575
578
 
576
579
  export {
@@ -576,33 +576,36 @@ const fetchSchemaJson = async (connection: string): Promise<DatabaseSchema> => {
576
576
  const { connectionString, schemas } = parseConnectionString(connection, 'jdbc');
577
577
  const client = await getValidatedClient(connectionString);
578
578
 
579
- const tablesAndFieldsRes = generateTablesAndFields(client, schemas);
580
- const indexesRes = generateIndexesAndConstraints(client, schemas);
581
- const refsRes = generateRawRefs(client, schemas);
582
- const enumsRes = generateRawEnums(client, schemas);
583
-
584
- const res = await Promise.all([
585
- tablesAndFieldsRes,
586
- indexesRes,
587
- refsRes,
588
- enumsRes,
589
- ]);
590
- client.end();
591
-
592
- const { tables, fields } = res[0];
593
- const { indexes, tableConstraints, checks } = res[1];
594
- const refs = res[2];
595
- const enums = res[3];
579
+ try {
580
+ const tablesAndFieldsRes = generateTablesAndFields(client, schemas);
581
+ const indexesRes = generateIndexesAndConstraints(client, schemas);
582
+ const refsRes = generateRawRefs(client, schemas);
583
+ const enumsRes = generateRawEnums(client, schemas);
584
+
585
+ const res = await Promise.all([
586
+ tablesAndFieldsRes,
587
+ indexesRes,
588
+ refsRes,
589
+ enumsRes,
590
+ ]);
591
+
592
+ const { tables, fields } = res[0];
593
+ const { indexes, tableConstraints, checks } = res[1];
594
+ const refs = res[2];
595
+ const enums = res[3];
596
596
 
597
- return {
598
- tables,
599
- fields,
600
- refs,
601
- enums,
602
- indexes,
603
- tableConstraints,
604
- checks,
605
- };
597
+ return {
598
+ tables,
599
+ fields,
600
+ refs,
601
+ enums,
602
+ indexes,
603
+ tableConstraints,
604
+ checks,
605
+ };
606
+ } finally {
607
+ await client.end();
608
+ }
606
609
  };
607
610
 
608
611
  export {