@fjell/core 4.4.48 → 4.4.50

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 (94) hide show
  1. package/README.md +92 -0
  2. package/dist/Coordinate.d.ts +7 -0
  3. package/dist/errors/ActionError.d.ts +51 -0
  4. package/dist/errors/BusinessLogicError.d.ts +4 -0
  5. package/dist/errors/DuplicateError.d.ts +4 -0
  6. package/dist/errors/NotFoundError.d.ts +4 -0
  7. package/dist/errors/PermissionError.d.ts +4 -0
  8. package/dist/errors/ValidationError.d.ts +4 -0
  9. package/dist/errors/index.d.ts +6 -0
  10. package/dist/event/emitter.d.ts +140 -0
  11. package/dist/event/events.d.ts +81 -0
  12. package/dist/event/index.d.ts +38 -0
  13. package/dist/event/matching.d.ts +54 -0
  14. package/dist/event/subscription.d.ts +74 -0
  15. package/dist/event/types.d.ts +186 -0
  16. package/dist/index.d.ts +13 -0
  17. package/dist/index.js +1584 -47
  18. package/dist/item/IUtils.d.ts +6 -3
  19. package/dist/operations/OperationContext.d.ts +10 -0
  20. package/dist/operations/Operations.d.ts +259 -0
  21. package/dist/operations/contained.d.ts +65 -0
  22. package/dist/operations/errorEnhancer.d.ts +79 -0
  23. package/dist/operations/index.d.ts +2 -0
  24. package/dist/operations/methods.d.ts +134 -0
  25. package/dist/operations/primary.d.ts +57 -0
  26. package/dist/operations/specialized.d.ts +41 -0
  27. package/dist/operations/wrappers/createActionWrapper.d.ts +28 -0
  28. package/dist/operations/wrappers/createAllActionWrapper.d.ts +28 -0
  29. package/dist/operations/wrappers/createAllFacetWrapper.d.ts +27 -0
  30. package/dist/operations/wrappers/createAllWrapper.d.ts +28 -0
  31. package/dist/operations/wrappers/createCreateWrapper.d.ts +28 -0
  32. package/dist/operations/wrappers/createFacetWrapper.d.ts +27 -0
  33. package/dist/operations/wrappers/createFindOneWrapper.d.ts +28 -0
  34. package/dist/operations/wrappers/createFindWrapper.d.ts +28 -0
  35. package/dist/operations/wrappers/createGetWrapper.d.ts +28 -0
  36. package/dist/operations/wrappers/createOneWrapper.d.ts +38 -0
  37. package/dist/operations/wrappers/createRemoveWrapper.d.ts +28 -0
  38. package/dist/operations/wrappers/createUpdateWrapper.d.ts +28 -0
  39. package/dist/operations/wrappers/createUpsertWrapper.d.ts +28 -0
  40. package/dist/operations/wrappers/index.d.ts +34 -0
  41. package/dist/operations/wrappers/types.d.ts +48 -0
  42. package/dist/validation/ItemValidator.d.ts +43 -0
  43. package/dist/validation/KeyValidator.d.ts +56 -0
  44. package/dist/validation/LocationValidator.d.ts +39 -0
  45. package/dist/validation/QueryValidator.d.ts +57 -0
  46. package/dist/validation/index.d.ts +15 -0
  47. package/dist/validation/index.js +501 -0
  48. package/dist/validation/types.d.ts +38 -0
  49. package/package.json +7 -2
  50. package/src/Coordinate.ts +35 -0
  51. package/src/errors/ActionError.ts +69 -0
  52. package/src/errors/BusinessLogicError.ts +24 -0
  53. package/src/errors/DuplicateError.ts +57 -0
  54. package/src/errors/NotFoundError.ts +24 -0
  55. package/src/errors/PermissionError.ts +31 -0
  56. package/src/errors/ValidationError.ts +27 -0
  57. package/src/errors/index.ts +7 -0
  58. package/src/event/emitter.ts +247 -0
  59. package/src/event/events.ts +178 -0
  60. package/src/event/index.ts +130 -0
  61. package/src/event/matching.ts +264 -0
  62. package/src/event/subscription.ts +181 -0
  63. package/src/event/types.ts +282 -0
  64. package/src/index.ts +57 -0
  65. package/src/item/IUtils.ts +9 -80
  66. package/src/operations/OperationContext.ts +12 -0
  67. package/src/operations/Operations.ts +357 -0
  68. package/src/operations/contained.ts +134 -0
  69. package/src/operations/errorEnhancer.ts +204 -0
  70. package/src/operations/index.ts +2 -0
  71. package/src/operations/methods.ts +363 -0
  72. package/src/operations/primary.ts +101 -0
  73. package/src/operations/specialized.ts +71 -0
  74. package/src/operations/wrappers/createActionWrapper.ts +108 -0
  75. package/src/operations/wrappers/createAllActionWrapper.ts +109 -0
  76. package/src/operations/wrappers/createAllFacetWrapper.ts +98 -0
  77. package/src/operations/wrappers/createAllWrapper.ts +103 -0
  78. package/src/operations/wrappers/createCreateWrapper.ts +117 -0
  79. package/src/operations/wrappers/createFacetWrapper.ts +97 -0
  80. package/src/operations/wrappers/createFindOneWrapper.ts +105 -0
  81. package/src/operations/wrappers/createFindWrapper.ts +105 -0
  82. package/src/operations/wrappers/createGetWrapper.ts +96 -0
  83. package/src/operations/wrappers/createOneWrapper.ts +128 -0
  84. package/src/operations/wrappers/createRemoveWrapper.ts +91 -0
  85. package/src/operations/wrappers/createUpdateWrapper.ts +106 -0
  86. package/src/operations/wrappers/createUpsertWrapper.ts +108 -0
  87. package/src/operations/wrappers/index.ts +39 -0
  88. package/src/operations/wrappers/types.ts +63 -0
  89. package/src/validation/ItemValidator.ts +131 -0
  90. package/src/validation/KeyValidator.ts +365 -0
  91. package/src/validation/LocationValidator.ts +136 -0
  92. package/src/validation/QueryValidator.ts +250 -0
  93. package/src/validation/index.ts +32 -0
  94. package/src/validation/types.ts +45 -0
package/dist/index.js CHANGED
@@ -64,11 +64,23 @@ var Dictionary = class _Dictionary {
64
64
  }
65
65
  };
66
66
 
67
+ // src/Coordinate.ts
68
+ var logger2 = logger_default.get("Coordinate");
69
+ var createCoordinate = (kta, scopes = []) => {
70
+ const ktArray = Array.isArray(kta) ? kta : [kta];
71
+ const toString = () => {
72
+ logger2.debug("toString", { kta, scopes });
73
+ return `${ktArray.join(", ")} - ${scopes.join(", ")}`;
74
+ };
75
+ logger2.debug("createCoordinate", { kta: ktArray, scopes, toString });
76
+ return { kta: ktArray, scopes, toString };
77
+ };
78
+
67
79
  // src/item/IFactory.ts
68
80
  import deepmerge from "deepmerge";
69
81
 
70
82
  // src/key/KUtils.ts
71
- var logger2 = logger_default.get("KUtils");
83
+ var logger3 = logger_default.get("KUtils");
72
84
  var normalizeKeyValue = (value) => {
73
85
  return String(value);
74
86
  };
@@ -96,15 +108,15 @@ var createNormalizedHashFunction = () => {
96
108
  };
97
109
  };
98
110
  var isPriKeyEqualNormalized = (a, b) => {
99
- logger2.trace("isPriKeyEqualNormalized", { a, b });
111
+ logger3.trace("isPriKeyEqualNormalized", { a, b });
100
112
  return a && b && normalizeKeyValue(a.pk) === normalizeKeyValue(b.pk) && a.kt === b.kt;
101
113
  };
102
114
  var isLocKeyEqualNormalized = (a, b) => {
103
- logger2.trace("isLocKeyEqualNormalized", { a, b });
115
+ logger3.trace("isLocKeyEqualNormalized", { a, b });
104
116
  return a && b && normalizeKeyValue(a.lk) === normalizeKeyValue(b.lk) && a.kt === b.kt;
105
117
  };
106
118
  var isComKeyEqualNormalized = (a, b) => {
107
- logger2.trace("isComKeyEqualNormalized", { a, b });
119
+ logger3.trace("isComKeyEqualNormalized", { a, b });
108
120
  if (a && b && isPriKeyEqualNormalized({ kt: a.kt, pk: a.pk }, { kt: b.kt, pk: b.pk })) {
109
121
  if (a.loc.length === b.loc.length) {
110
122
  for (let i = 0; i < a.loc.length; i++) {
@@ -121,7 +133,7 @@ var isComKeyEqualNormalized = (a, b) => {
121
133
  }
122
134
  };
123
135
  var isItemKeyEqualNormalized = (a, b) => {
124
- logger2.trace("isItemKeyEqualNormalized", { a, b });
136
+ logger3.trace("isItemKeyEqualNormalized", { a, b });
125
137
  if (isComKey(a) && isComKey(b)) {
126
138
  return isComKeyEqualNormalized(a, b);
127
139
  } else if (isPriKey(a) && isPriKey(b)) {
@@ -135,7 +147,7 @@ var isItemKeyEqualNormalized = (a, b) => {
135
147
  }
136
148
  };
137
149
  var isItemKeyEqual = (a, b) => {
138
- logger2.trace("isKeyEqual", { a, b });
150
+ logger3.trace("isKeyEqual", { a, b });
139
151
  if (isComKey(a) && isComKey(b)) {
140
152
  return isComKeyEqual(a, b);
141
153
  } else if (isPriKey(a) && isPriKey(b)) {
@@ -149,15 +161,15 @@ var isItemKeyEqual = (a, b) => {
149
161
  }
150
162
  };
151
163
  var isPriKeyEqual = (a, b) => {
152
- logger2.trace("isPriKeyEqual", { a, b });
164
+ logger3.trace("isPriKeyEqual", { a, b });
153
165
  return a && b && a.pk === b.pk && a.kt === b.kt;
154
166
  };
155
167
  var isLocKeyEqual = (a, b) => {
156
- logger2.trace("isLocKeyEqual", { a, b });
168
+ logger3.trace("isLocKeyEqual", { a, b });
157
169
  return a && b && a.lk === b.lk && a.kt === b.kt;
158
170
  };
159
171
  var isComKeyEqual = (a, b) => {
160
- logger2.trace("isComKeyEqual", { a, b });
172
+ logger3.trace("isComKeyEqual", { a, b });
161
173
  if (a && b && isPriKeyEqual({ kt: a.kt, pk: a.pk }, { kt: b.kt, pk: b.pk })) {
162
174
  if (a.loc.length === b.loc.length) {
163
175
  for (let i = 0; i < a.loc.length; i++) {
@@ -174,23 +186,23 @@ var isComKeyEqual = (a, b) => {
174
186
  }
175
187
  };
176
188
  var isItemKey = (key) => {
177
- logger2.trace("isItemKey", { key });
189
+ logger3.trace("isItemKey", { key });
178
190
  return key !== void 0 && (isComKey(key) || isPriKey(key));
179
191
  };
180
192
  var isComKey = (key) => {
181
- logger2.trace("isComKey", { key });
193
+ logger3.trace("isComKey", { key });
182
194
  return key !== void 0 && (key.pk !== void 0 && key.kt !== void 0) && (key.loc !== void 0 && Array.isArray(key.loc));
183
195
  };
184
196
  var isPriKey = (key) => {
185
- logger2.trace("isPriKey", { key });
197
+ logger3.trace("isPriKey", { key });
186
198
  return key !== void 0 && (key.pk !== void 0 && key.kt !== void 0) && key.loc === void 0;
187
199
  };
188
200
  var isLocKey = (key) => {
189
- logger2.trace("isLocKey", { key });
201
+ logger3.trace("isLocKey", { key });
190
202
  return key !== void 0 && (key.lk !== void 0 && key.kt !== void 0);
191
203
  };
192
204
  var generateKeyArray = (key) => {
193
- logger2.trace("generateKeyArray", { key });
205
+ logger3.trace("generateKeyArray", { key });
194
206
  const keys = [];
195
207
  if (isComKey(key) || isPriKey(key)) {
196
208
  if (isComKey(key)) {
@@ -211,7 +223,7 @@ var generateKeyArray = (key) => {
211
223
  return keys;
212
224
  };
213
225
  var constructPriKey = (pk, kt) => {
214
- logger2.trace("constructPriKey", { pk, kt });
226
+ logger3.trace("constructPriKey", { pk, kt });
215
227
  let pri;
216
228
  if (typeof pk === "string" || typeof pk === "number") {
217
229
  pri = { kt, pk };
@@ -222,7 +234,7 @@ var constructPriKey = (pk, kt) => {
222
234
  };
223
235
  var cPK = constructPriKey;
224
236
  var toKeyTypeArray = (ik) => {
225
- logger2.trace("toKeyTypeArray", { ik });
237
+ logger3.trace("toKeyTypeArray", { ik });
226
238
  if (isComKey(ik)) {
227
239
  const ck = ik;
228
240
  return [ck.kt, ...ck.loc.map((l) => l.kt)];
@@ -231,7 +243,7 @@ var toKeyTypeArray = (ik) => {
231
243
  }
232
244
  };
233
245
  var extractKeyTypeArray = (key) => {
234
- logger2.trace("extractKeyTypeArray", { key });
246
+ logger3.trace("extractKeyTypeArray", { key });
235
247
  if (isComKey(key)) {
236
248
  const ck = key;
237
249
  return [ck.kt, ...ck.loc.map((l) => l.kt)];
@@ -240,12 +252,12 @@ var extractKeyTypeArray = (key) => {
240
252
  } else if (Array.isArray(key)) {
241
253
  return key.map((locKey) => locKey.kt);
242
254
  } else {
243
- logger2.warning("extractKeyTypeArray: Unknown key type", { key });
255
+ logger3.warning("extractKeyTypeArray: Unknown key type", { key });
244
256
  return [];
245
257
  }
246
258
  };
247
259
  var abbrevIK = (ik) => {
248
- logger2.trace("abbrevIK", { ik });
260
+ logger3.trace("abbrevIK", { ik });
249
261
  if (ik) {
250
262
  if (isComKey(ik)) {
251
263
  const ck = ik;
@@ -258,7 +270,7 @@ var abbrevIK = (ik) => {
258
270
  }
259
271
  };
260
272
  var abbrevLKA = (keyArray) => {
261
- logger2.trace("abbrevLKA", { keyArray });
273
+ logger3.trace("abbrevLKA", { keyArray });
262
274
  if (keyArray === void 0 || keyArray === null) {
263
275
  return "null LKA";
264
276
  } else {
@@ -272,7 +284,7 @@ var abbrevLKA = (keyArray) => {
272
284
  }
273
285
  };
274
286
  var primaryType = (ik) => {
275
- logger2.trace("primaryType", { ik });
287
+ logger3.trace("primaryType", { ik });
276
288
  if (isComKey(ik)) {
277
289
  return ik.kt;
278
290
  } else {
@@ -280,7 +292,7 @@ var primaryType = (ik) => {
280
292
  }
281
293
  };
282
294
  var itemKeyToLocKeyArray = (ik) => {
283
- logger2.trace("itemKeyToLocKeyArray", { ik: abbrevIK(ik) });
295
+ logger3.trace("itemKeyToLocKeyArray", { ik: abbrevIK(ik) });
284
296
  let lka = [];
285
297
  if (isComKey(ik)) {
286
298
  const ck = ik;
@@ -289,12 +301,12 @@ var itemKeyToLocKeyArray = (ik) => {
289
301
  const pk = ik;
290
302
  lka = [{ kt: pk.kt, lk: pk.pk }];
291
303
  }
292
- logger2.trace("itemKeyToLocKeyArray Results", { ik: abbrevIK(ik), lka: abbrevLKA(lka) });
304
+ logger3.trace("itemKeyToLocKeyArray Results", { ik: abbrevIK(ik), lka: abbrevLKA(lka) });
293
305
  return lka;
294
306
  };
295
307
  var ikToLKA = itemKeyToLocKeyArray;
296
308
  var locKeyArrayToItemKey = (lka) => {
297
- logger2.trace("locKeyArrayToItemKey", { lka: abbrevLKA(lka) });
309
+ logger3.trace("locKeyArrayToItemKey", { lka: abbrevLKA(lka) });
298
310
  if (lka && lka.length === 1) {
299
311
  const priKey = cPK(lka[0].lk, lka[0].kt);
300
312
  return priKey;
@@ -538,7 +550,7 @@ var IQFactory = class _IQFactory {
538
550
 
539
551
  // src/item/IQUtils.ts
540
552
  import * as luxon from "luxon";
541
- var logger3 = logger_default.get("IQUtils");
553
+ var logger4 = logger_default.get("IQUtils");
542
554
  var queryToParams = (query) => {
543
555
  const params = {};
544
556
  if (query.compoundCondition) {
@@ -594,8 +606,8 @@ var paramsToQuery = (params) => {
594
606
  return query;
595
607
  };
596
608
  var isRefQueryMatch = (refKey, queryRef, references) => {
597
- logger3.trace("doesRefMatch", { queryRef, references });
598
- logger3.debug("Comparing Ref", { refKey, itemRef: references[refKey], queryRef });
609
+ logger4.trace("doesRefMatch", { queryRef, references });
610
+ logger4.debug("Comparing Ref", { refKey, itemRef: references[refKey], queryRef });
599
611
  if (!references[refKey]) {
600
612
  return false;
601
613
  }
@@ -614,12 +626,12 @@ var isCompoundConditionQueryMatch = (queryCondition, item) => {
614
626
  };
615
627
  var isConditionQueryMatch = (queryCondition, item) => {
616
628
  const propKey = queryCondition.column;
617
- logger3.trace("doesConditionMatch", { propKey, queryCondition, item });
629
+ logger4.trace("doesConditionMatch", { propKey, queryCondition, item });
618
630
  if (item[propKey] === void 0) {
619
- logger3.debug("Item does not contain prop under key", { propKey, item });
631
+ logger4.debug("Item does not contain prop under key", { propKey, item });
620
632
  return false;
621
633
  }
622
- logger3.debug("Comparing Condition", { propKey, itemProp: item[propKey], queryCondition });
634
+ logger4.debug("Comparing Condition", { propKey, itemProp: item[propKey], queryCondition });
623
635
  if (queryCondition.value === null) {
624
636
  if (queryCondition.operator === "==") {
625
637
  return item[propKey] === null;
@@ -668,7 +680,7 @@ var isConditionQueryMatch = (queryCondition, item) => {
668
680
  };
669
681
  var isAggQueryMatch = (aggKey, aggQuery, agg) => {
670
682
  const aggItem = agg.item;
671
- logger3.debug("Comparing Agg", { aggKey, aggItem, aggQuery });
683
+ logger4.debug("Comparing Agg", { aggKey, aggItem, aggQuery });
672
684
  if (!aggItem) {
673
685
  return false;
674
686
  }
@@ -676,35 +688,35 @@ var isAggQueryMatch = (aggKey, aggQuery, agg) => {
676
688
  };
677
689
  var isEventQueryMatch = (eventKey, eventQuery, item) => {
678
690
  if (!item.events[eventKey]) {
679
- logger3.debug("Item does not contain event under key", { eventKey, events: item.events });
691
+ logger4.debug("Item does not contain event under key", { eventKey, events: item.events });
680
692
  return false;
681
693
  } else {
682
694
  const itemEvent = item.events[eventKey];
683
695
  if (itemEvent.at !== null) {
684
696
  if (eventQuery.start && !(eventQuery.start.getTime() <= itemEvent.at.getTime())) {
685
- logger3.debug("Item date before event start query", { eventQuery, itemEvent });
697
+ logger4.debug("Item date before event start query", { eventQuery, itemEvent });
686
698
  return false;
687
699
  }
688
700
  if (eventQuery.end && !(eventQuery.end.getTime() > itemEvent.at.getTime())) {
689
- logger3.debug("Item date after event end query", { eventQuery, itemEvent });
701
+ logger4.debug("Item date after event end query", { eventQuery, itemEvent });
690
702
  return false;
691
703
  }
692
704
  } else {
693
- logger3.debug("Item event does contains a null at", { itemEvent });
705
+ logger4.debug("Item event does contains a null at", { itemEvent });
694
706
  return false;
695
707
  }
696
708
  return true;
697
709
  }
698
710
  };
699
711
  var isQueryMatch = (item, query) => {
700
- logger3.trace("isMatch", { item, query });
712
+ logger4.trace("isMatch", { item, query });
701
713
  if (query.refs && item.refs) {
702
714
  for (const key in query.refs) {
703
715
  const queryRef = query.refs[key];
704
716
  if (!isRefQueryMatch(key, queryRef.key, item.refs)) return false;
705
717
  }
706
718
  } else if (query.refs && !item.refs) {
707
- logger3.debug("Query contains refs but item does not have refs", { query, item });
719
+ logger4.debug("Query contains refs but item does not have refs", { query, item });
708
720
  return false;
709
721
  }
710
722
  if (query.compoundCondition && item) {
@@ -734,7 +746,7 @@ var isQueryMatch = (item, query) => {
734
746
  }
735
747
  }
736
748
  } else if (query.aggs && !item.aggs) {
737
- logger3.debug("Query contains aggs but item does not have aggs", { query, item });
749
+ logger4.debug("Query contains aggs but item does not have aggs", { query, item });
738
750
  return false;
739
751
  }
740
752
  return true;
@@ -796,33 +808,33 @@ var abbrevCondition = (condition) => {
796
808
  }
797
809
  };
798
810
 
799
- // src/item/IUtils.ts
800
- var logger4 = logger_default.get("IUtils");
811
+ // src/validation/ItemValidator.ts
812
+ var logger5 = logger_default.get("validation", "ItemValidator");
801
813
  var validatePKForItem = (item, pkType) => {
802
814
  if (!item) {
803
- logger4.error("Validating PK, Item is undefined", { item });
815
+ logger5.error("Validating PK, Item is undefined", { item });
804
816
  throw new Error("Validating PK, Item is undefined");
805
817
  }
806
818
  if (!item.key) {
807
- logger4.error("Validating PK, Item does not have a key", { item });
819
+ logger5.error("Validating PK, Item does not have a key", { item });
808
820
  throw new Error("Validating PK, Item does not have a key");
809
821
  }
810
822
  const keyTypeArray = toKeyTypeArray(item.key);
811
823
  if (keyTypeArray[0] !== pkType) {
812
- logger4.error("Key Type Array Mismatch", { keyTypeArray, pkType });
824
+ logger5.error("Key Type Array Mismatch", { keyTypeArray, pkType });
813
825
  throw new Error(`Item does not have the correct primary key type. Expected ${pkType}, got ${keyTypeArray[0]}`);
814
826
  }
815
827
  return item;
816
828
  };
817
829
  var validatePK = (input, pkType) => {
818
- logger4.trace("Checking Return Type", { input });
830
+ logger5.trace("Checking Return Type", { input });
819
831
  if (Array.isArray(input)) {
820
832
  return input.map((item) => validatePKForItem(item, pkType));
821
833
  }
822
834
  return validatePKForItem(input, pkType);
823
835
  };
824
836
  var validateKeys = (item, keyTypes) => {
825
- logger4.trace("Checking Return Type", { item });
837
+ logger5.trace("Checking Return Type", { item });
826
838
  if (!item) {
827
839
  throw new Error("validating keys, item is undefined");
828
840
  }
@@ -835,22 +847,1496 @@ var validateKeys = (item, keyTypes) => {
835
847
  }
836
848
  const match = JSON.stringify(keyTypeArray) === JSON.stringify(keyTypes);
837
849
  if (!match) {
838
- logger4.error("Key Type Array Mismatch", { keyTypeArray, thisKeyTypes: keyTypes });
850
+ logger5.error("Key Type Array Mismatch", { keyTypeArray, thisKeyTypes: keyTypes });
839
851
  throw new Error(`Item does not have the correct key types. Expected [${keyTypes.join(", ")}], but got [${keyTypeArray.join(", ")}]`);
840
852
  }
841
853
  return item;
842
854
  };
855
+
856
+ // src/item/IUtils.ts
843
857
  var isPriItem = (item) => {
844
858
  return !!(item && item.key && isPriKey(item.key));
845
859
  };
846
860
  var isComItem = (item) => {
847
861
  return !!(item && item.key && isComKey(item.key));
848
862
  };
863
+
864
+ // src/validation/LocationValidator.ts
865
+ var logger6 = logger_default.get("validation", "LocationValidator");
866
+ var validateLocations = (locations, coordinate, operation) => {
867
+ if (!locations || locations.length === 0) {
868
+ return;
869
+ }
870
+ const keyTypeArray = coordinate.kta;
871
+ const expectedLocationTypes = keyTypeArray.slice(1);
872
+ const actualLocationTypes = locations.map((loc) => loc.kt);
873
+ logger6.debug(`Validating locations for ${operation}`, {
874
+ expected: expectedLocationTypes,
875
+ actual: actualLocationTypes,
876
+ coordinate: keyTypeArray
877
+ });
878
+ if (actualLocationTypes.length > expectedLocationTypes.length) {
879
+ logger6.error("Location key array has too many elements", {
880
+ expected: expectedLocationTypes.length,
881
+ actual: actualLocationTypes.length,
882
+ expectedTypes: expectedLocationTypes,
883
+ actualTypes: actualLocationTypes,
884
+ coordinate,
885
+ operation
886
+ });
887
+ throw new Error(
888
+ `Invalid location key array for ${operation}: Expected at most ${expectedLocationTypes.length} location keys (hierarchy: [${expectedLocationTypes.join(", ")}]), but received ${actualLocationTypes.length} (types: [${actualLocationTypes.join(", ")}])`
889
+ );
890
+ }
891
+ for (let i = 0; i < actualLocationTypes.length; i++) {
892
+ if (expectedLocationTypes[i] !== actualLocationTypes[i]) {
893
+ logger6.error("Location key array order mismatch", {
894
+ position: i,
895
+ expected: expectedLocationTypes[i],
896
+ actual: actualLocationTypes[i],
897
+ expectedHierarchy: expectedLocationTypes,
898
+ actualOrder: actualLocationTypes,
899
+ coordinate,
900
+ operation
901
+ });
902
+ throw new Error(
903
+ `Invalid location key array order for ${operation}: At position ${i}, expected key type "${expectedLocationTypes[i]}" but received "${actualLocationTypes[i]}". Location keys must be ordered according to the hierarchy: [${expectedLocationTypes.join(", ")}]. Received order: [${actualLocationTypes.join(", ")}]`
904
+ );
905
+ }
906
+ }
907
+ logger6.debug(`Location key array validation passed for ${operation}`, { locations });
908
+ };
909
+ var isValidLocations = (locations, coordinate, operation) => {
910
+ try {
911
+ validateLocations(locations, coordinate, operation);
912
+ return { valid: true };
913
+ } catch (error) {
914
+ return {
915
+ valid: false,
916
+ error: error instanceof Error ? error.message : String(error)
917
+ };
918
+ }
919
+ };
920
+
921
+ // src/validation/KeyValidator.ts
922
+ var logger7 = logger_default.get("validation", "KeyValidator");
923
+ var validateLocationKeyOrder = (key, coordinate, operation) => {
924
+ const keyTypeArray = coordinate.kta;
925
+ const expectedLocationTypes = keyTypeArray.slice(1);
926
+ const actualLocationTypes = key.loc.map((loc) => loc.kt);
927
+ if (expectedLocationTypes.length !== actualLocationTypes.length) {
928
+ logger7.error("Location key array length mismatch", {
929
+ expected: expectedLocationTypes.length,
930
+ actual: actualLocationTypes.length,
931
+ key,
932
+ coordinate,
933
+ operation
934
+ });
935
+ const expectedOrder = expectedLocationTypes.map(
936
+ (kt, i) => ` [${i}] { kt: '${kt}', lk: <value> }`
937
+ ).join("\n");
938
+ const actualOrder = key.loc.map(
939
+ (loc, i) => ` [${i}] { kt: '${loc.kt}', lk: ${JSON.stringify(loc.lk)} }`
940
+ ).join("\n");
941
+ throw new Error(
942
+ `Location key array length mismatch for ${operation} operation.
943
+
944
+ Expected ${expectedLocationTypes.length} location keys but received ${actualLocationTypes.length}.
945
+
946
+ Expected location key order for '${keyTypeArray[0]}':
947
+ ${expectedOrder}
948
+
949
+ Received location key order:
950
+ ${actualOrder}`
951
+ );
952
+ }
953
+ for (let i = 0; i < expectedLocationTypes.length; i++) {
954
+ if (expectedLocationTypes[i] !== actualLocationTypes[i]) {
955
+ logger7.error("Location key array order mismatch", {
956
+ position: i,
957
+ expected: expectedLocationTypes[i],
958
+ actual: actualLocationTypes[i],
959
+ key,
960
+ coordinate,
961
+ operation
962
+ });
963
+ const expectedOrder = expectedLocationTypes.map(
964
+ (kt, i2) => ` [${i2}] { kt: '${kt}', lk: <value> }`
965
+ ).join("\n");
966
+ const actualOrder = key.loc.map(
967
+ (loc, i2) => ` [${i2}] { kt: '${loc.kt}', lk: ${JSON.stringify(loc.lk)} }`
968
+ ).join("\n");
969
+ throw new Error(
970
+ `Location key array order mismatch for ${operation} operation.
971
+
972
+ At position ${i}, expected key type "${expectedLocationTypes[i]}" but received "${actualLocationTypes[i]}".
973
+
974
+ Expected location key order for '${keyTypeArray[0]}':
975
+ ${expectedOrder}
976
+
977
+ Received location key order:
978
+ ${actualOrder}
979
+
980
+ Tip: Location keys must be ordered according to the hierarchy: [${expectedLocationTypes.join(", ")}]`
981
+ );
982
+ }
983
+ }
984
+ };
985
+ var validateKey = (key, coordinate, operation) => {
986
+ logger7.debug(`Validating key for ${operation}`, { key, coordinate: coordinate.kta });
987
+ if (!key || key === null) {
988
+ throw new Error(
989
+ `Invalid key structure for ${operation} operation.
990
+
991
+ The provided key is null or undefined.
992
+
993
+ Valid key formats:
994
+ PriKey: { kt: string, pk: string|number }
995
+ ComKey: { kt: string, pk: string|number, loc: Array<{ kt: string, lk: string|number }> }`
996
+ );
997
+ }
998
+ const isCompositeLibrary = coordinate.kta.length > 1;
999
+ const keyIsComposite = isComKey(key);
1000
+ const keyIsPrimary = isPriKey(key);
1001
+ if (isCompositeLibrary && !keyIsComposite) {
1002
+ logger7.error(`Composite library received primary key in ${operation}`, { key, coordinate });
1003
+ const keyTypeArray = coordinate.kta;
1004
+ throw new Error(
1005
+ `Invalid key type for ${operation} operation.
1006
+
1007
+ This is a composite item library. You must provide a ComKey with location keys.
1008
+
1009
+ Expected: ComKey with format:
1010
+ {
1011
+ kt: '${keyTypeArray[0]}',
1012
+ pk: string|number,
1013
+ loc: [
1014
+ ` + keyTypeArray.slice(1).map((kt) => ` { kt: '${kt}', lk: string|number }`).join(",\n") + `
1015
+ ]
1016
+ }
1017
+
1018
+ Received: PriKey with format:
1019
+ ${JSON.stringify(key, null, 2)}
1020
+
1021
+ Example correct usage:
1022
+ library.operations.${operation}({
1023
+ kt: '${keyTypeArray[0]}',
1024
+ pk: 'item-id',
1025
+ loc: [${keyTypeArray.slice(1).map((kt) => `{ kt: '${kt}', lk: 'parent-id' }`).join(", ")}]
1026
+ })`
1027
+ );
1028
+ }
1029
+ if (!isCompositeLibrary && keyIsComposite) {
1030
+ logger7.error(`Primary library received composite key in ${operation}`, { key, coordinate });
1031
+ const keyTypeArray = coordinate.kta;
1032
+ throw new Error(
1033
+ `Invalid key type for ${operation} operation.
1034
+
1035
+ This is a primary item library. You should provide a PriKey without location keys.
1036
+
1037
+ Expected: PriKey with format:
1038
+ { kt: '${keyTypeArray[0]}', pk: string|number }
1039
+
1040
+ Received: ComKey with format:
1041
+ ${JSON.stringify(key, null, 2)}
1042
+
1043
+ Example correct usage:
1044
+ library.operations.${operation}({ kt: '${keyTypeArray[0]}', pk: 'item-id' })`
1045
+ );
1046
+ }
1047
+ if (!keyIsPrimary && !keyIsComposite) {
1048
+ logger7.error(`Invalid key structure in ${operation}`, { key, coordinate });
1049
+ throw new Error(
1050
+ `Invalid key structure for ${operation} operation.
1051
+
1052
+ The provided key does not match PriKey or ComKey format.
1053
+
1054
+ Received:
1055
+ ${JSON.stringify(key, null, 2)}
1056
+
1057
+ Valid key formats:
1058
+ PriKey: { kt: string, pk: string|number }
1059
+ ComKey: { kt: string, pk: string|number, loc: Array<{ kt: string, lk: string|number }> }`
1060
+ );
1061
+ }
1062
+ const expectedKeyType = coordinate.kta[0];
1063
+ if (key.kt !== expectedKeyType) {
1064
+ logger7.error(`Key type mismatch in ${operation}`, {
1065
+ expected: expectedKeyType,
1066
+ received: key.kt,
1067
+ key,
1068
+ coordinate
1069
+ });
1070
+ throw new Error(
1071
+ `Invalid key type for ${operation} operation.
1072
+
1073
+ Expected key type: '${expectedKeyType}'
1074
+ Received key type: '${key.kt}'
1075
+
1076
+ Example correct usage:
1077
+ library.operations.${operation}({ kt: '${expectedKeyType}', pk: 'item-id', ... })`
1078
+ );
1079
+ }
1080
+ if (keyIsComposite) {
1081
+ const comKey = key;
1082
+ if (comKey.loc.length === 0) {
1083
+ logger7.debug(`Empty loc array detected in ${operation} - will search across all locations`, { key });
1084
+ } else {
1085
+ validateLocationKeyOrder(comKey, coordinate, operation);
1086
+ }
1087
+ }
1088
+ logger7.debug(`Key validation passed for ${operation}`, { key });
1089
+ };
1090
+ var validatePriKey = (key, expectedType, operation) => {
1091
+ if (!key || key === null) {
1092
+ throw new Error(`[${operation}] PriKey is undefined or null`);
1093
+ }
1094
+ if (!key.kt) {
1095
+ throw new Error(`[${operation}] PriKey is missing 'kt' field`);
1096
+ }
1097
+ if (key.kt !== expectedType) {
1098
+ throw new Error(
1099
+ `[${operation}] PriKey has incorrect type.
1100
+ Expected: '${expectedType}'
1101
+ Received: '${key.kt}'`
1102
+ );
1103
+ }
1104
+ if (typeof key.pk === "undefined" || key.pk === null) {
1105
+ throw new Error(`[${operation}] PriKey is missing 'pk' field`);
1106
+ }
1107
+ };
1108
+ var validateComKey = (key, coordinate, operation) => {
1109
+ if (!key || key === null) {
1110
+ throw new Error(`[${operation}] ComKey is undefined or null`);
1111
+ }
1112
+ if (!key.kt) {
1113
+ throw new Error(`[${operation}] ComKey is missing 'kt' field`);
1114
+ }
1115
+ if (key.kt !== coordinate.kta[0]) {
1116
+ throw new Error(
1117
+ `[${operation}] ComKey has incorrect type.
1118
+ Expected: '${coordinate.kta[0]}'
1119
+ Received: '${key.kt}'`
1120
+ );
1121
+ }
1122
+ if (typeof key.pk === "undefined" || key.pk === null) {
1123
+ throw new Error(`[${operation}] ComKey is missing 'pk' field`);
1124
+ }
1125
+ if (!Array.isArray(key.loc)) {
1126
+ throw new Error(`[${operation}] ComKey is missing or invalid 'loc' field (must be an array)`);
1127
+ }
1128
+ if (key.loc.length > 0) {
1129
+ validateLocationKeyOrder(key, coordinate, operation);
1130
+ }
1131
+ };
1132
+
1133
+ // src/validation/QueryValidator.ts
1134
+ var logger8 = logger_default.get("validation", "QueryValidator");
1135
+ var validateQuery = (query, operation) => {
1136
+ if (typeof query === "undefined" || query === null) {
1137
+ return;
1138
+ }
1139
+ if (typeof query !== "object") {
1140
+ logger8.error(`Invalid query type for ${operation}`, { query, type: typeof query });
1141
+ throw new Error(
1142
+ `[${operation}] Invalid query parameter.
1143
+
1144
+ Expected: object or undefined
1145
+ Received: ${typeof query}
1146
+
1147
+ Example valid queries:
1148
+ {}
1149
+ { filter: { status: 'active' } }
1150
+ { limit: 10, sort: { field: 'name', order: 'asc' } }`
1151
+ );
1152
+ }
1153
+ if (Array.isArray(query)) {
1154
+ logger8.error(`Query cannot be an array for ${operation}`, { query });
1155
+ throw new Error(
1156
+ `[${operation}] Invalid query parameter.
1157
+
1158
+ Query cannot be an array.
1159
+ Received: ${JSON.stringify(query)}`
1160
+ );
1161
+ }
1162
+ logger8.debug(`Query validation passed for ${operation}`, { query });
1163
+ };
1164
+ var validateOperationParams = (params, operation) => {
1165
+ if (typeof params === "undefined") {
1166
+ return;
1167
+ }
1168
+ if (params === null) {
1169
+ logger8.error(`Params cannot be null for ${operation}`, { params });
1170
+ throw new Error(
1171
+ `[${operation}] Invalid operation parameters.
1172
+
1173
+ Parameters cannot be null.
1174
+ Expected: object or undefined
1175
+ Received: null
1176
+
1177
+ Example valid parameters:
1178
+ {}
1179
+ { email: 'user@example.com' }
1180
+ { status: 'active', limit: 10 }`
1181
+ );
1182
+ }
1183
+ if (typeof params !== "object") {
1184
+ logger8.error(`Invalid params type for ${operation}`, { params, type: typeof params });
1185
+ throw new Error(
1186
+ `[${operation}] Invalid operation parameters.
1187
+
1188
+ Expected: object or undefined
1189
+ Received: ${typeof params}
1190
+
1191
+ Example valid parameters:
1192
+ {}
1193
+ { email: 'user@example.com' }
1194
+ { status: 'active', limit: 10 }`
1195
+ );
1196
+ }
1197
+ if (Array.isArray(params)) {
1198
+ logger8.error(`Params cannot be an array for ${operation}`, { params });
1199
+ throw new Error(
1200
+ `[${operation}] Invalid operation parameters.
1201
+
1202
+ Parameters cannot be an array.
1203
+ Received: ${JSON.stringify(params)}`
1204
+ );
1205
+ }
1206
+ for (const [key, value] of Object.entries(params)) {
1207
+ const valueType = typeof value;
1208
+ const isValidType = valueType === "string" || valueType === "number" || valueType === "boolean" || value instanceof Date || Array.isArray(value) && value.every(
1209
+ (v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean" || v instanceof Date
1210
+ );
1211
+ if (!isValidType) {
1212
+ logger8.error(`Invalid param value type for ${operation}`, { key, value, valueType });
1213
+ throw new Error(
1214
+ `[${operation}] Invalid value type for parameter "${key}".
1215
+
1216
+ Allowed types: string, number, boolean, Date, or arrays of these types
1217
+ Received: ${valueType}
1218
+ Value: ${JSON.stringify(value)}`
1219
+ );
1220
+ }
1221
+ }
1222
+ logger8.debug(`Operation params validation passed for ${operation}`, { params });
1223
+ };
1224
+ var validateFinderName = (finder, operation) => {
1225
+ if (!finder || typeof finder !== "string") {
1226
+ logger8.error(`Invalid finder name for ${operation}`, { finder, type: typeof finder });
1227
+ throw new Error(
1228
+ `[${operation}] Finder name must be a non-empty string.
1229
+
1230
+ Received: ${JSON.stringify(finder)}`
1231
+ );
1232
+ }
1233
+ if (finder.trim().length === 0) {
1234
+ logger8.error(`Empty finder name for ${operation}`, { finder });
1235
+ throw new Error(
1236
+ `[${operation}] Finder name cannot be empty or whitespace only.
1237
+
1238
+ Received: "${finder}"`
1239
+ );
1240
+ }
1241
+ logger8.debug(`Finder name validation passed for ${operation}`, { finder });
1242
+ };
1243
+ var validateActionName = (action, operation) => {
1244
+ if (!action || typeof action !== "string") {
1245
+ logger8.error(`Invalid action name for ${operation}`, { action, type: typeof action });
1246
+ throw new Error(
1247
+ `[${operation}] Action name must be a non-empty string.
1248
+
1249
+ Received: ${JSON.stringify(action)}`
1250
+ );
1251
+ }
1252
+ if (action.trim().length === 0) {
1253
+ logger8.error(`Empty action name for ${operation}`, { action });
1254
+ throw new Error(
1255
+ `[${operation}] Action name cannot be empty or whitespace only.
1256
+
1257
+ Received: "${action}"`
1258
+ );
1259
+ }
1260
+ logger8.debug(`Action name validation passed for ${operation}`, { action });
1261
+ };
1262
+ var validateFacetName = (facet, operation) => {
1263
+ if (!facet || typeof facet !== "string") {
1264
+ logger8.error(`Invalid facet name for ${operation}`, { facet, type: typeof facet });
1265
+ throw new Error(
1266
+ `[${operation}] Facet name must be a non-empty string.
1267
+
1268
+ Received: ${JSON.stringify(facet)}`
1269
+ );
1270
+ }
1271
+ if (facet.trim().length === 0) {
1272
+ logger8.error(`Empty facet name for ${operation}`, { facet });
1273
+ throw new Error(
1274
+ `[${operation}] Facet name cannot be empty or whitespace only.
1275
+
1276
+ Received: "${facet}"`
1277
+ );
1278
+ }
1279
+ logger8.debug(`Facet name validation passed for ${operation}`, { facet });
1280
+ };
1281
+
1282
+ // src/errors/ActionError.ts
1283
+ var ActionError = class extends Error {
1284
+ constructor(errorInfo, cause) {
1285
+ super(errorInfo.message);
1286
+ this.errorInfo = errorInfo;
1287
+ this.name = "ActionError";
1288
+ this.cause = cause;
1289
+ if (!this.errorInfo.technical) {
1290
+ this.errorInfo.technical = { timestamp: (/* @__PURE__ */ new Date()).toISOString() };
1291
+ }
1292
+ if (!this.errorInfo.technical.timestamp) {
1293
+ this.errorInfo.technical.timestamp = (/* @__PURE__ */ new Date()).toISOString();
1294
+ }
1295
+ }
1296
+ toJSON() {
1297
+ return this.errorInfo;
1298
+ }
1299
+ };
1300
+
1301
+ // src/errors/ValidationError.ts
1302
+ var ValidationError = class extends ActionError {
1303
+ constructor(message, validOptions, suggestedAction, conflictingValue) {
1304
+ super({
1305
+ code: "VALIDATION_ERROR",
1306
+ message,
1307
+ operation: { type: "create", name: "", params: {} },
1308
+ // Will be filled by wrapper
1309
+ context: { itemType: "" },
1310
+ // Will be filled by wrapper
1311
+ details: {
1312
+ validOptions,
1313
+ suggestedAction,
1314
+ retryable: true,
1315
+ conflictingValue
1316
+ },
1317
+ technical: {
1318
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1319
+ }
1320
+ });
1321
+ }
1322
+ };
1323
+
1324
+ // src/errors/NotFoundError.ts
1325
+ var NotFoundError = class extends ActionError {
1326
+ constructor(message, itemType, key) {
1327
+ super({
1328
+ code: "NOT_FOUND",
1329
+ message,
1330
+ operation: { type: "get", name: "", params: {} },
1331
+ context: {
1332
+ itemType,
1333
+ key: typeof key === "object" ? key : { primary: key }
1334
+ },
1335
+ details: { retryable: false },
1336
+ technical: {
1337
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1338
+ }
1339
+ });
1340
+ }
1341
+ };
1342
+
1343
+ // src/errors/BusinessLogicError.ts
1344
+ var BusinessLogicError = class extends ActionError {
1345
+ constructor(message, suggestedAction, retryable = false) {
1346
+ super({
1347
+ code: "BUSINESS_LOGIC_ERROR",
1348
+ message,
1349
+ operation: { type: "action", name: "", params: {} },
1350
+ context: { itemType: "" },
1351
+ details: {
1352
+ suggestedAction,
1353
+ retryable
1354
+ },
1355
+ technical: {
1356
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1357
+ }
1358
+ });
1359
+ }
1360
+ };
1361
+
1362
+ // src/errors/PermissionError.ts
1363
+ var PermissionError = class extends ActionError {
1364
+ constructor(message, requiredPermission, currentPermissions) {
1365
+ super({
1366
+ code: "PERMISSION_DENIED",
1367
+ message,
1368
+ operation: { type: "action", name: "", params: {} },
1369
+ context: {
1370
+ itemType: "",
1371
+ ...requiredPermission && { requiredPermission }
1372
+ },
1373
+ details: {
1374
+ ...requiredPermission && {
1375
+ suggestedAction: `Required permission: ${requiredPermission}`,
1376
+ expectedValue: requiredPermission
1377
+ },
1378
+ ...currentPermissions && { conflictingValue: currentPermissions },
1379
+ retryable: false
1380
+ },
1381
+ technical: {
1382
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1383
+ }
1384
+ });
1385
+ }
1386
+ };
1387
+
1388
+ // src/errors/DuplicateError.ts
1389
+ var DuplicateError = class extends ActionError {
1390
+ constructor(message, existingItemIdOrKey, duplicateField) {
1391
+ let existingItemId = null;
1392
+ let keyInfo = null;
1393
+ if (typeof existingItemIdOrKey === "object" && existingItemIdOrKey !== null) {
1394
+ existingItemId = existingItemIdOrKey.pk || existingItemIdOrKey.id || existingItemIdOrKey.primary || null;
1395
+ if (existingItemId !== null) {
1396
+ keyInfo = {
1397
+ primary: existingItemId,
1398
+ ...existingItemIdOrKey
1399
+ };
1400
+ } else {
1401
+ keyInfo = existingItemIdOrKey;
1402
+ }
1403
+ } else if (typeof existingItemIdOrKey !== "undefined") {
1404
+ existingItemId = existingItemIdOrKey;
1405
+ keyInfo = { primary: existingItemId };
1406
+ }
1407
+ super({
1408
+ code: "DUPLICATE_ERROR",
1409
+ message,
1410
+ operation: { type: "create", name: "", params: {} },
1411
+ context: {
1412
+ itemType: "",
1413
+ ...keyInfo && { key: keyInfo },
1414
+ ...existingItemId && {
1415
+ affectedItems: [{
1416
+ id: existingItemId,
1417
+ type: "",
1418
+ displayName: `Existing item with ${duplicateField || "key"}`
1419
+ }]
1420
+ }
1421
+ },
1422
+ details: {
1423
+ retryable: false,
1424
+ conflictingValue: duplicateField
1425
+ },
1426
+ technical: {
1427
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1428
+ }
1429
+ });
1430
+ }
1431
+ };
1432
+
1433
+ // src/operations/errorEnhancer.ts
1434
+ async function executeWithContext(operation, context) {
1435
+ try {
1436
+ return await operation();
1437
+ } catch (error) {
1438
+ throw enhanceError(error, context);
1439
+ }
1440
+ }
1441
+ function executeWithContextSync(operation, context) {
1442
+ try {
1443
+ return operation();
1444
+ } catch (error) {
1445
+ throw enhanceError(error, context);
1446
+ }
1447
+ }
1448
+ function enhanceError(error, context) {
1449
+ if (!(error instanceof ActionError)) {
1450
+ return error;
1451
+ }
1452
+ error.errorInfo.operation = {
1453
+ type: context.operationType,
1454
+ name: context.operationName,
1455
+ params: context.params
1456
+ };
1457
+ if (!error.errorInfo.context.itemType) {
1458
+ error.errorInfo.context.itemType = context.itemType;
1459
+ }
1460
+ if (context.key) {
1461
+ const existingKey = error.errorInfo.context.key;
1462
+ const hasNoPrimaryKey = !existingKey || typeof existingKey.primary === "undefined";
1463
+ const hasNoCompositeKey = !existingKey || !existingKey.composite;
1464
+ const shouldOverride = hasNoPrimaryKey && hasNoCompositeKey;
1465
+ if (shouldOverride) {
1466
+ error.errorInfo.context.key = extractKeyInfo(context.key);
1467
+ }
1468
+ }
1469
+ if (context.locations && context.locations.length > 0 && !error.errorInfo.context.parentLocation) {
1470
+ error.errorInfo.context.parentLocation = {
1471
+ id: context.locations[0].lk,
1472
+ type: context.locations[0].kt
1473
+ };
1474
+ }
1475
+ return error;
1476
+ }
1477
+ function extractKeyInfo(key) {
1478
+ if ("loc" in key) {
1479
+ const ktaArray = Array.isArray(key.kt) ? key.kt : [key.kt];
1480
+ const locations = Array.isArray(key.loc) ? key.loc : [];
1481
+ return {
1482
+ composite: {
1483
+ sk: key.pk,
1484
+ kta: ktaArray,
1485
+ locations: locations.map((loc) => ({
1486
+ lk: loc.lk,
1487
+ kt: loc.kt
1488
+ }))
1489
+ }
1490
+ };
1491
+ } else if ("pk" in key) {
1492
+ return { primary: key.pk };
1493
+ }
1494
+ return { primary: JSON.stringify(key) };
1495
+ }
1496
+ function isActionError(error) {
1497
+ return error instanceof ActionError;
1498
+ }
1499
+ function getErrorInfo(error) {
1500
+ if (isActionError(error)) {
1501
+ return error.toJSON();
1502
+ }
1503
+ if (error instanceof Error) {
1504
+ return {
1505
+ code: "UNKNOWN_ERROR",
1506
+ message: error.message,
1507
+ technical: {
1508
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1509
+ stackTrace: error.stack
1510
+ }
1511
+ };
1512
+ }
1513
+ return {
1514
+ code: "UNKNOWN_ERROR",
1515
+ message: String(error),
1516
+ technical: {
1517
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1518
+ }
1519
+ };
1520
+ }
1521
+
1522
+ // src/event/events.ts
1523
+ function isCreateEvent(event) {
1524
+ return event.eventType === "create";
1525
+ }
1526
+ function isUpdateEvent(event) {
1527
+ return event.eventType === "update";
1528
+ }
1529
+ function isDeleteEvent(event) {
1530
+ return event.eventType === "delete";
1531
+ }
1532
+ function isActionEvent(event) {
1533
+ return event.eventType === "action";
1534
+ }
1535
+
1536
+ // src/event/subscription.ts
1537
+ function isItemSubscription(subscription) {
1538
+ return "key" in subscription;
1539
+ }
1540
+ function isLocationSubscription(subscription) {
1541
+ return "kta" in subscription && "location" in subscription;
1542
+ }
1543
+ function generateSubscriptionId() {
1544
+ return `sub-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1545
+ }
1546
+ function createItemSubscription(key, options) {
1547
+ return {
1548
+ id: generateSubscriptionId(),
1549
+ key,
1550
+ eventTypes: options?.eventTypes,
1551
+ scopes: options?.scopes,
1552
+ query: options?.query
1553
+ };
1554
+ }
1555
+ function createLocationSubscription(kta, location, options) {
1556
+ return {
1557
+ id: generateSubscriptionId(),
1558
+ kta,
1559
+ location,
1560
+ eventTypes: options?.eventTypes,
1561
+ scopes: options?.scopes,
1562
+ query: options?.query
1563
+ };
1564
+ }
1565
+
1566
+ // src/operations/Operations.ts
1567
+ function isPriKey2(key) {
1568
+ return !("loc" in key) || !key.loc;
1569
+ }
1570
+ function isComKey2(key) {
1571
+ return "loc" in key && key.loc && Array.isArray(key.loc) && key.loc.length > 0;
1572
+ }
1573
+
1574
+ // src/event/matching.ts
1575
+ function doesEventMatchSubscription(event, subscription) {
1576
+ if (!doesScopeMatch(event.scopes, subscription.scopes)) {
1577
+ return false;
1578
+ }
1579
+ if (!doesEventTypeMatch(event.eventType, subscription.eventTypes)) {
1580
+ return false;
1581
+ }
1582
+ if (isItemSubscription(subscription)) {
1583
+ return doesKeyMatch(event.key, subscription.key);
1584
+ } else if (isLocationSubscription(subscription)) {
1585
+ return doesKeyMatchLocation(event.key, subscription.kta, subscription.location);
1586
+ }
1587
+ return false;
1588
+ }
1589
+ function doesScopeMatch(eventScopes, subscriptionScopes) {
1590
+ if (!subscriptionScopes || subscriptionScopes.length === 0) {
1591
+ return true;
1592
+ }
1593
+ return subscriptionScopes.some(
1594
+ (requiredScope) => eventScopes.includes(requiredScope)
1595
+ );
1596
+ }
1597
+ function doesEventTypeMatch(eventType, subscriptionEventTypes) {
1598
+ if (!subscriptionEventTypes || subscriptionEventTypes.length === 0) {
1599
+ return true;
1600
+ }
1601
+ return subscriptionEventTypes.includes(eventType);
1602
+ }
1603
+ function doesKeyMatch(eventKey, subscriptionKey) {
1604
+ if (isPriKey2(eventKey) && isPriKey2(subscriptionKey)) {
1605
+ return eventKey.pk === subscriptionKey.pk && eventKey.kt === subscriptionKey.kt;
1606
+ }
1607
+ if (isComKey2(eventKey) && isComKey2(subscriptionKey)) {
1608
+ const eventComKey = eventKey;
1609
+ const subscriptionComKey = subscriptionKey;
1610
+ if (eventComKey.pk !== subscriptionComKey.pk || eventComKey.kt !== subscriptionComKey.kt) {
1611
+ return false;
1612
+ }
1613
+ if (eventComKey.loc.length !== subscriptionComKey.loc.length) {
1614
+ return false;
1615
+ }
1616
+ return eventComKey.loc.every((eventLocKey, index) => {
1617
+ const subLocKey = subscriptionComKey.loc[index];
1618
+ return eventLocKey.lk === subLocKey.lk && eventLocKey.kt === subLocKey.kt;
1619
+ });
1620
+ }
1621
+ return false;
1622
+ }
1623
+ function doesKeyMatchLocation(eventKey, subscriptionKta, subscriptionLocation) {
1624
+ const targetItemType = subscriptionKta[subscriptionKta.length - 1];
1625
+ if (eventKey.kt !== targetItemType) {
1626
+ return false;
1627
+ }
1628
+ if (isPriKey2(eventKey)) {
1629
+ return subscriptionLocation.length === 0;
1630
+ }
1631
+ if (isComKey2(eventKey)) {
1632
+ const comKey = eventKey;
1633
+ return doesLocationMatch(comKey.loc, subscriptionLocation, subscriptionKta);
1634
+ }
1635
+ return false;
1636
+ }
1637
+ function doesLocationMatch(eventLocation, subscriptionLocation, _subscriptionKta) {
1638
+ if (subscriptionLocation.length === 0) {
1639
+ return true;
1640
+ }
1641
+ if (eventLocation.length < subscriptionLocation.length) {
1642
+ return false;
1643
+ }
1644
+ for (let i = 0; i < subscriptionLocation.length; i++) {
1645
+ const eventLocKey = eventLocation[i];
1646
+ const subLocKey = subscriptionLocation[i];
1647
+ if (!eventLocKey || !subLocKey) {
1648
+ return false;
1649
+ }
1650
+ if (eventLocKey.lk !== subLocKey.lk || eventLocKey.kt !== subLocKey.kt) {
1651
+ return false;
1652
+ }
1653
+ }
1654
+ return true;
1655
+ }
1656
+ function findMatchingSubscriptions(event, subscriptions) {
1657
+ return subscriptions.filter(
1658
+ (subscription) => doesEventMatchSubscription(event, subscription)
1659
+ );
1660
+ }
1661
+ function extractLocationValues(location) {
1662
+ return location.map((locKey) => String(locKey.lk));
1663
+ }
1664
+ function compareLocationValues(location1, location2) {
1665
+ if (location1.length !== location2.length) {
1666
+ return false;
1667
+ }
1668
+ return location1.every((locKey1, index) => {
1669
+ const locKey2 = location2[index];
1670
+ return locKey1.lk === locKey2.lk && locKey1.kt === locKey2.kt;
1671
+ });
1672
+ }
1673
+
1674
+ // src/event/types.ts
1675
+ var STANDARD_EVENT_TYPES = {
1676
+ CREATE: "create",
1677
+ UPDATE: "update",
1678
+ DELETE: "delete",
1679
+ ACTION: "action"
1680
+ };
1681
+ var STANDARD_SCOPES = {
1682
+ FIRESTORE: "firestore",
1683
+ SEQUELIZE: "sequelize",
1684
+ POSTGRESQL: "postgresql",
1685
+ MYSQL: "mysql",
1686
+ MONGODB: "mongodb",
1687
+ REDIS: "redis"
1688
+ };
1689
+ var SubscriptionStatus = /* @__PURE__ */ ((SubscriptionStatus2) => {
1690
+ SubscriptionStatus2["PENDING"] = "pending";
1691
+ SubscriptionStatus2["ACTIVE"] = "active";
1692
+ SubscriptionStatus2["PAUSED"] = "paused";
1693
+ SubscriptionStatus2["ERROR"] = "error";
1694
+ SubscriptionStatus2["CANCELLED"] = "cancelled";
1695
+ return SubscriptionStatus2;
1696
+ })(SubscriptionStatus || {});
1697
+ var DEFAULT_EVENT_CONFIG = {
1698
+ maxBatchSize: 100,
1699
+ maxBatchWaitTime: 50,
1700
+ // 50ms
1701
+ maxRetryAttempts: 3,
1702
+ retryDelay: 1e3,
1703
+ // 1 second
1704
+ enableStats: true,
1705
+ maxSubscriptions: 1e3,
1706
+ subscriptionCleanupInterval: 3e5
1707
+ // 5 minutes
1708
+ };
1709
+ var EventSystemError = class extends Error {
1710
+ constructor(message, code, details) {
1711
+ super(message);
1712
+ this.code = code;
1713
+ this.details = details;
1714
+ this.name = "EventSystemError";
1715
+ }
1716
+ };
1717
+ var SubscriptionError = class extends EventSystemError {
1718
+ constructor(message, subscriptionId, details) {
1719
+ super(message, "SUBSCRIPTION_ERROR", { subscriptionId, ...details });
1720
+ this.subscriptionId = subscriptionId;
1721
+ this.name = "SubscriptionError";
1722
+ }
1723
+ };
1724
+ var EventEmissionError = class extends EventSystemError {
1725
+ constructor(message, eventType, details) {
1726
+ super(message, "EVENT_EMISSION_ERROR", { eventType, ...details });
1727
+ this.eventType = eventType;
1728
+ this.name = "EventEmissionError";
1729
+ }
1730
+ };
1731
+ var EventMatchingError = class extends EventSystemError {
1732
+ constructor(message, details) {
1733
+ super(message, "EVENT_MATCHING_ERROR", details);
1734
+ this.name = "EventMatchingError";
1735
+ }
1736
+ };
1737
+ function createEventSystemError(type, message, details) {
1738
+ switch (type) {
1739
+ case "subscription":
1740
+ return new SubscriptionError(message, details?.subscriptionId || "unknown", details);
1741
+ case "emission":
1742
+ return new EventEmissionError(message, details?.eventType || "unknown", details);
1743
+ case "matching":
1744
+ return new EventMatchingError(message, details);
1745
+ case "general":
1746
+ default:
1747
+ return new EventSystemError(message, "GENERAL_ERROR", details);
1748
+ }
1749
+ }
1750
+ function isEventSystemError(error) {
1751
+ return error instanceof EventSystemError;
1752
+ }
1753
+
1754
+ // src/event/index.ts
1755
+ var EVENT_SYSTEM_VERSION = "1.0.0";
1756
+ var SUPPORTED_EVENT_TYPES = [
1757
+ "create",
1758
+ "update",
1759
+ "delete",
1760
+ "action"
1761
+ ];
1762
+ var SUPPORTED_SCOPES = [
1763
+ "firestore",
1764
+ "sequelize",
1765
+ "postgresql",
1766
+ "mysql",
1767
+ "mongodb",
1768
+ "redis"
1769
+ ];
1770
+
1771
+ // src/operations/wrappers/createOneWrapper.ts
1772
+ var logger9 = logger_default.get("operations", "wrappers", "one");
1773
+ function createOneWrapper(coordinate, implementation, options = {}) {
1774
+ const operationName = options.operationName || "one";
1775
+ return async (query, locations) => {
1776
+ if (options.debug) {
1777
+ logger9.debug(`[${operationName}] Called with:`, { query, locations });
1778
+ }
1779
+ if (!options.skipValidation) {
1780
+ try {
1781
+ validateQuery(query, operationName);
1782
+ validateLocations(locations, coordinate, operationName);
1783
+ } catch (error) {
1784
+ throw error;
1785
+ }
1786
+ }
1787
+ const normalizedQuery = query ?? {};
1788
+ const normalizedLocations = locations ?? [];
1789
+ try {
1790
+ const result = await implementation(normalizedQuery, normalizedLocations);
1791
+ if (options.debug) {
1792
+ logger9.debug(`[${operationName}] Result:`, result ? "found" : "not found");
1793
+ }
1794
+ if (result && !options.skipValidation) {
1795
+ return validatePK(result, coordinate.kta[0]);
1796
+ }
1797
+ return result;
1798
+ } catch (error) {
1799
+ if (options.onError) {
1800
+ const context = {
1801
+ operationName,
1802
+ params: [query, locations],
1803
+ coordinate
1804
+ };
1805
+ throw options.onError(error, context);
1806
+ }
1807
+ const enhanced = new Error(
1808
+ `[${operationName}] Operation failed: ${error.message}`,
1809
+ { cause: error }
1810
+ );
1811
+ if (error.stack) {
1812
+ enhanced.stack = error.stack;
1813
+ }
1814
+ throw enhanced;
1815
+ }
1816
+ };
1817
+ }
1818
+
1819
+ // src/operations/wrappers/createAllWrapper.ts
1820
+ var logger10 = logger_default.get("operations", "wrappers", "all");
1821
+ function createAllWrapper(coordinate, implementation, options = {}) {
1822
+ const operationName = options.operationName || "all";
1823
+ return async (query, locations) => {
1824
+ if (options.debug) {
1825
+ logger10.debug(`[${operationName}] Called with:`, { query, locations });
1826
+ }
1827
+ if (!options.skipValidation) {
1828
+ validateQuery(query, operationName);
1829
+ validateLocations(locations, coordinate, operationName);
1830
+ }
1831
+ const normalizedQuery = query ?? {};
1832
+ const normalizedLocations = locations ?? [];
1833
+ try {
1834
+ const result = await implementation(normalizedQuery, normalizedLocations);
1835
+ if (options.debug) {
1836
+ logger10.debug(`[${operationName}] Result: ${result.length} items`);
1837
+ }
1838
+ if (!options.skipValidation) {
1839
+ return validatePK(result, coordinate.kta[0]);
1840
+ }
1841
+ return result;
1842
+ } catch (error) {
1843
+ if (options.onError) {
1844
+ const context = {
1845
+ operationName,
1846
+ params: [query, locations],
1847
+ coordinate
1848
+ };
1849
+ throw options.onError(error, context);
1850
+ }
1851
+ throw new Error(
1852
+ `[${operationName}] Operation failed: ${error.message}`,
1853
+ { cause: error }
1854
+ );
1855
+ }
1856
+ };
1857
+ }
1858
+
1859
+ // src/operations/wrappers/createGetWrapper.ts
1860
+ var logger11 = logger_default.get("operations", "wrappers", "get");
1861
+ function createGetWrapper(coordinate, implementation, options = {}) {
1862
+ const operationName = options.operationName || "get";
1863
+ return async (key) => {
1864
+ if (options.debug) {
1865
+ logger11.debug(`[${operationName}] Called with key:`, key);
1866
+ }
1867
+ if (!options.skipValidation) {
1868
+ validateKey(key, coordinate, operationName);
1869
+ }
1870
+ try {
1871
+ const result = await implementation(key);
1872
+ if (options.debug) {
1873
+ logger11.debug(`[${operationName}] Result:`, result ? "found" : "not found");
1874
+ }
1875
+ if (result && !options.skipValidation) {
1876
+ return validatePK(result, coordinate.kta[0]);
1877
+ }
1878
+ return result;
1879
+ } catch (error) {
1880
+ if (options.onError) {
1881
+ const context = {
1882
+ operationName,
1883
+ params: [key],
1884
+ coordinate
1885
+ };
1886
+ throw options.onError(error, context);
1887
+ }
1888
+ throw new Error(
1889
+ `[${operationName}] Operation failed: ${error.message}`,
1890
+ { cause: error }
1891
+ );
1892
+ }
1893
+ };
1894
+ }
1895
+
1896
+ // src/operations/wrappers/createCreateWrapper.ts
1897
+ var logger12 = logger_default.get("operations", "wrappers", "create");
1898
+ function createCreateWrapper(coordinate, implementation, wrapperOptions = {}) {
1899
+ const operationName = wrapperOptions.operationName || "create";
1900
+ return async (item, createOptions) => {
1901
+ if (wrapperOptions.debug) {
1902
+ logger12.debug(`[${operationName}] Called with:`, { item, options: createOptions });
1903
+ }
1904
+ if (!wrapperOptions.skipValidation) {
1905
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
1906
+ throw new Error(
1907
+ `[${operationName}] Invalid item parameter.
1908
+
1909
+ Expected: object
1910
+ Received: ${Array.isArray(item) ? "array" : typeof item}`
1911
+ );
1912
+ }
1913
+ if (createOptions) {
1914
+ if ("key" in createOptions) {
1915
+ validateKey(createOptions.key, coordinate, operationName);
1916
+ }
1917
+ if ("locations" in createOptions) {
1918
+ validateLocations(createOptions.locations, coordinate, operationName);
1919
+ }
1920
+ }
1921
+ }
1922
+ try {
1923
+ const result = await implementation(item, createOptions);
1924
+ if (wrapperOptions.debug) {
1925
+ logger12.debug(`[${operationName}] Created item:`, result.key);
1926
+ }
1927
+ if (!wrapperOptions.skipValidation) {
1928
+ return validatePK(result, coordinate.kta[0]);
1929
+ }
1930
+ return result;
1931
+ } catch (error) {
1932
+ if (wrapperOptions.onError) {
1933
+ const context = {
1934
+ operationName,
1935
+ params: [item, createOptions],
1936
+ coordinate
1937
+ };
1938
+ throw wrapperOptions.onError(error, context);
1939
+ }
1940
+ throw new Error(
1941
+ `[${operationName}] Operation failed: ${error.message}`,
1942
+ { cause: error }
1943
+ );
1944
+ }
1945
+ };
1946
+ }
1947
+
1948
+ // src/operations/wrappers/createUpdateWrapper.ts
1949
+ var logger13 = logger_default.get("operations", "wrappers", "update");
1950
+ function createUpdateWrapper(coordinate, implementation, options = {}) {
1951
+ const operationName = options.operationName || "update";
1952
+ return async (key, item) => {
1953
+ if (options.debug) {
1954
+ logger13.debug(`[${operationName}] Called with:`, { key, item });
1955
+ }
1956
+ if (!options.skipValidation) {
1957
+ validateKey(key, coordinate, operationName);
1958
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
1959
+ throw new Error(
1960
+ `[${operationName}] Invalid item parameter.
1961
+
1962
+ Expected: object
1963
+ Received: ${Array.isArray(item) ? "array" : typeof item}`
1964
+ );
1965
+ }
1966
+ }
1967
+ try {
1968
+ const result = await implementation(key, item);
1969
+ if (options.debug) {
1970
+ logger13.debug(`[${operationName}] Updated item:`, result.key);
1971
+ }
1972
+ if (!options.skipValidation) {
1973
+ return validatePK(result, coordinate.kta[0]);
1974
+ }
1975
+ return result;
1976
+ } catch (error) {
1977
+ if (options.onError) {
1978
+ const context = {
1979
+ operationName,
1980
+ params: [key, item],
1981
+ coordinate
1982
+ };
1983
+ throw options.onError(error, context);
1984
+ }
1985
+ throw new Error(
1986
+ `[${operationName}] Operation failed: ${error.message}`,
1987
+ { cause: error }
1988
+ );
1989
+ }
1990
+ };
1991
+ }
1992
+
1993
+ // src/operations/wrappers/createUpsertWrapper.ts
1994
+ var logger14 = logger_default.get("operations", "wrappers", "upsert");
1995
+ function createUpsertWrapper(coordinate, implementation, options = {}) {
1996
+ const operationName = options.operationName || "upsert";
1997
+ return async (key, item, locations) => {
1998
+ if (options.debug) {
1999
+ logger14.debug(`[${operationName}] Called with:`, { key, item, locations });
2000
+ }
2001
+ if (!options.skipValidation) {
2002
+ validateKey(key, coordinate, operationName);
2003
+ validateLocations(locations, coordinate, operationName);
2004
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
2005
+ throw new Error(
2006
+ `[${operationName}] Invalid item parameter.
2007
+
2008
+ Expected: object
2009
+ Received: ${Array.isArray(item) ? "array" : typeof item}`
2010
+ );
2011
+ }
2012
+ }
2013
+ try {
2014
+ const result = await implementation(key, item);
2015
+ if (options.debug) {
2016
+ logger14.debug(`[${operationName}] Upserted item:`, result.key);
2017
+ }
2018
+ if (!options.skipValidation) {
2019
+ return validatePK(result, coordinate.kta[0]);
2020
+ }
2021
+ return result;
2022
+ } catch (error) {
2023
+ if (options.onError) {
2024
+ const context = {
2025
+ operationName,
2026
+ params: [key, item, locations],
2027
+ coordinate
2028
+ };
2029
+ throw options.onError(error, context);
2030
+ }
2031
+ throw new Error(
2032
+ `[${operationName}] Operation failed: ${error.message}`,
2033
+ { cause: error }
2034
+ );
2035
+ }
2036
+ };
2037
+ }
2038
+
2039
+ // src/operations/wrappers/createRemoveWrapper.ts
2040
+ var logger15 = logger_default.get("operations", "wrappers", "remove");
2041
+ function createRemoveWrapper(coordinate, implementation, options = {}) {
2042
+ const operationName = options.operationName || "remove";
2043
+ return async (key) => {
2044
+ if (options.debug) {
2045
+ logger15.debug(`[${operationName}] Called with key:`, key);
2046
+ }
2047
+ if (!options.skipValidation) {
2048
+ validateKey(key, coordinate, operationName);
2049
+ }
2050
+ try {
2051
+ const result = await implementation(key);
2052
+ if (options.debug) {
2053
+ logger15.debug(`[${operationName}] Removed item:`, key);
2054
+ }
2055
+ return result;
2056
+ } catch (error) {
2057
+ if (options.onError) {
2058
+ const context = {
2059
+ operationName,
2060
+ params: [key],
2061
+ coordinate
2062
+ };
2063
+ throw options.onError(error, context);
2064
+ }
2065
+ throw new Error(
2066
+ `[${operationName}] Operation failed: ${error.message}`,
2067
+ { cause: error }
2068
+ );
2069
+ }
2070
+ };
2071
+ }
2072
+
2073
+ // src/operations/wrappers/createFindWrapper.ts
2074
+ var logger16 = logger_default.get("operations", "wrappers", "find");
2075
+ function createFindWrapper(coordinate, implementation, options = {}) {
2076
+ const operationName = options.operationName || "find";
2077
+ return async (finder, params, locations) => {
2078
+ if (options.debug) {
2079
+ logger16.debug(`[${operationName}] Called:`, { finder, params, locations });
2080
+ }
2081
+ if (!options.skipValidation) {
2082
+ validateFinderName(finder, operationName);
2083
+ validateOperationParams(params, operationName);
2084
+ validateLocations(locations, coordinate, operationName);
2085
+ }
2086
+ const normalizedParams = params ?? {};
2087
+ const normalizedLocations = locations ?? [];
2088
+ try {
2089
+ const result = await implementation(finder, normalizedParams, normalizedLocations);
2090
+ if (options.debug) {
2091
+ logger16.debug(`[${operationName}] Found ${result.length} items for finder "${finder}"`);
2092
+ }
2093
+ if (!options.skipValidation) {
2094
+ return validatePK(result, coordinate.kta[0]);
2095
+ }
2096
+ return result;
2097
+ } catch (error) {
2098
+ if (options.onError) {
2099
+ const context = {
2100
+ operationName,
2101
+ params: [finder, params, locations],
2102
+ coordinate
2103
+ };
2104
+ throw options.onError(error, context);
2105
+ }
2106
+ throw new Error(
2107
+ `[${operationName}] Operation failed for finder "${finder}": ${error.message}`,
2108
+ { cause: error }
2109
+ );
2110
+ }
2111
+ };
2112
+ }
2113
+
2114
+ // src/operations/wrappers/createFindOneWrapper.ts
2115
+ var logger17 = logger_default.get("operations", "wrappers", "findOne");
2116
+ function createFindOneWrapper(coordinate, implementation, options = {}) {
2117
+ const operationName = options.operationName || "findOne";
2118
+ return async (finder, params, locations) => {
2119
+ if (options.debug) {
2120
+ logger17.debug(`[${operationName}] Called:`, { finder, params, locations });
2121
+ }
2122
+ if (!options.skipValidation) {
2123
+ validateFinderName(finder, operationName);
2124
+ validateOperationParams(params, operationName);
2125
+ validateLocations(locations, coordinate, operationName);
2126
+ }
2127
+ const normalizedParams = params ?? {};
2128
+ const normalizedLocations = locations ?? [];
2129
+ try {
2130
+ const result = await implementation(finder, normalizedParams, normalizedLocations);
2131
+ if (options.debug) {
2132
+ logger17.debug(`[${operationName}] Result for finder "${finder}":`, result ? "found" : "not found");
2133
+ }
2134
+ if (result && !options.skipValidation) {
2135
+ return validatePK(result, coordinate.kta[0]);
2136
+ }
2137
+ return result;
2138
+ } catch (error) {
2139
+ if (options.onError) {
2140
+ const context = {
2141
+ operationName,
2142
+ params: [finder, params, locations],
2143
+ coordinate
2144
+ };
2145
+ throw options.onError(error, context);
2146
+ }
2147
+ throw new Error(
2148
+ `[${operationName}] Operation failed for finder "${finder}": ${error.message}`,
2149
+ { cause: error }
2150
+ );
2151
+ }
2152
+ };
2153
+ }
2154
+
2155
+ // src/operations/wrappers/createActionWrapper.ts
2156
+ var logger18 = logger_default.get("operations", "wrappers", "action");
2157
+ function createActionWrapper(coordinate, implementation, options = {}) {
2158
+ const operationName = options.operationName || "action";
2159
+ return async (key, action, params) => {
2160
+ if (options.debug) {
2161
+ logger18.debug(`[${operationName}] Called:`, { key, action, params });
2162
+ }
2163
+ if (!options.skipValidation) {
2164
+ validateKey(key, coordinate, operationName);
2165
+ validateActionName(action, operationName);
2166
+ validateOperationParams(params, operationName);
2167
+ }
2168
+ const normalizedParams = params ?? {};
2169
+ try {
2170
+ const result = await implementation(key, action, normalizedParams);
2171
+ if (options.debug) {
2172
+ logger18.debug(`[${operationName}] Action "${action}" completed:`, {
2173
+ itemKey: result[0].key,
2174
+ affectedKeys: result[1].length
2175
+ });
2176
+ }
2177
+ if (!options.skipValidation) {
2178
+ const validatedItem = validatePK(result[0], coordinate.kta[0]);
2179
+ return [validatedItem, result[1]];
2180
+ }
2181
+ return result;
2182
+ } catch (error) {
2183
+ if (options.onError) {
2184
+ const context = {
2185
+ operationName,
2186
+ params: [key, action, params],
2187
+ coordinate
2188
+ };
2189
+ throw options.onError(error, context);
2190
+ }
2191
+ throw new Error(
2192
+ `[${operationName}] Action "${action}" failed: ${error.message}`,
2193
+ { cause: error }
2194
+ );
2195
+ }
2196
+ };
2197
+ }
2198
+
2199
+ // src/operations/wrappers/createAllActionWrapper.ts
2200
+ var logger19 = logger_default.get("operations", "wrappers", "allAction");
2201
+ function createAllActionWrapper(coordinate, implementation, options = {}) {
2202
+ const operationName = options.operationName || "allAction";
2203
+ return async (action, params, locations) => {
2204
+ if (options.debug) {
2205
+ logger19.debug(`[${operationName}] Called:`, { action, params, locations });
2206
+ }
2207
+ if (!options.skipValidation) {
2208
+ validateActionName(action, operationName);
2209
+ validateOperationParams(params, operationName);
2210
+ validateLocations(locations, coordinate, operationName);
2211
+ }
2212
+ const normalizedParams = params ?? {};
2213
+ const normalizedLocations = locations ?? [];
2214
+ try {
2215
+ const result = await implementation(action, normalizedParams, normalizedLocations);
2216
+ if (options.debug) {
2217
+ logger19.debug(`[${operationName}] Action "${action}" completed:`, {
2218
+ itemsAffected: result[0].length,
2219
+ affectedKeys: result[1].length
2220
+ });
2221
+ }
2222
+ if (!options.skipValidation) {
2223
+ const validatedItems = validatePK(result[0], coordinate.kta[0]);
2224
+ return [validatedItems, result[1]];
2225
+ }
2226
+ return result;
2227
+ } catch (error) {
2228
+ if (options.onError) {
2229
+ const context = {
2230
+ operationName,
2231
+ params: [action, params, locations],
2232
+ coordinate
2233
+ };
2234
+ throw options.onError(error, context);
2235
+ }
2236
+ throw new Error(
2237
+ `[${operationName}] Action "${action}" failed: ${error.message}`,
2238
+ { cause: error }
2239
+ );
2240
+ }
2241
+ };
2242
+ }
2243
+
2244
+ // src/operations/wrappers/createFacetWrapper.ts
2245
+ var logger20 = logger_default.get("operations", "wrappers", "facet");
2246
+ function createFacetWrapper(coordinate, implementation, options = {}) {
2247
+ const operationName = options.operationName || "facet";
2248
+ return async (key, facet, params) => {
2249
+ if (options.debug) {
2250
+ logger20.debug(`[${operationName}] Called:`, { key, facet, params });
2251
+ }
2252
+ if (!options.skipValidation) {
2253
+ validateKey(key, coordinate, operationName);
2254
+ validateFacetName(facet, operationName);
2255
+ validateOperationParams(params, operationName);
2256
+ }
2257
+ const normalizedParams = params ?? {};
2258
+ try {
2259
+ const result = await implementation(key, facet, normalizedParams);
2260
+ if (options.debug) {
2261
+ logger20.debug(`[${operationName}] Facet "${facet}" completed`);
2262
+ }
2263
+ return result;
2264
+ } catch (error) {
2265
+ if (options.onError) {
2266
+ const context = {
2267
+ operationName,
2268
+ params: [key, facet, params],
2269
+ coordinate
2270
+ };
2271
+ throw options.onError(error, context);
2272
+ }
2273
+ throw new Error(
2274
+ `[${operationName}] Facet "${facet}" failed: ${error.message}`,
2275
+ { cause: error }
2276
+ );
2277
+ }
2278
+ };
2279
+ }
2280
+
2281
+ // src/operations/wrappers/createAllFacetWrapper.ts
2282
+ var logger21 = logger_default.get("operations", "wrappers", "allFacet");
2283
+ function createAllFacetWrapper(coordinate, implementation, options = {}) {
2284
+ const operationName = options.operationName || "allFacet";
2285
+ return async (facet, params, locations) => {
2286
+ if (options.debug) {
2287
+ logger21.debug(`[${operationName}] Called:`, { facet, params, locations });
2288
+ }
2289
+ if (!options.skipValidation) {
2290
+ validateFacetName(facet, operationName);
2291
+ validateOperationParams(params, operationName);
2292
+ validateLocations(locations, coordinate, operationName);
2293
+ }
2294
+ const normalizedParams = params ?? {};
2295
+ const normalizedLocations = locations ?? [];
2296
+ try {
2297
+ const result = await implementation(facet, normalizedParams, normalizedLocations);
2298
+ if (options.debug) {
2299
+ logger21.debug(`[${operationName}] Facet "${facet}" completed`);
2300
+ }
2301
+ return result;
2302
+ } catch (error) {
2303
+ if (options.onError) {
2304
+ const context = {
2305
+ operationName,
2306
+ params: [facet, params, locations],
2307
+ coordinate
2308
+ };
2309
+ throw options.onError(error, context);
2310
+ }
2311
+ throw new Error(
2312
+ `[${operationName}] Facet "${facet}" failed: ${error.message}`,
2313
+ { cause: error }
2314
+ );
2315
+ }
2316
+ };
2317
+ }
849
2318
  export {
850
2319
  AItemService,
2320
+ ActionError,
2321
+ BusinessLogicError,
2322
+ DEFAULT_EVENT_CONFIG,
851
2323
  Dictionary,
2324
+ DuplicateError,
2325
+ EVENT_SYSTEM_VERSION,
2326
+ EventEmissionError,
2327
+ EventMatchingError,
2328
+ EventSystemError,
852
2329
  IFactory,
853
2330
  IQFactory,
2331
+ NotFoundError,
2332
+ PermissionError,
2333
+ STANDARD_EVENT_TYPES,
2334
+ STANDARD_SCOPES,
2335
+ SUPPORTED_EVENT_TYPES,
2336
+ SUPPORTED_SCOPES,
2337
+ SubscriptionError,
2338
+ SubscriptionStatus,
2339
+ ValidationError,
854
2340
  abbrevAgg,
855
2341
  abbrevCompoundCondition,
856
2342
  abbrevCondition,
@@ -859,31 +2345,73 @@ export {
859
2345
  abbrevQuery,
860
2346
  abbrevRef,
861
2347
  cPK,
2348
+ compareLocationValues,
862
2349
  constructPriKey,
2350
+ createActionWrapper,
2351
+ createAllActionWrapper,
2352
+ createAllFacetWrapper,
2353
+ createAllWrapper,
2354
+ createCoordinate,
2355
+ createCreateWrapper,
2356
+ createEventSystemError,
2357
+ createFacetWrapper,
2358
+ createFindOneWrapper,
2359
+ createFindWrapper,
2360
+ createGetWrapper,
2361
+ createItemSubscription,
2362
+ createLocationSubscription,
863
2363
  createNormalizedHashFunction,
2364
+ createOneWrapper,
2365
+ createRemoveWrapper,
2366
+ createUpdateWrapper,
2367
+ createUpsertWrapper,
2368
+ doesEventMatchSubscription,
2369
+ doesEventTypeMatch,
2370
+ doesKeyMatch,
2371
+ doesKeyMatchLocation,
2372
+ doesLocationMatch,
2373
+ doesScopeMatch,
2374
+ enhanceError,
2375
+ executeWithContext,
2376
+ executeWithContextSync,
864
2377
  extractKeyTypeArray,
2378
+ extractLocationValues,
2379
+ findMatchingSubscriptions,
865
2380
  generateKeyArray,
2381
+ generateSubscriptionId,
2382
+ getErrorInfo,
866
2383
  ikToLKA,
2384
+ isActionError,
2385
+ isActionEvent,
867
2386
  isComItem,
868
2387
  isComKey,
869
2388
  isComKeyEqual,
870
2389
  isComKeyEqualNormalized,
871
2390
  isCondition,
2391
+ isCreateEvent,
2392
+ isDeleteEvent,
2393
+ isEventSystemError,
872
2394
  isItemKey,
873
2395
  isItemKeyEqual,
874
2396
  isItemKeyEqualNormalized,
2397
+ isItemSubscription,
875
2398
  isLocKey,
876
2399
  isLocKeyEqual,
877
2400
  isLocKeyEqualNormalized,
2401
+ isLocationSubscription,
2402
+ isComKey2 as isOperationComKey,
2403
+ isPriKey2 as isOperationPriKey,
878
2404
  isPriItem,
879
2405
  isPriKey,
880
2406
  isPriKeyEqual,
881
2407
  isPriKeyEqualNormalized,
882
2408
  isQueryMatch,
2409
+ isUpdateEvent,
883
2410
  isValidComKey,
884
2411
  isValidItemKey,
885
2412
  isValidLocKey,
886
2413
  isValidLocKeyArray,
2414
+ isValidLocations,
887
2415
  isValidPriKey,
888
2416
  itemKeyToLocKeyArray,
889
2417
  lkaToIK,
@@ -892,6 +2420,15 @@ export {
892
2420
  primaryType,
893
2421
  queryToParams,
894
2422
  toKeyTypeArray,
2423
+ validateActionName,
2424
+ validateComKey,
2425
+ validateFacetName,
2426
+ validateFinderName,
2427
+ validateKey,
895
2428
  validateKeys,
896
- validatePK
2429
+ validateLocations,
2430
+ validateOperationParams,
2431
+ validatePK,
2432
+ validatePriKey,
2433
+ validateQuery
897
2434
  };