@nsshunt/stsdatamanagement 1.15.0 → 1.15.4
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.
- package/{databaseutils.js → databaseutils.ts} +36 -31
- package/{datagenerator.js → datagenerator.ts} +30 -36
- package/db-scripts/{cliworker.js → cliworker.ts} +16 -14
- package/dbaccess.js +1 -2
- package/{dbaccess.test.js → dbaccess.test.ts} +0 -0
- package/{dberrors.js → dberrors.ts} +9 -14
- package/dist/databaseutils.js +38 -40
- package/dist/databaseutils.js.map +1 -1
- package/dist/datagenerator.js +34 -48
- package/dist/datagenerator.js.map +1 -1
- package/dist/db-scripts/cliworker.js +19 -16
- package/dist/db-scripts/cliworker.js.map +1 -1
- package/dist/dbaccess.js +1 -2
- package/dist/dbaccess.js.map +1 -1
- package/dist/dbaccess.test.js.map +1 -1
- package/dist/dberrors.js +9 -6
- package/dist/dberrors.js.map +1 -1
- package/dist/pgaccesslayer.js +28 -23
- package/dist/pgaccesslayer.js.map +1 -1
- package/dist/pgpoolmanager.js +36 -41
- package/dist/pgpoolmanager.js.map +1 -1
- package/dist/pgutils.js +13 -9
- package/dist/pgutils.js.map +1 -1
- package/dist/setupdb.js +10 -6
- package/dist/setupdb.js.map +1 -1
- package/package.json +2 -2
- package/{pgaccesslayer.js → pgaccesslayer.ts} +25 -25
- package/{pgpoolmanager.js → pgpoolmanager.ts} +33 -31
- package/{pgutils.js → pgutils.ts} +7 -8
- package/setupdb.ts +13 -0
- package/types/databaseutils.d.ts +4 -3
- package/types/databaseutils.d.ts.map +1 -1
- package/types/datagenerator.d.ts +4 -3
- package/types/datagenerator.d.ts.map +1 -1
- package/types/db-scripts/cliworker.d.ts.map +1 -1
- package/types/dbaccess.d.ts +8 -2
- package/types/dbaccess.test.d.ts.map +1 -1
- package/types/dberrors.d.ts +21 -1
- package/types/dberrors.d.ts.map +1 -1
- package/types/l1cache.d.ts +1 -1
- package/types/pgaccesslayer.d.ts +192 -1
- package/types/pgaccesslayer.d.ts.map +1 -1
- package/types/pgpoolmanager.d.ts +6 -2
- package/types/pgpoolmanager.d.ts.map +1 -1
- package/types/pgutils.d.ts +1 -1
- package/types/pgutils.d.ts.map +1 -1
- package/types/setupdb.d.ts +1 -1
- package/types/setupdb.d.ts.map +1 -1
- package/dist/l1cache.js +0 -40
- package/dist/l1cache.js.map +0 -1
- package/l1cache.js +0 -57
- package/setupdb.js +0 -15
|
@@ -1,38 +1,42 @@
|
|
|
1
|
-
const prompts = require('prompts')
|
|
2
|
-
require('colors');
|
|
3
|
-
const goptions = require('@nsshunt/stsconfig').$options;
|
|
1
|
+
const prompts = require('prompts')
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
const { PGAccessLayer } = require('./pgaccesslayer');
|
|
7
|
-
const { PGUtils } = require('./pgutils');
|
|
8
|
-
const { DataGenerator } = require('./datagenerator.js');
|
|
3
|
+
import 'colors'
|
|
9
4
|
|
|
10
|
-
|
|
5
|
+
import { $Options } from '@nsshunt/stsconfig'
|
|
6
|
+
const goptions = $Options()
|
|
7
|
+
|
|
8
|
+
import { PGPoolManager } from './pgpoolmanager'
|
|
9
|
+
import { PGAccessLayer } from './pgaccesslayer'
|
|
10
|
+
import { PGUtils } from './pgutils'
|
|
11
|
+
import { DataGenerator } from './datagenerator'
|
|
12
|
+
|
|
13
|
+
import Debug from "debug";
|
|
14
|
+
const debug = Debug(`proc:${process.pid}:DatabaseUtils`);
|
|
15
|
+
|
|
16
|
+
export class DatabaseUtils
|
|
11
17
|
{
|
|
12
|
-
|
|
13
|
-
#ns = `proc:${process.pid}:DatabaseUtils`; // namespace for debug
|
|
14
|
-
#accessLayer = null;
|
|
18
|
+
private _accessLayer = null;
|
|
15
19
|
|
|
16
|
-
constructor(accessLayer)
|
|
20
|
+
constructor(accessLayer?: any)
|
|
17
21
|
{
|
|
18
|
-
this
|
|
19
|
-
this.#accessLayer = accessLayer;
|
|
22
|
+
this._accessLayer = accessLayer;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
// options ::= { start: <int>, entries: <int>, minextradata: <int>, maxextradata: <int>,
|
|
23
26
|
// user: { name: <string>, password: <string>, email: <string> } }
|
|
24
27
|
CreateDatabase = async (options) =>
|
|
25
28
|
{
|
|
26
|
-
|
|
29
|
+
const fname = 'CreateDatabase';
|
|
27
30
|
const { start, entries, minextradata, maxextradata } = options;
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
const builddbscript = `${goptions.databasescriptfolder}/builddb.sql`;
|
|
32
|
+
debug(`Database Build Script: [${builddbscript}]`.yellow);
|
|
30
33
|
|
|
34
|
+
let localAccesslayer = null;
|
|
31
35
|
try
|
|
32
36
|
{
|
|
33
37
|
try
|
|
34
38
|
{
|
|
35
|
-
|
|
39
|
+
debug(`Dropping database.`.yellow);
|
|
36
40
|
await PGUtils.dropdatabase();
|
|
37
41
|
} catch (error)
|
|
38
42
|
{
|
|
@@ -41,44 +45,47 @@ class DatabaseUtils
|
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
// Create a new empty database
|
|
44
|
-
|
|
48
|
+
debug(`Creating new empty database.`.yellow);
|
|
45
49
|
await PGUtils.createdatabase();
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
localAccesslayer = new PGAccessLayer(new PGPoolManager());
|
|
48
52
|
|
|
49
53
|
// Build the database assets (tables, functions, etc.)
|
|
50
|
-
|
|
54
|
+
debug(`Building database assets (tables, functions, etc.).`.yellow);
|
|
51
55
|
await localAccesslayer.executedbscript(builddbscript);
|
|
52
56
|
|
|
53
57
|
//@@ make optional or move
|
|
54
58
|
if (typeof entries !== 'undefined') {
|
|
55
59
|
// Add new faker entries
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
debug(`Adding test data.`.yellow);
|
|
61
|
+
const dg = new DataGenerator(localAccesslayer);
|
|
58
62
|
await dg.RunAddFakerBulkInsert(start, entries, minextradata, maxextradata);
|
|
59
63
|
}
|
|
60
64
|
|
|
61
|
-
await localAccesslayer.enddatabase();
|
|
62
|
-
|
|
63
65
|
//@@ double logging
|
|
64
|
-
|
|
66
|
+
debug(`Database successfully initiailized.`.green);
|
|
65
67
|
console.log(`Database successfully initiailized.`.green);
|
|
66
68
|
} catch (error)
|
|
67
69
|
{
|
|
68
70
|
console.error(`[${fname}]: Could not create fresh database: ${error}`);
|
|
69
|
-
|
|
71
|
+
debug(`[${fname}]: Could not create fresh database: ${error}`);
|
|
72
|
+
} finally {
|
|
73
|
+
if (localAccesslayer !== null) {
|
|
74
|
+
await localAccesslayer.enddatabase();
|
|
75
|
+
}
|
|
70
76
|
}
|
|
77
|
+
|
|
71
78
|
};
|
|
72
79
|
|
|
73
80
|
ResourceCount = async () =>
|
|
74
81
|
{
|
|
75
|
-
|
|
82
|
+
const retVal = await this._accessLayer.getResourceCount();
|
|
76
83
|
return retVal;
|
|
77
84
|
};
|
|
78
85
|
|
|
79
86
|
ExecuteDatabaseScript = async (scriptfile) =>
|
|
80
87
|
{
|
|
81
|
-
|
|
88
|
+
const retVal = await this._accessLayer.executedbscript(scriptfile);
|
|
82
89
|
return retVal;
|
|
83
90
|
};
|
|
84
91
|
|
|
@@ -93,5 +100,3 @@ class DatabaseUtils
|
|
|
93
100
|
return this.ExecuteDatabaseScript(response.script);
|
|
94
101
|
};
|
|
95
102
|
}
|
|
96
|
-
|
|
97
|
-
module.exports = { DatabaseUtils };
|
|
@@ -1,31 +1,35 @@
|
|
|
1
1
|
// https://medium.com/netscape/a-guide-to-create-a-nodejs-command-line-package-c2166ad0452e
|
|
2
2
|
// https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs
|
|
3
|
-
|
|
3
|
+
import { $Options } from '@nsshunt/stsconfig'
|
|
4
|
+
const goptions = $Options()
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
const path = require('path');
|
|
6
7
|
global.appRoot = path.resolve(__dirname);
|
|
7
8
|
|
|
8
9
|
const axios = require('axios');
|
|
9
10
|
const cliProgress = require('cli-progress');
|
|
10
|
-
|
|
11
|
+
|
|
12
|
+
import colors from 'colors';
|
|
13
|
+
|
|
11
14
|
const prompts = require('prompts');
|
|
12
15
|
const { Worker } = require('worker_threads');
|
|
13
|
-
|
|
16
|
+
|
|
17
|
+
import { Sleep } from '@nsshunt/stsutils';
|
|
14
18
|
|
|
15
19
|
const userid = 'dbinitrunner';
|
|
16
20
|
const resrandomuserprefix = 'RESRU-';
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
import Debug from "debug";
|
|
23
|
+
const debug = Debug(`proc:${process.pid}:DataGenerator`);
|
|
24
|
+
|
|
25
|
+
export class DataGenerator
|
|
19
26
|
{
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
#accessLayer = null;
|
|
23
|
-
#useMultiBar = false;
|
|
27
|
+
private _accessLayer = null;
|
|
28
|
+
private _useMultiBar = false;
|
|
24
29
|
|
|
25
30
|
constructor(accessLayer)
|
|
26
31
|
{
|
|
27
|
-
this
|
|
28
|
-
this.#accessLayer = accessLayer;
|
|
32
|
+
this._accessLayer = accessLayer;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
//@@ JSON to come from config file if bound to a K6 script - so each K6 script should also have a prompts JSON for configuration - make generic
|
|
@@ -83,9 +87,9 @@ class DataGenerator
|
|
|
83
87
|
|
|
84
88
|
for (let i=0; i < iterations; i++)
|
|
85
89
|
{
|
|
86
|
-
|
|
90
|
+
const pp = async() =>
|
|
87
91
|
{
|
|
88
|
-
|
|
92
|
+
const retVal = await axios.get(`https://randomuser.me/api/?results=${blocksize}&nat=au,nz,us,gb`).catch(error =>
|
|
89
93
|
{
|
|
90
94
|
console.error(`Error in randomuser get: ${error}`);
|
|
91
95
|
if (error.response) {
|
|
@@ -107,11 +111,11 @@ class DataGenerator
|
|
|
107
111
|
console.error('Skipping this test as we cannot control the randomuser service.');
|
|
108
112
|
return;
|
|
109
113
|
}
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
const randomusers = retVal.data.results;
|
|
115
|
+
const allPromises = randomusers.map(async data => {
|
|
112
116
|
totalDataSize += JSON.stringify(data).length;
|
|
113
|
-
|
|
114
|
-
await this
|
|
117
|
+
const id = resrandomuserprefix + uuidv4();
|
|
118
|
+
await this._accessLayer.createResource(userid, id, data);
|
|
115
119
|
b1.increment({ totalDataSize: totalDataSize });
|
|
116
120
|
});
|
|
117
121
|
await Promise.all(allPromises);
|
|
@@ -165,7 +169,7 @@ class DataGenerator
|
|
|
165
169
|
RunAddFakerBulkInsert = async(start, iterations, datamin, datamax) =>
|
|
166
170
|
{
|
|
167
171
|
let multibar = null;
|
|
168
|
-
if (this
|
|
172
|
+
if (this._useMultiBar === true) {
|
|
169
173
|
multibar = new cliProgress.MultiBar({
|
|
170
174
|
format: 'Faker |' + colors.cyan('{bar}') + '| {percentage}% || {value}/{total} Iterations || Avg Size: {avgDataSize} || Size: {totalDataSize} || Duration: {duration_formatted} || ETA: {eta_formatted}',
|
|
171
175
|
clearOnComplete: false,
|
|
@@ -174,22 +178,22 @@ class DataGenerator
|
|
|
174
178
|
}
|
|
175
179
|
|
|
176
180
|
|
|
177
|
-
|
|
178
|
-
|
|
181
|
+
const pa = [ ];
|
|
182
|
+
const numCPUs = goptions.useCPUs;
|
|
179
183
|
//let numCPUs = require('os').cpus().length;
|
|
180
|
-
|
|
181
|
-
|
|
184
|
+
const blocksize = Math.floor(iterations / numCPUs);
|
|
185
|
+
const lastBlockSize = blocksize + (iterations % numCPUs);
|
|
182
186
|
|
|
183
187
|
let count = 0;
|
|
184
188
|
if (start === -1)
|
|
185
189
|
{
|
|
186
|
-
|
|
190
|
+
const countresult = await this._accessLayer.getResourceCount(`r.resname like 'RESFK-%'`);
|
|
187
191
|
count = Number(countresult.detail.count);
|
|
188
192
|
} else {
|
|
189
193
|
count = start;
|
|
190
194
|
}
|
|
191
195
|
|
|
192
|
-
|
|
196
|
+
const bars = [ ];
|
|
193
197
|
for (let i=0; i < numCPUs; i++)
|
|
194
198
|
{
|
|
195
199
|
const iterations = (i === numCPUs-1 ? lastBlockSize : blocksize);
|
|
@@ -198,7 +202,7 @@ class DataGenerator
|
|
|
198
202
|
bar = multibar.create(iterations, 0);
|
|
199
203
|
}
|
|
200
204
|
if (typeof bar === 'undefined') {
|
|
201
|
-
|
|
205
|
+
debug(`No TTY available for multi-bar progress.`.yellow);
|
|
202
206
|
bar = null;
|
|
203
207
|
}
|
|
204
208
|
bars.push(bar);
|
|
@@ -206,27 +210,22 @@ class DataGenerator
|
|
|
206
210
|
}
|
|
207
211
|
await Promise.all(pa);
|
|
208
212
|
|
|
209
|
-
if (this
|
|
213
|
+
if (this._useMultiBar === true) {
|
|
210
214
|
multibar.stop();
|
|
211
215
|
}
|
|
212
216
|
};
|
|
213
217
|
|
|
214
218
|
StartWorkerThread = (start, iterations, datamin, datamax, bar) =>
|
|
215
219
|
{
|
|
216
|
-
this.#debug(this.#ns, `Starting worker thread.`.yellow);
|
|
217
220
|
return new Promise((resolve, reject) =>
|
|
218
221
|
{
|
|
219
222
|
const worker = new Worker(`${goptions.databasescriptfolder}/cliworker.js`, {
|
|
220
223
|
workerData: { start: start, iterations: iterations, datamin: datamin, datamax: datamax }
|
|
221
224
|
});
|
|
222
225
|
|
|
223
|
-
//this.#debug(this.#ns, `Worker: [${worker.process.pid}] thread started.`.yellow);
|
|
224
|
-
|
|
225
226
|
worker.on('message', (data) => {
|
|
226
|
-
//this.#debug(this.#ns, `Received message [${data.command}] worker thread.`.grey);
|
|
227
227
|
if (data.command === 'done')
|
|
228
228
|
{
|
|
229
|
-
//this.#debug(this.#ns, `Terminating worker thread [${worker.process.pid}].`.yellow);
|
|
230
229
|
worker.terminate();
|
|
231
230
|
resolve(data.data);
|
|
232
231
|
} else
|
|
@@ -238,18 +237,13 @@ class DataGenerator
|
|
|
238
237
|
});
|
|
239
238
|
|
|
240
239
|
worker.on('error', (error) => {
|
|
241
|
-
//this.#debug(this.#ns, `Worker [${worker.process.pid}] error: ${error}.`.red);
|
|
242
240
|
reject(error);
|
|
243
241
|
});
|
|
244
242
|
worker.on('exit', (code) => {
|
|
245
|
-
//this.#debug(this.#ns, `Worker [${worker.process.pid}] exiting. Exit code: ${code}.`.green);
|
|
246
243
|
if (code !== 0) {
|
|
247
|
-
//this.#debug(this.#ns, `Worker [${worker.process.pid}] exited in a non success state. Exit code: ${code}.`.red);
|
|
248
244
|
reject(new Error(`Worker stopped with exit code ${code}`));
|
|
249
245
|
}
|
|
250
246
|
});
|
|
251
247
|
});
|
|
252
248
|
};
|
|
253
249
|
}
|
|
254
|
-
|
|
255
|
-
module.exports = { DataGenerator };
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
//let faker = require('faker');
|
|
2
2
|
const { parentPort, workerData } = require('worker_threads');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
import { PGPoolManager } from './../pgpoolmanager'
|
|
6
|
+
import { PGAccessLayer } from './../pgaccesslayer'
|
|
6
7
|
|
|
7
8
|
const userid = 'dbinitrunner';
|
|
8
9
|
const resfakerprefix = 'RESFK-';
|
|
9
10
|
const rescopyprefix = 'RESCP-';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
const { start, iterations, datamin } = workerData;
|
|
13
|
+
let { datamax } = workerData;
|
|
12
14
|
|
|
13
15
|
let fakerdataarray = [ ];
|
|
14
16
|
let fakerdataarray2 = [ ];
|
|
15
17
|
let count = start;
|
|
16
18
|
let totalDataSize = 0;
|
|
17
19
|
let avgDataSize = 0;
|
|
18
|
-
|
|
20
|
+
const copybatchsize = 250;
|
|
19
21
|
|
|
20
22
|
(async () =>
|
|
21
23
|
{
|
|
@@ -31,29 +33,29 @@ let copybatchsize = 250;
|
|
|
31
33
|
|
|
32
34
|
const coreRecordSizeRange = crypto.randomInt(128, 192);
|
|
33
35
|
const coreRecordSize = Math.floor(coreRecordSizeRange / 2);
|
|
34
|
-
|
|
36
|
+
const randcard = crypto.randomBytes(coreRecordSize).toString('hex');
|
|
35
37
|
|
|
36
38
|
if (datamax > 0)
|
|
37
39
|
{
|
|
38
40
|
const n = crypto.randomInt(datamin, datamax);
|
|
39
41
|
const n2 = Math.floor(n / 2);
|
|
40
|
-
|
|
42
|
+
const rndid = crypto.randomBytes(n2).toString('hex');
|
|
41
43
|
randcard.extradata = rndid;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
|
|
46
|
+
const randcardstring = JSON.stringify(randcard);
|
|
45
47
|
|
|
46
48
|
totalDataSize += randcardstring.length;
|
|
47
49
|
avgDataSize = Math.floor(totalDataSize / (i+1));
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
const pn = ('' + count++).padStart(10, '0');
|
|
52
|
+
const id = resfakerprefix + pn;
|
|
53
|
+
const cpid = rescopyprefix + pn;
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
const datanow = new Date().toISOString();
|
|
54
56
|
//let datanow = new Date().toUTCString();
|
|
55
57
|
|
|
56
|
-
|
|
58
|
+
const fakerdata =
|
|
57
59
|
{
|
|
58
60
|
//oid: i
|
|
59
61
|
resname: id
|
|
@@ -66,7 +68,7 @@ let copybatchsize = 250;
|
|
|
66
68
|
};
|
|
67
69
|
|
|
68
70
|
fakerdataarray.push(fakerdata);
|
|
69
|
-
|
|
71
|
+
const fakerdata2 = Object.assign({}, fakerdata);
|
|
70
72
|
fakerdata2.resname = cpid;
|
|
71
73
|
fakerdataarray2.push(fakerdata2);
|
|
72
74
|
|
package/dbaccess.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const { PGPoolManager } = require('./pgpoolmanager');
|
|
2
|
-
const { L1Cache } = require('./l1cache');
|
|
3
2
|
const { PGAccessLayer } = require('./pgaccesslayer');
|
|
4
3
|
const { PGUtils } = require('./pgutils');
|
|
5
4
|
const { DatabaseUtils } = require('./databaseutils');
|
|
@@ -12,7 +11,7 @@ const {
|
|
|
12
11
|
|
|
13
12
|
module.exports = {
|
|
14
13
|
// Utilities and Helpers
|
|
15
|
-
PGPoolManager,
|
|
14
|
+
PGPoolManager, PGAccessLayer, PGUtils, DatabaseUtils, DataGenerator,
|
|
16
15
|
// Errors
|
|
17
16
|
STSDataManagementError, STSResourceNotFoundError, STSResourceMalformedError,
|
|
18
17
|
STSDatabaseAccessError, STSInvalidCredentials, STSNotAuthorized,
|
|
File without changes
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { StatusCodes } from 'http-status-codes';
|
|
2
2
|
|
|
3
3
|
// https://rclayton.silvrback.com/custom-errors-in-node-js
|
|
4
|
-
class STSDataManagementError extends Error {
|
|
4
|
+
export class STSDataManagementError extends Error {
|
|
5
|
+
detail: any = null;
|
|
6
|
+
status: StatusCodes = StatusCodes.INTERNAL_SERVER_ERROR;
|
|
7
|
+
|
|
5
8
|
constructor(message) {
|
|
6
9
|
super(message);
|
|
7
10
|
|
|
@@ -12,13 +15,10 @@ class STSDataManagementError extends Error {
|
|
|
12
15
|
// It's not absolutely essential, but it does make the stack trace a little nicer.
|
|
13
16
|
// @see Node.js reference (bottom)
|
|
14
17
|
Error.captureStackTrace(this, this.constructor);
|
|
15
|
-
|
|
16
|
-
this.detail = null;
|
|
17
|
-
this.status = StatusCodes.INTERNAL_SERVER_ERROR;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
class STSResourceNotFoundError extends STSDataManagementError {
|
|
21
|
+
export class STSResourceNotFoundError extends STSDataManagementError {
|
|
22
22
|
constructor(resource, resourceId) {
|
|
23
23
|
super(`Resource [${resource}] not found using ID: [${resourceId}]`);
|
|
24
24
|
this.detail = {
|
|
@@ -29,7 +29,7 @@ class STSResourceNotFoundError extends STSDataManagementError {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
class STSResourceMalformedError extends STSDataManagementError {
|
|
32
|
+
export class STSResourceMalformedError extends STSDataManagementError {
|
|
33
33
|
constructor(resource, resourceId, resourceRaw, error, extraDetail) {
|
|
34
34
|
super(`Resource [${resource}:${resourceId}] is malformed.`);
|
|
35
35
|
this.detail = {
|
|
@@ -43,7 +43,7 @@ class STSResourceMalformedError extends STSDataManagementError {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
class STSDatabaseAccessError extends STSDataManagementError {
|
|
46
|
+
export class STSDatabaseAccessError extends STSDataManagementError {
|
|
47
47
|
constructor(resource, resourceId, error) {
|
|
48
48
|
super(`Database access error reading resource: [${resource}:${resourceId}]`);
|
|
49
49
|
this.detail = {
|
|
@@ -55,7 +55,7 @@ class STSDatabaseAccessError extends STSDataManagementError {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
class STSInvalidCredentials extends STSDataManagementError {
|
|
58
|
+
export class STSInvalidCredentials extends STSDataManagementError {
|
|
59
59
|
constructor(resource, resourceId) {
|
|
60
60
|
super(`Invalid credentials for resource: [${resource}:${resourceId}]`);
|
|
61
61
|
this.detail = {
|
|
@@ -66,7 +66,7 @@ class STSInvalidCredentials extends STSDataManagementError {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
class STSNotAuthorized extends STSDataManagementError {
|
|
69
|
+
export class STSNotAuthorized extends STSDataManagementError {
|
|
70
70
|
constructor(clientId, audience) {
|
|
71
71
|
super(`Not Authorized: [${clientId}:${audience}]`);
|
|
72
72
|
this.detail = {
|
|
@@ -76,8 +76,3 @@ class STSNotAuthorized extends STSDataManagementError {
|
|
|
76
76
|
this.status = StatusCodes.INTERNAL_SERVER_ERROR;
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
-
|
|
80
|
-
module.exports = {
|
|
81
|
-
STSDataManagementError, STSResourceNotFoundError, STSResourceMalformedError,
|
|
82
|
-
STSDatabaseAccessError, STSInvalidCredentials, STSNotAuthorized
|
|
83
|
-
}
|
package/dist/databaseutils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -7,76 +8,75 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
9
|
});
|
|
9
10
|
};
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
13
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
13
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
18
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
19
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
20
|
-
};
|
|
21
|
-
var _DatabaseUtils_debug, _DatabaseUtils_ns, _DatabaseUtils_accessLayer;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.DatabaseUtils = void 0;
|
|
22
16
|
const prompts = require('prompts');
|
|
23
|
-
require(
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
17
|
+
require("colors");
|
|
18
|
+
const stsconfig_1 = require("@nsshunt/stsconfig");
|
|
19
|
+
const goptions = (0, stsconfig_1.$Options)();
|
|
20
|
+
const pgpoolmanager_1 = require("./pgpoolmanager");
|
|
21
|
+
const pgaccesslayer_1 = require("./pgaccesslayer");
|
|
22
|
+
const pgutils_1 = require("./pgutils");
|
|
23
|
+
const datagenerator_1 = require("./datagenerator");
|
|
24
|
+
const debug_1 = __importDefault(require("debug"));
|
|
25
|
+
const debug = (0, debug_1.default)(`proc:${process.pid}:DatabaseUtils`);
|
|
29
26
|
class DatabaseUtils {
|
|
30
27
|
constructor(accessLayer) {
|
|
31
|
-
|
|
32
|
-
_DatabaseUtils_ns.set(this, `proc:${process.pid}:DatabaseUtils`); // namespace for debug
|
|
33
|
-
_DatabaseUtils_accessLayer.set(this, null);
|
|
28
|
+
this._accessLayer = null;
|
|
34
29
|
// options ::= { start: <int>, entries: <int>, minextradata: <int>, maxextradata: <int>,
|
|
35
30
|
// user: { name: <string>, password: <string>, email: <string> } }
|
|
36
31
|
this.CreateDatabase = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
|
|
32
|
+
const fname = 'CreateDatabase';
|
|
38
33
|
const { start, entries, minextradata, maxextradata } = options;
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
const builddbscript = `${goptions.databasescriptfolder}/builddb.sql`;
|
|
35
|
+
debug(`Database Build Script: [${builddbscript}]`.yellow);
|
|
36
|
+
let localAccesslayer = null;
|
|
41
37
|
try {
|
|
42
38
|
try {
|
|
43
|
-
|
|
44
|
-
yield PGUtils.dropdatabase();
|
|
39
|
+
debug(`Dropping database.`.yellow);
|
|
40
|
+
yield pgutils_1.PGUtils.dropdatabase();
|
|
45
41
|
}
|
|
46
42
|
catch (error) {
|
|
47
43
|
console.error(`[${fname}]: Could not drop database: ${error}`);
|
|
48
44
|
// Do not re-throw. If there is no initial database, this will throw an error, ignore this.
|
|
49
45
|
}
|
|
50
46
|
// Create a new empty database
|
|
51
|
-
|
|
52
|
-
yield PGUtils.createdatabase();
|
|
53
|
-
|
|
47
|
+
debug(`Creating new empty database.`.yellow);
|
|
48
|
+
yield pgutils_1.PGUtils.createdatabase();
|
|
49
|
+
localAccesslayer = new pgaccesslayer_1.PGAccessLayer(new pgpoolmanager_1.PGPoolManager());
|
|
54
50
|
// Build the database assets (tables, functions, etc.)
|
|
55
|
-
|
|
51
|
+
debug(`Building database assets (tables, functions, etc.).`.yellow);
|
|
56
52
|
yield localAccesslayer.executedbscript(builddbscript);
|
|
57
53
|
//@@ make optional or move
|
|
58
54
|
if (typeof entries !== 'undefined') {
|
|
59
55
|
// Add new faker entries
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
debug(`Adding test data.`.yellow);
|
|
57
|
+
const dg = new datagenerator_1.DataGenerator(localAccesslayer);
|
|
62
58
|
yield dg.RunAddFakerBulkInsert(start, entries, minextradata, maxextradata);
|
|
63
59
|
}
|
|
64
|
-
yield localAccesslayer.enddatabase();
|
|
65
60
|
//@@ double logging
|
|
66
|
-
|
|
61
|
+
debug(`Database successfully initiailized.`.green);
|
|
67
62
|
console.log(`Database successfully initiailized.`.green);
|
|
68
63
|
}
|
|
69
64
|
catch (error) {
|
|
70
65
|
console.error(`[${fname}]: Could not create fresh database: ${error}`);
|
|
71
|
-
|
|
66
|
+
debug(`[${fname}]: Could not create fresh database: ${error}`);
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
if (localAccesslayer !== null) {
|
|
70
|
+
yield localAccesslayer.enddatabase();
|
|
71
|
+
}
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
this.ResourceCount = () => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
|
|
75
|
+
const retVal = yield this._accessLayer.getResourceCount();
|
|
76
76
|
return retVal;
|
|
77
77
|
});
|
|
78
78
|
this.ExecuteDatabaseScript = (scriptfile) => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
|
|
79
|
+
const retVal = yield this._accessLayer.executedbscript(scriptfile);
|
|
80
80
|
return retVal;
|
|
81
81
|
});
|
|
82
82
|
this.ExecutedCustomScript = () => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -88,10 +88,8 @@ class DatabaseUtils {
|
|
|
88
88
|
});
|
|
89
89
|
return this.ExecuteDatabaseScript(response.script);
|
|
90
90
|
});
|
|
91
|
-
|
|
92
|
-
__classPrivateFieldSet(this, _DatabaseUtils_accessLayer, accessLayer, "f");
|
|
91
|
+
this._accessLayer = accessLayer;
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
|
-
|
|
96
|
-
module.exports = { DatabaseUtils };
|
|
94
|
+
exports.DatabaseUtils = DatabaseUtils;
|
|
97
95
|
//# sourceMappingURL=databaseutils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"databaseutils.js","sourceRoot":"","sources":["../databaseutils.
|
|
1
|
+
{"version":3,"file":"databaseutils.js","sourceRoot":"","sources":["../databaseutils.ts"],"names":[],"mappings":"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"}
|