@fjell/core 4.4.13 → 4.4.16

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