reduce dbgate-tools package size

This commit is contained in:
Jan Prochazka 2020-11-24 19:06:05 +01:00
parent 5862a2cdc4
commit 556a35f4ba
7 changed files with 27 additions and 30 deletions

View File

@ -1,23 +1,20 @@
{
"version": "1.0.1",
"version": "1.0.3",
"name": "dbgate-tools",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"homepage": "https://dbgate.org/",
"repository": {
"type": "git",
"url": "https://github.com/dbshell/dbgate.git"
},
},
"funding": "https://www.paypal.com/paypalme/JanProchazkaCz/30eur",
"author": "Jan Prochazka",
"license": "GPL",
"keywords": [
"sql",
"dbgate"
],
"scripts": {
"prepare": "yarn build",
"build": "tsc",
@ -28,14 +25,13 @@
"lib"
],
"devDependencies": {
"dbgate-types": "^1.0.0",
"@types/node": "^13.7.0",
"dbgate-types": "^1.0.0",
"jest": "^24.9.0",
"ts-jest": "^25.2.1",
"typescript": "^3.7.5"
},
"dependencies": {
"lodash": "^4.17.15",
"moment": "^2.24.0"
"lodash": "^4.17.15"
}
}

View File

@ -1,6 +1,8 @@
import { DatabaseInfo, DatabaseModification, EngineDriver } from 'dbgate-types';
import _ from 'lodash';
import fp from 'lodash/fp';
import _sortBy from 'lodash/sortBy';
import _groupBy from 'lodash/groupBy';
import _pick from 'lodash/pick';
import fp_pick from 'lodash/fp/pick';
export class DatabaseAnalyser {
structure: DatabaseInfo;
@ -54,7 +56,7 @@ export class DatabaseAnalyser {
const newArray = newlyAnalysed[field] || [];
const addedChangedIds = newArray.map((x) => extractObjectId(x));
const removeAllIds = [...removedIds, ...addedChangedIds];
res[field] = _.sortBy(
res[field] = _sortBy(
[...this.structure[field].filter((x) => !removeAllIds.includes(extractObjectId(x))), ...newArray],
(x) => x.pureName
);
@ -92,17 +94,17 @@ export class DatabaseAnalyser {
const filtered = pkColumns.filter(DatabaseAnalyser.byTableFilter(table));
if (filtered.length == 0) return undefined;
return {
..._.pick(filtered[0], ['constraintName', 'schemaName', 'pureName']),
..._pick(filtered[0], ['constraintName', 'schemaName', 'pureName']),
constraintType: 'primaryKey',
columns: filtered.map(fp.pick('columnName')),
columns: filtered.map(fp_pick('columnName')),
};
}
static extractForeignKeys(table, fkColumns) {
const grouped = _.groupBy(fkColumns.filter(DatabaseAnalyser.byTableFilter(table)), 'constraintName');
return _.keys(grouped).map((constraintName) => ({
const grouped = _groupBy(fkColumns.filter(DatabaseAnalyser.byTableFilter(table)), 'constraintName');
return Object.keys(grouped).map((constraintName) => ({
constraintName,
constraintType: 'foreignKey',
..._.pick(grouped[constraintName][0], [
..._pick(grouped[constraintName][0], [
'constraintName',
'schemaName',
'pureName',
@ -111,7 +113,7 @@ export class DatabaseAnalyser {
'updateAction',
'deleteAction',
]),
columns: grouped[constraintName].map(fp.pick(['columnName', 'refColumnName'])),
columns: grouped[constraintName].map(fp_pick(['columnName', 'refColumnName'])),
}));
}
}

View File

@ -7,8 +7,9 @@ import {
TableInfo,
TransformType,
} from 'dbgate-types';
import _ from 'lodash';
import moment from 'moment';
import _isString from 'lodash/isString'
import _isNumber from 'lodash/isNumber'
import _isDate from 'lodash/isDate'
export class SqlDumper {
s = '';
@ -47,9 +48,9 @@ export class SqlDumper {
if (value === null) this.putRaw('NULL');
if (value === true) this.putRaw('1');
if (value === false) this.putRaw('0');
else if (_.isString(value)) this.putStringValue(value);
else if (_.isNumber(value)) this.putRaw(value.toString());
else if (_.isDate(value)) this.putStringValue(moment(value).toISOString());
else if (_isString(value)) this.putStringValue(value);
else if (_isNumber(value)) this.putRaw(value.toString());
else if (_isDate(value)) this.putStringValue(new Date(value).toISOString());
}
putCmd(format, ...args) {
this.put(format, ...args);

View File

@ -1,5 +1,5 @@
import { EngineDriver } from 'dbgate-types';
import _ from 'lodash';
import _intersection from 'lodash/intersection';
import { prepareTableForImport } from './tableTransforms';
export function createBulkInsertStreamBase(driver, stream, pool, name, options): any {
@ -43,7 +43,7 @@ export function createBulkInsertStreamBase(driver, stream, pool, name, options):
await driver.query(pool, `TRUNCATE TABLE ${fullNameQuoted}`);
}
this.columnNames = _.intersection(
this.columnNames = _intersection(
structure.columns.map((x) => x.columnName),
writable.structure.columns.map((x) => x.columnName)
);

View File

@ -1,5 +1,3 @@
import { createBulkInsertStreamBase } from './createBulkInsertStreamBase';
export const driverBase = {
analyserClass: null,
dumperClass: null,

View File

@ -1,4 +1,4 @@
import _ from 'lodash';
import _camelCase from 'lodash/camelCase';
export function extractShellApiPlugins(functionName, props): string[] {
const res = [];
@ -18,7 +18,7 @@ export function extractShellApiPlugins(functionName, props): string[] {
export function extractShellApiFunctionName(functionName) {
const nsMatch = functionName.match(/^([^@]+)@([^@]+)/);
if (nsMatch) {
return `${_.camelCase(nsMatch[2])}.shellApi.${nsMatch[1]}`;
return `${_camelCase(nsMatch[2])}.shellApi.${nsMatch[1]}`;
}
return `dbgateApi.${functionName}`;
}

View File

@ -1,8 +1,8 @@
import { TableInfo } from 'dbgate-types';
import _ from 'lodash';
import _cloneDeep from 'lodash/cloneDeep';
export function prepareTableForImport(table: TableInfo): TableInfo {
const res = _.cloneDeep(table);
const res = _cloneDeep(table);
res.foreignKeys = [];
if (res.primaryKey) res.primaryKey.constraintName = null;
return res;