@fjell/core 4.4.47 → 4.4.49

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 (83) 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/index.d.ts +11 -0
  11. package/dist/index.js +1314 -49
  12. package/dist/item/IUtils.d.ts +6 -3
  13. package/dist/operations/OperationContext.d.ts +10 -0
  14. package/dist/operations/Operations.d.ts +259 -0
  15. package/dist/operations/contained.d.ts +65 -0
  16. package/dist/operations/errorEnhancer.d.ts +79 -0
  17. package/dist/operations/index.d.ts +2 -0
  18. package/dist/operations/methods.d.ts +134 -0
  19. package/dist/operations/primary.d.ts +57 -0
  20. package/dist/operations/specialized.d.ts +41 -0
  21. package/dist/operations/wrappers/createActionWrapper.d.ts +28 -0
  22. package/dist/operations/wrappers/createAllActionWrapper.d.ts +28 -0
  23. package/dist/operations/wrappers/createAllFacetWrapper.d.ts +27 -0
  24. package/dist/operations/wrappers/createAllWrapper.d.ts +28 -0
  25. package/dist/operations/wrappers/createCreateWrapper.d.ts +28 -0
  26. package/dist/operations/wrappers/createFacetWrapper.d.ts +27 -0
  27. package/dist/operations/wrappers/createFindOneWrapper.d.ts +28 -0
  28. package/dist/operations/wrappers/createFindWrapper.d.ts +28 -0
  29. package/dist/operations/wrappers/createGetWrapper.d.ts +28 -0
  30. package/dist/operations/wrappers/createOneWrapper.d.ts +38 -0
  31. package/dist/operations/wrappers/createRemoveWrapper.d.ts +28 -0
  32. package/dist/operations/wrappers/createUpdateWrapper.d.ts +28 -0
  33. package/dist/operations/wrappers/createUpsertWrapper.d.ts +28 -0
  34. package/dist/operations/wrappers/index.d.ts +34 -0
  35. package/dist/operations/wrappers/types.d.ts +48 -0
  36. package/dist/validation/ItemValidator.d.ts +43 -0
  37. package/dist/validation/KeyValidator.d.ts +56 -0
  38. package/dist/validation/LocationValidator.d.ts +39 -0
  39. package/dist/validation/QueryValidator.d.ts +57 -0
  40. package/dist/validation/index.d.ts +15 -0
  41. package/dist/validation/index.js +501 -0
  42. package/dist/validation/types.d.ts +38 -0
  43. package/package.json +7 -2
  44. package/src/Coordinate.ts +35 -0
  45. package/src/errors/ActionError.ts +69 -0
  46. package/src/errors/BusinessLogicError.ts +24 -0
  47. package/src/errors/DuplicateError.ts +57 -0
  48. package/src/errors/NotFoundError.ts +24 -0
  49. package/src/errors/PermissionError.ts +31 -0
  50. package/src/errors/ValidationError.ts +27 -0
  51. package/src/errors/index.ts +7 -0
  52. package/src/index.ts +53 -0
  53. package/src/item/IUtils.ts +9 -80
  54. package/src/key/KUtils.ts +2 -2
  55. package/src/operations/OperationContext.ts +12 -0
  56. package/src/operations/Operations.ts +357 -0
  57. package/src/operations/contained.ts +134 -0
  58. package/src/operations/errorEnhancer.ts +204 -0
  59. package/src/operations/index.ts +2 -0
  60. package/src/operations/methods.ts +363 -0
  61. package/src/operations/primary.ts +101 -0
  62. package/src/operations/specialized.ts +71 -0
  63. package/src/operations/wrappers/createActionWrapper.ts +108 -0
  64. package/src/operations/wrappers/createAllActionWrapper.ts +109 -0
  65. package/src/operations/wrappers/createAllFacetWrapper.ts +98 -0
  66. package/src/operations/wrappers/createAllWrapper.ts +103 -0
  67. package/src/operations/wrappers/createCreateWrapper.ts +117 -0
  68. package/src/operations/wrappers/createFacetWrapper.ts +97 -0
  69. package/src/operations/wrappers/createFindOneWrapper.ts +105 -0
  70. package/src/operations/wrappers/createFindWrapper.ts +105 -0
  71. package/src/operations/wrappers/createGetWrapper.ts +96 -0
  72. package/src/operations/wrappers/createOneWrapper.ts +128 -0
  73. package/src/operations/wrappers/createRemoveWrapper.ts +91 -0
  74. package/src/operations/wrappers/createUpdateWrapper.ts +106 -0
  75. package/src/operations/wrappers/createUpsertWrapper.ts +108 -0
  76. package/src/operations/wrappers/index.ts +39 -0
  77. package/src/operations/wrappers/types.ts +63 -0
  78. package/src/validation/ItemValidator.ts +131 -0
  79. package/src/validation/KeyValidator.ts +365 -0
  80. package/src/validation/LocationValidator.ts +136 -0
  81. package/src/validation/QueryValidator.ts +250 -0
  82. package/src/validation/index.ts +32 -0
  83. package/src/validation/types.ts +45 -0
@@ -1,7 +1,10 @@
1
1
  import { Item } from "../items";
2
- import { AllItemTypeArrays, ComKey, PriKey } from "../keys";
3
- export declare const validatePK: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(input: Item<S, L1, L2, L3, L4, L5> | Item<S, L1, L2, L3, L4, L5>[], pkType: S) => Item<S, L1, L2, L3, L4, L5> | Item<S, L1, L2, L3, L4, L5>[];
4
- export declare const validateKeys: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(item: Item<S, L1, L2, L3, L4, L5>, keyTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>) => Item<S, L1, L2, L3, L4, L5>;
2
+ import { ComKey, PriKey } from "../keys";
3
+ /**
4
+ * @deprecated Use validatePK from @fjell/core/validation instead
5
+ * Re-exported for backward compatibility
6
+ */
7
+ export { validatePK, validateKeys } from '../validation/ItemValidator';
5
8
  export declare const isPriItem: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(item: Item<S, L1, L2, L3, L4, L5>) => item is Item<S> & {
6
9
  key: PriKey<S>;
7
10
  };
@@ -0,0 +1,10 @@
1
+ import { ComKey, LocKeyArray, PriKey } from '../keys';
2
+ import type { ErrorInfo } from '../errors/ActionError';
3
+ export interface OperationContext {
4
+ itemType: string;
5
+ operationType: ErrorInfo['operation']['type'];
6
+ operationName: string;
7
+ params: Record<string, any>;
8
+ key?: PriKey<any> | ComKey<any, any, any, any, any, any>;
9
+ locations?: LocKeyArray<any, any, any, any, any>;
10
+ }
@@ -0,0 +1,259 @@
1
+ import { Item } from "../items";
2
+ import { ComKey, LocKeyArray, PriKey } from "../keys";
3
+ import { ItemQuery } from "../item/ItemQuery";
4
+ /**
5
+ * Standard operation parameters type
6
+ */
7
+ export type OperationParams = Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>;
8
+ /**
9
+ * Type for affected keys returned by actions
10
+ */
11
+ export type AffectedKeys = Array<PriKey<any> | ComKey<any, any, any, any, any, any> | LocKeyArray<any, any, any, any, any>>;
12
+ /**
13
+ * Options for create operation
14
+ */
15
+ export type CreateOptions<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = {
16
+ key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>;
17
+ locations?: never;
18
+ } | {
19
+ key?: never;
20
+ locations: LocKeyArray<L1, L2, L3, L4, L5>;
21
+ };
22
+ /**
23
+ * Core Operations interface for Item-based data access.
24
+ * This interface defines the standard contract for all fjell libraries
25
+ * that operate on Items.
26
+ *
27
+ * @template V - The Item type
28
+ * @template S - The primary key type
29
+ * @template L1-L5 - Location key types (optional, for contained items)
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * // Primary item operations
34
+ * const userOps: Operations<User, 'user'> = ...;
35
+ * const users = await userOps.all();
36
+ *
37
+ * // Contained item operations
38
+ * const commentOps: Operations<Comment, 'comment', 'post'> = ...;
39
+ * const comments = await commentOps.all({}, [{kt: 'post', lk: 'post-123'}]);
40
+ * ```
41
+ */
42
+ export interface Operations<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
43
+ /**
44
+ * Retrieves all items matching the query.
45
+ *
46
+ * @param query - Optional query to filter items
47
+ * @param locations - Optional location hierarchy to scope the query
48
+ * @returns Array of items matching the query
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * // Get all users
53
+ * const users = await operations.all();
54
+ *
55
+ * // Get users with filter
56
+ * const activeUsers = await operations.all({ filter: { status: 'active' } });
57
+ *
58
+ * // Get items in specific location
59
+ * const comments = await operations.all({}, [{kt: 'post', lk: 'post-123'}]);
60
+ * ```
61
+ */
62
+ all(query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
63
+ /**
64
+ * Retrieves the first item matching the query.
65
+ *
66
+ * @param query - Optional query to filter items
67
+ * @param locations - Optional location hierarchy to scope the query
68
+ * @returns Single item or null if not found
69
+ */
70
+ one(query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
71
+ /**
72
+ * Creates a new item.
73
+ *
74
+ * @param item - Partial item properties for creation
75
+ * @param options - Optional key or locations for the new item
76
+ * @returns The created item
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * // Create with auto-generated key
81
+ * const user = await operations.create({ name: 'Alice' });
82
+ *
83
+ * // Create with specific key
84
+ * const user = await operations.create(
85
+ * { name: 'Alice' },
86
+ * { key: { kt: 'user', pk: 'alice-123' } }
87
+ * );
88
+ *
89
+ * // Create in specific location
90
+ * const comment = await operations.create(
91
+ * { text: 'Great post!' },
92
+ * { locations: [{kt: 'post', lk: 'post-123'}] }
93
+ * );
94
+ * ```
95
+ */
96
+ create(item: Partial<Item<S, L1, L2, L3, L4, L5>>, options?: CreateOptions<S, L1, L2, L3, L4, L5>): Promise<V>;
97
+ /**
98
+ * Retrieves a single item by its key.
99
+ *
100
+ * @param key - Primary or composite key
101
+ * @returns The item or null if not found
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * // Get by primary key
106
+ * const user = await operations.get({ kt: 'user', pk: 'user-123' });
107
+ *
108
+ * // Get by composite key
109
+ * const comment = await operations.get({
110
+ * kt: 'comment',
111
+ * pk: 'comment-456',
112
+ * loc: [{kt: 'post', lk: 'post-123'}]
113
+ * });
114
+ * ```
115
+ */
116
+ get(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): Promise<V | null>;
117
+ /**
118
+ * Updates an existing item.
119
+ *
120
+ * @param key - Primary or composite key
121
+ * @param item - Partial item properties to update
122
+ * @returns The updated item
123
+ */
124
+ update(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>): Promise<V>;
125
+ /**
126
+ * Updates an item if it exists, creates it if it doesn't.
127
+ *
128
+ * @param key - Primary or composite key
129
+ * @param item - Partial item properties
130
+ * @param locations - Optional locations for creation
131
+ * @returns The upserted item
132
+ */
133
+ upsert(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>, locations?: LocKeyArray<L1, L2, L3, L4, L5>): Promise<V>;
134
+ /**
135
+ * Removes an item.
136
+ *
137
+ * @param key - Primary or composite key
138
+ * @returns The removed item or void
139
+ */
140
+ remove(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): Promise<V | void>;
141
+ /**
142
+ * Executes a finder method by name.
143
+ *
144
+ * @param finder - Name of the finder method
145
+ * @param params - Parameters for the finder
146
+ * @param locations - Optional location hierarchy to scope the query
147
+ * @returns Array of items found
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * // Find users by email
152
+ * const users = await operations.find('byEmail', { email: 'alice@example.com' });
153
+ *
154
+ * // Find in specific location
155
+ * const comments = await operations.find(
156
+ * 'byAuthor',
157
+ * { author: 'alice' },
158
+ * [{kt: 'post', lk: 'post-123'}]
159
+ * );
160
+ * ```
161
+ */
162
+ find(finder: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
163
+ /**
164
+ * Executes a finder method and returns the first result.
165
+ *
166
+ * @param finder - Name of the finder method
167
+ * @param params - Parameters for the finder
168
+ * @param locations - Optional location hierarchy to scope the query
169
+ * @returns Single item or null
170
+ */
171
+ findOne(finder: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
172
+ /**
173
+ * Executes an action on a specific item.
174
+ * Actions are operations that may have side effects or modify the item.
175
+ *
176
+ * @param key - Primary or composite key
177
+ * @param action - Name of the action
178
+ * @param params - Parameters for the action
179
+ * @returns Tuple of [updated item, affected keys for cache invalidation]
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * const [user, affectedKeys] = await operations.action(
184
+ * { kt: 'user', pk: 'user-123' },
185
+ * 'promote',
186
+ * { role: 'admin' }
187
+ * );
188
+ * ```
189
+ */
190
+ action(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, action: string, params?: OperationParams): Promise<[V, AffectedKeys]>;
191
+ /**
192
+ * Executes an action on all items matching criteria.
193
+ *
194
+ * @param action - Name of the action
195
+ * @param params - Parameters for the action
196
+ * @param locations - Optional location hierarchy to scope the action
197
+ * @returns Tuple of [updated items, affected keys for cache invalidation]
198
+ */
199
+ allAction(action: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[V[], AffectedKeys]>;
200
+ /**
201
+ * Executes a facet query on a specific item.
202
+ * Facets are read-only computed views of item data.
203
+ *
204
+ * @param key - Primary or composite key
205
+ * @param facet - Name of the facet
206
+ * @param params - Parameters for the facet
207
+ * @returns Facet result data
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * const stats = await operations.facet(
212
+ * { kt: 'user', pk: 'user-123' },
213
+ * 'statistics',
214
+ * { period: 'month' }
215
+ * );
216
+ * ```
217
+ */
218
+ facet(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, facet: string, params?: OperationParams): Promise<any>;
219
+ /**
220
+ * Executes a facet query on all items matching criteria.
221
+ * Facets are read-only computed views of item data.
222
+ *
223
+ * @param facet - Name of the facet
224
+ * @param params - Parameters for the facet
225
+ * @param locations - Optional location hierarchy to scope the query
226
+ * @returns Facet result data
227
+ */
228
+ allFacet(facet: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<any>;
229
+ }
230
+ /**
231
+ * Type guard to check if key is a PriKey
232
+ *
233
+ * @param key - Key to check
234
+ * @returns true if key is a PriKey
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * if (isPriKey(key)) {
239
+ * // key is PriKey<S>
240
+ * console.log(key.kt, key.pk);
241
+ * }
242
+ * ```
243
+ */
244
+ export declare function isPriKey<S extends string>(key: PriKey<S> | ComKey<S, any, any, any, any, any>): key is PriKey<S>;
245
+ /**
246
+ * Type guard to check if key is a ComKey
247
+ *
248
+ * @param key - Key to check
249
+ * @returns true if key is a ComKey
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * if (isComKey(key)) {
254
+ * // key is ComKey<S, L1, L2, L3, L4, L5>
255
+ * console.log(key.kt, key.pk, key.loc);
256
+ * }
257
+ * ```
258
+ */
259
+ export declare function isComKey<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): key is ComKey<S, L1, L2, L3, L4, L5>;
@@ -0,0 +1,65 @@
1
+ import { Item } from "../items";
2
+ import { ComKey, LocKeyArray } from "../keys";
3
+ import { ItemQuery } from "../item/ItemQuery";
4
+ import { AffectedKeys, OperationParams, Operations } from "./Operations";
5
+ /**
6
+ * Contained Operations interface - specialized for contained (hierarchical) items only.
7
+ *
8
+ * This interface narrows the generic Operations interface to work exclusively
9
+ * with ComKey and requires locations for collection operations.
10
+ *
11
+ * Contained items exist within a location hierarchy and belong to parent items.
12
+ * Examples: Comments (in Posts), Annotations (in Documents), Tasks (in Projects)
13
+ *
14
+ * Use this for:
15
+ * - Libraries that store contained items
16
+ * - Caches for contained items
17
+ * - API clients for contained endpoints
18
+ * - Any operations that work with hierarchical data
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Define a contained item type
23
+ * interface Comment extends Item<'comment', 'post'> {
24
+ * text: string;
25
+ * author: string;
26
+ * }
27
+ *
28
+ * // Create operations for contained items
29
+ * const commentOps: ContainedOperations<Comment, 'comment', 'post'> = createCommentOperations();
30
+ *
31
+ * // Only ComKey allowed - includes location hierarchy
32
+ * await commentOps.get({
33
+ * kt: 'comment',
34
+ * pk: '123',
35
+ * loc: [{ kt: 'post', lk: 'post-1' }]
36
+ * });
37
+ *
38
+ * await commentOps.update(
39
+ * { kt: 'comment', pk: '123', loc: [{ kt: 'post', lk: 'post-1' }] },
40
+ * { text: 'Updated comment' }
41
+ * );
42
+ *
43
+ * // Locations required on collection methods
44
+ * await commentOps.all({}, [{ kt: 'post', lk: 'post-1' }]);
45
+ * await commentOps.find('recent', {}, [{ kt: 'post', lk: 'post-1' }]);
46
+ * await commentOps.allAction('archive', {}, [{ kt: 'post', lk: 'post-1' }]);
47
+ * ```
48
+ */
49
+ export interface ContainedOperations<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends Omit<Operations<V, S, L1, L2, L3, L4, L5>, 'get' | 'update' | 'remove' | 'upsert' | 'create' | 'action' | 'facet' | 'allAction' | 'allFacet' | 'all' | 'one' | 'find' | 'findOne'> {
50
+ all(query: ItemQuery | undefined, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
51
+ one(query: ItemQuery | undefined, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
52
+ find(finder: string, params: OperationParams | undefined, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
53
+ findOne(finder: string, params: OperationParams | undefined, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
54
+ create(item: Partial<Item<S, L1, L2, L3, L4, L5>>, options?: {
55
+ locations?: LocKeyArray<L1, L2, L3, L4, L5>;
56
+ }): Promise<V>;
57
+ get(key: ComKey<S, L1, L2, L3, L4, L5>): Promise<V | null>;
58
+ update(key: ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>): Promise<V>;
59
+ upsert(key: ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>): Promise<V>;
60
+ remove(key: ComKey<S, L1, L2, L3, L4, L5>): Promise<V | void>;
61
+ action(key: ComKey<S, L1, L2, L3, L4, L5>, action: string, params?: OperationParams): Promise<[V, AffectedKeys]>;
62
+ allAction(action: string, params: OperationParams | undefined, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[V[], AffectedKeys]>;
63
+ facet(key: ComKey<S, L1, L2, L3, L4, L5>, facet: string, params?: OperationParams): Promise<any>;
64
+ allFacet(facet: string, params: OperationParams | undefined, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<any>;
65
+ }
@@ -0,0 +1,79 @@
1
+ import { ActionError } from "../errors/ActionError";
2
+ import { OperationContext } from "./OperationContext";
3
+ /**
4
+ * Executes an async operation and enhances any ActionError thrown with operation context.
5
+ *
6
+ * This is the primary utility for wrapping operations across all fjell packages.
7
+ *
8
+ * @param operation - The async operation to execute
9
+ * @param context - The operation context to add to any errors
10
+ * @returns The result of the operation
11
+ * @throws Enhanced ActionError or original error
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // In fjell/lib
16
+ * return executeWithContext(
17
+ * () => toWrap.get(key),
18
+ * {
19
+ * itemType: 'user',
20
+ * operationType: 'get',
21
+ * operationName: 'get',
22
+ * params: { key },
23
+ * key
24
+ * }
25
+ * );
26
+ *
27
+ * // In express-router
28
+ * return executeWithContext(
29
+ * () => operations.action('approve', params, key),
30
+ * {
31
+ * itemType: 'invoice',
32
+ * operationType: 'action',
33
+ * operationName: 'approve',
34
+ * params,
35
+ * key
36
+ * }
37
+ * );
38
+ * ```
39
+ */
40
+ export declare function executeWithContext<T>(operation: () => Promise<T>, context: OperationContext): Promise<T>;
41
+ /**
42
+ * Synchronous version of executeWithContext for non-async operations.
43
+ *
44
+ * @param operation - The sync operation to execute
45
+ * @param context - The operation context to add to any errors
46
+ * @returns The result of the operation
47
+ * @throws Enhanced ActionError or original error
48
+ */
49
+ export declare function executeWithContextSync<T>(operation: () => T, context: OperationContext): T;
50
+ /**
51
+ * Enhances an error with operation context if it's an ActionError.
52
+ * If it's not an ActionError, returns the original error unchanged.
53
+ *
54
+ * This allows database implementations and business logic to throw
55
+ * ActionErrors with minimal context, and have that context filled in
56
+ * by the operation wrapper.
57
+ *
58
+ * @param error - The error to enhance
59
+ * @param context - The operation context to add
60
+ * @returns The enhanced or original error
61
+ */
62
+ export declare function enhanceError(error: Error, context: OperationContext): Error;
63
+ /**
64
+ * Type guard to check if an error is an ActionError.
65
+ *
66
+ * @param error - The error to check
67
+ * @returns True if the error is an ActionError
68
+ */
69
+ export declare function isActionError(error: unknown): error is ActionError;
70
+ /**
71
+ * Extracts error info from an ActionError, or creates a generic error info
72
+ * for non-ActionErrors.
73
+ *
74
+ * Useful for logging and error reporting.
75
+ *
76
+ * @param error - The error to extract info from
77
+ * @returns The error info
78
+ */
79
+ export declare function getErrorInfo(error: unknown): any;
@@ -0,0 +1,2 @@
1
+ export * from './OperationContext';
2
+ export * from './errorEnhancer';
@@ -0,0 +1,134 @@
1
+ import { Item } from "../items";
2
+ import { ComKey, LocKeyArray, PriKey } from "../keys";
3
+ import { ItemQuery } from "../item/ItemQuery";
4
+ import { AffectedKeys, CreateOptions, OperationParams } from "./Operations";
5
+ /**
6
+ * Get method signature - retrieves single item by key
7
+ */
8
+ export interface GetMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
9
+ (key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): Promise<V | null>;
10
+ }
11
+ /**
12
+ * Create method signature - creates new item
13
+ */
14
+ export interface CreateMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
15
+ (item: Partial<Item<S, L1, L2, L3, L4, L5>>, options?: CreateOptions<S, L1, L2, L3, L4, L5>): Promise<V>;
16
+ }
17
+ /**
18
+ * Update method signature - updates existing item
19
+ */
20
+ export interface UpdateMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
21
+ (key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>): Promise<V>;
22
+ }
23
+ /**
24
+ * Remove method signature - removes item
25
+ */
26
+ export interface RemoveMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
27
+ (key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): Promise<V | void>;
28
+ }
29
+ /**
30
+ * Upsert method signature - updates or creates item
31
+ */
32
+ export interface UpsertMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
33
+ (key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>): Promise<V>;
34
+ }
35
+ /**
36
+ * All method signature - retrieves all items matching query
37
+ */
38
+ export interface AllMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
39
+ (query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
40
+ }
41
+ /**
42
+ * One method signature - retrieves first item matching query
43
+ */
44
+ export interface OneMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
45
+ (query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
46
+ }
47
+ /**
48
+ * Find method signature - finds multiple items using finder
49
+ */
50
+ export interface FindMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
51
+ (finder: string, params: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
52
+ }
53
+ /**
54
+ * FindOne method signature - finds single item using finder
55
+ */
56
+ export interface FindOneMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
57
+ (finder: string, params: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
58
+ }
59
+ /**
60
+ * Finder method signature - finds multiple items
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const byEmailFinder: FinderMethod<User, 'user'> = async (params) => {
65
+ * return await database.findUsers({ email: params.email });
66
+ * };
67
+ * ```
68
+ */
69
+ export interface FinderMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
70
+ (params: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
71
+ }
72
+ /**
73
+ * Action operation method signature - executes action on specific item by key
74
+ */
75
+ export interface ActionOperationMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
76
+ (key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, action: string, params?: OperationParams): Promise<[V, AffectedKeys]>;
77
+ }
78
+ /**
79
+ * Action method signature - user-defined action implementation
80
+ */
81
+ export interface ActionMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
82
+ (item: V, params: OperationParams): Promise<[V, AffectedKeys]>;
83
+ }
84
+ /**
85
+ * AllAction operation method signature - executes action on all items
86
+ */
87
+ export interface AllActionOperationMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
88
+ (action: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[V[], AffectedKeys]>;
89
+ }
90
+ /**
91
+ * All-action method signature - user-defined all-action implementation
92
+ */
93
+ export interface AllActionMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
94
+ (params: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[V[], AffectedKeys]>;
95
+ }
96
+ /**
97
+ * Facet operation method signature - executes facet on specific item by key
98
+ */
99
+ export interface FacetOperationMethod<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
100
+ (key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, facet: string, params?: OperationParams): Promise<any>;
101
+ }
102
+ /**
103
+ * Facet method signature - user-defined facet implementation
104
+ */
105
+ export interface FacetMethod<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
106
+ (item: V, params: OperationParams): Promise<any>;
107
+ }
108
+ /**
109
+ * AllFacet operation method signature - executes facet on all items
110
+ */
111
+ export interface AllFacetOperationMethod<L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
112
+ (facet: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<any>;
113
+ }
114
+ /**
115
+ * All-facet method signature - user-defined all-facet implementation
116
+ */
117
+ export interface AllFacetMethod<L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
118
+ (params: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<any>;
119
+ }
120
+ /**
121
+ * Extension maps for operations
122
+ */
123
+ export interface OperationsExtensions<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
124
+ /** Registered finder methods */
125
+ finders?: Record<string, FinderMethod<V, S, L1, L2, L3, L4, L5>>;
126
+ /** Registered action methods */
127
+ actions?: Record<string, ActionMethod<V, S, L1, L2, L3, L4, L5>>;
128
+ /** Registered facet methods */
129
+ facets?: Record<string, FacetMethod<V, S, L1, L2, L3, L4, L5>>;
130
+ /** Registered all-action methods */
131
+ allActions?: Record<string, AllActionMethod<V, S, L1, L2, L3, L4, L5>>;
132
+ /** Registered all-facet methods */
133
+ allFacets?: Record<string, AllFacetMethod<L1, L2, L3, L4, L5>>;
134
+ }
@@ -0,0 +1,57 @@
1
+ import { Item } from "../items";
2
+ import { PriKey } from "../keys";
3
+ import { ItemQuery } from "../item/ItemQuery";
4
+ import { AffectedKeys, OperationParams, Operations } from "./Operations";
5
+ /**
6
+ * Primary Operations interface - specialized for primary (top-level) items only.
7
+ *
8
+ * This interface narrows the generic Operations interface to work exclusively
9
+ * with PriKey. All operations accept only PriKey, never ComKey or locations.
10
+ *
11
+ * Primary items are top-level entities that don't belong to a location hierarchy.
12
+ * Examples: Users, Organizations, Products, Documents
13
+ *
14
+ * Use this for:
15
+ * - Libraries that store primary items
16
+ * - Caches for primary items
17
+ * - API clients for primary endpoints
18
+ * - Any operations that work with non-hierarchical data
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Define a primary item type
23
+ * interface User extends Item<'user'> {
24
+ * name: string;
25
+ * email: string;
26
+ * }
27
+ *
28
+ * // Create operations for primary items
29
+ * const userOps: PrimaryOperations<User, 'user'> = createUserOperations();
30
+ *
31
+ * // Only PriKey allowed - no locations
32
+ * await userOps.get({ kt: 'user', pk: '123' });
33
+ * await userOps.update({ kt: 'user', pk: '123' }, { name: 'Updated' });
34
+ *
35
+ * // No locations parameter on collection methods
36
+ * await userOps.all({});
37
+ * await userOps.find('byEmail', { email: 'test@example.com' });
38
+ * await userOps.allAction('deactivate', { reason: 'inactive' });
39
+ * ```
40
+ */
41
+ export interface PrimaryOperations<V extends Item<S>, S extends string> extends Omit<Operations<V, S>, 'all' | 'one' | 'create' | 'get' | 'update' | 'upsert' | 'remove' | 'find' | 'findOne' | 'action' | 'allAction' | 'facet' | 'allFacet'> {
42
+ all(query?: ItemQuery): Promise<V[]>;
43
+ one(query?: ItemQuery): Promise<V | null>;
44
+ find(finder: string, params?: OperationParams): Promise<V[]>;
45
+ findOne(finder: string, params?: OperationParams): Promise<V | null>;
46
+ create(item: Partial<Item<S>>, options?: {
47
+ key?: PriKey<S>;
48
+ }): Promise<V>;
49
+ get(key: PriKey<S>): Promise<V | null>;
50
+ update(key: PriKey<S>, item: Partial<Item<S>>): Promise<V>;
51
+ upsert(key: PriKey<S>, item: Partial<Item<S>>): Promise<V>;
52
+ remove(key: PriKey<S>): Promise<V | void>;
53
+ action(key: PriKey<S>, action: string, params?: OperationParams): Promise<[V, AffectedKeys]>;
54
+ allAction(action: string, params?: OperationParams): Promise<[V[], AffectedKeys]>;
55
+ facet(key: PriKey<S>, facet: string, params?: OperationParams): Promise<any>;
56
+ allFacet(facet: string, params?: OperationParams): Promise<any>;
57
+ }
@@ -0,0 +1,41 @@
1
+ import { Item } from "../items";
2
+ import { ComKey, LocKeyArray, PriKey } from "../keys";
3
+ import { ItemQuery } from "../item/ItemQuery";
4
+ import { AffectedKeys, OperationParams } from "./Operations";
5
+ /**
6
+ * Specialized Operations Interfaces
7
+ *
8
+ * This file contains operation interfaces for specific usage patterns.
9
+ * These are primarily used in UI layers (like React providers) to group
10
+ * operations by their usage pattern rather than data structure.
11
+ *
12
+ * For core architectural patterns (Primary vs. Contained), see:
13
+ * - primary.ts - Operations for top-level items
14
+ * - contained.ts - Operations for hierarchical items
15
+ */
16
+ /**
17
+ * Collection-focused operations interface.
18
+ * Optimized for working with groups of items.
19
+ * Used by React providers for array/list contexts.
20
+ */
21
+ export interface CollectionOperations<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
22
+ all(query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
23
+ one(query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
24
+ find(finder: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V[]>;
25
+ findOne(finder: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V | null>;
26
+ create(item: Partial<Item<S, L1, L2, L3, L4, L5>>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<V>;
27
+ allAction(action: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[V[], AffectedKeys]>;
28
+ allFacet(facet: string, params?: OperationParams, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<any>;
29
+ }
30
+ /**
31
+ * Instance-focused operations interface.
32
+ * Optimized for working with a single specific item.
33
+ * Used by React providers for single-item contexts.
34
+ */
35
+ export interface InstanceOperations<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
36
+ get(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): Promise<V | null>;
37
+ update(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item: Partial<Item<S, L1, L2, L3, L4, L5>>): Promise<V>;
38
+ remove(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): Promise<V | void>;
39
+ action(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, action: string, params?: OperationParams): Promise<[V, AffectedKeys]>;
40
+ facet(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, facet: string, params?: OperationParams): Promise<any>;
41
+ }