@fjell/core 4.4.2 → 4.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/.kodrdriv/config.yaml +10 -0
  2. package/.kodrdriv/context/content.md +1 -0
  3. package/commit.sh +8 -0
  4. package/dist/cjs/AItemService.js +42 -0
  5. package/dist/cjs/AItemService.js.map +1 -0
  6. package/dist/cjs/dictionary.js +71 -0
  7. package/dist/cjs/dictionary.js.map +1 -0
  8. package/dist/cjs/index.js +57 -0
  9. package/dist/cjs/index.js.map +1 -0
  10. package/dist/cjs/item/IFactory.js +93 -0
  11. package/dist/cjs/item/IFactory.js.map +1 -0
  12. package/dist/cjs/item/IQFactory.js +154 -0
  13. package/dist/cjs/item/IQFactory.js.map +1 -0
  14. package/dist/cjs/item/IQUtils.js +316 -0
  15. package/dist/cjs/item/IQUtils.js.map +1 -0
  16. package/dist/cjs/item/IUtils.js +76 -0
  17. package/dist/cjs/item/IUtils.js.map +1 -0
  18. package/dist/cjs/item/ItemQuery.js +10 -0
  19. package/dist/cjs/item/ItemQuery.js.map +1 -0
  20. package/dist/cjs/key/KUtils.js +298 -0
  21. package/dist/cjs/key/KUtils.js.map +1 -0
  22. package/dist/cjs/logger.js +10 -0
  23. package/dist/cjs/logger.js.map +1 -0
  24. package/dist/esm/AItemService.js.map +1 -0
  25. package/dist/esm/dictionary.js.map +1 -0
  26. package/dist/esm/item/IFactory.js.map +1 -0
  27. package/dist/esm/item/IQFactory.js.map +1 -0
  28. package/dist/esm/item/IQUtils.js.map +1 -0
  29. package/dist/esm/item/IUtils.js +69 -0
  30. package/dist/esm/item/IUtils.js.map +1 -0
  31. package/dist/esm/item/ItemQuery.js.map +1 -0
  32. package/dist/{key → esm/key}/KUtils.js +8 -8
  33. package/dist/esm/key/KUtils.js.map +1 -0
  34. package/dist/esm/logger.js.map +1 -0
  35. package/dist/index.cjs +1013 -0
  36. package/dist/index.cjs.map +1 -0
  37. package/dist/item/IUtils.d.ts +7 -3
  38. package/dist/items.d.ts +18 -10
  39. package/dist/key/KUtils.d.ts +8 -8
  40. package/package.json +24 -23
  41. package/release.sh +103 -0
  42. package/src/item/IUtils.ts +67 -55
  43. package/src/items.ts +35 -26
  44. package/src/key/KUtils.ts +13 -13
  45. package/vitest.config.ts +37 -0
  46. package/dist/AItemService.js.map +0 -1
  47. package/dist/dictionary.js.map +0 -1
  48. package/dist/item/IFactory.js.map +0 -1
  49. package/dist/item/IQFactory.js.map +0 -1
  50. package/dist/item/IQUtils.js.map +0 -1
  51. package/dist/item/IUtils.js +0 -77
  52. package/dist/item/IUtils.js.map +0 -1
  53. package/dist/item/ItemQuery.js.map +0 -1
  54. package/dist/key/KUtils.js.map +0 -1
  55. package/dist/logger.js.map +0 -1
  56. /package/dist/{AItemService.js → esm/AItemService.js} +0 -0
  57. /package/dist/{dictionary.js → esm/dictionary.js} +0 -0
  58. /package/dist/{index.js → esm/index.js} +0 -0
  59. /package/dist/{index.js.map → esm/index.js.map} +0 -0
  60. /package/dist/{item → esm/item}/IFactory.js +0 -0
  61. /package/dist/{item → esm/item}/IQFactory.js +0 -0
  62. /package/dist/{item → esm/item}/IQUtils.js +0 -0
  63. /package/dist/{item → esm/item}/ItemQuery.js +0 -0
  64. /package/dist/{logger.js → esm/logger.js} +0 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,1013 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const Logging = require('@fjell/logging');
6
+ const deepmerge = require('deepmerge');
7
+ const luxon = require('luxon');
8
+
9
+ const LibLogger = Logging.getLogger('@fjell/core');
10
+
11
+ function _define_property$3(obj, key, value) {
12
+ if (key in obj) {
13
+ Object.defineProperty(obj, key, {
14
+ value: value,
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true
18
+ });
19
+ } else {
20
+ obj[key] = value;
21
+ }
22
+ return obj;
23
+ }
24
+ const logger$3 = LibLogger.get("Dictionary");
25
+ class Dictionary {
26
+ set(key, item) {
27
+ logger$3.trace('set', {
28
+ key,
29
+ item
30
+ });
31
+ const hashedKey = this.hashFunction(key);
32
+ this.map[hashedKey] = item;
33
+ }
34
+ get(key) {
35
+ logger$3.trace('get', {
36
+ key
37
+ });
38
+ const hashedKey = this.hashFunction(key);
39
+ return this.map[hashedKey] || null;
40
+ }
41
+ delete(key) {
42
+ logger$3.trace('delete', {
43
+ key
44
+ });
45
+ const hashedKey = this.hashFunction(key);
46
+ delete this.map[hashedKey];
47
+ }
48
+ keys() {
49
+ return Object.keys(this.map).map((key)=>JSON.parse(key));
50
+ }
51
+ values() {
52
+ return Object.values(this.map);
53
+ }
54
+ includesKey(key) {
55
+ return Object.keys(this.map).includes(this.hashFunction(key));
56
+ }
57
+ clone() {
58
+ const clonedMap = Object.assign({}, this.map);
59
+ const clone = new Dictionary(clonedMap, this.hashFunction);
60
+ return clone;
61
+ }
62
+ constructor(map, hashFunction){
63
+ _define_property$3(this, "map", {});
64
+ _define_property$3(this, "hashFunction", (key)=>JSON.stringify(key));
65
+ if (map) {
66
+ this.map = map;
67
+ }
68
+ if (hashFunction) {
69
+ this.hashFunction = hashFunction;
70
+ }
71
+ }
72
+ }
73
+
74
+ const logger$2 = LibLogger.get('KUtils');
75
+ const isItemKeyEqual = (a, b)=>{
76
+ logger$2.trace('isKeyEqual', {
77
+ a,
78
+ b
79
+ });
80
+ if (isComKey(a) && isComKey(b)) {
81
+ return isComKeyEqual(a, b);
82
+ } else if (isPriKey(a) && isPriKey(b)) {
83
+ if (isComKey(a) || isComKey(b)) {
84
+ return false;
85
+ } else {
86
+ return isPriKeyEqual(a, b);
87
+ }
88
+ } else {
89
+ return false;
90
+ }
91
+ };
92
+ const isPriKeyEqual = (a, b)=>{
93
+ logger$2.trace('isPriKeyEqual', {
94
+ a,
95
+ b
96
+ });
97
+ return a && b && a.pk === b.pk && a.kt === b.kt;
98
+ };
99
+ const isLocKeyEqual = (a, b)=>{
100
+ logger$2.trace('isLocKeyEqual', {
101
+ a,
102
+ b
103
+ });
104
+ return a && b && a.lk === b.lk && a.kt === b.kt;
105
+ };
106
+ const isComKeyEqual = (a, b)=>{
107
+ logger$2.trace('isComKeyEqual', {
108
+ a,
109
+ b
110
+ });
111
+ if (a && b && isPriKeyEqual({
112
+ kt: a.kt,
113
+ pk: a.pk
114
+ }, {
115
+ kt: b.kt,
116
+ pk: b.pk
117
+ })) {
118
+ if (a.loc.length === b.loc.length) {
119
+ for(let i = 0; i < a.loc.length; i++){
120
+ if (!isLocKeyEqual(a.loc[i], b.loc[i])) {
121
+ return false;
122
+ }
123
+ }
124
+ return true;
125
+ } else {
126
+ return false;
127
+ }
128
+ } else {
129
+ return false;
130
+ }
131
+ };
132
+ const isItemKey = (key)=>{
133
+ logger$2.trace('isItemKey', {
134
+ key
135
+ });
136
+ return key !== undefined && (isComKey(key) || isPriKey(key));
137
+ };
138
+ const isComKey = (key)=>{
139
+ logger$2.trace('isComKey', {
140
+ key
141
+ });
142
+ return key !== undefined && key.pk !== undefined && key.kt !== undefined && key.loc !== undefined && key.loc.length > 0;
143
+ };
144
+ const isPriKey = (key)=>{
145
+ logger$2.trace('isPriKey', {
146
+ key
147
+ });
148
+ return key !== undefined && key.pk !== undefined && key.kt !== undefined && (key.loc === undefined || key.loc.length === 0);
149
+ };
150
+ const isLocKey = (key)=>{
151
+ logger$2.trace('isLocKey', {
152
+ key
153
+ });
154
+ return key !== undefined && key.lk !== undefined && key.kt !== undefined;
155
+ };
156
+ const generateKeyArray = (key)=>{
157
+ logger$2.trace('generateKeyArray', {
158
+ key
159
+ });
160
+ const keys = [];
161
+ if (isComKey(key) || isPriKey(key)) {
162
+ // console.log('it is an item key');
163
+ if (isComKey(key)) {
164
+ // console.log('it is a composite key');
165
+ const comKey = key;
166
+ keys.push({
167
+ pk: comKey.pk,
168
+ kt: comKey.kt
169
+ });
170
+ for(let i = 0; i < comKey.loc.length; i++){
171
+ keys.push(comKey.loc[i]);
172
+ }
173
+ } else {
174
+ keys.push(key);
175
+ }
176
+ } else {
177
+ // console.log('is is an array, length: ' + key.length);
178
+ const locKeys = key;
179
+ for(let i = 0; i < locKeys.length; i++){
180
+ // console.log('Pushing a key');
181
+ keys.push(locKeys[i]);
182
+ }
183
+ }
184
+ return keys;
185
+ };
186
+ // TODO: Exactly the same as in ContainedItemLib
187
+ const constructPriKey = (pk, kt)=>{
188
+ logger$2.trace('constructPriKey', {
189
+ pk,
190
+ kt
191
+ });
192
+ let pri;
193
+ if (typeof pk === 'string') {
194
+ pri = {
195
+ kt: kt,
196
+ pk: pk
197
+ };
198
+ } else {
199
+ pri = pk;
200
+ }
201
+ return pri;
202
+ };
203
+ // TODO: Exactly the same as in ContainedItemLib
204
+ const cPK = constructPriKey;
205
+ const toKeyTypeArray = (ik)=>{
206
+ logger$2.trace('toKeyTypeArray', {
207
+ ik
208
+ });
209
+ if (isComKey(ik)) {
210
+ const ck = ik;
211
+ return [
212
+ ck.kt,
213
+ ...ck.loc.map((l)=>l.kt)
214
+ ];
215
+ } else {
216
+ return [
217
+ ik.kt
218
+ ];
219
+ }
220
+ };
221
+ const abbrevIK = (ik)=>{
222
+ logger$2.trace('abbrevIK', {
223
+ ik
224
+ });
225
+ if (ik) {
226
+ if (isComKey(ik)) {
227
+ const ck = ik;
228
+ return `${ck.kt}:${ck.pk}:${ck.loc.map((l)=>`${l.kt}:${l.lk}`).join(',')}`;
229
+ } else {
230
+ return `${ik.kt}:${ik.pk}`;
231
+ }
232
+ } else {
233
+ return 'null IK';
234
+ }
235
+ };
236
+ const abbrevLKA = (keyArray)=>{
237
+ logger$2.trace('abbrevLKA', {
238
+ keyArray
239
+ });
240
+ if (keyArray === undefined || keyArray === null) {
241
+ return 'null LKA';
242
+ } else {
243
+ return keyArray.map((key)=>{
244
+ if (key) {
245
+ return `${key.kt}:${key.lk}`;
246
+ } else {
247
+ return key;
248
+ }
249
+ }).join(',');
250
+ }
251
+ };
252
+ const primaryType = (ik)=>{
253
+ logger$2.trace('primaryType', {
254
+ ik
255
+ });
256
+ if (isComKey(ik)) {
257
+ return ik.kt;
258
+ } else {
259
+ return ik.kt;
260
+ }
261
+ };
262
+ /**
263
+ *
264
+ * @param ik ItemKey to be used as a basis for a location
265
+ * @returns
266
+ */ const itemKeyToLocKeyArray = (ik)=>{
267
+ logger$2.trace('itemKeyToLocKeyArray', {
268
+ ik: abbrevIK(ik)
269
+ });
270
+ let lka = [];
271
+ if (isComKey(ik)) {
272
+ const ck = ik;
273
+ lka = [
274
+ {
275
+ kt: ck.kt,
276
+ lk: ck.pk
277
+ },
278
+ ...ck.loc
279
+ ];
280
+ } else {
281
+ const pk = ik;
282
+ lka = [
283
+ {
284
+ kt: pk.kt,
285
+ lk: pk.pk
286
+ }
287
+ ];
288
+ }
289
+ logger$2.trace('itemKeyToLocKeyArray Results', {
290
+ ik: abbrevIK(ik),
291
+ lka: abbrevLKA(lka)
292
+ });
293
+ return lka;
294
+ };
295
+ const ikToLKA = itemKeyToLocKeyArray;
296
+ /**
297
+ * Sometimes you need to take a location key array and convert it to the item key that points to the containing item.
298
+ * @param lka A location key array
299
+ * @returns An item key corresponding to the containing item this location refers to.
300
+ */ const locKeyArrayToItemKey = (lka)=>{
301
+ logger$2.trace('locKeyArrayToItemKey', {
302
+ lka: abbrevLKA(lka)
303
+ });
304
+ if (lka && lka.length === 1) {
305
+ const priKey = cPK(lka[0].lk, lka[0].kt);
306
+ return priKey;
307
+ } else if (lka && lka.length > 1 && lka[0] !== undefined) {
308
+ const locs = lka.slice(1);
309
+ const priKey = cPK(lka[0].lk, lka[0].kt);
310
+ const comKey = {
311
+ kt: priKey.kt,
312
+ pk: priKey.pk,
313
+ loc: locs
314
+ };
315
+ return comKey;
316
+ } else {
317
+ throw new Error('locKeyArrayToItemKey: lka is undefined or empty');
318
+ }
319
+ };
320
+ // TODO: This is annoying that we have to check for '' and 'null'
321
+ const isValidPriKey = (key)=>{
322
+ const valid = key !== undefined && key !== null && key.pk !== undefined && key.pk !== null && key.pk !== '' && key.pk !== 'null' && key.kt !== undefined && key.kt !== null && key.kt !== '' && key.kt !== 'null';
323
+ return valid;
324
+ };
325
+ // TODO: This is annoying that we have to check for '' and 'null'
326
+ const isValidLocKey = (key)=>{
327
+ const valid = key !== undefined && key !== null && key.lk !== undefined && key.lk !== null && key.lk !== '' && key.lk !== 'null' && key.kt !== undefined && key.kt !== null && key.kt !== '' && key.kt !== 'null';
328
+ return valid;
329
+ };
330
+ const isValidLocKeyArray = (keyArray)=>{
331
+ return keyArray !== undefined && keyArray !== null && keyArray.every(isValidLocKey);
332
+ };
333
+ const isValidComKey = (key)=>{
334
+ return key !== undefined && key !== null && isValidPriKey(key) && isValidLocKeyArray(key.loc);
335
+ };
336
+ const isValidItemKey = (key)=>{
337
+ return isComKey(key) && isValidComKey(key) || isPriKey(key) && isValidPriKey(key);
338
+ };
339
+ const lkaToIK = locKeyArrayToItemKey;
340
+
341
+ function _define_property$2(obj, key, value) {
342
+ if (key in obj) {
343
+ Object.defineProperty(obj, key, {
344
+ value: value,
345
+ enumerable: true,
346
+ configurable: true,
347
+ writable: true
348
+ });
349
+ } else {
350
+ obj[key] = value;
351
+ }
352
+ return obj;
353
+ }
354
+ class IFactory {
355
+ addRef(i, name) {
356
+ const ik = i.key;
357
+ const refName = name || primaryType(ik);
358
+ if (!this.item.refs) {
359
+ this.item.refs = {};
360
+ }
361
+ this.item.refs[refName] = ik;
362
+ return this;
363
+ }
364
+ static addRef(i, name) {
365
+ return new IFactory().addRef(i, name);
366
+ }
367
+ addDefaultEvents() {
368
+ if (!this.item.events) {
369
+ this.item.events = {};
370
+ }
371
+ const now = new Date();
372
+ if (!this.item.events.created) {
373
+ this.item.events.created = {
374
+ at: now
375
+ };
376
+ }
377
+ if (!this.item.events.updated) {
378
+ this.item.events.updated = {
379
+ at: now
380
+ };
381
+ }
382
+ if (!this.item.events.deleted) {
383
+ this.item.events.deleted = {
384
+ at: null
385
+ };
386
+ }
387
+ return this;
388
+ }
389
+ addEvent(name, at, by) {
390
+ if (!this.item.events) {
391
+ this.item.events = {};
392
+ }
393
+ this.item.events[name] = {
394
+ at,
395
+ by
396
+ };
397
+ return this;
398
+ }
399
+ static addEvent(name, at, by) {
400
+ return new IFactory().addEvent(name, at, by);
401
+ }
402
+ addProp(name, value) {
403
+ this.item[name] = value;
404
+ return this;
405
+ }
406
+ static addProp(name, value) {
407
+ return new IFactory().addProp(name, value);
408
+ }
409
+ addProps(props) {
410
+ this.item = deepmerge(this.item, props);
411
+ return this;
412
+ }
413
+ static addProps(props) {
414
+ return new IFactory().addProps(props);
415
+ }
416
+ toItem() {
417
+ return this.item;
418
+ }
419
+ constructor(props = {}){
420
+ _define_property$2(this, "item", {});
421
+ this.item = deepmerge(this.item, props);
422
+ }
423
+ }
424
+
425
+ function _define_property$1(obj, key, value) {
426
+ if (key in obj) {
427
+ Object.defineProperty(obj, key, {
428
+ value: value,
429
+ enumerable: true,
430
+ configurable: true,
431
+ writable: true
432
+ });
433
+ } else {
434
+ obj[key] = value;
435
+ }
436
+ return obj;
437
+ }
438
+ class AItemService {
439
+ constructor(pkType, parentService){
440
+ _define_property$1(this, "pkType", void 0);
441
+ _define_property$1(this, "parentService", null);
442
+ _define_property$1(this, "getPkType", ()=>{
443
+ return this.pkType;
444
+ });
445
+ _define_property$1(this, "getKeyTypes", ()=>{
446
+ let keyTypes = [
447
+ this.getPkType()
448
+ ];
449
+ if (this.parentService) {
450
+ keyTypes = keyTypes.concat(this.parentService.getKeyTypes());
451
+ }
452
+ return keyTypes;
453
+ });
454
+ this.pkType = pkType;
455
+ if (parentService) {
456
+ this.parentService = parentService;
457
+ }
458
+ }
459
+ }
460
+
461
+ const isCondition = (condition)=>{
462
+ return (typeof condition.column === 'string' && Array.isArray(condition.value) && condition.value.every((item)=>typeof item === 'string') || Array.isArray(condition.value) && condition.value.every((item)=>typeof item === 'number') || typeof condition.value === 'string' || typeof condition.value === 'number' || typeof condition.value === 'boolean' || condition.value instanceof Date) && (condition.operator ? typeof condition.operator === 'string' : true);
463
+ };
464
+
465
+ function _define_property(obj, key, value) {
466
+ if (key in obj) {
467
+ Object.defineProperty(obj, key, {
468
+ value: value,
469
+ enumerable: true,
470
+ configurable: true,
471
+ writable: true
472
+ });
473
+ } else {
474
+ obj[key] = value;
475
+ }
476
+ return obj;
477
+ }
478
+ class IQFactory {
479
+ orderBy(field, direction = 'asc') {
480
+ if (!this.query.orderBy) {
481
+ this.query.orderBy = [];
482
+ }
483
+ this.query.orderBy.push({
484
+ field,
485
+ direction
486
+ });
487
+ return this;
488
+ }
489
+ agg(name, query) {
490
+ if (!this.query.aggs) {
491
+ this.query.aggs = {};
492
+ }
493
+ this.query.aggs[name] = query;
494
+ return this;
495
+ }
496
+ event(name, query) {
497
+ if (!this.query.events) {
498
+ this.query.events = {};
499
+ }
500
+ this.query.events[name] = query;
501
+ return this;
502
+ }
503
+ conditions(conditions, compoundType = 'AND') {
504
+ for (const condition of conditions){
505
+ if (!isCondition(condition)) {
506
+ throw new Error(`Invalid condition: ${JSON.stringify(condition)}`);
507
+ }
508
+ }
509
+ if (!this.query.compoundCondition) {
510
+ // If there is no top-level compound condition, create one
511
+ // with the given compound type. This will mostly likely be the most common case.
512
+ this.query.compoundCondition = {
513
+ compoundType,
514
+ conditions: conditions
515
+ };
516
+ } else {
517
+ // If there is already a top-level compound condition, create a new compound condition
518
+ // and add it to the conditions array of the top-level compound condition.
519
+ const compoundCondition = {
520
+ compoundType,
521
+ conditions
522
+ };
523
+ this.query.compoundCondition.conditions.push(compoundCondition);
524
+ }
525
+ return this;
526
+ }
527
+ limit(limit) {
528
+ this.query.limit = limit;
529
+ return this;
530
+ }
531
+ offset(offset) {
532
+ this.query.offset = offset;
533
+ return this;
534
+ }
535
+ // TODO: right now, we're only supporting PK refs for queries. Should add support for CKs
536
+ pk(kt, pk, name) {
537
+ if (!this.query.refs) {
538
+ this.query.refs = {};
539
+ }
540
+ const refName = name || kt;
541
+ this.query.refs[refName] = cPK(pk, kt);
542
+ return this;
543
+ }
544
+ condition(column, value, operator = '==') {
545
+ const condition = {
546
+ column,
547
+ value,
548
+ operator
549
+ };
550
+ if (isCondition(condition)) {
551
+ if (!this.query.compoundCondition) {
552
+ // If there is no top-level compound condition, create one
553
+ // with the default compound type of 'AND'.
554
+ this.query.compoundCondition = {
555
+ compoundType: 'AND',
556
+ conditions: []
557
+ };
558
+ }
559
+ this.query.compoundCondition.conditions.push(condition);
560
+ return this;
561
+ } else {
562
+ throw new Error(`Invalid condition: ${JSON.stringify(condition)}`);
563
+ }
564
+ }
565
+ static all() {
566
+ const iqFactory = new IQFactory();
567
+ return iqFactory;
568
+ }
569
+ static orderBy(field, direction = 'asc') {
570
+ const iqFactory = new IQFactory();
571
+ return iqFactory.orderBy(field, direction);
572
+ }
573
+ static agg(name, query) {
574
+ const iqFactory = new IQFactory();
575
+ return iqFactory.agg(name, query);
576
+ }
577
+ static event(name, query) {
578
+ const iqFactory = new IQFactory();
579
+ return iqFactory.event(name, query);
580
+ }
581
+ static limit(limit) {
582
+ const iqFactory = new IQFactory();
583
+ return iqFactory.limit(limit);
584
+ }
585
+ static offset(offset) {
586
+ const iqFactory = new IQFactory();
587
+ return iqFactory.offset(offset);
588
+ }
589
+ static pk(kt, pk, name) {
590
+ const iqFactory = new IQFactory();
591
+ return iqFactory.pk(kt, pk, name);
592
+ }
593
+ static condition(column, value, operator = '==') {
594
+ const iqFactory = new IQFactory();
595
+ return iqFactory.condition(column, value, operator);
596
+ }
597
+ static conditions(conditions, compoundType = 'AND') {
598
+ const iqFactory = new IQFactory();
599
+ return iqFactory.conditions(conditions, compoundType);
600
+ }
601
+ toQuery() {
602
+ return this.query;
603
+ }
604
+ constructor(query = {}){
605
+ _define_property(this, "query", {});
606
+ this.query = query;
607
+ }
608
+ }
609
+
610
+ const logger$1 = LibLogger.get('IQUtils');
611
+ /**
612
+ * When we query or search, we're sending a GET request. This converts everything in ItemQuery into a flat
613
+ * object that can be sent over a GET request.
614
+ *
615
+ * Note that there is some discussion about this. Evidently Elastic supports search with POST, but that also
616
+ * feels like a bit of a hack. It's not a RESTful way to do things. So we're sticking with GET for now.
617
+ *
618
+ * For reference, look at RFC 9110 "HTTP Semantics", June which clarified on top of and RFC 7231. It's possible
619
+ * but there are so many caveats and conditions in the standard, it's not worth it.
620
+ *
621
+ * Anticipating the next question - "isn't there a limit to the length of a URL?" The specification does not
622
+ * specify a limit, and there are limits in various browsers and servers - Apache is 4,000 chars, Chrome is 2M chars.
623
+ * Short answer is that if this query is being used to craft something that complex, it is probably a better idea
624
+ * to provide an action or a custom query endpoint on the server.
625
+ *
626
+ * @param query
627
+ * @returns QueryParams ready to be get over a GET request.
628
+ */ const queryToParams = (query)=>{
629
+ const params = {};
630
+ if (query.compoundCondition) {
631
+ params.compoundCondition = JSON.stringify(query.compoundCondition);
632
+ }
633
+ if (query.refs) {
634
+ params.refs = JSON.stringify(query.refs);
635
+ }
636
+ if (query.limit) {
637
+ params.limit = query.limit;
638
+ }
639
+ if (query.offset) {
640
+ params.offset = query.offset;
641
+ }
642
+ if (query.aggs) {
643
+ params.aggs = JSON.stringify(query.aggs);
644
+ }
645
+ if (query.events) {
646
+ params.events = JSON.stringify(query.events);
647
+ }
648
+ return params;
649
+ };
650
+ // This is a dateTimeReviver used for JSON parse - when we convert a param back to a query, we need this.
651
+ const dateTimeReviver = function(key, value) {
652
+ if (typeof value === 'string') {
653
+ const parsedDate = luxon.DateTime.fromISO(value);
654
+ if (parsedDate.isValid) {
655
+ return parsedDate.toJSDate();
656
+ }
657
+ }
658
+ return value;
659
+ };
660
+ /**
661
+ * This method translates from a flat QueryParams object with stringify'd JSON back to a full ItemQuery.
662
+ *
663
+ * @param params Parameters sent over a GET request
664
+ * @returns A fully hydrated ItemQuery object.
665
+ */ const paramsToQuery = (params)=>{
666
+ const query = {};
667
+ if (params.compoundCondition) {
668
+ query.compoundCondition = JSON.parse(params.compoundCondition);
669
+ }
670
+ if (params.refs) {
671
+ query.refs = JSON.parse(params.refs);
672
+ }
673
+ if (params.limit) {
674
+ query.limit = Number(params.limit);
675
+ }
676
+ if (params.offset) {
677
+ query.offset = Number(params.offset);
678
+ }
679
+ if (params.aggs) {
680
+ query.aggs = JSON.parse(params.aggs);
681
+ }
682
+ if (params.events) {
683
+ query.events = JSON.parse(params.events, dateTimeReviver);
684
+ }
685
+ return query;
686
+ };
687
+ const isRefQueryMatch = (refKey, queryRef, references)=>{
688
+ logger$1.trace('doesRefMatch', {
689
+ queryRef,
690
+ references
691
+ });
692
+ logger$1.debug('Comparing Ref', {
693
+ refKey,
694
+ itemRef: references[refKey],
695
+ queryRef
696
+ });
697
+ return isItemKeyEqual(queryRef, references[refKey]);
698
+ };
699
+ const isCompoundConditionQueryMatch = (queryCondition, item)=>{
700
+ if (queryCondition.compoundType === 'AND') {
701
+ // If this is an AND compound condition, we need to check if all of the conditions match
702
+ return queryCondition.conditions.every((condition)=>isCondition(condition) ? isConditionQueryMatch(condition, item) : isCompoundConditionQueryMatch(condition, item));
703
+ } else {
704
+ // If this is an OR compound condition, we need to check if any of the conditions match
705
+ return queryCondition.conditions.some((condition)=>isCondition(condition) ? isConditionQueryMatch(condition, item) : isCompoundConditionQueryMatch(condition, item));
706
+ }
707
+ };
708
+ const isConditionQueryMatch = (queryCondition, item)=>{
709
+ const propKey = queryCondition.column;
710
+ logger$1.trace('doesConditionMatch', {
711
+ propKey,
712
+ queryCondition,
713
+ item
714
+ });
715
+ // eslint-disable-next-line no-undefined
716
+ if (item[propKey] === undefined) {
717
+ logger$1.debug('Item does not contain prop under key', {
718
+ propKey,
719
+ item
720
+ });
721
+ return false;
722
+ }
723
+ logger$1.debug('Comparing Condition', {
724
+ propKey,
725
+ itemProp: item[propKey],
726
+ queryCondition
727
+ });
728
+ let result = false;
729
+ switch(queryCondition.operator){
730
+ case '==':
731
+ result = item[propKey] === queryCondition.value;
732
+ break;
733
+ case '!=':
734
+ result = item[propKey] !== queryCondition.value;
735
+ break;
736
+ case '>':
737
+ result = item[propKey] > queryCondition.value;
738
+ break;
739
+ case '>=':
740
+ result = item[propKey] >= queryCondition.value;
741
+ break;
742
+ case '<':
743
+ result = item[propKey] < queryCondition.value;
744
+ break;
745
+ case '<=':
746
+ result = item[propKey] <= queryCondition.value;
747
+ break;
748
+ case 'in':
749
+ result = queryCondition.value.includes(item[propKey]);
750
+ break;
751
+ case 'not-in':
752
+ result = !queryCondition.value.includes(item[propKey]);
753
+ break;
754
+ case 'array-contains':
755
+ result = item[propKey].includes(queryCondition.value);
756
+ break;
757
+ case 'array-contains-any':
758
+ result = queryCondition.value.some((value)=>item[propKey].includes(value));
759
+ break;
760
+ }
761
+ return result;
762
+ };
763
+ const isAggQueryMatch = (aggKey, aggQuery, agg)=>{
764
+ const aggItem = agg.item;
765
+ logger$1.debug('Comparing Agg', {
766
+ aggKey,
767
+ aggItem,
768
+ aggQuery
769
+ });
770
+ // Fancy, right? This is a recursive call to isQueryMatch
771
+ return isQueryMatch(aggItem, aggQuery);
772
+ };
773
+ const isEventQueryMatch = (eventKey, eventQuery, item)=>{
774
+ if (!item.events[eventKey]) {
775
+ logger$1.debug('Item does not contain event under key', {
776
+ eventKey,
777
+ events: item.events
778
+ });
779
+ return false;
780
+ } else {
781
+ const itemEvent = item.events[eventKey];
782
+ if (itemEvent.at !== null) {
783
+ if (eventQuery.start && !(eventQuery.start.getTime() <= itemEvent.at.getTime())) {
784
+ logger$1.debug('Item date before event start query', {
785
+ eventQuery,
786
+ itemEvent
787
+ });
788
+ return false;
789
+ }
790
+ if (eventQuery.end && !(eventQuery.end.getTime() > itemEvent.at.getTime())) {
791
+ logger$1.debug('Item date after event end query', {
792
+ eventQuery,
793
+ itemEvent
794
+ });
795
+ return false;
796
+ }
797
+ } else {
798
+ logger$1.debug('Item event does contains a null at', {
799
+ itemEvent
800
+ });
801
+ return false;
802
+ }
803
+ return true;
804
+ }
805
+ };
806
+ const isQueryMatch = (item, query)=>{
807
+ logger$1.trace('isMatch', {
808
+ item,
809
+ query
810
+ });
811
+ if (query.refs && item.refs) {
812
+ for(const key in query.refs){
813
+ const queryRef = query.refs[key];
814
+ if (!isRefQueryMatch(key, queryRef, item.refs)) return false;
815
+ }
816
+ } else if (query.refs && !item.refs) {
817
+ logger$1.debug('Query contains refs but item does not have refs', {
818
+ query,
819
+ item
820
+ });
821
+ return false;
822
+ }
823
+ if (query.compoundCondition && item) {
824
+ if (!isCompoundConditionQueryMatch(query.compoundCondition, item)) return false;
825
+ }
826
+ if (query.events && item.events) {
827
+ for(const key in query.events){
828
+ const queryEvent = query.events[key];
829
+ if (!isEventQueryMatch(key, queryEvent, item)) return false;
830
+ }
831
+ return true;
832
+ }
833
+ if (query.aggs && item.aggs) {
834
+ for(const key in query.aggs){
835
+ const aggQuery = query.aggs[key];
836
+ if (item.aggs[key] && !isAggQueryMatch(key, aggQuery, item.aggs[key])) return false;
837
+ }
838
+ }
839
+ if (query.aggs && !item.aggs) {
840
+ logger$1.debug('Query contains aggs but item does not have aggs', {
841
+ query,
842
+ item
843
+ });
844
+ return false;
845
+ }
846
+ // If it hasn't returned false by now, it must be a match
847
+ return true;
848
+ };
849
+ const abbrevQuery = (query)=>{
850
+ const abbrev = [
851
+ 'IQ'
852
+ ];
853
+ if (query) {
854
+ if (query.refs) {
855
+ for(const key in query.refs){
856
+ const ref = abbrevRef(key, query.refs[key]);
857
+ abbrev.push(ref);
858
+ }
859
+ }
860
+ if (query.compoundCondition) {
861
+ const props = abbrevCompoundCondition(query.compoundCondition);
862
+ abbrev.push(props);
863
+ }
864
+ if (query.aggs) {
865
+ for(const key in query.aggs){
866
+ const agg = abbrevAgg(key, query.aggs[key]);
867
+ abbrev.push(agg);
868
+ }
869
+ }
870
+ if (query.events) {
871
+ const events = `(E${Object.keys(query.events).join(',')})`;
872
+ abbrev.push(events);
873
+ }
874
+ if (query.limit) {
875
+ abbrev.push(`L${query.limit}`);
876
+ }
877
+ if (query.offset) {
878
+ abbrev.push(`O${query.offset}`);
879
+ }
880
+ } else {
881
+ abbrev.push('(empty)');
882
+ }
883
+ return abbrev.join(' ');
884
+ };
885
+ const abbrevRef = (key, ref)=>{
886
+ if (isPriKey(ref)) {
887
+ const priKey = ref;
888
+ return `R(${key},${priKey.kt},${priKey.pk})`;
889
+ } else {
890
+ const comKey = ref;
891
+ return `R(${key},${JSON.stringify(comKey)})`;
892
+ }
893
+ };
894
+ const abbrevAgg = (key, agg)=>{
895
+ return `A(${key},${abbrevQuery(agg)})`;
896
+ };
897
+ const abbrevCompoundCondition = (compoundCondition)=>{
898
+ return `CC(${compoundCondition.compoundType},` + `${compoundCondition.conditions ? compoundCondition.conditions.map(abbrevCondition).join(',') : 'No Conditions'})`;
899
+ };
900
+ const abbrevCondition = (condition)=>{
901
+ if (isCondition(condition)) {
902
+ return `(${condition.column},${condition.value},${condition.operator})`;
903
+ } else {
904
+ return abbrevCompoundCondition(condition);
905
+ }
906
+ };
907
+
908
+ const logger = LibLogger.get('IUtils');
909
+ const validatePKForItem = (item, pkType)=>{
910
+ if (!item) {
911
+ logger.error('Validating PK, Item is undefined', {
912
+ item
913
+ });
914
+ throw new Error('Validating PK, Item is undefined');
915
+ }
916
+ if (!item.key) {
917
+ logger.error('Validating PK, Item does not have a key', {
918
+ item
919
+ });
920
+ throw new Error('Validating PK, Item does not have a key');
921
+ }
922
+ const keyTypeArray = toKeyTypeArray(item.key);
923
+ if (keyTypeArray[0] !== pkType) {
924
+ logger.error('Key Type Array Mismatch', {
925
+ keyTypeArray,
926
+ pkType
927
+ });
928
+ throw new Error(`Item does not have the correct primary key type. Expected ${pkType}, got ${keyTypeArray[0]}`);
929
+ }
930
+ return item;
931
+ };
932
+ const validatePK = (input, pkType)=>{
933
+ logger.trace('Checking Return Type', {
934
+ input
935
+ });
936
+ if (Array.isArray(input)) {
937
+ return input.map((item)=>validatePKForItem(item, pkType));
938
+ }
939
+ return validatePKForItem(input, pkType);
940
+ };
941
+ const validateKeys = (item, keyTypes)=>{
942
+ logger.trace('Checking Return Type', {
943
+ item
944
+ });
945
+ if (!item) {
946
+ throw new Error('validating keys, item is undefined');
947
+ }
948
+ if (!item.key) {
949
+ throw new Error('validating keys, item does not have a key: ' + JSON.stringify(item));
950
+ }
951
+ const keyTypeArray = toKeyTypeArray(item.key);
952
+ if (keyTypeArray.length !== keyTypes.length) {
953
+ throw new Error(`Item does not have the correct number of keys. Expected ${keyTypes.length}, but got ${keyTypeArray.length}`);
954
+ }
955
+ const match = JSON.stringify(keyTypeArray) === JSON.stringify(keyTypes);
956
+ if (!match) {
957
+ logger.error('Key Type Array Mismatch', {
958
+ keyTypeArray,
959
+ thisKeyTypes: keyTypes
960
+ });
961
+ throw new Error(`Item does not have the correct key types. Expected [${keyTypes.join(', ')}], but got [${keyTypeArray.join(', ')}]`);
962
+ }
963
+ return item;
964
+ };
965
+ const isPriItem = (item)=>{
966
+ return !!(item && item.key && isPriKey(item.key));
967
+ };
968
+ const isComItem = (item)=>{
969
+ return !!(item && item.key && isComKey(item.key));
970
+ };
971
+
972
+ exports.AItemService = AItemService;
973
+ exports.Dictionary = Dictionary;
974
+ exports.IFactory = IFactory;
975
+ exports.IQFactory = IQFactory;
976
+ exports.abbrevAgg = abbrevAgg;
977
+ exports.abbrevCompoundCondition = abbrevCompoundCondition;
978
+ exports.abbrevCondition = abbrevCondition;
979
+ exports.abbrevIK = abbrevIK;
980
+ exports.abbrevLKA = abbrevLKA;
981
+ exports.abbrevQuery = abbrevQuery;
982
+ exports.abbrevRef = abbrevRef;
983
+ exports.cPK = cPK;
984
+ exports.constructPriKey = constructPriKey;
985
+ exports.generateKeyArray = generateKeyArray;
986
+ exports.ikToLKA = ikToLKA;
987
+ exports.isComItem = isComItem;
988
+ exports.isComKey = isComKey;
989
+ exports.isComKeyEqual = isComKeyEqual;
990
+ exports.isCondition = isCondition;
991
+ exports.isItemKey = isItemKey;
992
+ exports.isItemKeyEqual = isItemKeyEqual;
993
+ exports.isLocKey = isLocKey;
994
+ exports.isLocKeyEqual = isLocKeyEqual;
995
+ exports.isPriItem = isPriItem;
996
+ exports.isPriKey = isPriKey;
997
+ exports.isPriKeyEqual = isPriKeyEqual;
998
+ exports.isQueryMatch = isQueryMatch;
999
+ exports.isValidComKey = isValidComKey;
1000
+ exports.isValidItemKey = isValidItemKey;
1001
+ exports.isValidLocKey = isValidLocKey;
1002
+ exports.isValidLocKeyArray = isValidLocKeyArray;
1003
+ exports.isValidPriKey = isValidPriKey;
1004
+ exports.itemKeyToLocKeyArray = itemKeyToLocKeyArray;
1005
+ exports.lkaToIK = lkaToIK;
1006
+ exports.locKeyArrayToItemKey = locKeyArrayToItemKey;
1007
+ exports.paramsToQuery = paramsToQuery;
1008
+ exports.primaryType = primaryType;
1009
+ exports.queryToParams = queryToParams;
1010
+ exports.toKeyTypeArray = toKeyTypeArray;
1011
+ exports.validateKeys = validateKeys;
1012
+ exports.validatePK = validatePK;
1013
+ //# sourceMappingURL=index.cjs.map