dbgate-tools 4.5.0 → 4.6.2

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.
@@ -21,6 +21,7 @@ export declare class DatabaseAnalyser {
21
21
  getDeletedObjectsForField(snapshot: any, objectTypeField: any): any;
22
22
  getDeletedObjects(snapshot: any): any[];
23
23
  getModifications(): Promise<any[]>;
24
+ safeQuery(sql: any): Promise<import("dbgate-types").QueryResult>;
24
25
  static createEmptyStructure(): DatabaseInfo;
25
26
  static byTableFilter(table: any): (x: any) => boolean;
26
27
  static extractPrimaryKeys(table: any, pkColumns: any): {
@@ -231,6 +231,19 @@ class DatabaseAnalyser {
231
231
  return [...(0, compact_1.default)(res), ...this.getDeletedObjects(snapshot)];
232
232
  });
233
233
  }
234
+ safeQuery(sql) {
235
+ return __awaiter(this, void 0, void 0, function* () {
236
+ try {
237
+ return yield this.driver.query(this.pool, sql);
238
+ }
239
+ catch (err) {
240
+ console.log('Error running analyser query', err.message);
241
+ return {
242
+ rows: [],
243
+ };
244
+ }
245
+ });
246
+ }
234
247
  static createEmptyStructure() {
235
248
  return {
236
249
  tables: [],
@@ -9,6 +9,7 @@ export declare class SqlDumper implements AlterProcessor {
9
9
  putRaw(text: any): void;
10
10
  escapeString(value: any): string;
11
11
  putStringValue(value: any): void;
12
+ putByteArrayValue(value: any): void;
12
13
  putValue(value: any): void;
13
14
  putCmd(format: any, ...args: any[]): void;
14
15
  putFormattedValue(c: any, value: any): void;
package/lib/SqlDumper.js CHANGED
@@ -8,6 +8,8 @@ const lodash_1 = __importDefault(require("lodash"));
8
8
  const isString_1 = __importDefault(require("lodash/isString"));
9
9
  const isNumber_1 = __importDefault(require("lodash/isNumber"));
10
10
  const isDate_1 = __importDefault(require("lodash/isDate"));
11
+ const isArray_1 = __importDefault(require("lodash/isArray"));
12
+ const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
11
13
  const v1_1 = __importDefault(require("uuid/v1"));
12
14
  class SqlDumper {
13
15
  constructor(driver) {
@@ -39,6 +41,9 @@ class SqlDumper {
39
41
  this.putRaw(this.escapeString(value));
40
42
  this.putRaw("'");
41
43
  }
44
+ putByteArrayValue(value) {
45
+ this.putRaw('NULL');
46
+ }
42
47
  putValue(value) {
43
48
  if (value === null)
44
49
  this.putRaw('NULL');
@@ -52,6 +57,10 @@ class SqlDumper {
52
57
  this.putRaw(value.toString());
53
58
  else if ((0, isDate_1.default)(value))
54
59
  this.putStringValue(new Date(value).toISOString());
60
+ else if ((value === null || value === void 0 ? void 0 : value.type) == 'Buffer' && (0, isArray_1.default)(value === null || value === void 0 ? void 0 : value.data))
61
+ this.putByteArrayValue(value === null || value === void 0 ? void 0 : value.data);
62
+ else if ((0, isPlainObject_1.default)(value) || (0, isArray_1.default)(value))
63
+ this.putStringValue(JSON.stringify(value));
55
64
  else
56
65
  this.putRaw('NULL');
57
66
  }
@@ -39,18 +39,18 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
39
39
  // console.log('ANALYSING', name, structure);
40
40
  if (structure && options.dropIfExists) {
41
41
  console.log(`Dropping table ${fullNameQuoted}`);
42
- yield driver.query(pool, `DROP TABLE ${fullNameQuoted}`);
42
+ yield driver.script(pool, `DROP TABLE ${fullNameQuoted}`);
43
43
  }
44
44
  if (options.createIfNotExists && (!structure || options.dropIfExists)) {
45
45
  console.log(`Creating table ${fullNameQuoted}`);
46
46
  const dmp = driver.createDumper();
47
47
  dmp.createTable((0, tableTransforms_1.prepareTableForImport)(Object.assign(Object.assign({}, writable.structure), name)));
48
48
  console.log(dmp.s);
49
- yield driver.query(pool, dmp.s);
49
+ yield driver.script(pool, dmp.s);
50
50
  structure = yield driver.analyseSingleTable(pool, name);
51
51
  }
52
52
  if (options.truncate) {
53
- yield driver.query(pool, `TRUNCATE TABLE ${fullNameQuoted}`);
53
+ yield driver.script(pool, `TRUNCATE TABLE ${fullNameQuoted}`);
54
54
  }
55
55
  writable.columnNames = (0, intersection_1.default)(structure.columns.map(x => x.columnName), writable.structure.columns.map(x => x.columnName));
56
56
  });
@@ -1,2 +1,4 @@
1
1
  export declare function arrayToHexString(byteArray: any): any;
2
2
  export declare function hexStringToArray(inputString: any): any[];
3
+ export declare function parseCellValue(value: any): any;
4
+ export declare function stringifyCellValue(value: any): any;
@@ -1,8 +1,14 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hexStringToArray = exports.arrayToHexString = void 0;
6
+ exports.stringifyCellValue = exports.parseCellValue = exports.hexStringToArray = exports.arrayToHexString = void 0;
7
+ const isString_1 = __importDefault(require("lodash/isString"));
8
+ const isArray_1 = __importDefault(require("lodash/isArray"));
9
+ const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
4
10
  function arrayToHexString(byteArray) {
5
- return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '');
11
+ return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '').toUpperCase();
6
12
  }
7
13
  exports.arrayToHexString = arrayToHexString;
8
14
  function hexStringToArray(inputString) {
@@ -14,3 +20,36 @@ function hexStringToArray(inputString) {
14
20
  return res;
15
21
  }
16
22
  exports.hexStringToArray = hexStringToArray;
23
+ function parseCellValue(value) {
24
+ if (!(0, isString_1.default)(value))
25
+ return value;
26
+ if (value == '(NULL)')
27
+ return null;
28
+ const mHex = value.match(/^0x([0-9a-fA-F][0-9a-fA-F])+$/);
29
+ if (mHex) {
30
+ return {
31
+ type: 'Buffer',
32
+ data: hexStringToArray(value.substring(2)),
33
+ };
34
+ }
35
+ const mOid = value.match(/^ObjectId\("([0-9a-f]{24})"\)$/);
36
+ if (mOid) {
37
+ return { $oid: mOid[1] };
38
+ }
39
+ return value;
40
+ }
41
+ exports.parseCellValue = parseCellValue;
42
+ function stringifyCellValue(value) {
43
+ if (value === null)
44
+ return '(NULL)';
45
+ if (value === undefined)
46
+ return '(NoField)';
47
+ if ((value === null || value === void 0 ? void 0 : value.type) == 'Buffer' && (0, isArray_1.default)(value.data))
48
+ return '0x' + arrayToHexString(value.data);
49
+ if (value === null || value === void 0 ? void 0 : value.$oid)
50
+ return `ObjectId("${value === null || value === void 0 ? void 0 : value.$oid}")`;
51
+ if ((0, isPlainObject_1.default)(value) || (0, isArray_1.default)(value))
52
+ return JSON.stringify(value);
53
+ return value;
54
+ }
55
+ exports.stringifyCellValue = stringifyCellValue;
@@ -1,4 +1,6 @@
1
- import { DatabaseInfo, TableInfo } from 'dbgate-types';
1
+ import { DatabaseInfo, TableInfo, ApplicationDefinition } from 'dbgate-types';
2
2
  export declare function addTableDependencies(db: DatabaseInfo): DatabaseInfo;
3
3
  export declare function extendTableInfo(table: TableInfo): TableInfo;
4
4
  export declare function extendDatabaseInfo(db: DatabaseInfo): DatabaseInfo;
5
+ export declare function extendDatabaseInfoFromApps(db: DatabaseInfo, apps: ApplicationDefinition[]): DatabaseInfo;
6
+ export declare function isTableColumnUnique(table: TableInfo, column: string): boolean;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.extendDatabaseInfo = exports.extendTableInfo = exports.addTableDependencies = void 0;
6
+ exports.isTableColumnUnique = exports.extendDatabaseInfoFromApps = exports.extendDatabaseInfo = exports.extendTableInfo = exports.addTableDependencies = void 0;
7
7
  const flatten_1 = __importDefault(require("lodash/flatten"));
8
8
  function addTableDependencies(db) {
9
9
  const allForeignKeys = (0, flatten_1.default)(db.tables.map(x => x.foreignKeys || []));
@@ -22,3 +22,26 @@ function extendDatabaseInfo(db) {
22
22
  return fillDatabaseExtendedInfo(addTableDependencies(db));
23
23
  }
24
24
  exports.extendDatabaseInfo = extendDatabaseInfo;
25
+ function extendDatabaseInfoFromApps(db, apps) {
26
+ if (!db || !apps)
27
+ return db;
28
+ const dbExt = Object.assign(Object.assign({}, db), { tables: db.tables.map(table => (Object.assign(Object.assign({}, table), { foreignKeys: [
29
+ ...(table.foreignKeys || []),
30
+ ...(0, flatten_1.default)(apps.map(app => app.virtualReferences || []))
31
+ .filter(fk => fk.pureName == table.pureName && fk.schemaName == table.schemaName)
32
+ .map(fk => (Object.assign(Object.assign({}, fk), { constraintType: 'foreignKey', isVirtual: true }))),
33
+ ] }))) });
34
+ return addTableDependencies(dbExt);
35
+ }
36
+ exports.extendDatabaseInfoFromApps = extendDatabaseInfoFromApps;
37
+ function isTableColumnUnique(table, column) {
38
+ if (table.primaryKey && table.primaryKey.columns.length == 1 && table.primaryKey.columns[0].columnName == column) {
39
+ return true;
40
+ }
41
+ const uqs = [...(table.uniques || []), ...(table.indexes || []).filter(x => x.isUnique)];
42
+ if (uqs.find(uq => uq.columns.length == 1 && uq.columns[0].columnName == column)) {
43
+ return true;
44
+ }
45
+ return false;
46
+ }
47
+ exports.isTableColumnUnique = isTableColumnUnique;
@@ -8,6 +8,9 @@ const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
8
8
  function prepareTableForImport(table) {
9
9
  const res = (0, cloneDeep_1.default)(table);
10
10
  res.foreignKeys = [];
11
+ res.indexes = [];
12
+ res.uniques = [];
13
+ res.checks = [];
11
14
  if (res.primaryKey)
12
15
  res.primaryKey.constraintName = null;
13
16
  return res;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.5.0",
2
+ "version": "4.6.2",
3
3
  "name": "dbgate-tools",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -25,14 +25,14 @@
25
25
  ],
26
26
  "devDependencies": {
27
27
  "@types/node": "^13.7.0",
28
- "dbgate-types": "^4.5.0",
28
+ "dbgate-types": "^4.6.2",
29
29
  "jest": "^24.9.0",
30
30
  "ts-jest": "^25.2.1",
31
31
  "typescript": "^4.4.3"
32
32
  },
33
33
  "dependencies": {
34
34
  "lodash": "^4.17.21",
35
- "dbgate-query-splitter": "^4.5.0",
35
+ "dbgate-query-splitter": "^4.6.2",
36
36
  "uuid": "^3.4.0"
37
37
  }
38
38
  }