-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathABFactory.js
More file actions
687 lines (608 loc) · 20.1 KB
/
ABFactory.js
File metadata and controls
687 lines (608 loc) · 20.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* ABFactory
* an object that contains the definitions and references for a single tenant.
* It is expected that an instance of this should be returned from an
* ABBootstrap.init(req).then((AB)=>{}) call.
*/
const _ = require("lodash");
const Knex = require("knex");
const moment = require("moment");
const { nanoid } = require("nanoid");
const Papa = require("papaparse");
const { serializeError, deserializeError } = require("serialize-error");
const uuid = require("uuid");
var ABFactoryCore = require("./core/ABFactoryCore");
const SecretManager = require("./platform/ABSecretManager");
const LocalPlugins = require("./platform/plugins/included");
function stringifyErrors(param) {
if (param instanceof Error) {
return serializeError(param);
}
// traverse given data structure:
if (Array.isArray(param)) {
for (var i = 0; i < param.length; i++) {
param[i] = stringifyErrors(param[i]);
}
} else if (param && typeof param == "object") {
// maybe one of my Keys are an Error Object:
Object.keys(param).forEach((k) => {
param[k] = stringifyErrors(param[k]);
});
}
return param;
}
class ABFactory extends ABFactoryCore {
constructor(definitions, DefinitionManager, req, knexConnection = null) {
/**
* @param {hash} definitions
* { ABDefinition.id : {ABDefinition} }
* of all the definitions defined for the current Tenant
* @param {obj} DefinitionManager
* an interface for how to perform CRUD operations on definitions
* for this platform.
* Should Expose:
* DefinitionManager.Create(req, values)
* DefinitionManager.Destroy(req, cond);
* DefinitionManager.Find(req, cond);
* DefinitionManager.Update(req, cond, values);
* @param {ABUtil.request} req
* A req object tied to the proper tenant for this Factory.
* Should be created by the service (Bootstrap.js) when it
* needs to communicate to a tenant.
* @param {ABFactory.Knex.connection()} knexConnection
* An existing Knex Connection to reuse for this Factory.
* (optional)
*/
super(definitions);
this.platform = "service";
// {string} the platform this ABFactory is running on.
this.Definitions = DefinitionManager;
// {obj} the provided interface for working with the ABDefinition table.
this.req = req;
// {ABUtils.request} a tenant aware request object for interacting with
// the data in our Tenant's db.
this._knexConn = knexConnection;
// {Knex}
// an instance of a {Knex} object that is tied to this Tenant's MySQL
// connection settings. The base definition is found in config/local.js
// and can be returned by the this.req object.
this.__ModelPool = {};
// {hash} { modelName : Knex.connection() }
// This is a cached Objection(knex) cache of our Object Models for
// interacting with our DB tables using Objection.js
this.__Cache = {};
// {hash} { serviceKey : { serviceCacheData } }
// allow a service to store temporary cache information associated
// with the current ABFactory. This data is only around as long as
// this factory is.
//
// Config Data
//
this.config = {
// connections: {} // TODO:
};
//
// Knex: Migration utilities
//
this.Knex = {
/**
* @method AB.Knex.connection(name);
* return a configured {Knex} object used for generating
* sql statements against a Tenant's DB.
* @param {string} name
* the configuration entry representing the MySql connection
* settings that are stored in config/local.js
* ( for now, the plan is that all tenant DBs are stored in the
* same MySql instance. However it is possible that a Tenant's
* settings might differ and we eventually spread them out across
* different mysql instances)
* @return {Knex}
*/
connection: () => {
if (!this._knexConn) {
// NOTE: .tenantDB() returns the db name enclosed with ` `
// our KNEX connection doesn't want that for the DB Name:
var tenantDB = this.req.tenantDB().replaceAll("`", "");
if (!tenantDB) {
throw new Error(
`ABFactory.Knex.connection(): Could not find Tenant DB information for id[${this.req.tenantID()}]`,
);
}
var config = this.req.connections()["appbuilder"];
if (!config) {
throw new Error(
`ABFactory.Knex.connection(): Could not find configuration settings`,
);
}
this._knexConn = Knex({
client: "mysql",
connection: {
host: config.host,
user: config.user,
port: config.port,
password: config.password,
database: tenantDB,
timezone: "UTC",
},
// FIX : ER_CON_COUNT_ERROR: Too many connections
// https://github.com/tgriesser/knex/issues/1027
pool: {
min: 2,
max: config.poolMax || 30,
// this should reduce Knex Timeout Errors
// (https://github.com/knex/knex/issues/2820)
acquireTimeoutMillis: config.acquireTimeout || 90000,
},
});
}
return this._knexConn;
},
/**
* @method AB.Knex.createTransaction
* create Knex.Transaction.
* There are 2 expected ways to call this method:
* @codestart
* AB.Knex.createTransaction((trx)=>{
* // you can use the Transaction object (trx) now
* })
* @codeend
* or using the promise:
* @codestart
* AB.Knex.createTransaction().then((trx)=>{
* // you can use the Transaction object (trx) now
* })
* @codeend
* @param {function} - callback
* a callback to receive the newly created {Knex.transaction}
* object.
* @return {Promise} - resolve(Knex.Transaction)
*/
createTransaction: (callback) => {
let knex = this.Knex.connection();
return knex.transaction(callback);
},
};
//
// Rules
//
const platformRules = {
/**
* @method AB.rules.toSQLDate
* return a properly formatted DateTime string for MYSQL 5.7 but ignore the time information
* @param {string} date String of a date you want converted
* @return {string}
*/
toSQLDate: function (date) {
return moment(date).format("YYYY-MM-DD");
// return moment(date).format("YYYY-MM-DD 00:00:00");
},
/**
* @method AB.rules.toSQLDateTime
* return a properly formatted DateTime string for MYSQL 5.7
* @param {string} date String of a date you want converted
* @return {string}
*/
toSQLDateTime: function (date) {
return moment(date).utc().format("YYYY-MM-DD HH:mm:ss");
},
/**
* @method AB.rules.toDate
* return the given string as a Date object.
* @param {string} dateText
* @param {Object} options
* {
* format: "string",
* ignoreTime: boolean
* }
* @return {Date}
*/
toDate(dateText = "", options = {}) {
if (!dateText) return;
if (options.ignoreTime) dateText = dateText.replace(/T.*/, "");
let result = options.format
? moment(dateText, options.format)
: moment(dateText);
let supportFormats = [
"YYYY-MM-DD",
"YYYY/MM/DD",
"DD/MM/YYYY",
"MM/DD/YYYY",
"DD-MM-YYYY",
"MM-DD-YYYY",
];
supportFormats.forEach((format) => {
if (!result || !result.isValid())
result = moment(dateText, format);
});
return new Date(result);
},
/**
* @method AB.rules.toDateFormat
* convert a {Date} into a string representation we recognize.
* @param {Date} date
* @param {Object} options -
* {
* format: "string",
* localeCode: "string"
* }
* @return {string}
*/
toDateFormat(date, options) {
if (!date) return "";
let momentObj = moment(date);
if (options.localeCode) momentObj.locale(options.localeCode);
return momentObj.format(options.format);
},
/**
* @method AB.rules.subtractDate
* return a {Date} object representing a date that is a number of units
* before the given date.
* @param {Date} date
* @param {number} number
* @param {string} unit
* @return {Date}
*/
subtractDate(date, number, unit) {
return moment(date).subtract(number, unit).toDate();
},
/**
* @method AB.rules.addDate
* return a {Date} object representing a date that is a number of units
* after the given date.
* @param {Date} date
* @param {number} number
* @param {string} unit
* @return {Date}
*/
addDate(date, number, unit) {
return moment(date).add(number, unit).toDate();
},
/**
* Get today's UTC time range in "YYYY-MM-DD HH:MM:SS" format.
*
* It converts the start and end of today to UTC to keep things consistent
* across time zones. Handy when you need to deal with dates in different regions.
*
* @returns {string} UTC time range for today.
*/
getUTCDayTimeRange: () => {
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth();
let date = now.getDate();
let startOfDay = new Date(year, month, date, 0, 0, 0);
let endOfDay = new Date(year, month, date, 23, 59, 59);
// Convert to UTC by subtracting the timezone offset
let startOfDayUTC = new Date(
startOfDay.getTime() + startOfDay.getTimezoneOffset() * 60000,
);
let endOfDayUTC = new Date(
endOfDay.getTime() + endOfDay.getTimezoneOffset() * 60000,
);
// Format the date in "YYYY-MM-DD HH:MM:SS" format
let formatDate = (date) => {
let isoString = date.toISOString();
return `${isoString.slice(0, 10)} ${isoString.slice(11, 19)}`;
};
return formatDate(startOfDayUTC).concat(
"|",
formatDate(endOfDayUTC),
);
},
};
(Object.keys(platformRules) || []).forEach((k) => {
this.rules[k] = platformRules[k];
});
this.Secret = new SecretManager(this);
}
async init() {
await super.init();
await this.Secret.init(this);
}
//
// Definitions
//
/**
* definiitonCreate()
* create a new ABDefinition
* @param {obj} def
* the value hash of the new definition entry
* @return {Promise}
* resolved with a new {ABDefinition} for the entry.
*/
definitionCreate(req, def, options = {}) {
return this.Definitions.Create(this, req, def, options).then(
(fullDef) => {
let newDef = this.definitionNew(fullDef);
this.emit("definition.created", newDef);
return newDef;
},
);
}
/**
* definitionDestroy()
* delete an ABDefinition
* @param {string} id
* the uuid of the ABDefinition to delete
* @return {Promise}
*/
definitionDestroy(req, id, options = {}) {
return this.Definitions.Destroy(this, req, { id }, options).then(() => {
delete this._definitions[id];
this.emit("definition.destroyed", id);
});
}
/**
* definitionFind()
* return the definitions that match the provided condition.
* @param {string} id
* the uuid of the ABDefinition to delete
* @return {Promise}
*/
definitionFind(req, cond, options = {}) {
return this.Definitions.Find(this, req, cond, options);
}
/**
* definitionUpdate()
* update an existing ABDefinition
* @param {string} id
* the uuid of the ABDefinition to update.
* @param {obj} def
* the value hash of the new definition values
* @return {Promise}
* resolved with a new {ABDefinition} for the entry.
*/
definitionUpdate(req, id, values, options = {}) {
return this.Definitions.Update(this, req, { id }, values, options).then(
(rows) => {
let newDef = this.definitionNew(rows[0] || rows);
this._definitions[id] = newDef;
this.emit("definition.updated", id);
return newDef;
},
);
}
//
// Cached Data
//
/**
* @method cache()
* provide an interface for a service to store cached data.
* This data persists as long as the current ABFactory exists.
* @param {string} key
* The unique key to retrieve the cached data.
* @param {various} data
* Any type of data you want to store.
* @return {undefined | various}
*/
cache(key, data) {
if (typeof data != "undefined") {
this.__Cache[key] = data;
return;
}
return this.__Cache[key];
}
/**
* @method cacheClear()
* provide an interface for a service to clear cached data.
* @param {string} key
* The unique key to retrieve the cached data.
* @return {undefined}
*/
cacheClear(key) {
delete this.__Cache[key];
}
/**
* @method cacheMatch()
* this lets you work with a set of cached entries whose keys match the provided key.
* This is useful for updating a number of cached entries at a time.
* @param {string} key
* The searchKey to set/retrieve the cached data.
* @return {undefined | various}
*/
cacheMatch(key, data) {
let matches = Object.keys(this.__Cache).filter(
(k) => k.indexOf(key) > -1,
);
if (typeof data != "undefined") {
matches.forEach((k) => {
this.cache(k, data);
});
} else {
let response = {};
matches.forEach((k) => {
response[k] = this.cache(k);
});
return response;
}
}
/**
* @method modelPool()
* return the cached Model connection for the given modelName.
* @param {string} modelName
* the name of the model connection we are requesting.
* (this is assigned by the ABModel object)
* @return {Objection Model Connection}
*/
modelPool(modelName) {
return this.__ModelPool[modelName];
}
/**
* @method modelPoolDelete()
* remove the current cached Model connection.
* @param {string} modelName
* the name of the model connection we are requesting.
* (this is assigned by the ABModel object)
*/
modelPoolDelete(modelName) {
delete this.__ModelPool[modelName];
}
/**
* @method modelPoolSet()
* store the cached Model connection for the given modelName.
* This is set by the ABModel Object
* @param {string} modelName
* the name of the model connection we are requesting.
* (this is assigned by the ABModel object)
* @param {ConnectionModel} Model
* @return {Objection Model Connection}
*/
modelPoolSet(modelName, Model) {
this.__ModelPool[modelName] = Model;
}
//
// Communications
//
/**
* notify()
* will send alerts to a group of people. These alerts are usually about
* configuration errors, or software problems.
* @param {string} domain
* which group of people we are sending a notification to.
* @param {Error} error
* An error object generated at the point of issue.
* @param {json} info
* Additional related information concerning the issue.
*/
notify(domain, error, info) {
return this.req.notify(domain, error, this._notifyInfo(info));
}
//
// Plugins
//
/**
* pluginLocalLoad()
* load the local plugins for the current ABFactory.
* @returns {Promise}
*/
pluginLocalLoad() {
return LocalPlugins.load(this);
}
//
// Utilities
//
clone(value) {
return _.clone(value);
}
cloneDeep(value) {
return _.cloneDeep(value);
}
defaultsDeep(target, source) {
return _.defaultsDeep(target, source);
}
defaultSystemRoles() {
return [
"dd6c2d34-0982-48b7-bc44-2456474edbea", // System Admin
"6cc04894-a61b-4fb5-b3e5-b8c3f78bd331", // Sytem Builder
"e1be4d22-1d00-4c34-b205-ef84b8334b19", // Builder
];
}
error(message) {
message = deserializeError(message);
console.error(`ABFactory[${this.req.tenantID()}]:${message.toString()}`);
if (message instanceof Error) {
console.error(message);
}
this.emit("error", message);
}
toError(...params) {
var error = new Error(params.shift() || "Error:");
if (params.length > 0) {
// replace Error objects with a string that can be passed over the
// wire and deserialize later.
stringifyErrors(params);
error._context = JSON.stringify(params);
}
return error;
}
isEmpty(...params) {
return _.isEmpty(...params);
}
isNil(value) {
return _.isNil(value);
}
isUndefined(...params) {
return _.isUndefined(...params);
}
jobID(length = 21) {
return nanoid(length);
}
merge(...params) {
return _.merge(...params);
}
orderBy(...params) {
return _.orderBy(...params);
}
uniq(...params) {
return _.uniq(...params);
}
uuid() {
return uuid.v4();
}
toJSON() {
return { tenantID: this.req.tenantID() };
}
csvToJson(csvData) {
return Papa.parse(csvData, {
header: true,
skipEmptyLines: true,
});
}
jsonToCsv(jsonData) {
return Papa.unparse(jsonData);
}
async jsonToCsvBatched(
data,
batchSize = 10000,
jobID = this.jobID(8),
columns = null,
start = 0,
headers = true,
batches = [],
) {
return new Promise((resolve, reject) => {
// make sure data is an array
if (!Array.isArray(data)) {
data = [data];
}
if (Array.isArray(data) && data.length === 0) {
resolve("");
return;
}
if (!columns) {
columns = Object.keys(data[0]);
}
let end = start + batchSize;
if (data.length > batchSize) {
console.log(
`${jobID}:: jsonToCSVBatched:[${data.length}] : ${start} - ${end}`,
);
}
batches.push(
Papa.unparse(data.slice(start, end), {
header: headers,
newline: "\r\n",
columns,
}),
);
if (data.length <= end) {
resolve(batches.join("\r\n"));
return;
}
setImmediate(() => {
this.jsonToCsvBatched(
data,
batchSize,
jobID,
columns,
end,
false,
batches,
)
.then(resolve)
.catch(reject);
});
});
}
}
module.exports = ABFactory;