-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi.js
More file actions
240 lines (207 loc) · 6.46 KB
/
api.js
File metadata and controls
240 lines (207 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const _ = require('lodash');
const { createLogger } = require('./helpers/loggerHelper');
const postgresService = require('./helpers/postgresService');
module.exports = {
async disconnect(connectionInfo, logger, callback, app) {
const sshService = app.require('@hackolade/ssh-service');
await postgresService.disconnect(sshService);
callback();
},
async testConnection(connectionInfo, logger, callback, app) {
const sshService = app.require('@hackolade/ssh-service');
try {
const postgresLogger = createLogger({
title: 'Test connection instance log',
hiddenKeys: connectionInfo.hiddenKeys,
logger,
});
await postgresService.connect(connectionInfo, sshService, postgresLogger);
await postgresService.pingDb();
await postgresService.logVersion();
callback();
} catch (error) {
logger.log('error', prepareError(error), 'Test connection instance log');
callback(prepareError(error));
} finally {
await postgresService.disconnect(sshService);
}
},
async getDatabases(connectionInfo, logger, cb, app) {
const sshService = app.require('@hackolade/ssh-service');
await postgresService.disconnect(sshService);
try {
const postgresLogger = createLogger({
title: 'Get DB names',
hiddenKeys: connectionInfo.hiddenKeys,
logger,
});
await postgresService.connect(connectionInfo, sshService, postgresLogger);
await postgresService.logVersion();
const dbs = await postgresService.getDatabaseNames();
logger.log('info', dbs, 'All databases list', connectionInfo.hiddenKeys);
return cb(null, dbs);
} catch (err) {
logger.log('error', err);
return cb(prepareError(err));
}
},
getDocumentKinds: function (connectionInfo, logger, cb) {
cb(null, []);
},
async getDbCollectionsNames(connectionInfo, logger, callback, app) {
const sshService = app.require('@hackolade/ssh-service');
try {
const postgresLogger = createLogger({
title: 'Get DB collections names',
hiddenKeys: connectionInfo.hiddenKeys,
logger,
});
if (!connectionInfo.ssh) {
await postgresService.connect(connectionInfo, sshService, postgresLogger);
}
await postgresService.logVersion();
const schemasNames = await postgresService.getAllSchemasNames();
const collections = await schemasNames.reduce(async (next, dbName) => {
const result = await next;
try {
const dbCollections = await postgresService.getTablesNames(dbName);
return result.concat({
dbName,
dbCollections,
isEmpty: dbCollections.length === 0,
});
} catch (error) {
postgresLogger.info(`Error reading database "${dbName}"`);
postgresLogger.error(error);
return result.concat({
dbName,
dbCollections: [],
isEmpty: true,
status: true,
});
}
}, Promise.resolve([]));
callback(null, collections);
} catch (error) {
logger.log('error', prepareError(error), 'Get DB collections names');
callback(prepareError(error));
await postgresService.disconnect(sshService);
}
},
async getDbCollectionsData(data, logger, callback, app) {
const sshService = app.require('@hackolade/ssh-service');
try {
const postgresLogger = createLogger({
title: 'Get DB collections data log',
hiddenKeys: data.hiddenKeys,
logger,
});
postgresLogger.progress('Start reverse engineering...');
const collections = data.collectionData.collections;
const schemasNames = data.collectionData.dataBaseNames;
const modelData = await postgresService.getDbLevelData();
const { packages, relationships } = await Promise.all(
schemasNames.map(async schemaName => {
const { tables, views, modelDefinitions } = await postgresService.retrieveEntitiesData(
schemaName,
collections[schemaName],
data.recordSamplingSettings,
data.includePartitions,
data.ignoreUdfUdpTriggers,
);
const { functions, procedures, triggers, sequences } =
await postgresService.retrieveSchemaLevelData(schemaName, data.ignoreUdfUdpTriggers);
postgresLogger.progress('Schema data fetched successfully', schemaName);
return {
schemaName,
tables,
views,
functions,
procedures,
modelDefinitions,
triggers,
sequences,
};
}),
)
.then(schemaData => {
const relationships = schemaData
.flatMap(({ tables }) => tables.map(entityData => entityData.relationships))
.flat();
const packages = schemaData.flatMap(
({
schemaName,
tables,
views,
functions,
procedures,
triggers,
modelDefinitions,
sequences,
}) => {
const bucketInfo = {
UDFs: functions,
Procedures: procedures,
triggers,
sequences,
};
const tablePackages = tables
.map(entityData => ({
dbName: schemaName,
collectionName: entityData.name,
documents: entityData.documents,
views: [],
emptyBucket: false,
entityLevel: entityData.entityLevel,
validation: {
jsonSchema: entityData.jsonSchema,
},
bucketInfo,
modelDefinitions,
}))
.sort(data => (_.isEmpty(data.entityLevel.inherits) ? -1 : 1));
if (views?.length) {
const viewPackage = {
dbName: schemaName,
views: views,
emptyBucket: false,
};
return [...tablePackages, viewPackage];
}
return tablePackages;
},
);
return { packages, relationships };
})
.then(({ packages, relationships }) => ({ packages: orderPackages(packages), relationships }));
postgresLogger.info('The data is processed and sent to the application', {
packagesLength: packages?.length,
relationshipsLength: relationships?.length,
});
callback(null, packages, modelData, relationships);
} catch (error) {
logger.log('error', prepareError(error), 'Retrieve tables data');
callback(prepareError(error));
} finally {
await postgresService.disconnect(sshService);
}
},
};
const prepareError = error => {
error = JSON.stringify(error, Object.getOwnPropertyNames(error));
error = JSON.parse(error);
return error;
};
const orderPackages = packages => {
return packages.sort((packA, packB) => {
if (!packA.collectionName && !packB.collectionName) {
return 0;
} else if (!packA.collectionName) {
return 1;
} else if (!packB.collectionName) {
return -1;
} else {
return packA.collectionName.localeCompare(packB.collectionName);
}
});
};