@carbonorm/carbonnode 3.0.0 → 3.0.1

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.
Files changed (41) hide show
  1. package/dist/api/builders/sqlBuilder.d.ts +3 -0
  2. package/dist/api/convertForRequestBody.d.ts +1 -1
  3. package/dist/api/executors/Executor.d.ts +18 -0
  4. package/dist/api/executors/HttpExecutor.d.ts +15 -0
  5. package/dist/api/executors/SqlExecutor.d.ts +11 -0
  6. package/dist/api/restRequest.d.ts +5 -164
  7. package/dist/api/types/dynamicFetching.d.ts +10 -0
  8. package/dist/api/types/modifyTypes.d.ts +9 -0
  9. package/dist/api/types/mysqlTypes.d.ts +4 -0
  10. package/dist/api/types/ormInterfaces.d.ts +223 -0
  11. package/dist/api/utils/apiHelpers.d.ts +9 -0
  12. package/dist/api/utils/cacheManager.d.ts +10 -0
  13. package/dist/api/utils/logger.d.ts +7 -0
  14. package/dist/api/utils/sortAndSerializeQueryObject.d.ts +1 -0
  15. package/dist/api/utils/testHelpers.d.ts +1 -0
  16. package/dist/api/utils/toastNotifier.d.ts +2 -0
  17. package/dist/index.cjs.js +614 -605
  18. package/dist/index.cjs.js.map +1 -1
  19. package/dist/index.d.ts +14 -2
  20. package/dist/index.esm.js +603 -607
  21. package/dist/index.esm.js.map +1 -1
  22. package/package.json +22 -6
  23. package/src/api/builders/sqlBuilder.ts +173 -0
  24. package/src/api/convertForRequestBody.ts +1 -2
  25. package/src/api/executors/Executor.ts +26 -0
  26. package/src/api/executors/HttpExecutor.ts +790 -0
  27. package/src/api/executors/SqlExecutor.ts +90 -0
  28. package/src/api/restRequest.ts +20 -1273
  29. package/src/api/types/dynamicFetching.ts +10 -0
  30. package/src/api/types/modifyTypes.ts +25 -0
  31. package/src/api/types/mysqlTypes.ts +33 -0
  32. package/src/api/types/ormInterfaces.ts +287 -0
  33. package/src/api/utils/apiHelpers.ts +83 -0
  34. package/src/api/utils/cacheManager.ts +67 -0
  35. package/src/api/utils/logger.ts +24 -0
  36. package/src/api/utils/sortAndSerializeQueryObject.ts +12 -0
  37. package/src/api/utils/testHelpers.ts +24 -0
  38. package/src/api/utils/toastNotifier.ts +11 -0
  39. package/src/index.ts +14 -2
  40. package/src/api/carbonSqlExecutor.ts +0 -279
  41. package/src/api/interfaces/ormInterfaces.ts +0 -87
package/dist/index.cjs.js CHANGED
@@ -235,366 +235,6 @@ var axiosInstance = (axios.create({
235
235
  */
236
236
  }));
237
237
 
238
- // import { validatePayloadAgainstSchema } from './validator'; // C6 schema validator
239
- var CarbonSqlExecutor = /** @class */ (function () {
240
- function CarbonSqlExecutor(pool, C6) {
241
- this.pool = pool;
242
- this.C6 = C6;
243
- }
244
- CarbonSqlExecutor.prototype.withConnection = function (cb) {
245
- return tslib.__awaiter(this, void 0, void 0, function () {
246
- var conn;
247
- return tslib.__generator(this, function (_a) {
248
- switch (_a.label) {
249
- case 0: return [4 /*yield*/, this.pool.getConnection()];
250
- case 1:
251
- conn = _a.sent();
252
- _a.label = 2;
253
- case 2:
254
- _a.trys.push([2, , 4, 5]);
255
- return [4 /*yield*/, cb(conn)];
256
- case 3: return [2 /*return*/, _a.sent()];
257
- case 4:
258
- conn.release();
259
- return [7 /*endfinally*/];
260
- case 5: return [2 /*return*/];
261
- }
262
- });
263
- });
264
- };
265
- CarbonSqlExecutor.prototype.handle = function (req, res, next) {
266
- return tslib.__awaiter(this, void 0, void 0, function () {
267
- var method, table, primary, payload, result, _a, err_1;
268
- return tslib.__generator(this, function (_b) {
269
- switch (_b.label) {
270
- case 0:
271
- _b.trys.push([0, 11, , 12]);
272
- method = req.method.toUpperCase();
273
- table = req.params.table;
274
- primary = req.params.primary;
275
- payload = method === 'GET' ? req.query : req.body;
276
- if (!(table in this.C6.TABLES)) {
277
- res.status(400).json({ error: "Invalid table: ".concat(table) });
278
- return [2 /*return*/];
279
- }
280
- result = void 0;
281
- _a = method;
282
- switch (_a) {
283
- case 'GET': return [3 /*break*/, 1];
284
- case 'OPTIONS': return [3 /*break*/, 1];
285
- case 'POST': return [3 /*break*/, 3];
286
- case 'PUT': return [3 /*break*/, 5];
287
- case 'DELETE': return [3 /*break*/, 7];
288
- }
289
- return [3 /*break*/, 9];
290
- case 1: return [4 /*yield*/, this.select(table, primary, payload)];
291
- case 2:
292
- result = _b.sent();
293
- return [3 /*break*/, 10];
294
- case 3: return [4 /*yield*/, this.insert(table, payload)];
295
- case 4:
296
- result = _b.sent();
297
- return [3 /*break*/, 10];
298
- case 5: return [4 /*yield*/, this.update(table, primary, payload)];
299
- case 6:
300
- result = _b.sent();
301
- return [3 /*break*/, 10];
302
- case 7: return [4 /*yield*/, this.delete(table, primary, payload)];
303
- case 8:
304
- result = _b.sent();
305
- return [3 /*break*/, 10];
306
- case 9: throw new Error("Unsupported method: ".concat(method));
307
- case 10:
308
- res.status(200).json({ success: true, result: result });
309
- return [3 /*break*/, 12];
310
- case 11:
311
- err_1 = _b.sent();
312
- next(err_1);
313
- return [3 /*break*/, 12];
314
- case 12: return [2 /*return*/];
315
- }
316
- });
317
- });
318
- };
319
- CarbonSqlExecutor.prototype.buildBooleanJoinedConditions = function (set, andMode) {
320
- if (andMode === void 0) { andMode = true; }
321
- var booleanOperator = andMode ? 'AND' : 'OR';
322
- var sql = '';
323
- var OPERATORS = ['=', '!=', '<', '<=', '>', '>=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'IS', 'IS NOT'];
324
- var isAggregateArray = function (value) { return Array.isArray(value) && typeof value[0] === 'string' && OPERATORS.includes(value[0]); };
325
- var isNumericKeyed = function (obj) { return Array.isArray(obj) && Object.keys(obj).every(function (k) { return /^\d+$/.test(k); }); };
326
- // todo - we should be doing something with value no????
327
- var addCondition = function (column, op, _value) {
328
- var paramName = column.replace(/\W+/g, '_');
329
- return "(".concat(column, " ").concat(op, " :").concat(paramName, ")");
330
- };
331
- if (isNumericKeyed(set)) {
332
- switch (set.length) {
333
- case 2:
334
- sql += addCondition(set[0], '=', set[1]);
335
- break;
336
- case 3:
337
- if (!OPERATORS.includes(set[1])) {
338
- throw new Error("Invalid operator: ".concat(set[1]));
339
- }
340
- sql += addCondition(set[0], set[1], set[2]);
341
- break;
342
- default:
343
- throw new Error("Invalid array condition: ".concat(JSON.stringify(set)));
344
- }
345
- }
346
- else {
347
- var parts = [];
348
- for (var _i = 0, _a = Object.entries(set); _i < _a.length; _i++) {
349
- var _b = _a[_i], key = _b[0], value = _b[1];
350
- if (/^\d+$/.test(key)) {
351
- parts.push(this.buildBooleanJoinedConditions(value, !andMode));
352
- continue;
353
- }
354
- if (!Array.isArray(value) || isAggregateArray(value)) {
355
- parts.push(addCondition(key, '='));
356
- continue;
357
- }
358
- if (value.length === 2 && OPERATORS.includes(value[0])) {
359
- parts.push(addCondition(key, value[0], value[1]));
360
- }
361
- else if (value.length === 1 && isAggregateArray(value[0])) {
362
- parts.push(addCondition(key, '=', value[0]));
363
- }
364
- else {
365
- throw new Error("Invalid condition for ".concat(key, ": ").concat(JSON.stringify(value)));
366
- }
367
- }
368
- sql = parts.join(" ".concat(booleanOperator, " "));
369
- }
370
- return "(".concat(sql, ")");
371
- };
372
- CarbonSqlExecutor.prototype.buildAggregateField = function (field) {
373
- if (typeof field === 'string')
374
- return field;
375
- if (!Array.isArray(field))
376
- throw new Error('Invalid SELECT entry: must be string or array');
377
- var agg = field[0], args = field.slice(1);
378
- switch (agg) {
379
- case 'COUNT':
380
- return "COUNT(".concat(args[0] || '*', ")");
381
- case 'SUM':
382
- case 'AVG':
383
- case 'MIN':
384
- case 'MAX':
385
- return "".concat(agg, "(").concat(args[0], ")").concat(args[1] ? " AS ".concat(args[1]) : '');
386
- case 'DISTINCT':
387
- return "DISTINCT(".concat(args[0], ")").concat(args[1] ? " AS ".concat(args[1]) : '');
388
- case 'GROUP_CONCAT': {
389
- var col = args[0], alias = args[1], sortCol = args[2], sortType = args[3];
390
- var order = sortCol ? " ORDER BY ".concat(sortCol, " ").concat(sortType || 'ASC') : '';
391
- return "GROUP_CONCAT(DISTINCT ".concat(col).concat(order, " SEPARATOR ',')").concat(alias ? " AS ".concat(alias) : '');
392
- }
393
- case 'AS': {
394
- var col = args[0], alias = args[1];
395
- return "".concat(col, " AS ").concat(alias);
396
- }
397
- case 'CONVERT_TZ': {
398
- var ts = args[0], fromTz = args[1], toTz = args[2];
399
- return "CONVERT_TZ(".concat(ts, ", ").concat(fromTz, ", ").concat(toTz, ")");
400
- }
401
- case 'NOW':
402
- return 'NOW()';
403
- default:
404
- throw new Error("Unsupported aggregate: ".concat(agg));
405
- }
406
- };
407
- CarbonSqlExecutor.prototype.buildSelectQuery = function (table, primary, args, isSubSelect) {
408
- var _this = this;
409
- var _a, _b, _c, _d;
410
- if (isSubSelect === void 0) { isSubSelect = false; }
411
- var selectList = (_a = args === null || args === void 0 ? void 0 : args[this.C6.SELECT]) !== null && _a !== void 0 ? _a : ['*'];
412
- var selectFields = Array.isArray(selectList)
413
- ? selectList.map(function (f) { return _this.buildAggregateField(f); }).join(', ')
414
- : '*';
415
- var sql = "SELECT ".concat(selectFields, " FROM `").concat(table, "`");
416
- if (args === null || args === void 0 ? void 0 : args[this.C6.JOIN]) {
417
- var joins = args[this.C6.JOIN];
418
- for (var joinType in joins) {
419
- var joinKeyword = joinType.replace('_', ' ').toUpperCase();
420
- for (var joinTable in joins[joinType]) {
421
- var onClause = this.buildBooleanJoinedConditions(joins[joinType][joinTable]);
422
- sql += " ".concat(joinKeyword, " JOIN `").concat(joinTable, "` ON ").concat(onClause);
423
- }
424
- }
425
- }
426
- if (args === null || args === void 0 ? void 0 : args[this.C6.WHERE]) {
427
- sql += " WHERE ".concat(this.buildBooleanJoinedConditions(args[this.C6.WHERE]));
428
- }
429
- if (args === null || args === void 0 ? void 0 : args[this.C6.GROUP_BY]) {
430
- var groupByFields = Array.isArray(args[this.C6.GROUP_BY]) ? args[this.C6.GROUP_BY].join(', ') : args[this.C6.GROUP_BY];
431
- sql += " GROUP BY ".concat(groupByFields);
432
- }
433
- if (args === null || args === void 0 ? void 0 : args[this.C6.HAVING]) {
434
- sql += " HAVING ".concat(this.buildBooleanJoinedConditions(args[this.C6.HAVING]));
435
- }
436
- if (args === null || args === void 0 ? void 0 : args[this.C6.PAGINATION]) {
437
- var p = args[this.C6.PAGINATION];
438
- var limitClause = '';
439
- if (p[this.C6.ORDER]) {
440
- var orderArray = Object.entries(p[this.C6.ORDER]).map(function (_a) {
441
- var col = _a[0], dir = _a[1];
442
- if (!['ASC', 'DESC'].includes(String(dir).toUpperCase())) {
443
- throw new Error("Invalid order direction: ".concat(dir));
444
- }
445
- return "".concat(col, " ").concat(String(dir).toUpperCase());
446
- });
447
- sql += " ORDER BY ".concat(orderArray.join(', '));
448
- }
449
- else if (primary) {
450
- sql += " ORDER BY ".concat(primary, " DESC");
451
- }
452
- else {
453
- // todo this is wrong
454
- var primaryKey = (_c = (_b = this.C6.TABLES['users'].PRIMARY_SHORT) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : 'user_id';
455
- sql += " ORDER BY ".concat(primaryKey, " DESC");
456
- }
457
- if (p[this.C6.LIMIT] != null) {
458
- var limit = parseInt(p[this.C6.LIMIT], 10);
459
- if (isNaN(limit) || limit < 0) {
460
- throw new Error("Invalid LIMIT: ".concat(p[this.C6.LIMIT]));
461
- }
462
- var page = parseInt((_d = p[this.C6.PAGE]) !== null && _d !== void 0 ? _d : 1, 10);
463
- if (isNaN(page) || page < 1) {
464
- throw new Error("PAGE must be >= 1 (got ".concat(p[this.C6.PAGE], ")"));
465
- }
466
- var offset = (page - 1) * limit;
467
- limitClause += " LIMIT ".concat(offset, ", ").concat(limit);
468
- }
469
- sql += limitClause;
470
- }
471
- else if (!isSubSelect && primary) {
472
- sql += " ORDER BY ".concat(primary, " ASC LIMIT 1");
473
- }
474
- else if (!isSubSelect && !primary) {
475
- sql += " ORDER BY id ASC LIMIT 100"; // fallback default limit
476
- }
477
- return sql;
478
- };
479
- CarbonSqlExecutor.prototype.select = function (table, primary, args) {
480
- return tslib.__awaiter(this, void 0, void 0, function () {
481
- var sql;
482
- var _this = this;
483
- return tslib.__generator(this, function (_a) {
484
- switch (_a.label) {
485
- case 0:
486
- sql = this.buildSelectQuery(table, primary, args);
487
- return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
488
- var rows;
489
- return tslib.__generator(this, function (_a) {
490
- switch (_a.label) {
491
- case 0:
492
- console.log(sql);
493
- return [4 /*yield*/, conn.query(sql)];
494
- case 1:
495
- rows = (_a.sent())[0];
496
- return [2 /*return*/, rows];
497
- }
498
- });
499
- }); })];
500
- case 1: return [2 /*return*/, _a.sent()];
501
- }
502
- });
503
- });
504
- };
505
- CarbonSqlExecutor.prototype.insert = function (table, data) {
506
- return tslib.__awaiter(this, void 0, void 0, function () {
507
- var keys, values, placeholders, sql;
508
- var _this = this;
509
- return tslib.__generator(this, function (_a) {
510
- switch (_a.label) {
511
- case 0:
512
- keys = Object.keys(data);
513
- values = keys.map(function (k) { return data[k]; });
514
- placeholders = keys.map(function () { return '?'; }).join(', ');
515
- sql = "INSERT INTO `".concat(table, "` (").concat(keys.join(', '), ") VALUES (").concat(placeholders, ")");
516
- return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
517
- var result;
518
- return tslib.__generator(this, function (_a) {
519
- switch (_a.label) {
520
- case 0: return [4 /*yield*/, conn.execute(sql, values)];
521
- case 1:
522
- result = (_a.sent())[0];
523
- return [2 /*return*/, result];
524
- }
525
- });
526
- }); })];
527
- case 1: return [2 /*return*/, _a.sent()];
528
- }
529
- });
530
- });
531
- };
532
- CarbonSqlExecutor.prototype.update = function (table, primary, data) {
533
- return tslib.__awaiter(this, void 0, void 0, function () {
534
- var keys, values, updates, sql;
535
- var _this = this;
536
- return tslib.__generator(this, function (_a) {
537
- switch (_a.label) {
538
- case 0:
539
- if (!primary) {
540
- throw new Error('Primary key is required for update');
541
- }
542
- keys = Object.keys(data);
543
- values = keys.map(function (k) { return data[k]; });
544
- updates = keys.map(function (k) { return "`".concat(k, "` = ?"); }).join(', ');
545
- sql = "UPDATE `".concat(table, "` SET ").concat(updates, " WHERE `").concat(primary, "` = ?");
546
- values.push(data[primary]);
547
- return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
548
- var result;
549
- return tslib.__generator(this, function (_a) {
550
- switch (_a.label) {
551
- case 0: return [4 /*yield*/, conn.execute(sql, values)];
552
- case 1:
553
- result = (_a.sent())[0];
554
- return [2 /*return*/, result];
555
- }
556
- });
557
- }); })];
558
- case 1: return [2 /*return*/, _a.sent()];
559
- }
560
- });
561
- });
562
- };
563
- CarbonSqlExecutor.prototype.delete = function (table, primary, args) {
564
- return tslib.__awaiter(this, void 0, void 0, function () {
565
- var sql;
566
- var _this = this;
567
- return tslib.__generator(this, function (_a) {
568
- switch (_a.label) {
569
- case 0:
570
- if (!primary || !(args === null || args === void 0 ? void 0 : args[primary])) {
571
- throw new Error('Primary key and value required for delete');
572
- }
573
- sql = "DELETE FROM `".concat(table, "` WHERE `").concat(primary, "` = ?");
574
- return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
575
- var result;
576
- return tslib.__generator(this, function (_a) {
577
- switch (_a.label) {
578
- case 0: return [4 /*yield*/, conn.execute(sql, [args[primary]])];
579
- case 1:
580
- result = (_a.sent())[0];
581
- return [2 /*return*/, result];
582
- }
583
- });
584
- }); })];
585
- case 1: return [2 /*return*/, _a.sent()];
586
- }
587
- });
588
- });
589
- };
590
- return CarbonSqlExecutor;
591
- }());
592
-
593
- var carbonSqlExecutor = /*#__PURE__*/Object.freeze({
594
- __proto__: null,
595
- CarbonSqlExecutor: CarbonSqlExecutor
596
- });
597
-
598
238
  function convertForRequestBody (restfulObject, tableName, C6, regexErrorHandler) {
599
239
  if (regexErrorHandler === void 0) { regexErrorHandler = alert; }
600
240
  var payload = {};
@@ -669,6 +309,221 @@ function convertForRequestBody (restfulObject, tableName, C6, regexErrorHandler)
669
309
  var _a;
670
310
  var isNode = typeof process !== 'undefined' && !!((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
671
311
 
312
+ /**
313
+ * Facade: routes API calls to SQL or HTTP executors based on runtime context.
314
+ */
315
+ function restRequest(config) {
316
+ var _this = this;
317
+ return function () {
318
+ var args_1 = [];
319
+ for (var _i = 0; _i < arguments.length; _i++) {
320
+ args_1[_i] = arguments[_i];
321
+ }
322
+ return tslib.__awaiter(_this, tslib.__spreadArray([], args_1, true), void 0, function (request) {
323
+ var SqlExecutor, executor, HttpExecutor, http;
324
+ if (request === void 0) { request = {}; }
325
+ return tslib.__generator(this, function (_a) {
326
+ switch (_a.label) {
327
+ case 0:
328
+ if (!(isNode && config.mysqlPool)) return [3 /*break*/, 2];
329
+ return [4 /*yield*/, Promise.resolve().then(function () { return SqlExecutor$1; })];
330
+ case 1:
331
+ SqlExecutor = (_a.sent()).SqlExecutor;
332
+ executor = new SqlExecutor(config, request);
333
+ return [2 /*return*/, executor.execute()];
334
+ case 2: return [4 /*yield*/, Promise.resolve().then(function () { return HttpExecutor$1; })];
335
+ case 3:
336
+ HttpExecutor = (_a.sent()).HttpExecutor;
337
+ http = new HttpExecutor(config, request);
338
+ return [2 /*return*/, http.execute()];
339
+ }
340
+ });
341
+ });
342
+ };
343
+ }
344
+
345
+ function timeout(shouldContinueAfterTimeout, cb, timeoutMs) {
346
+ if (timeoutMs === void 0) { timeoutMs = 3000; }
347
+ var timer = function () { return setTimeout(function () {
348
+ if (false === shouldContinueAfterTimeout()) {
349
+ return;
350
+ }
351
+ cb();
352
+ }, timeoutMs); };
353
+ var timerId = timer();
354
+ return function () {
355
+ clearTimeout(timerId);
356
+ };
357
+ }
358
+
359
+ function buildBooleanJoinedConditions(set, andMode) {
360
+ if (andMode === void 0) { andMode = true; }
361
+ var booleanOperator = andMode ? 'AND' : 'OR';
362
+ var sql = '';
363
+ var OPERATORS = ['=', '!=', '<', '<=', '>', '>=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'IS', 'IS NOT'];
364
+ var isAggregateArray = function (value) { return Array.isArray(value) && typeof value[0] === 'string' && OPERATORS.includes(value[0]); };
365
+ var isNumericKeyed = function (obj) { return Array.isArray(obj) && Object.keys(obj).every(function (k) { return /^\d+$/.test(k); }); };
366
+ // todo - we should be doing something with value no????
367
+ var addCondition = function (column, op, _value) {
368
+ var paramName = column.replace(/\W+/g, '_');
369
+ return "(".concat(column, " ").concat(op, " :").concat(paramName, ")");
370
+ };
371
+ if (isNumericKeyed(set)) {
372
+ switch (set.length) {
373
+ case 2:
374
+ sql += addCondition(set[0], '=', set[1]);
375
+ break;
376
+ case 3:
377
+ if (!OPERATORS.includes(set[1])) {
378
+ throw new Error("Invalid operator: ".concat(set[1]));
379
+ }
380
+ sql += addCondition(set[0], set[1], set[2]);
381
+ break;
382
+ default:
383
+ throw new Error("Invalid array condition: ".concat(JSON.stringify(set)));
384
+ }
385
+ }
386
+ else {
387
+ var parts = [];
388
+ for (var _i = 0, _a = Object.entries(set); _i < _a.length; _i++) {
389
+ var _b = _a[_i], key = _b[0], value = _b[1];
390
+ if (/^\d+$/.test(key)) {
391
+ parts.push(buildBooleanJoinedConditions(value, !andMode));
392
+ continue;
393
+ }
394
+ if (!Array.isArray(value) || isAggregateArray(value)) {
395
+ parts.push(addCondition(key, '='));
396
+ continue;
397
+ }
398
+ if (value.length === 2 && OPERATORS.includes(value[0])) {
399
+ parts.push(addCondition(key, value[0], value[1]));
400
+ }
401
+ else if (value.length === 1 && isAggregateArray(value[0])) {
402
+ parts.push(addCondition(key, '=', value[0]));
403
+ }
404
+ else {
405
+ throw new Error("Invalid condition for ".concat(key, ": ").concat(JSON.stringify(value)));
406
+ }
407
+ }
408
+ sql = parts.join(" ".concat(booleanOperator, " "));
409
+ }
410
+ return "(".concat(sql, ")");
411
+ }
412
+ function buildAggregateField(field) {
413
+ if (typeof field === 'string')
414
+ return field;
415
+ if (!Array.isArray(field))
416
+ throw new Error('Invalid SELECT entry: must be string or array');
417
+ var agg = field[0], args = field.slice(1);
418
+ switch (agg) {
419
+ case 'COUNT':
420
+ return "COUNT(".concat(args[0] || '*', ")");
421
+ case 'SUM':
422
+ case 'AVG':
423
+ case 'MIN':
424
+ case 'MAX':
425
+ return "".concat(agg, "(").concat(args[0], ")").concat(args[1] ? " AS ".concat(args[1]) : '');
426
+ case 'DISTINCT':
427
+ return "DISTINCT(".concat(args[0], ")").concat(args[1] ? " AS ".concat(args[1]) : '');
428
+ case 'GROUP_CONCAT': {
429
+ var col = args[0], alias = args[1], sortCol = args[2], sortType = args[3];
430
+ var order = sortCol ? " ORDER BY ".concat(sortCol, " ").concat(sortType || 'ASC') : '';
431
+ return "GROUP_CONCAT(DISTINCT ".concat(col).concat(order, " SEPARATOR ',')").concat(alias ? " AS ".concat(alias) : '');
432
+ }
433
+ case 'AS': {
434
+ var col = args[0], alias = args[1];
435
+ return "".concat(col, " AS ").concat(alias);
436
+ }
437
+ case 'CONVERT_TZ': {
438
+ var ts = args[0], fromTz = args[1], toTz = args[2];
439
+ return "CONVERT_TZ(".concat(ts, ", ").concat(fromTz, ", ").concat(toTz, ")");
440
+ }
441
+ case 'NOW':
442
+ return 'NOW()';
443
+ default:
444
+ throw new Error("Unsupported aggregate: ".concat(agg));
445
+ }
446
+ }
447
+ function buildSelectQuery(table, primary, args, isSubSelect) {
448
+ var _a, _b;
449
+ if (isSubSelect === void 0) { isSubSelect = false; }
450
+ var selectList = (_a = args === null || args === void 0 ? void 0 : args[C6Constants.SELECT]) !== null && _a !== void 0 ? _a : ['*'];
451
+ var selectFields = Array.isArray(selectList)
452
+ ? selectList.map(function (f) { return buildAggregateField(f); }).join(', ')
453
+ : '*';
454
+ var sql = "SELECT ".concat(selectFields, " FROM `").concat(table, "`");
455
+ if (args === null || args === void 0 ? void 0 : args[C6Constants.JOIN]) {
456
+ var joins = args[C6Constants.JOIN];
457
+ for (var joinType in joins) {
458
+ var joinKeyword = joinType.replace('_', ' ').toUpperCase();
459
+ for (var joinTable in joins[joinType]) {
460
+ var onClause = buildBooleanJoinedConditions(joins[joinType][joinTable]);
461
+ sql += " ".concat(joinKeyword, " JOIN `").concat(joinTable, "` ON ").concat(onClause);
462
+ }
463
+ }
464
+ }
465
+ if (args === null || args === void 0 ? void 0 : args[C6Constants.WHERE]) {
466
+ sql += " WHERE ".concat(buildBooleanJoinedConditions(args[C6Constants.WHERE]));
467
+ }
468
+ if (args === null || args === void 0 ? void 0 : args[C6Constants.GROUP_BY]) {
469
+ var groupByFields = Array.isArray(args[C6Constants.GROUP_BY]) ? args[C6Constants.GROUP_BY].join(', ') : args[C6Constants.GROUP_BY];
470
+ sql += " GROUP BY ".concat(groupByFields);
471
+ }
472
+ if (args === null || args === void 0 ? void 0 : args[C6Constants.HAVING]) {
473
+ sql += " HAVING ".concat(buildBooleanJoinedConditions(args[C6Constants.HAVING]));
474
+ }
475
+ if (args === null || args === void 0 ? void 0 : args[C6Constants.PAGINATION]) {
476
+ var p = args[C6Constants.PAGINATION];
477
+ var limitClause = '';
478
+ if (p[C6Constants.ORDER]) {
479
+ var orderArray = Object.entries(p[C6Constants.ORDER]).map(function (_a) {
480
+ var col = _a[0], dir = _a[1];
481
+ if (!['ASC', 'DESC'].includes(String(dir).toUpperCase())) {
482
+ throw new Error("Invalid order direction: ".concat(dir));
483
+ }
484
+ return "".concat(col, " ").concat(String(dir).toUpperCase());
485
+ });
486
+ sql += " ORDER BY ".concat(orderArray.join(', '));
487
+ }
488
+ else if (primary) {
489
+ sql += " ORDER BY ".concat(primary, " DESC");
490
+ } /*else {
491
+ // todo - this is wrong
492
+ const primaryKey = C6Constants.TABLES['users'].PRIMARY_SHORT?.[0] ?? 'user_id';
493
+ sql += ` ORDER BY ${primaryKey} DESC`;
494
+ }*/
495
+ if (p[C6Constants.LIMIT] != null) {
496
+ var limit = parseInt(p[C6Constants.LIMIT], 10);
497
+ if (isNaN(limit) || limit < 0) {
498
+ throw new Error("Invalid LIMIT: ".concat(p[C6Constants.LIMIT]));
499
+ }
500
+ var page = parseInt((_b = p[C6Constants.PAGE]) !== null && _b !== void 0 ? _b : 1, 10);
501
+ if (isNaN(page) || page < 1) {
502
+ throw new Error("PAGE must be >= 1 (got ".concat(p[C6Constants.PAGE], ")"));
503
+ }
504
+ var offset = (page - 1) * limit;
505
+ limitClause += " LIMIT ".concat(offset, ", ").concat(limit);
506
+ }
507
+ sql += limitClause;
508
+ }
509
+ else if (!isSubSelect && primary) {
510
+ sql += " ORDER BY ".concat(primary, " ASC LIMIT 1");
511
+ }
512
+ else if (!isSubSelect && !primary) {
513
+ sql += " ORDER BY id ASC LIMIT 100"; // fallback default limit
514
+ }
515
+ return sql;
516
+ }
517
+
518
+ var Executor = /** @class */ (function () {
519
+ function Executor(config, request) {
520
+ if (request === void 0) { request = {}; }
521
+ this.config = config;
522
+ this.request = request;
523
+ }
524
+ return Executor;
525
+ }());
526
+
672
527
  function getEnvVar(key, fallback) {
673
528
  if (fallback === void 0) { fallback = ''; }
674
529
  // Vite-style injection
@@ -693,6 +548,39 @@ var isTest = getEnvVar('JEST_WORKER_ID') || getEnvVar('NODE_ENV') === 'test'
693
548
  var envVerbose = getEnvVar('VERBOSE') || getEnvVar('REACT_APP_VERBOSE') || getEnvVar('VITE_VERBOSE') || '';
694
549
  var isVerbose = ['true', '1', 'yes', 'on'].includes(envVerbose.toLowerCase());
695
550
 
551
+ exports.eFetchDependencies = void 0;
552
+ (function (eFetchDependencies) {
553
+ eFetchDependencies[eFetchDependencies["NONE"] = 0] = "NONE";
554
+ eFetchDependencies[eFetchDependencies["REFERENCED"] = 1] = "REFERENCED";
555
+ eFetchDependencies[eFetchDependencies["CHILDREN"] = 1] = "CHILDREN";
556
+ eFetchDependencies[eFetchDependencies["REFERENCES"] = 2] = "REFERENCES";
557
+ eFetchDependencies[eFetchDependencies["PARENTS"] = 2] = "PARENTS";
558
+ eFetchDependencies[eFetchDependencies["ALL"] = 3] = "ALL";
559
+ eFetchDependencies[eFetchDependencies["C6ENTITY"] = 4] = "C6ENTITY";
560
+ eFetchDependencies[eFetchDependencies["RECURSIVE"] = 8] = "RECURSIVE";
561
+ })(exports.eFetchDependencies || (exports.eFetchDependencies = {}));
562
+
563
+ /**
564
+ * the first argument ....
565
+ *
566
+ * Our api returns a zero argument function iff the method is get and the previous request reached the predefined limit.
567
+ * This function can be aliased as GetNextPageOfResults(). If the end is reached undefined will be returned.
568
+ *
569
+ *
570
+ * For POST, PUT, and DELETE requests one can expect the primary key of the new or modified index, or a boolean success
571
+ * indication if no primary key exists.
572
+ **/
573
+ var POST = 'POST';
574
+ var PUT = 'PUT';
575
+ var GET = 'GET';
576
+ var DELETE = 'DELETE';
577
+ function isPromise(x) {
578
+ return Object(x).constructor === Promise;
579
+ }
580
+ function extendedTypeHints() {
581
+ return function (argv) { return restRequest(argv); };
582
+ }
583
+
696
584
  var toastOptions = {
697
585
  position: "bottom-left",
698
586
  autoClose: 10000,
@@ -733,6 +621,12 @@ function TestRestfulResponse(response, success, error) {
733
621
  }
734
622
  return false;
735
623
  }
624
+ function removePrefixIfExists(tableName, prefix) {
625
+ if (tableName.startsWith(prefix.toLowerCase())) {
626
+ return tableName.slice(prefix.length);
627
+ }
628
+ return tableName;
629
+ }
736
630
  function removeInvalidKeys(request, c6Tables) {
737
631
  var intersection = {};
738
632
  var restfulObjectKeys = [];
@@ -750,32 +644,17 @@ function removeInvalidKeys(request, c6Tables) {
750
644
  isTest || console.log('intersection', intersection);
751
645
  return intersection;
752
646
  }
753
- exports.eFetchDependencies = void 0;
754
- (function (eFetchDependencies) {
755
- eFetchDependencies[eFetchDependencies["NONE"] = 0] = "NONE";
756
- eFetchDependencies[eFetchDependencies["REFERENCED"] = 1] = "REFERENCED";
757
- eFetchDependencies[eFetchDependencies["CHILDREN"] = 1] = "CHILDREN";
758
- eFetchDependencies[eFetchDependencies["REFERENCES"] = 2] = "REFERENCES";
759
- eFetchDependencies[eFetchDependencies["PARENTS"] = 2] = "PARENTS";
760
- eFetchDependencies[eFetchDependencies["ALL"] = 3] = "ALL";
761
- eFetchDependencies[eFetchDependencies["C6ENTITY"] = 4] = "C6ENTITY";
762
- eFetchDependencies[eFetchDependencies["RECURSIVE"] = 8] = "RECURSIVE";
763
- })(exports.eFetchDependencies || (exports.eFetchDependencies = {}));
647
+
764
648
  // do not remove entries from this array. It is used to track the progress of API requests.
765
649
  // position in array is important. Do not sort. To not add to begging.
766
- var apiRequestCache = [];
767
- var userCustomClearCache = [];
768
- function checkAllRequestsComplete() {
769
- var stillRunning = apiRequestCache.filter(function (cache) { return undefined === cache.response; });
770
- if (stillRunning.length !== 0) {
771
- if (document === null || document === undefined) {
772
- throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(apiRequestCache) + ')');
773
- }
774
- // when requests return emtpy sets in full renders, it may not be possible to track their progress.
775
- console.warn('stillRunning...', stillRunning);
776
- return stillRunning.map(function (cache) { return cache.requestArgumentsSerialized; });
650
+ exports.apiRequestCache = [];
651
+ exports.userCustomClearCache = [];
652
+ function clearCache(props) {
653
+ if (false === (props === null || props === void 0 ? void 0 : props.ignoreWarning)) {
654
+ console.warn('The rest api clearCache should only be used with extreme care! Avoid using this in favor of using `cacheResults : boolean`.');
777
655
  }
778
- return true;
656
+ exports.userCustomClearCache.map(function (f) { return 'function' === typeof f && f(); });
657
+ exports.userCustomClearCache = exports.apiRequestCache = [];
779
658
  }
780
659
  function checkCache(cacheResult, requestMethod, tableName, request) {
781
660
  var _a, _b, _c;
@@ -798,6 +677,7 @@ function checkCache(cacheResult, requestMethod, tableName, request) {
798
677
  }
799
678
  return false;
800
679
  }
680
+
801
681
  function sortAndSerializeQueryObject(tables, query) {
802
682
  var orderedQuery = Object.keys(query).sort().reduce(function (obj, key) {
803
683
  obj[key] = query[key];
@@ -805,84 +685,79 @@ function sortAndSerializeQueryObject(tables, query) {
805
685
  }, {});
806
686
  return tables + ' ' + JSON.stringify(orderedQuery);
807
687
  }
808
- function clearCache(props) {
809
- if (false === (props === null || props === void 0 ? void 0 : props.ignoreWarning)) {
810
- console.warn('The rest api clearCache should only be used with extreme care! Avoid using this in favor of using `cacheResults : boolean`.');
811
- }
812
- userCustomClearCache.map(function (f) { return 'function' === typeof f && f(); });
813
- userCustomClearCache = apiRequestCache = [];
814
- }
815
- function removePrefixIfExists(tableName, prefix) {
816
- if (tableName.startsWith(prefix.toLowerCase())) {
817
- return tableName.slice(prefix.length);
818
- }
819
- return tableName;
820
- }
821
- /**
822
- * the first argument ....
823
- *
824
- * Our api returns a zero argument function iff the method is get and the previous request reached the predefined limit.
825
- * This function can be aliased as GetNextPageOfResults(). If the end is reached undefined will be returned.
826
- *
827
- *
828
- * For POST, PUT, and DELETE requests one can expect the primary key of the new or modified index, or a boolean success
829
- * indication if no primary key exists.
830
- **/
831
- var POST = 'POST';
832
- var PUT = 'PUT';
833
- var GET = 'GET';
834
- var DELETE = 'DELETE';
835
- function isPromise(x) {
836
- return Object(x).constructor === Promise;
837
- }
838
- function extendedTypeHints() {
839
- return function (argv) { return restRequest(argv); };
840
- }
841
- function restRequest(_a) {
842
- var _this = this;
843
- var C6 = _a.C6, _b = _a.axios, axios = _b === void 0 ? axiosInstance : _b, _c = _a.restURL, restURL = _c === void 0 ? '/rest/' : _c, mysqlPool = _a.mysqlPool, _d = _a.withCredentials, withCredentials = _d === void 0 ? true : _d, tableName = _a.tableName, _e = _a.requestMethod, requestMethod = _e === void 0 ? GET : _e, _f = _a.queryCallback, queryCallback = _f === void 0 ? {} : _f, responseCallback = _a.responseCallback, _g = _a.skipPrimaryCheck, skipPrimaryCheck = _g === void 0 ? false : _g, _h = _a.clearCache, clearCache = _h === void 0 ? undefined : _h;
844
- var fullTableList = Array.isArray(tableName) ? tableName : [tableName];
845
- var operatingTableFullName = fullTableList[0];
846
- var operatingTable = removePrefixIfExists(operatingTableFullName, C6.PREFIX);
847
- var tables = fullTableList.join(',');
848
- switch (requestMethod) {
849
- case GET:
850
- case POST:
851
- case PUT:
852
- case DELETE:
853
- break;
854
- default:
855
- throw Error('Bad request method passed to getApi');
856
- }
857
- if (null !== clearCache || undefined !== clearCache) {
858
- userCustomClearCache[tables + requestMethod] = clearCache;
688
+
689
+ var HttpExecutor = /** @class */ (function (_super) {
690
+ tslib.__extends(HttpExecutor, _super);
691
+ function HttpExecutor() {
692
+ return _super !== null && _super.apply(this, arguments) || this;
859
693
  }
860
- return function () {
861
- var args_1 = [];
862
- for (var _i = 0; _i < arguments.length; _i++) {
863
- args_1[_i] = arguments[_i];
864
- }
865
- return tslib.__awaiter(_this, tslib.__spreadArray([], args_1, true), void 0, function (request) {
866
- // this could return itself with a new page number, or undefined if the end is reached
867
- function apiRequest() {
868
- return tslib.__awaiter(this, void 0, void 0, function () {
869
- var querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, addBackPK, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKey, CarbonSqlExecutor, engine, removedPkValue_1, axiosActiveRequest;
870
- var _this = this;
871
- var _a, _b, _c, _d, _e, _f, _g;
872
- return tslib.__generator(this, function (_h) {
873
- switch (_h.label) {
874
- case 0:
875
- (_a = request.cacheResults) !== null && _a !== void 0 ? _a : (request.cacheResults = C6.GET === requestMethod);
694
+ HttpExecutor.prototype.execute = function () {
695
+ return tslib.__awaiter(this, void 0, void 0, function () {
696
+ var _a, C6, axios, restURL, withCredentials, tableName, requestMethod, queryCallback, responseCallback, skipPrimaryCheck, clearCache, fullTableList, operatingTableFullName, operatingTable, tables, query, apiRequest;
697
+ var _this = this;
698
+ return tslib.__generator(this, function (_b) {
699
+ switch (_b.label) {
700
+ case 0:
701
+ _a = this.config, C6 = _a.C6, axios = _a.axios, restURL = _a.restURL, withCredentials = _a.withCredentials, tableName = _a.tableName, requestMethod = _a.requestMethod, queryCallback = _a.queryCallback, responseCallback = _a.responseCallback, skipPrimaryCheck = _a.skipPrimaryCheck, clearCache = _a.clearCache;
702
+ fullTableList = Array.isArray(tableName) ? tableName : [tableName];
703
+ operatingTableFullName = fullTableList[0];
704
+ operatingTable = removePrefixIfExists(operatingTableFullName, C6.PREFIX);
705
+ tables = fullTableList.join(',');
706
+ switch (requestMethod) {
707
+ case GET:
708
+ case POST:
709
+ case PUT:
710
+ case DELETE:
711
+ break;
712
+ default:
713
+ throw Error('Bad request method passed to getApi');
714
+ }
715
+ if (null !== clearCache || undefined !== clearCache) {
716
+ exports.userCustomClearCache[tables + requestMethod] = clearCache;
717
+ }
718
+ console.groupCollapsed('%c API: (' + requestMethod + ') Request for (' + tableName + ')', 'color: #0c0');
719
+ console.log('request', this.request);
720
+ console.groupEnd();
721
+ if ('function' === typeof queryCallback) {
722
+ query = queryCallback(this.request); // obj or obj[]
723
+ }
724
+ else {
725
+ query = queryCallback;
726
+ }
727
+ if (undefined === query || null === query) {
728
+ if (this.request.debug && isDevelopment) {
729
+ reactToastify.toast.warning("DEV: queryCallback returned undefined, signaling in Custom Cache. (returning null)", toastOptionsDevs);
730
+ }
731
+ console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + tableName + ') undefined, returning null (will not fire ajax)!', 'color: #c00');
732
+ console.log('%c Returning (undefined|null) for a query would indicate a custom cache hit (outside API.tsx), thus the request should not fire.', 'color: #c00');
733
+ console.trace();
734
+ console.groupEnd();
735
+ return [2 /*return*/, null];
736
+ }
737
+ if (C6.GET === requestMethod) {
738
+ if (undefined === query[C6.PAGINATION]) {
739
+ query[C6.PAGINATION] = {};
740
+ }
741
+ query[C6.PAGINATION][C6.PAGE] = query[C6.PAGINATION][C6.PAGE] || 1;
742
+ query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
743
+ }
744
+ apiRequest = function () { return tslib.__awaiter(_this, void 0, void 0, function () {
745
+ var _a, debug, _b, cacheResults, dataInsertMultipleRows, success, _c, fetchDependencies, _d, error, querySerialized, cacheResult, cachingConfirmed, cacheCheck, cacheCheck, addBackPK, apiResponse, returnGetNextPageFunction, restRequestUri, needsConditionOrPrimaryCheck, TABLES, primaryKey, removedPkValue_1, axiosActiveRequest;
746
+ var _e;
747
+ var _this = this;
748
+ var _f, _g, _h, _j, _k, _l;
749
+ return tslib.__generator(this, function (_m) {
750
+ _a = this.request, debug = _a.debug, _b = _a.cacheResults, cacheResults = _b === void 0 ? (C6.GET === requestMethod) : _b, dataInsertMultipleRows = _a.dataInsertMultipleRows, success = _a.success, _c = _a.fetchDependencies, fetchDependencies = _c === void 0 ? exports.eFetchDependencies.NONE : _c, _d = _a.error, error = _d === void 0 ? "An unexpected API error occurred!" : _d;
876
751
  if (C6.GET === requestMethod
877
- && undefined !== ((_b = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _b === void 0 ? void 0 : _b[C6.PAGE])
752
+ && undefined !== ((_f = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _f === void 0 ? void 0 : _f[C6.PAGE])
878
753
  && 1 !== query[C6.PAGINATION][C6.PAGE]) {
879
754
  console.groupCollapsed('Request on table (' + tableName + ') is firing for page (' + query[C6.PAGINATION][C6.PAGE] + '), please wait!');
880
- console.log('Request Data (note you may see the success and/or error prompt):', request);
755
+ console.log('Request Data (note you may see the success and/or error prompt):', this.request);
881
756
  console.trace();
882
757
  console.groupEnd();
883
758
  }
884
759
  querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
885
- cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
760
+ cacheResult = exports.apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
886
761
  cachingConfirmed = false;
887
762
  // determine if we need to paginate.
888
763
  if (requestMethod === C6.GET) {
@@ -895,11 +770,11 @@ function restRequest(_a) {
895
770
  query[C6.PAGINATION][C6.PAGE] = query[C6.PAGINATION][C6.PAGE] || 1;
896
771
  query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
897
772
  // this will evaluate true most the time
898
- if (true === request.cacheResults) {
773
+ if (true === cacheResults) {
899
774
  // just find the next, non-fetched, page and return a function to request it
900
775
  if (undefined !== cacheResult) {
901
776
  do {
902
- cacheCheck = checkCache(cacheResult, requestMethod, tableName, request);
777
+ cacheCheck = checkCache(cacheResult, requestMethod, tableName, this.request);
903
778
  if (false !== cacheCheck) {
904
779
  return [2 /*return*/, cacheCheck];
905
780
  }
@@ -907,10 +782,10 @@ function restRequest(_a) {
907
782
  ++query[C6.PAGINATION][C6.PAGE];
908
783
  // this json stringify is to capture the new page number
909
784
  querySerialized = sortAndSerializeQueryObject(tables, query !== null && query !== void 0 ? query : {});
910
- cacheResult = apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
785
+ cacheResult = exports.apiRequestCache.find(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
911
786
  } while (undefined !== cacheResult);
912
- if (request.debug && isDevelopment) {
913
- reactToastify.toast.warning("DEVS: Request in cache. (" + apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; }) + "). Returning function to request page (" + query[C6.PAGINATION][C6.PAGE] + ")", toastOptionsDevs);
787
+ if (debug && isDevelopment) {
788
+ reactToastify.toast.warning("DEVS: Request in cache. (" + exports.apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; }) + "). Returning function to request page (" + query[C6.PAGINATION][C6.PAGE] + ")", toastOptionsDevs);
914
789
  }
915
790
  // @ts-ignore - this is an incorrect warning on TS, it's well typed
916
791
  return [2 /*return*/, apiRequest];
@@ -918,17 +793,17 @@ function restRequest(_a) {
918
793
  cachingConfirmed = true;
919
794
  }
920
795
  else {
921
- if (request.debug && isDevelopment) {
796
+ if (debug && isDevelopment) {
922
797
  reactToastify.toast.info("DEVS: Ignore cache was set to true.", toastOptionsDevs);
923
798
  }
924
799
  }
925
- if (request.debug && isDevelopment) {
800
+ if (debug && isDevelopment) {
926
801
  reactToastify.toast.success("DEVS: Request not in cache." + (requestMethod === C6.GET ? "Page (" + query[C6.PAGINATION][C6.PAGE] + ")." : '') + " Logging cache 2 console.", toastOptionsDevs);
927
802
  }
928
803
  }
929
- else if (request.cacheResults) { // if we are not getting, we are updating, deleting, or inserting
804
+ else if (cacheResults) { // if we are not getting, we are updating, deleting, or inserting
930
805
  if (cacheResult) {
931
- cacheCheck = checkCache(cacheResult, requestMethod, tableName, request);
806
+ cacheCheck = checkCache(cacheResult, requestMethod, tableName, this.request);
932
807
  if (false !== cacheCheck) {
933
808
  return [2 /*return*/, cacheCheck];
934
809
  }
@@ -941,7 +816,7 @@ function restRequest(_a) {
941
816
  needsConditionOrPrimaryCheck = (PUT === requestMethod || DELETE === requestMethod)
942
817
  && false === skipPrimaryCheck;
943
818
  TABLES = C6.TABLES;
944
- primaryKey = (_f = (_e = (_d = structuredClone((_c = TABLES[operatingTable]) === null || _c === void 0 ? void 0 : _c.PRIMARY)) === null || _d === void 0 ? void 0 : _d.pop()) === null || _e === void 0 ? void 0 : _e.split('.')) === null || _f === void 0 ? void 0 : _f.pop();
819
+ primaryKey = (_k = (_j = (_h = structuredClone((_g = TABLES[operatingTable]) === null || _g === void 0 ? void 0 : _g.PRIMARY)) === null || _h === void 0 ? void 0 : _h.pop()) === null || _j === void 0 ? void 0 : _j.split('.')) === null || _k === void 0 ? void 0 : _k.pop();
945
820
  if (needsConditionOrPrimaryCheck) {
946
821
  if (undefined === primaryKey) {
947
822
  if (null === query
@@ -951,17 +826,17 @@ function restRequest(_a) {
951
826
  || query[C6.WHERE].length === 0)
952
827
  || (Object.keys(query === null || query === void 0 ? void 0 : query[C6.WHERE]).length === 0)) {
953
828
  console.error(query);
954
- throw Error('Failed to parse primary key information. Query: (' + JSON.stringify(query) + ') Primary Key: (' + JSON.stringify(primaryKey) + ') TABLES[operatingTable]?.PRIMARY: (' + JSON.stringify((_g = TABLES[operatingTable]) === null || _g === void 0 ? void 0 : _g.PRIMARY) + ') for operatingTable (' + operatingTable + ').');
829
+ throw Error('Failed to parse primary key information. Query: (' + JSON.stringify(query) + ') Primary Key: (' + JSON.stringify(primaryKey) + ') TABLES[operatingTable]?.PRIMARY: (' + JSON.stringify((_l = TABLES[operatingTable]) === null || _l === void 0 ? void 0 : _l.PRIMARY) + ') for operatingTable (' + operatingTable + ').');
955
830
  }
956
831
  }
957
832
  else {
958
833
  if (undefined === query
959
834
  || null === query
960
835
  || false === primaryKey in query) {
961
- if (true === request.debug && isDevelopment) {
836
+ if (true === debug && isDevelopment) {
962
837
  reactToastify.toast.error('DEVS: The primary key (' + primaryKey + ') was not provided!!');
963
838
  }
964
- throw Error('You must provide the primary key (' + primaryKey + ') for table (' + operatingTable + '). Request (' + JSON.stringify(request, undefined, 4) + ') Query (' + JSON.stringify(query) + ')');
839
+ throw Error('You must provide the primary key (' + primaryKey + ') for table (' + operatingTable + '). Request (' + JSON.stringify(this.request, undefined, 4) + ') Query (' + JSON.stringify(query) + ')');
965
840
  }
966
841
  if (undefined === (query === null || query === void 0 ? void 0 : query[primaryKey])
967
842
  || null === (query === null || query === void 0 ? void 0 : query[primaryKey])) {
@@ -970,55 +845,6 @@ function restRequest(_a) {
970
845
  }
971
846
  }
972
847
  }
973
- if (!(isNode && mysqlPool)) return [3 /*break*/, 2];
974
- return [4 /*yield*/, Promise.resolve().then(function () { return carbonSqlExecutor; })];
975
- case 1:
976
- CarbonSqlExecutor = (_h.sent()).CarbonSqlExecutor;
977
- engine = new CarbonSqlExecutor(mysqlPool, C6);
978
- switch (requestMethod) {
979
- case GET:
980
- return [2 /*return*/, engine
981
- .select(tableName, undefined, request)
982
- .then(function (rows) {
983
- // mirror the front‐end shape
984
- var serverResponse = {
985
- rest: rows,
986
- session: undefined,
987
- sql: true,
988
- };
989
- return serverResponse;
990
- })];
991
- case POST:
992
- return [2 /*return*/, engine
993
- .insert(tableName, request)
994
- .then(function (created) { return ({
995
- rest: created,
996
- }); })];
997
- case PUT:
998
- return [2 /*return*/, engine
999
- .update(tableName, undefined, request)
1000
- .then(function (updatedResult) {
1001
- var _a;
1002
- return ({
1003
- rest: updatedResult,
1004
- rowCount: (_a = updatedResult.affectedRows) !== null && _a !== void 0 ? _a : 0,
1005
- });
1006
- })];
1007
- case DELETE:
1008
- return [2 /*return*/, engine
1009
- .delete(tableName, undefined, request)
1010
- .then(function (deletedResult) {
1011
- var _a;
1012
- return ({
1013
- rest: deletedResult,
1014
- rowCount: (_a = deletedResult.affectedRows) !== null && _a !== void 0 ? _a : 0,
1015
- deleted: true,
1016
- });
1017
- })];
1018
- default:
1019
- throw new Error("Unsupported method: ".concat(requestMethod));
1020
- }
1021
- case 2:
1022
848
  // A part of me exists that wants to remove this, but it's a good feature
1023
849
  // this allows developers the ability to cache requests based on primary key
1024
850
  // for tables like `photos` this can be a huge performance boost
@@ -1040,12 +866,12 @@ function restRequest(_a) {
1040
866
  }
1041
867
  try {
1042
868
  console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + operatingTable + ') is about to fire, will return with promise!', 'color: #A020F0');
1043
- console.log(request);
869
+ console.log(this.request);
1044
870
  console.log('%c If this is the first request for this datatype; thus the value being set is currently undefined, please remember to update the state to null.', 'color: #A020F0');
1045
871
  console.log('%c Remember undefined indicated the request has not fired, null indicates the request is firing, an empty array would signal no data was returned for the sql stmt.', 'color: #A020F0');
1046
872
  console.trace();
1047
873
  console.groupEnd();
1048
- axiosActiveRequest = axios[requestMethod.toLowerCase()].apply(axios, tslib.__spreadArray([restRequestUri], ((function () {
874
+ axiosActiveRequest = (_e = axios)[requestMethod.toLowerCase()].apply(_e, tslib.__spreadArray([restRequestUri], ((function () {
1049
875
  // @link - https://axios-http.com/docs/instance
1050
876
  // How configuration vs data is passed is variable, use documentation above for reference
1051
877
  if (requestMethod === GET) {
@@ -1055,9 +881,9 @@ function restRequest(_a) {
1055
881
  }];
1056
882
  }
1057
883
  else if (requestMethod === POST) {
1058
- if (undefined !== (request === null || request === void 0 ? void 0 : request.dataInsertMultipleRows)) {
884
+ if (undefined !== dataInsertMultipleRows) {
1059
885
  return [
1060
- request.dataInsertMultipleRows.map(function (data) {
886
+ dataInsertMultipleRows.map(function (data) {
1061
887
  return convertForRequestBody(data, fullTableList, C6, function (message) { return reactToastify.toast.error(message, toastOptions); });
1062
888
  }),
1063
889
  {
@@ -1092,7 +918,7 @@ function restRequest(_a) {
1092
918
  })()), false));
1093
919
  if (cachingConfirmed) {
1094
920
  // push to cache so we do not repeat the request
1095
- apiRequestCache.push({
921
+ exports.apiRequestCache.push({
1096
922
  requestArgumentsSerialized: querySerialized,
1097
923
  request: axiosActiveRequest
1098
924
  });
@@ -1103,11 +929,11 @@ function restRequest(_a) {
1103
929
  // returning the promise with this then is important for tests. todo - we could make that optional.
1104
930
  // https://rapidapi.com/guides/axios-async-await
1105
931
  return [2 /*return*/, axiosActiveRequest.then(function (response) { return tslib.__awaiter(_this, void 0, void 0, function () {
1106
- var cacheIndex, responseData_1, fetchDependencies, dependencies_1, fetchReferences_1, apiRequestPromises, _loop_1, _a, _b, _c, _i, tableToFetch;
932
+ var cacheIndex, responseData_1, dependencies_1, fetchReferences_1, apiRequestPromises, _loop_1, _a, _b, _c, _i, tableToFetch;
1107
933
  var _this = this;
1108
- var _d, _e, _f, _g, _h, _j, _k, _l, _m;
1109
- return tslib.__generator(this, function (_o) {
1110
- switch (_o.label) {
934
+ var _d, _e, _f, _g, _h, _j, _k;
935
+ return tslib.__generator(this, function (_l) {
936
+ switch (_l.label) {
1111
937
  case 0:
1112
938
  if (typeof response.data === 'string') {
1113
939
  if (isTest) {
@@ -1117,45 +943,43 @@ function restRequest(_a) {
1117
943
  return [2 /*return*/, Promise.reject(response)];
1118
944
  }
1119
945
  if (cachingConfirmed) {
1120
- cacheIndex = apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
1121
- apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction;
946
+ cacheIndex = exports.apiRequestCache.findIndex(function (cache) { return cache.requestArgumentsSerialized === querySerialized; });
947
+ exports.apiRequestCache[cacheIndex].final = false === returnGetNextPageFunction;
1122
948
  // only cache get method requests
1123
- apiRequestCache[cacheIndex].response = response;
949
+ exports.apiRequestCache[cacheIndex].response = response;
1124
950
  }
1125
- apiResponse = TestRestfulResponse(response, request === null || request === void 0 ? void 0 : request.success, (_d = request === null || request === void 0 ? void 0 : request.error) !== null && _d !== void 0 ? _d : "An unexpected API error occurred!");
951
+ apiResponse = TestRestfulResponse(response, success, error);
1126
952
  if (false === apiResponse) {
1127
- if (request.debug && isDevelopment) {
953
+ if (debug && isDevelopment) {
1128
954
  reactToastify.toast.warning("DEVS: TestRestfulResponse returned false for (" + operatingTable + ").", toastOptionsDevs);
1129
955
  }
1130
956
  return [2 /*return*/, response];
1131
957
  }
1132
958
  // stateful operations are done in the response callback - its leverages rest generated functions
1133
959
  if (responseCallback) {
1134
- responseCallback(response, request, apiResponse);
960
+ responseCallback(response, this.request, apiResponse);
1135
961
  }
1136
962
  if (!(C6.GET === requestMethod)) return [3 /*break*/, 6];
1137
963
  responseData_1 = response.data;
1138
- returnGetNextPageFunction = 1 !== ((_e = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _e === void 0 ? void 0 : _e[C6.LIMIT]) &&
1139
- ((_f = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _f === void 0 ? void 0 : _f[C6.LIMIT]) === responseData_1.rest.length;
964
+ returnGetNextPageFunction = 1 !== ((_d = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _d === void 0 ? void 0 : _d[C6.LIMIT]) &&
965
+ ((_e = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _e === void 0 ? void 0 : _e[C6.LIMIT]) === responseData_1.rest.length;
1140
966
  if (false === isTest || true === isVerbose) {
1141
- console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + ((_g = responseData_1.rest) === null || _g === void 0 ? void 0 : _g.length) + ') of possible (' + ((_h = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _h === void 0 ? void 0 : _h[C6.LIMIT]) + ') limit!', 'color: #0c0');
967
+ console.groupCollapsed('%c API: Response (' + requestMethod + ' ' + tableName + ') returned length (' + ((_f = responseData_1.rest) === null || _f === void 0 ? void 0 : _f.length) + ') of possible (' + ((_g = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _g === void 0 ? void 0 : _g[C6.LIMIT]) + ') limit!', 'color: #0c0');
1142
968
  console.log('%c ' + requestMethod + ' ' + tableName, 'color: #0c0');
1143
- console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', request);
969
+ console.log('%c Request Data (note you may see the success and/or error prompt):', 'color: #0c0', this.request);
1144
970
  console.log('%c Response Data:', 'color: #0c0', responseData_1.rest);
1145
- console.log('%c Will return get next page function:' + (1 !== ((_j = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _j === void 0 ? void 0 : _j[C6.LIMIT]) ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction);
971
+ console.log('%c Will return get next page function:' + (1 !== ((_h = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _h === void 0 ? void 0 : _h[C6.LIMIT]) ? '' : ' (Will not return with explicit limit 1 set)'), 'color: #0c0', true === returnGetNextPageFunction);
1146
972
  console.trace();
1147
973
  console.groupEnd();
1148
974
  }
1149
975
  if (false === returnGetNextPageFunction
1150
- && true === request.debug
976
+ && true === debug
1151
977
  && isDevelopment) {
1152
- reactToastify.toast.success("DEVS: Response returned length (" + ((_k = responseData_1.rest) === null || _k === void 0 ? void 0 : _k.length) + ") less than limit (" + ((_l = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _l === void 0 ? void 0 : _l[C6.LIMIT]) + ").", toastOptionsDevs);
978
+ reactToastify.toast.success("DEVS: Response returned length (" + ((_j = responseData_1.rest) === null || _j === void 0 ? void 0 : _j.length) + ") less than limit (" + ((_k = query === null || query === void 0 ? void 0 : query[C6.PAGINATION]) === null || _k === void 0 ? void 0 : _k[C6.LIMIT]) + ").", toastOptionsDevs);
1153
979
  }
1154
- (_m = request.fetchDependencies) !== null && _m !== void 0 ? _m : (request.fetchDependencies = exports.eFetchDependencies.NONE);
1155
- if (!(request.fetchDependencies
1156
- && 'number' === typeof request.fetchDependencies
980
+ if (!(fetchDependencies
981
+ && 'number' === typeof fetchDependencies
1157
982
  && responseData_1.rest.length > 0)) return [3 /*break*/, 6];
1158
- fetchDependencies = request.fetchDependencies;
1159
983
  console.groupCollapsed('%c API: Fetch Dependencies segment (' + requestMethod + ' ' + tableName + ')'
1160
984
  + (fetchDependencies & exports.eFetchDependencies.CHILDREN ? ' | (CHILDREN|REFERENCED) ' : '')
1161
985
  + (fetchDependencies & exports.eFetchDependencies.PARENTS ? ' | (PARENTS|REFERENCED_BY)' : '')
@@ -1226,9 +1050,9 @@ function restRequest(_a) {
1226
1050
  console.log('fetchReferences', fetchReferences_1);
1227
1051
  _loop_1 = function (tableToFetch) {
1228
1052
  var referencesTables, shouldContinue, fetchTable, RestApi, nextFetchDependencies;
1229
- var _p;
1230
- return tslib.__generator(this, function (_q) {
1231
- switch (_q.label) {
1053
+ var _m;
1054
+ return tslib.__generator(this, function (_o) {
1055
+ switch (_o.label) {
1232
1056
  case 0:
1233
1057
  if (fetchDependencies & exports.eFetchDependencies.C6ENTITY
1234
1058
  && 'string' === typeof tableName
@@ -1248,7 +1072,7 @@ function restRequest(_a) {
1248
1072
  }
1249
1073
  return [4 /*yield*/, C6.IMPORT(tableToFetch)];
1250
1074
  case 1:
1251
- fetchTable = _q.sent();
1075
+ fetchTable = _o.sent();
1252
1076
  RestApi = fetchTable.default;
1253
1077
  console.log('%c Fetch Dependencies will select (' + tableToFetch + ') using GET request', 'color: #33ccff');
1254
1078
  nextFetchDependencies = exports.eFetchDependencies.NONE;
@@ -1271,8 +1095,8 @@ function restRequest(_a) {
1271
1095
  // it not certain that they are using carbons' entities either
1272
1096
  // this is a dynamic call to the rest api, any generated table may resolve with (RestApi)
1273
1097
  // todo - using value to avoid joins.... but. maybe this should be a parameterizable option -- think race conditions; its safer to join
1274
- apiRequestPromises.push(RestApi.Get((_p = {},
1275
- _p[C6.WHERE] = {
1098
+ apiRequestPromises.push(RestApi.Get((_m = {},
1099
+ _m[C6.WHERE] = {
1276
1100
  0: Object.keys(fetchReferences_1[tableToFetch]).reduce(function (sum, column) {
1277
1101
  fetchReferences_1[tableToFetch][column] = fetchReferences_1[tableToFetch][column].flat(Infinity);
1278
1102
  if (0 === fetchReferences_1[tableToFetch][column].length) {
@@ -1287,8 +1111,8 @@ function restRequest(_a) {
1287
1111
  return sum;
1288
1112
  }, {})
1289
1113
  },
1290
- _p.fetchDependencies = nextFetchDependencies,
1291
- _p)));
1114
+ _m.fetchDependencies = nextFetchDependencies,
1115
+ _m)));
1292
1116
  return [2 /*return*/];
1293
1117
  }
1294
1118
  });
@@ -1298,7 +1122,7 @@ function restRequest(_a) {
1298
1122
  for (_c in _a)
1299
1123
  _b.push(_c);
1300
1124
  _i = 0;
1301
- _o.label = 1;
1125
+ _l.label = 1;
1302
1126
  case 1:
1303
1127
  if (!(_i < _b.length)) return [3 /*break*/, 4];
1304
1128
  _c = _b[_i];
@@ -1306,8 +1130,8 @@ function restRequest(_a) {
1306
1130
  tableToFetch = _c;
1307
1131
  return [5 /*yield**/, _loop_1(tableToFetch)];
1308
1132
  case 2:
1309
- _o.sent();
1310
- _o.label = 3;
1133
+ _l.sent();
1134
+ _l.label = 3;
1311
1135
  case 3:
1312
1136
  _i++;
1313
1137
  return [3 /*break*/, 1];
@@ -1315,16 +1139,17 @@ function restRequest(_a) {
1315
1139
  console.groupEnd();
1316
1140
  return [4 /*yield*/, Promise.all(apiRequestPromises)];
1317
1141
  case 5:
1318
- _o.sent();
1142
+ _l.sent();
1319
1143
  apiRequestPromises.map(function (promise) { return tslib.__awaiter(_this, void 0, void 0, function () {
1320
1144
  var _a, _b;
1321
1145
  return tslib.__generator(this, function (_c) {
1322
1146
  switch (_c.label) {
1323
1147
  case 0:
1324
- if (!Array.isArray(request.fetchDependencies)) {
1325
- request.fetchDependencies = [];
1148
+ if (!Array.isArray(this.request.fetchDependencies)) {
1149
+ // to reassign value we must ref the root
1150
+ this.request.fetchDependencies = [];
1326
1151
  }
1327
- _b = (_a = request.fetchDependencies).push;
1152
+ _b = (_a = this.request.fetchDependencies).push;
1328
1153
  return [4 /*yield*/, promise];
1329
1154
  case 1:
1330
1155
  _b.apply(_a, [_c.sent()]);
@@ -1332,9 +1157,9 @@ function restRequest(_a) {
1332
1157
  }
1333
1158
  });
1334
1159
  }); });
1335
- _o.label = 6;
1160
+ _l.label = 6;
1336
1161
  case 6:
1337
- if (request.debug && isDevelopment) {
1162
+ if (debug && isDevelopment) {
1338
1163
  reactToastify.toast.success("DEVS: (" + requestMethod + ") request complete.", toastOptionsDevs);
1339
1164
  }
1340
1165
  return [2 /*return*/, response];
@@ -1342,98 +1167,282 @@ function restRequest(_a) {
1342
1167
  });
1343
1168
  }); })];
1344
1169
  }
1345
- catch (error) {
1170
+ catch (throwableError) {
1346
1171
  if (isTest) {
1347
- throw new Error(JSON.stringify(error));
1172
+ throw new Error(JSON.stringify(throwableError));
1348
1173
  }
1349
1174
  console.groupCollapsed('%c API: An error occurred in the try catch block. returning null!', 'color: #ff0000');
1350
1175
  console.log('%c ' + requestMethod + ' ' + tableName, 'color: #A020F0');
1351
- console.warn(error);
1176
+ console.warn(throwableError);
1352
1177
  console.trace();
1353
1178
  console.groupEnd();
1354
- TestRestfulResponse(error, request === null || request === void 0 ? void 0 : request.success, (request === null || request === void 0 ? void 0 : request.error) || "An restful API error occurred!");
1179
+ TestRestfulResponse(throwableError, success, error);
1355
1180
  return [2 /*return*/, null];
1356
1181
  }
1357
1182
  return [2 /*return*/];
1358
- }
1359
- });
1360
- });
1361
- }
1362
- var query;
1363
- if (request === void 0) { request = {}; }
1183
+ });
1184
+ }); };
1185
+ return [4 /*yield*/, apiRequest()];
1186
+ case 1: return [2 /*return*/, _b.sent()];
1187
+ }
1188
+ });
1189
+ });
1190
+ };
1191
+ return HttpExecutor;
1192
+ }(Executor));
1193
+
1194
+ var HttpExecutor$1 = /*#__PURE__*/Object.freeze({
1195
+ __proto__: null,
1196
+ HttpExecutor: HttpExecutor
1197
+ });
1198
+
1199
+ var SqlExecutor = /** @class */ (function (_super) {
1200
+ tslib.__extends(SqlExecutor, _super);
1201
+ function SqlExecutor() {
1202
+ return _super !== null && _super.apply(this, arguments) || this;
1203
+ }
1204
+ SqlExecutor.prototype.execute = function () {
1205
+ switch (this.config.requestMethod) {
1206
+ case 'GET':
1207
+ return this.select(this.config.tableName, undefined, this.request).then(function (rows) { return ({ rest: rows }); });
1208
+ case 'POST':
1209
+ return this.insert(this.config.tableName, this.request);
1210
+ case 'PUT':
1211
+ return this.update(this.config.tableName, undefined, this.request);
1212
+ case 'DELETE':
1213
+ return this.delete(this.config.tableName, undefined, this.request);
1214
+ }
1215
+ };
1216
+ SqlExecutor.prototype.withConnection = function (cb) {
1217
+ return tslib.__awaiter(this, void 0, void 0, function () {
1218
+ var conn;
1219
+ return tslib.__generator(this, function (_a) {
1220
+ switch (_a.label) {
1221
+ case 0: return [4 /*yield*/, this.config.mysqlPool.getConnection()];
1222
+ case 1:
1223
+ conn = _a.sent();
1224
+ _a.label = 2;
1225
+ case 2:
1226
+ _a.trys.push([2, , 4, 5]);
1227
+ return [4 /*yield*/, cb(conn)];
1228
+ case 3: return [2 /*return*/, _a.sent()];
1229
+ case 4:
1230
+ conn.release();
1231
+ return [7 /*endfinally*/];
1232
+ case 5: return [2 /*return*/];
1233
+ }
1234
+ });
1235
+ });
1236
+ };
1237
+ SqlExecutor.prototype.select = function (table, primary, args) {
1238
+ return tslib.__awaiter(this, void 0, void 0, function () {
1239
+ var sql;
1240
+ var _this = this;
1364
1241
  return tslib.__generator(this, function (_a) {
1365
1242
  switch (_a.label) {
1366
1243
  case 0:
1367
- console.groupCollapsed('%c API: (' + requestMethod + ') Request for (' + operatingTable + ')', 'color: #0c0');
1368
- console.log('request', request);
1369
- console.groupEnd();
1370
- if ('function' === typeof queryCallback) {
1371
- query = queryCallback(request); // obj or obj[]
1372
- }
1373
- else {
1374
- query = queryCallback;
1375
- }
1376
- if (undefined === query || null === query) {
1377
- if (request.debug && isDevelopment) {
1378
- reactToastify.toast.warning("DEV: queryCallback returned undefined, signaling in Custom Cache. (returning null)", toastOptionsDevs);
1379
- }
1380
- console.groupCollapsed('%c API: (' + requestMethod + ') Request Query for (' + operatingTable + ') undefined, returning null (will not fire ajax)!', 'color: #c00');
1381
- console.log('%c Returning (undefined|null) for a query would indicate a custom cache hit (outside API.tsx), thus the request should not fire.', 'color: #c00');
1382
- console.trace();
1383
- console.groupEnd();
1384
- return [2 /*return*/, null];
1244
+ sql = buildSelectQuery(table, primary, args);
1245
+ return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
1246
+ var rows;
1247
+ return tslib.__generator(this, function (_a) {
1248
+ switch (_a.label) {
1249
+ case 0:
1250
+ console.log(sql);
1251
+ return [4 /*yield*/, conn.query(sql)];
1252
+ case 1:
1253
+ rows = (_a.sent())[0];
1254
+ return [2 /*return*/, rows];
1255
+ }
1256
+ });
1257
+ }); })];
1258
+ case 1: return [2 /*return*/, _a.sent()];
1259
+ }
1260
+ });
1261
+ });
1262
+ };
1263
+ SqlExecutor.prototype.insert = function (table, data) {
1264
+ return tslib.__awaiter(this, void 0, void 0, function () {
1265
+ var keys, values, placeholders, sql;
1266
+ var _this = this;
1267
+ return tslib.__generator(this, function (_a) {
1268
+ switch (_a.label) {
1269
+ case 0:
1270
+ keys = Object.keys(data);
1271
+ values = keys.map(function (k) { return data[k]; });
1272
+ placeholders = keys.map(function () { return '?'; }).join(', ');
1273
+ sql = "INSERT INTO `".concat(table, "` (").concat(keys.join(', '), ") VALUES (").concat(placeholders, ")");
1274
+ return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
1275
+ var result;
1276
+ return tslib.__generator(this, function (_a) {
1277
+ switch (_a.label) {
1278
+ case 0: return [4 /*yield*/, conn.execute(sql, values)];
1279
+ case 1:
1280
+ result = (_a.sent())[0];
1281
+ return [2 /*return*/, result];
1282
+ }
1283
+ });
1284
+ }); })];
1285
+ case 1: return [2 /*return*/, _a.sent()];
1286
+ }
1287
+ });
1288
+ });
1289
+ };
1290
+ SqlExecutor.prototype.update = function (table, primary, data) {
1291
+ return tslib.__awaiter(this, void 0, void 0, function () {
1292
+ var keys, values, updates, sql;
1293
+ var _this = this;
1294
+ return tslib.__generator(this, function (_a) {
1295
+ switch (_a.label) {
1296
+ case 0:
1297
+ if (!primary) {
1298
+ throw new Error('Primary key is required for update');
1385
1299
  }
1386
- if (C6.GET === requestMethod) {
1387
- if (undefined === query[C6.PAGINATION]) {
1388
- query[C6.PAGINATION] = {};
1389
- }
1390
- query[C6.PAGINATION][C6.PAGE] = query[C6.PAGINATION][C6.PAGE] || 1;
1391
- query[C6.PAGINATION][C6.LIMIT] = query[C6.PAGINATION][C6.LIMIT] || 100;
1300
+ keys = Object.keys(data);
1301
+ values = keys.map(function (k) { return data[k]; });
1302
+ updates = keys.map(function (k) { return "`".concat(k, "` = ?"); }).join(', ');
1303
+ sql = "UPDATE `".concat(table, "` SET ").concat(updates, " WHERE `").concat(primary, "` = ?");
1304
+ values.push(data[primary]);
1305
+ return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
1306
+ var result;
1307
+ return tslib.__generator(this, function (_a) {
1308
+ switch (_a.label) {
1309
+ case 0: return [4 /*yield*/, conn.execute(sql, values)];
1310
+ case 1:
1311
+ result = (_a.sent())[0];
1312
+ return [2 /*return*/, result];
1313
+ }
1314
+ });
1315
+ }); })];
1316
+ case 1: return [2 /*return*/, _a.sent()];
1317
+ }
1318
+ });
1319
+ });
1320
+ };
1321
+ SqlExecutor.prototype.delete = function (table, primary, args) {
1322
+ return tslib.__awaiter(this, void 0, void 0, function () {
1323
+ var sql;
1324
+ var _this = this;
1325
+ return tslib.__generator(this, function (_a) {
1326
+ switch (_a.label) {
1327
+ case 0:
1328
+ if (!primary || !(args === null || args === void 0 ? void 0 : args[primary])) {
1329
+ throw new Error('Primary key and value required for delete');
1392
1330
  }
1393
- return [4 /*yield*/, apiRequest()];
1331
+ sql = "DELETE FROM `".concat(table, "` WHERE `").concat(primary, "` = ?");
1332
+ return [4 /*yield*/, this.withConnection(function (conn) { return tslib.__awaiter(_this, void 0, void 0, function () {
1333
+ var result;
1334
+ return tslib.__generator(this, function (_a) {
1335
+ switch (_a.label) {
1336
+ case 0: return [4 /*yield*/, conn.execute(sql, [args[primary]])];
1337
+ case 1:
1338
+ result = (_a.sent())[0];
1339
+ return [2 /*return*/, result];
1340
+ }
1341
+ });
1342
+ }); })];
1394
1343
  case 1: return [2 /*return*/, _a.sent()];
1395
1344
  }
1396
1345
  });
1397
1346
  });
1398
1347
  };
1348
+ return SqlExecutor;
1349
+ }(Executor));
1350
+
1351
+ var SqlExecutor$1 = /*#__PURE__*/Object.freeze({
1352
+ __proto__: null,
1353
+ SqlExecutor: SqlExecutor
1354
+ });
1355
+
1356
+ /**
1357
+ * Conditionally group a log if verbose.
1358
+ */
1359
+ function group(title, data) {
1360
+ if (!isVerbose)
1361
+ return;
1362
+ console.groupCollapsed("%c".concat(title), "color: #007acc");
1363
+ if (data !== undefined)
1364
+ console.log(data);
1365
+ console.groupEnd();
1366
+ }
1367
+ function info(message) {
1368
+ var optional = [];
1369
+ for (var _i = 1; _i < arguments.length; _i++) {
1370
+ optional[_i - 1] = arguments[_i];
1371
+ }
1372
+ if (!isVerbose)
1373
+ return;
1374
+ console.info.apply(console, tslib.__spreadArray(["%cINFO: ".concat(message), "color: #0a0"], optional, false));
1375
+ }
1376
+ function warn(message) {
1377
+ var optional = [];
1378
+ for (var _i = 1; _i < arguments.length; _i++) {
1379
+ optional[_i - 1] = arguments[_i];
1380
+ }
1381
+ console.warn.apply(console, tslib.__spreadArray(["%cWARN: ".concat(message), "color: #e90"], optional, false));
1382
+ }
1383
+ function error(message) {
1384
+ var optional = [];
1385
+ for (var _i = 1; _i < arguments.length; _i++) {
1386
+ optional[_i - 1] = arguments[_i];
1387
+ }
1388
+ console.error.apply(console, tslib.__spreadArray(["%cERROR: ".concat(message), "color: #c00"], optional, false));
1399
1389
  }
1400
1390
 
1401
- function timeout(shouldContinueAfterTimeout, cb, timeoutMs) {
1402
- if (timeoutMs === void 0) { timeoutMs = 3000; }
1403
- var timer = function () { return setTimeout(function () {
1404
- if (false === shouldContinueAfterTimeout()) {
1405
- return;
1391
+ function checkAllRequestsComplete() {
1392
+ var stillRunning = exports.apiRequestCache.filter(function (cache) { return undefined === cache.response; });
1393
+ if (stillRunning.length !== 0) {
1394
+ if (document === null || document === undefined) {
1395
+ throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(exports.apiRequestCache) + ')');
1406
1396
  }
1407
- cb();
1408
- }, timeoutMs); };
1409
- var timerId = timer();
1410
- return function () {
1411
- clearTimeout(timerId);
1412
- };
1397
+ // when requests return emtpy sets in full renders, it may not be possible to track their progress.
1398
+ console.warn('stillRunning...', stillRunning);
1399
+ return stillRunning.map(function (cache) { return cache.requestArgumentsSerialized; });
1400
+ }
1401
+ return true;
1402
+ }
1403
+
1404
+ function onSuccess(message) {
1405
+ reactToastify.toast.success(message, isDevelopment ? toastOptionsDevs : toastOptions);
1406
+ }
1407
+ function onError(message) {
1408
+ reactToastify.toast.error(message, isDevelopment ? toastOptionsDevs : toastOptions);
1413
1409
  }
1414
1410
 
1415
1411
  exports.C6Constants = C6Constants;
1416
- exports.CarbonSqlExecutor = CarbonSqlExecutor;
1417
1412
  exports.DELETE = DELETE;
1413
+ exports.Executor = Executor;
1418
1414
  exports.GET = GET;
1415
+ exports.HttpExecutor = HttpExecutor;
1419
1416
  exports.POST = POST;
1420
1417
  exports.PUT = PUT;
1418
+ exports.SqlExecutor = SqlExecutor;
1421
1419
  exports.TestRestfulResponse = TestRestfulResponse;
1422
1420
  exports.axiosInstance = axiosInstance;
1421
+ exports.buildAggregateField = buildAggregateField;
1422
+ exports.buildBooleanJoinedConditions = buildBooleanJoinedConditions;
1423
+ exports.buildSelectQuery = buildSelectQuery;
1423
1424
  exports.checkAllRequestsComplete = checkAllRequestsComplete;
1425
+ exports.checkCache = checkCache;
1424
1426
  exports.clearCache = clearCache;
1425
1427
  exports.convertForRequestBody = convertForRequestBody;
1428
+ exports.error = error;
1426
1429
  exports.extendedTypeHints = extendedTypeHints;
1427
1430
  exports.getEnvVar = getEnvVar;
1431
+ exports.group = group;
1432
+ exports.info = info;
1428
1433
  exports.isLocal = isDevelopment;
1429
1434
  exports.isNode = isNode;
1430
1435
  exports.isPromise = isPromise;
1431
1436
  exports.isTest = isTest;
1432
1437
  exports.isVerbose = isVerbose;
1438
+ exports.onError = onError;
1439
+ exports.onSuccess = onSuccess;
1433
1440
  exports.removeInvalidKeys = removeInvalidKeys;
1434
1441
  exports.removePrefixIfExists = removePrefixIfExists;
1435
1442
  exports.restRequest = restRequest;
1443
+ exports.sortAndSerializeQueryObject = sortAndSerializeQueryObject;
1436
1444
  exports.timeout = timeout;
1437
1445
  exports.toastOptions = toastOptions;
1438
1446
  exports.toastOptionsDevs = toastOptionsDevs;
1447
+ exports.warn = warn;
1439
1448
  //# sourceMappingURL=index.cjs.js.map