@fjell/core 4.4.49 → 4.4.51
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.
- package/dist/event/emitter.d.ts +140 -0
- package/dist/event/events.d.ts +81 -0
- package/dist/event/index.d.ts +38 -0
- package/dist/event/matching.d.ts +54 -0
- package/dist/event/subscription.d.ts +74 -0
- package/dist/event/types.d.ts +186 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +272 -0
- package/package.json +3 -3
- package/src/AItemService.ts +0 -38
- package/src/Coordinate.ts +0 -35
- package/src/dictionary.ts +0 -84
- package/src/errors/ActionError.ts +0 -69
- package/src/errors/BusinessLogicError.ts +0 -24
- package/src/errors/DuplicateError.ts +0 -57
- package/src/errors/NotFoundError.ts +0 -24
- package/src/errors/PermissionError.ts +0 -31
- package/src/errors/ValidationError.ts +0 -27
- package/src/errors/index.ts +0 -7
- package/src/index.ts +0 -66
- package/src/item/IFactory.ts +0 -122
- package/src/item/IQFactory.ts +0 -163
- package/src/item/IQUtils.ts +0 -392
- package/src/item/IUtils.ts +0 -40
- package/src/item/ItemQuery.ts +0 -88
- package/src/items.ts +0 -120
- package/src/key/KUtils.ts +0 -484
- package/src/keys.ts +0 -95
- package/src/logger.ts +0 -5
- package/src/operations/OperationContext.ts +0 -12
- package/src/operations/Operations.ts +0 -357
- package/src/operations/contained.ts +0 -134
- package/src/operations/errorEnhancer.ts +0 -204
- package/src/operations/index.ts +0 -2
- package/src/operations/methods.ts +0 -363
- package/src/operations/primary.ts +0 -101
- package/src/operations/specialized.ts +0 -71
- package/src/operations/wrappers/createActionWrapper.ts +0 -108
- package/src/operations/wrappers/createAllActionWrapper.ts +0 -109
- package/src/operations/wrappers/createAllFacetWrapper.ts +0 -98
- package/src/operations/wrappers/createAllWrapper.ts +0 -103
- package/src/operations/wrappers/createCreateWrapper.ts +0 -117
- package/src/operations/wrappers/createFacetWrapper.ts +0 -97
- package/src/operations/wrappers/createFindOneWrapper.ts +0 -105
- package/src/operations/wrappers/createFindWrapper.ts +0 -105
- package/src/operations/wrappers/createGetWrapper.ts +0 -96
- package/src/operations/wrappers/createOneWrapper.ts +0 -128
- package/src/operations/wrappers/createRemoveWrapper.ts +0 -91
- package/src/operations/wrappers/createUpdateWrapper.ts +0 -106
- package/src/operations/wrappers/createUpsertWrapper.ts +0 -108
- package/src/operations/wrappers/index.ts +0 -39
- package/src/operations/wrappers/types.ts +0 -63
- package/src/validation/ItemValidator.ts +0 -131
- package/src/validation/KeyValidator.ts +0 -365
- package/src/validation/LocationValidator.ts +0 -136
- package/src/validation/QueryValidator.ts +0 -250
- package/src/validation/index.ts +0 -32
- package/src/validation/types.ts +0 -45
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { ComKey, ItemTypeArray, LocKeyArray, PriKey } from '../keys';
|
|
2
|
+
import { Item } from '../items';
|
|
3
|
+
import { BaseEvent } from './events';
|
|
4
|
+
import { Subscription, SubscriptionOptions } from './subscription';
|
|
5
|
+
/**
|
|
6
|
+
* Core EventEmitter interface that storage libraries implement.
|
|
7
|
+
* Each item type gets its own EventEmitter instance for full type safety.
|
|
8
|
+
* Libraries implement separate EventEmitters per item type (UserEventEmitter, MessageEventEmitter, etc.)
|
|
9
|
+
*/
|
|
10
|
+
export interface EventEmitter<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
11
|
+
/**
|
|
12
|
+
* Emit a generic event with full control over event properties.
|
|
13
|
+
* Libraries can use this for custom events or when they need full control.
|
|
14
|
+
*/
|
|
15
|
+
emit(event: BaseEvent<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Emit a create event when an item is created.
|
|
18
|
+
* Convenience method that constructs a properly typed CreateEvent.
|
|
19
|
+
*/
|
|
20
|
+
emitCreate(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, scopes: string[], item: Item<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Emit an update event when an item is modified.
|
|
23
|
+
* Convenience method that constructs a properly typed UpdateEvent.
|
|
24
|
+
*/
|
|
25
|
+
emitUpdate(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, scopes: string[], changes: string[], before?: Item<S, L1, L2, L3, L4, L5>, after?: Item<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Emit a delete event when an item is deleted.
|
|
28
|
+
* Convenience method that constructs a properly typed DeleteEvent.
|
|
29
|
+
*/
|
|
30
|
+
emitDelete(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, scopes: string[], item?: Item<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Emit an action event when a custom action is performed.
|
|
33
|
+
* Convenience method that constructs a properly typed ActionEvent.
|
|
34
|
+
*/
|
|
35
|
+
emitAction(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, scopes: string[], actionName: string, actionData?: Record<string, unknown>): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Create a scoped emitter that automatically includes the specified scopes.
|
|
38
|
+
* Libraries can use this to avoid passing scopes to every emit call.
|
|
39
|
+
*/
|
|
40
|
+
withScopes(scopes: string[]): ScopedEventEmitter<S, L1, L2, L3, L4, L5>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Scoped EventEmitter that automatically includes configured scopes.
|
|
44
|
+
* Convenience interface for libraries to avoid passing scopes repeatedly.
|
|
45
|
+
*/
|
|
46
|
+
export interface ScopedEventEmitter<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
47
|
+
/** The scopes that will be automatically included in all events */
|
|
48
|
+
readonly scopes: string[];
|
|
49
|
+
/**
|
|
50
|
+
* Emit a generic event with automatic scope inclusion.
|
|
51
|
+
* The event should omit scopes since they'll be added automatically.
|
|
52
|
+
*/
|
|
53
|
+
emit(event: Omit<BaseEvent<S, L1, L2, L3, L4, L5>, 'scopes'>): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Emit a create event with automatic scope inclusion.
|
|
56
|
+
*/
|
|
57
|
+
emitCreate(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item: Item<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Emit an update event with automatic scope inclusion.
|
|
60
|
+
*/
|
|
61
|
+
emitUpdate(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, changes: string[], before?: Item<S, L1, L2, L3, L4, L5>, after?: Item<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Emit a delete event with automatic scope inclusion.
|
|
64
|
+
*/
|
|
65
|
+
emitDelete(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, item?: Item<S, L1, L2, L3, L4, L5>): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Emit an action event with automatic scope inclusion.
|
|
68
|
+
*/
|
|
69
|
+
emitAction(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, actionName: string, actionData?: Record<string, unknown>): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* EventSubscriber interface for subscribing to and receiving events.
|
|
73
|
+
* Each item type gets its own EventSubscriber instance for full type safety.
|
|
74
|
+
*/
|
|
75
|
+
export interface EventSubscriber<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
76
|
+
/**
|
|
77
|
+
* Subscribe to events using a full subscription object.
|
|
78
|
+
* Returns the subscription ID for later unsubscribing.
|
|
79
|
+
*/
|
|
80
|
+
subscribe(subscription: Omit<Subscription<S, L1, L2, L3, L4, L5>, 'id'>): Promise<string>;
|
|
81
|
+
/**
|
|
82
|
+
* Unsubscribe from events using the subscription ID.
|
|
83
|
+
*/
|
|
84
|
+
unsubscribe(subscriptionId: string): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Register a callback to be called when events are received.
|
|
87
|
+
* Multiple callbacks can be registered and they'll all be called.
|
|
88
|
+
*/
|
|
89
|
+
onEvent(callback: (event: BaseEvent<S, L1, L2, L3, L4, L5>) => void): void;
|
|
90
|
+
/**
|
|
91
|
+
* Remove a previously registered event callback.
|
|
92
|
+
*/
|
|
93
|
+
removeEventListener(callback: (event: BaseEvent<S, L1, L2, L3, L4, L5>) => void): void;
|
|
94
|
+
/**
|
|
95
|
+
* Convenience method to subscribe to a specific item.
|
|
96
|
+
* Automatically creates an ItemSubscription with the provided options.
|
|
97
|
+
*/
|
|
98
|
+
subscribeToItem(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, options?: SubscriptionOptions<S, L1, L2, L3, L4, L5>): Promise<string>;
|
|
99
|
+
/**
|
|
100
|
+
* Convenience method to subscribe to a location.
|
|
101
|
+
* Automatically creates a LocationSubscription with the provided options.
|
|
102
|
+
*/
|
|
103
|
+
subscribeToLocation(kta: ItemTypeArray<S, L1, L2, L3, L4, L5>, location: LocKeyArray<L1, L2, L3, L4, L5>, options?: SubscriptionOptions<S, L1, L2, L3, L4, L5>): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Get all currently active subscriptions.
|
|
106
|
+
* Useful for debugging and subscription management.
|
|
107
|
+
*/
|
|
108
|
+
getActiveSubscriptions(): Subscription<S, L1, L2, L3, L4, L5>[];
|
|
109
|
+
/**
|
|
110
|
+
* Check if an event matches any active subscriptions.
|
|
111
|
+
* Used internally by libraries to determine if an event should be processed.
|
|
112
|
+
*/
|
|
113
|
+
matchesSubscription(event: BaseEvent<S, L1, L2, L3, L4, L5>): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Check if an event matches a specific subscription.
|
|
116
|
+
* Used internally for subscription matching logic.
|
|
117
|
+
*/
|
|
118
|
+
matchesSpecificSubscription(event: BaseEvent<S, L1, L2, L3, L4, L5>, subscription: Subscription<S, L1, L2, L3, L4, L5>): boolean;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Combined EventSystem interface that includes both emitter and subscriber.
|
|
122
|
+
* Libraries can implement this interface to provide both event emission and subscription.
|
|
123
|
+
*/
|
|
124
|
+
export interface EventSystem<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
125
|
+
/** Event emitter for publishing events */
|
|
126
|
+
readonly emitter: EventEmitter<S, L1, L2, L3, L4, L5>;
|
|
127
|
+
/** Event subscriber for receiving events */
|
|
128
|
+
readonly subscriber: EventSubscriber<S, L1, L2, L3, L4, L5>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Factory function type for creating EventSystems.
|
|
132
|
+
* Libraries implement this to create properly configured event systems.
|
|
133
|
+
*/
|
|
134
|
+
export type EventSystemFactory<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = (scopes: string[]) => EventSystem<S, L1, L2, L3, L4, L5>;
|
|
135
|
+
export type UserEventEmitter = EventEmitter<'User'>;
|
|
136
|
+
export type UserEventSubscriber = EventSubscriber<'User'>;
|
|
137
|
+
export type UserEventSystem = EventSystem<'User'>;
|
|
138
|
+
export type MessageEventEmitter<L1 extends string, L2 extends string> = EventEmitter<'Message', L1, L2>;
|
|
139
|
+
export type MessageEventSubscriber<L1 extends string, L2 extends string> = EventSubscriber<'Message', L1, L2>;
|
|
140
|
+
export type MessageEventSystem<L1 extends string, L2 extends string> = EventSystem<'Message', L1, L2>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ComKey, PriKey } from '../keys';
|
|
2
|
+
import { Item } from '../items';
|
|
3
|
+
/**
|
|
4
|
+
* Base event interface that all events extend.
|
|
5
|
+
* Provides core event properties with full type safety using the existing PriKey/ComKey system.
|
|
6
|
+
*/
|
|
7
|
+
export interface BaseEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
8
|
+
/** Type of event - "create", "update", "delete", etc. */
|
|
9
|
+
eventType: string;
|
|
10
|
+
/** The key of the item that was affected - maintains full type safety */
|
|
11
|
+
key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>;
|
|
12
|
+
/** Which storage backend(s) generated this event - enables filtering by implementation */
|
|
13
|
+
scopes: string[];
|
|
14
|
+
/** When the event occurred */
|
|
15
|
+
timestamp: Date;
|
|
16
|
+
/** Optional: the full item content - fully typed, no loss of type information */
|
|
17
|
+
item?: Item<S, L1, L2, L3, L4, L5>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Event emitted when an item is created.
|
|
21
|
+
* The item property is required since we always have the created item data.
|
|
22
|
+
*/
|
|
23
|
+
export interface CreateEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends BaseEvent<S, L1, L2, L3, L4, L5> {
|
|
24
|
+
eventType: 'create';
|
|
25
|
+
/** The created item - always available for create events */
|
|
26
|
+
item: Item<S, L1, L2, L3, L4, L5>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Event emitted when an item is updated.
|
|
30
|
+
* Provides detailed change tracking with before/after states.
|
|
31
|
+
*/
|
|
32
|
+
export interface UpdateEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends BaseEvent<S, L1, L2, L3, L4, L5> {
|
|
33
|
+
eventType: 'update';
|
|
34
|
+
/** List of field names that were changed */
|
|
35
|
+
changes: string[];
|
|
36
|
+
/** Optional: item state before the update */
|
|
37
|
+
before?: Item<S, L1, L2, L3, L4, L5>;
|
|
38
|
+
/** Optional: item state after the update */
|
|
39
|
+
after?: Item<S, L1, L2, L3, L4, L5>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Event emitted when an item is deleted.
|
|
43
|
+
* May include the deleted item data for cleanup/undo operations.
|
|
44
|
+
*/
|
|
45
|
+
export interface DeleteEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends BaseEvent<S, L1, L2, L3, L4, L5> {
|
|
46
|
+
eventType: 'delete';
|
|
47
|
+
/** Optional: the deleted item content - useful for cleanup/undo */
|
|
48
|
+
item?: Item<S, L1, L2, L3, L4, L5>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Event emitted when a custom action is performed on an item.
|
|
52
|
+
* Allows libraries to define custom event types beyond standard CRUD.
|
|
53
|
+
*/
|
|
54
|
+
export interface ActionEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends BaseEvent<S, L1, L2, L3, L4, L5> {
|
|
55
|
+
eventType: 'action';
|
|
56
|
+
/** Name of the action that was performed */
|
|
57
|
+
actionName: string;
|
|
58
|
+
/** Optional: action-specific data */
|
|
59
|
+
actionData?: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Union type of all standard event types.
|
|
63
|
+
* Libraries can extend this with custom events if needed.
|
|
64
|
+
*/
|
|
65
|
+
export type Event<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = CreateEvent<S, L1, L2, L3, L4, L5> | UpdateEvent<S, L1, L2, L3, L4, L5> | DeleteEvent<S, L1, L2, L3, L4, L5> | ActionEvent<S, L1, L2, L3, L4, L5>;
|
|
66
|
+
/**
|
|
67
|
+
* Type guard to check if an event is a CreateEvent
|
|
68
|
+
*/
|
|
69
|
+
export declare function isCreateEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(event: BaseEvent<S, L1, L2, L3, L4, L5>): event is CreateEvent<S, L1, L2, L3, L4, L5>;
|
|
70
|
+
/**
|
|
71
|
+
* Type guard to check if an event is an UpdateEvent
|
|
72
|
+
*/
|
|
73
|
+
export declare function isUpdateEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(event: BaseEvent<S, L1, L2, L3, L4, L5>): event is UpdateEvent<S, L1, L2, L3, L4, L5>;
|
|
74
|
+
/**
|
|
75
|
+
* Type guard to check if an event is a DeleteEvent
|
|
76
|
+
*/
|
|
77
|
+
export declare function isDeleteEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(event: BaseEvent<S, L1, L2, L3, L4, L5>): event is DeleteEvent<S, L1, L2, L3, L4, L5>;
|
|
78
|
+
/**
|
|
79
|
+
* Type guard to check if an event is an ActionEvent
|
|
80
|
+
*/
|
|
81
|
+
export declare function isActionEvent<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(event: BaseEvent<S, L1, L2, L3, L4, L5>): event is ActionEvent<S, L1, L2, L3, L4, L5>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Event System Public API
|
|
3
|
+
*
|
|
4
|
+
* This module exports all the public interfaces and utilities for the Fjell event system.
|
|
5
|
+
* The event system provides type-safe, item-level change events with full PriKey/ComKey integration.
|
|
6
|
+
*
|
|
7
|
+
* Key Features:
|
|
8
|
+
* - Full type safety using existing PriKey/ComKey system
|
|
9
|
+
* - Storage-agnostic event interfaces
|
|
10
|
+
* - Item-specific and location-based subscriptions
|
|
11
|
+
* - Separate EventEmitters per item type for optimal type safety
|
|
12
|
+
* - Real-time awareness for application needs (not reliable business execution)
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* - Libraries implement EventEmitter/EventSubscriber interfaces
|
|
16
|
+
* - Applications subscribe to events through library instances
|
|
17
|
+
* - Events are delivered through callback functions with full type safety
|
|
18
|
+
*/
|
|
19
|
+
export { BaseEvent, CreateEvent, UpdateEvent, DeleteEvent, ActionEvent, Event, isCreateEvent, isUpdateEvent, isDeleteEvent, isActionEvent, } from './events';
|
|
20
|
+
export { BaseSubscription, ItemSubscription, LocationSubscription, Subscription, SubscriptionOptions, isItemSubscription, isLocationSubscription, generateSubscriptionId, createItemSubscription, createLocationSubscription, } from './subscription';
|
|
21
|
+
export { EventEmitter, ScopedEventEmitter, EventSubscriber, EventSystem, EventSystemFactory, UserEventEmitter, UserEventSubscriber, UserEventSystem, MessageEventEmitter, MessageEventSubscriber, MessageEventSystem, } from './emitter';
|
|
22
|
+
export { doesEventMatchSubscription, doesScopeMatch, doesEventTypeMatch, doesKeyMatch, doesKeyMatchLocation, doesLocationMatch, findMatchingSubscriptions, extractLocationValues, compareLocationValues, } from './matching';
|
|
23
|
+
export { STANDARD_EVENT_TYPES, StandardEventType, STANDARD_SCOPES, StandardScope, SubscriptionStatus, SubscriptionMetadata, ManagedSubscription, EventHandler, SafeEventHandler, EventBatch, EventStats, EventSystemConfig, DEFAULT_EVENT_CONFIG, EventSystemError, SubscriptionError, EventEmissionError, EventMatchingError, createEventSystemError, isEventSystemError, ExtractItemType, ExtractEventTypes, } from './types';
|
|
24
|
+
/**
|
|
25
|
+
* Version of the event system API.
|
|
26
|
+
* Used for compatibility checking and debugging.
|
|
27
|
+
*/
|
|
28
|
+
export declare const EVENT_SYSTEM_VERSION = "1.0.0";
|
|
29
|
+
/**
|
|
30
|
+
* Supported event types for reference.
|
|
31
|
+
* Libraries should use these standard types for consistency.
|
|
32
|
+
*/
|
|
33
|
+
export declare const SUPPORTED_EVENT_TYPES: readonly ["create", "update", "delete", "action"];
|
|
34
|
+
/**
|
|
35
|
+
* Supported storage scopes for reference.
|
|
36
|
+
* Libraries should use these standard scopes for consistency.
|
|
37
|
+
*/
|
|
38
|
+
export declare const SUPPORTED_SCOPES: readonly ["firestore", "sequelize", "postgresql", "mysql", "mongodb", "redis"];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ComKey, ItemTypeArray, LocKeyArray, PriKey } from '../keys';
|
|
2
|
+
import { BaseEvent } from './events';
|
|
3
|
+
import { Subscription } from './subscription';
|
|
4
|
+
/**
|
|
5
|
+
* Core subscription matching logic.
|
|
6
|
+
* Determines whether an event should be delivered to a specific subscription.
|
|
7
|
+
*/
|
|
8
|
+
export declare function doesEventMatchSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(event: BaseEvent<S, L1, L2, L3, L4, L5>, subscription: Subscription<S, L1, L2, L3, L4, L5>): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Check if event scopes match subscription scope requirements.
|
|
11
|
+
*
|
|
12
|
+
* @param eventScopes - Scopes from the event (e.g., ["firestore"])
|
|
13
|
+
* @param subscriptionScopes - Optional scopes required by subscription
|
|
14
|
+
* @returns true if scopes are compatible
|
|
15
|
+
*/
|
|
16
|
+
export declare function doesScopeMatch(eventScopes: string[], subscriptionScopes?: string[]): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Check if event type matches subscription event type requirements.
|
|
19
|
+
*
|
|
20
|
+
* @param eventType - Type from the event (e.g., "create", "update")
|
|
21
|
+
* @param subscriptionEventTypes - Optional event types required by subscription
|
|
22
|
+
* @returns true if event type is compatible
|
|
23
|
+
*/
|
|
24
|
+
export declare function doesEventTypeMatch(eventType: string, subscriptionEventTypes?: string[]): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Check if two keys are exactly equal.
|
|
27
|
+
* Used for item-based subscriptions that want events for a specific key.
|
|
28
|
+
*/
|
|
29
|
+
export declare function doesKeyMatch<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(eventKey: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, subscriptionKey: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Check if an event key matches a location-based subscription.
|
|
32
|
+
* This is more complex as it needs to determine if the event key is "within" the subscription location.
|
|
33
|
+
*/
|
|
34
|
+
export declare function doesKeyMatchLocation<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(eventKey: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, subscriptionKta: ItemTypeArray<S, L1, L2, L3, L4, L5>, subscriptionLocation: LocKeyArray<L1, L2, L3, L4, L5>): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Check if an event's location keys match a subscription's location requirements.
|
|
37
|
+
* This implements the hierarchical location matching logic.
|
|
38
|
+
*/
|
|
39
|
+
export declare function doesLocationMatch<L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(eventLocation: LocKeyArray<L1, L2, L3, L4, L5>, subscriptionLocation: LocKeyArray<L1, L2, L3, L4, L5>, _subscriptionKta: ItemTypeArray<string, L1, L2, L3, L4, L5>): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Find all subscriptions that match a given event.
|
|
42
|
+
* Used by EventSubscriber implementations to determine which subscriptions should receive an event.
|
|
43
|
+
*/
|
|
44
|
+
export declare function findMatchingSubscriptions<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(event: BaseEvent<S, L1, L2, L3, L4, L5>, subscriptions: Subscription<S, L1, L2, L3, L4, L5>[]): Subscription<S, L1, L2, L3, L4, L5>[];
|
|
45
|
+
/**
|
|
46
|
+
* Utility function to extract the location from a ComKey for comparison purposes.
|
|
47
|
+
* Returns the location key values as strings for easier comparison.
|
|
48
|
+
*/
|
|
49
|
+
export declare function extractLocationValues<L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(location: LocKeyArray<L1, L2, L3, L4, L5>): string[];
|
|
50
|
+
/**
|
|
51
|
+
* Utility function to compare two location arrays by their values.
|
|
52
|
+
* Useful for debugging and testing location matching logic.
|
|
53
|
+
*/
|
|
54
|
+
export declare function compareLocationValues<L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(location1: LocKeyArray<L1, L2, L3, L4, L5>, location2: LocKeyArray<L1, L2, L3, L4, L5>): boolean;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ComKey, ItemTypeArray, LocKeyArray, PriKey } from '../keys';
|
|
2
|
+
import { ItemQuery } from '../item/ItemQuery';
|
|
3
|
+
/**
|
|
4
|
+
* Base subscription interface that all subscription types extend.
|
|
5
|
+
* Provides core subscription properties with full type safety.
|
|
6
|
+
*/
|
|
7
|
+
export interface BaseSubscription {
|
|
8
|
+
/** Unique subscription identifier - generated when subscription is created */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Optional: specific event types to listen for (defaults to all if not specified) */
|
|
11
|
+
eventTypes?: string[];
|
|
12
|
+
/** Optional: storage backends to listen to (defaults to all if not specified) */
|
|
13
|
+
scopes?: string[];
|
|
14
|
+
/** Optional: additional filtering criteria using existing ItemQuery system */
|
|
15
|
+
query?: ItemQuery;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Subscription to events for a specific item using PriKey or ComKey.
|
|
19
|
+
* Provides exact item-level event subscriptions with full type safety.
|
|
20
|
+
*/
|
|
21
|
+
export interface ItemSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends BaseSubscription {
|
|
22
|
+
/** The specific key to subscribe to - fully typed PriKey or ComKey */
|
|
23
|
+
key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Subscription to events for all items in a location using KTA + location array.
|
|
27
|
+
* Provides location-based event subscriptions with full type safety.
|
|
28
|
+
*/
|
|
29
|
+
export interface LocationSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> extends BaseSubscription {
|
|
30
|
+
/** Item type array defining the type hierarchy */
|
|
31
|
+
kta: ItemTypeArray<S, L1, L2, L3, L4, L5>;
|
|
32
|
+
/** Location key array defining the specific location */
|
|
33
|
+
location: LocKeyArray<L1, L2, L3, L4, L5>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Union type of all subscription types.
|
|
37
|
+
* This allows handling any subscription type generically while maintaining type safety.
|
|
38
|
+
*/
|
|
39
|
+
export type Subscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = ItemSubscription<S, L1, L2, L3, L4, L5> | LocationSubscription<S, L1, L2, L3, L4, L5>;
|
|
40
|
+
/**
|
|
41
|
+
* Options for creating subscriptions.
|
|
42
|
+
* Used by convenience methods to create subscriptions without requiring full subscription objects.
|
|
43
|
+
*/
|
|
44
|
+
export interface SubscriptionOptions<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
45
|
+
/** Optional: specific event types to listen for */
|
|
46
|
+
eventTypes?: string[];
|
|
47
|
+
/** Optional: storage backends to listen to */
|
|
48
|
+
scopes?: string[];
|
|
49
|
+
/** Optional: additional filtering criteria */
|
|
50
|
+
query?: ItemQuery;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Type guard to check if a subscription is an ItemSubscription
|
|
54
|
+
*/
|
|
55
|
+
export declare function isItemSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(subscription: Subscription<S, L1, L2, L3, L4, L5>): subscription is ItemSubscription<S, L1, L2, L3, L4, L5>;
|
|
56
|
+
/**
|
|
57
|
+
* Type guard to check if a subscription is a LocationSubscription
|
|
58
|
+
*/
|
|
59
|
+
export declare function isLocationSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(subscription: Subscription<S, L1, L2, L3, L4, L5>): subscription is LocationSubscription<S, L1, L2, L3, L4, L5>;
|
|
60
|
+
/**
|
|
61
|
+
* Utility function to generate unique subscription IDs.
|
|
62
|
+
* Libraries can use this or implement their own ID generation strategy.
|
|
63
|
+
*/
|
|
64
|
+
export declare function generateSubscriptionId(): string;
|
|
65
|
+
/**
|
|
66
|
+
* Utility function to create an ItemSubscription with generated ID.
|
|
67
|
+
* Simplifies subscription creation for library implementations.
|
|
68
|
+
*/
|
|
69
|
+
export declare function createItemSubscription<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>, options?: SubscriptionOptions<S, L1, L2, L3, L4, L5>): ItemSubscription<S, L1, L2, L3, L4, L5>;
|
|
70
|
+
/**
|
|
71
|
+
* Utility function to create a LocationSubscription with generated ID.
|
|
72
|
+
* Simplifies subscription creation for library implementations.
|
|
73
|
+
*/
|
|
74
|
+
export declare function createLocationSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(kta: ItemTypeArray<S, L1, L2, L3, L4, L5>, location: LocKeyArray<L1, L2, L3, L4, L5>, options?: SubscriptionOptions<S, L1, L2, L3, L4, L5>): LocationSubscription<S, L1, L2, L3, L4, L5>;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { BaseEvent } from './events';
|
|
2
|
+
import { Subscription } from './subscription';
|
|
3
|
+
/**
|
|
4
|
+
* Common event types used throughout the event system.
|
|
5
|
+
* Libraries can extend these with custom event types as needed.
|
|
6
|
+
*/
|
|
7
|
+
export declare const STANDARD_EVENT_TYPES: {
|
|
8
|
+
readonly CREATE: "create";
|
|
9
|
+
readonly UPDATE: "update";
|
|
10
|
+
readonly DELETE: "delete";
|
|
11
|
+
readonly ACTION: "action";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Type for standard event type values.
|
|
15
|
+
*/
|
|
16
|
+
export type StandardEventType = typeof STANDARD_EVENT_TYPES[keyof typeof STANDARD_EVENT_TYPES];
|
|
17
|
+
/**
|
|
18
|
+
* Common scope identifiers used by storage libraries.
|
|
19
|
+
* Libraries should use these standard scopes for consistency.
|
|
20
|
+
*/
|
|
21
|
+
export declare const STANDARD_SCOPES: {
|
|
22
|
+
readonly FIRESTORE: "firestore";
|
|
23
|
+
readonly SEQUELIZE: "sequelize";
|
|
24
|
+
readonly POSTGRESQL: "postgresql";
|
|
25
|
+
readonly MYSQL: "mysql";
|
|
26
|
+
readonly MONGODB: "mongodb";
|
|
27
|
+
readonly REDIS: "redis";
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Type for standard scope values.
|
|
31
|
+
*/
|
|
32
|
+
export type StandardScope = typeof STANDARD_SCOPES[keyof typeof STANDARD_SCOPES];
|
|
33
|
+
/**
|
|
34
|
+
* Status of a subscription.
|
|
35
|
+
* Used to track subscription lifecycle and health.
|
|
36
|
+
*/
|
|
37
|
+
export declare enum SubscriptionStatus {
|
|
38
|
+
PENDING = "pending",// Subscription created but not yet active
|
|
39
|
+
ACTIVE = "active",// Subscription is active and receiving events
|
|
40
|
+
PAUSED = "paused",// Subscription is paused (not receiving events)
|
|
41
|
+
ERROR = "error",// Subscription has encountered an error
|
|
42
|
+
CANCELLED = "cancelled"
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Metadata about a subscription's current state.
|
|
46
|
+
* Used for monitoring and debugging subscription health.
|
|
47
|
+
*/
|
|
48
|
+
export interface SubscriptionMetadata {
|
|
49
|
+
/** Current status of the subscription */
|
|
50
|
+
status: SubscriptionStatus;
|
|
51
|
+
/** When the subscription was created */
|
|
52
|
+
createdAt: Date;
|
|
53
|
+
/** When the subscription was last updated */
|
|
54
|
+
updatedAt: Date;
|
|
55
|
+
/** When the subscription last received an event */
|
|
56
|
+
lastEventAt?: Date;
|
|
57
|
+
/** Total number of events received by this subscription */
|
|
58
|
+
eventCount: number;
|
|
59
|
+
/** Any error that occurred with this subscription */
|
|
60
|
+
lastError?: Error;
|
|
61
|
+
/** Additional metadata specific to the storage implementation */
|
|
62
|
+
implementationMetadata?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Enhanced subscription interface that includes metadata.
|
|
66
|
+
* Used internally by libraries for subscription management.
|
|
67
|
+
*/
|
|
68
|
+
export type ManagedSubscription<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = Subscription<S, L1, L2, L3, L4, L5> & {
|
|
69
|
+
/** Metadata about this subscription's state */
|
|
70
|
+
metadata: SubscriptionMetadata;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Event handler function type.
|
|
74
|
+
* Used for type-safe event callbacks.
|
|
75
|
+
*/
|
|
76
|
+
export type EventHandler<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = (event: BaseEvent<S, L1, L2, L3, L4, L5>) => void | Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Event handler with error handling.
|
|
79
|
+
* Allows handlers to indicate success/failure for better error tracking.
|
|
80
|
+
*/
|
|
81
|
+
export type SafeEventHandler<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> = (event: BaseEvent<S, L1, L2, L3, L4, L5>) => Promise<{
|
|
82
|
+
success: boolean;
|
|
83
|
+
error?: Error;
|
|
84
|
+
}>;
|
|
85
|
+
/**
|
|
86
|
+
* Batch of events for efficient processing.
|
|
87
|
+
* Used when multiple events need to be processed together.
|
|
88
|
+
*/
|
|
89
|
+
export interface EventBatch<S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
|
|
90
|
+
/** Events in this batch */
|
|
91
|
+
events: BaseEvent<S, L1, L2, L3, L4, L5>[];
|
|
92
|
+
/** When this batch was created */
|
|
93
|
+
createdAt: Date;
|
|
94
|
+
/** Optional: transaction ID if these events are part of a transaction */
|
|
95
|
+
transactionId?: string;
|
|
96
|
+
/** Optional: batch metadata */
|
|
97
|
+
metadata?: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Statistics about event processing.
|
|
101
|
+
* Used for monitoring and performance analysis.
|
|
102
|
+
*/
|
|
103
|
+
export interface EventStats {
|
|
104
|
+
/** Total events processed */
|
|
105
|
+
totalEvents: number;
|
|
106
|
+
/** Events processed by type */
|
|
107
|
+
eventsByType: Record<string, number>;
|
|
108
|
+
/** Events processed by scope */
|
|
109
|
+
eventsByScope: Record<string, number>;
|
|
110
|
+
/** Average event processing time in milliseconds */
|
|
111
|
+
averageProcessingTime: number;
|
|
112
|
+
/** Number of active subscriptions */
|
|
113
|
+
activeSubscriptions: number;
|
|
114
|
+
/** Number of failed event deliveries */
|
|
115
|
+
failedDeliveries: number;
|
|
116
|
+
/** When these stats were last updated */
|
|
117
|
+
lastUpdated: Date;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Configuration for event system behavior.
|
|
121
|
+
* Used by libraries to customize event processing.
|
|
122
|
+
*/
|
|
123
|
+
export interface EventSystemConfig {
|
|
124
|
+
/** Maximum number of events to batch together */
|
|
125
|
+
maxBatchSize?: number;
|
|
126
|
+
/** Maximum time to wait before processing a batch (milliseconds) */
|
|
127
|
+
maxBatchWaitTime?: number;
|
|
128
|
+
/** Maximum number of retry attempts for failed event deliveries */
|
|
129
|
+
maxRetryAttempts?: number;
|
|
130
|
+
/** Delay between retry attempts (milliseconds) */
|
|
131
|
+
retryDelay?: number;
|
|
132
|
+
/** Whether to enable event statistics collection */
|
|
133
|
+
enableStats?: boolean;
|
|
134
|
+
/** Maximum number of subscriptions to allow */
|
|
135
|
+
maxSubscriptions?: number;
|
|
136
|
+
/** Cleanup interval for inactive subscriptions (milliseconds) */
|
|
137
|
+
subscriptionCleanupInterval?: number;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Default configuration values.
|
|
141
|
+
* Libraries can use these as defaults and override as needed.
|
|
142
|
+
*/
|
|
143
|
+
export declare const DEFAULT_EVENT_CONFIG: Required<EventSystemConfig>;
|
|
144
|
+
/**
|
|
145
|
+
* Error types specific to the event system.
|
|
146
|
+
* Used for better error handling and debugging.
|
|
147
|
+
*/
|
|
148
|
+
export declare class EventSystemError extends Error {
|
|
149
|
+
readonly code: string;
|
|
150
|
+
readonly details?: Record<string, unknown> | undefined;
|
|
151
|
+
constructor(message: string, code: string, details?: Record<string, unknown> | undefined);
|
|
152
|
+
}
|
|
153
|
+
export declare class SubscriptionError extends EventSystemError {
|
|
154
|
+
readonly subscriptionId: string;
|
|
155
|
+
constructor(message: string, subscriptionId: string, details?: Record<string, unknown>);
|
|
156
|
+
}
|
|
157
|
+
export declare class EventEmissionError extends EventSystemError {
|
|
158
|
+
readonly eventType: string;
|
|
159
|
+
constructor(message: string, eventType: string, details?: Record<string, unknown>);
|
|
160
|
+
}
|
|
161
|
+
export declare class EventMatchingError extends EventSystemError {
|
|
162
|
+
constructor(message: string, details?: Record<string, unknown>);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Utility function to create standardized error messages.
|
|
166
|
+
*/
|
|
167
|
+
export declare function createEventSystemError(type: 'subscription' | 'emission' | 'matching' | 'general', message: string, details?: Record<string, unknown>): EventSystemError;
|
|
168
|
+
/**
|
|
169
|
+
* Utility function to check if an error is an EventSystemError.
|
|
170
|
+
*/
|
|
171
|
+
export declare function isEventSystemError(error: unknown): error is EventSystemError;
|
|
172
|
+
/**
|
|
173
|
+
* Utility type for extracting the item type from an event.
|
|
174
|
+
*/
|
|
175
|
+
export type ExtractItemType<T> = T extends BaseEvent<infer S, any, any, any, any, any> ? S : never;
|
|
176
|
+
/**
|
|
177
|
+
* Utility type for extracting all type parameters from an event.
|
|
178
|
+
*/
|
|
179
|
+
export type ExtractEventTypes<T> = T extends BaseEvent<infer S, infer L1, infer L2, infer L3, infer L4, infer L5> ? {
|
|
180
|
+
S: S;
|
|
181
|
+
L1: L1;
|
|
182
|
+
L2: L2;
|
|
183
|
+
L3: L3;
|
|
184
|
+
L4: L4;
|
|
185
|
+
L5: L5;
|
|
186
|
+
} : never;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./dictionary";
|
|
|
2
2
|
export * from "./keys";
|
|
3
3
|
export * from "./items";
|
|
4
4
|
export * from "./Coordinate";
|
|
5
|
+
export * from "./Coordinate";
|
|
5
6
|
export { IFactory } from "./item/IFactory";
|
|
6
7
|
export { AItemService } from "./AItemService";
|
|
7
8
|
export * from './key/KUtils';
|
|
@@ -13,6 +14,7 @@ export * from './item/ItemQuery';
|
|
|
13
14
|
export * from './validation';
|
|
14
15
|
export * from './errors';
|
|
15
16
|
export * from './operations';
|
|
17
|
+
export * from './event';
|
|
16
18
|
export type { Operations, OperationParams, AffectedKeys, CreateOptions } from './operations/Operations';
|
|
17
19
|
export { isPriKey as isOperationPriKey, isComKey as isOperationComKey } from './operations/Operations';
|
|
18
20
|
export type { GetMethod, CreateMethod, UpdateMethod, RemoveMethod, UpsertMethod, AllMethod, OneMethod, FindMethod, FindOneMethod, FinderMethod, ActionMethod, ActionOperationMethod, AllActionMethod, AllActionOperationMethod, FacetMethod, FacetOperationMethod, AllFacetMethod, AllFacetOperationMethod, OperationsExtensions } from './operations/methods';
|