@asaidimu/utils-database 3.1.1 → 3.1.2

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/index.d.mts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { QueryFilter, PaginationOptions } from '@asaidimu/query';
2
2
  import { IndexDefinition, SchemaDefinition, SchemaChange, DataTransform, PredicateMap } from '@asaidimu/anansi';
3
- import { EventBus } from '@core/events';
4
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
5
4
 
6
5
  /**
@@ -741,23 +740,6 @@ declare const createIndexedDbStore: <T extends Record<string, any>>(config: Stor
741
740
 
742
741
  declare function DatabaseConnection(config: Omit<DatabaseConfig, "keyPath">, createStore: <T extends Record<string, any>>(config: StoreConfig, indexes: IndexDefinition[]) => Store<T>): Promise<Database>;
743
742
 
744
- interface MiddlewareContext {
745
- collection?: string;
746
- documentId?: string;
747
- operation: string;
748
- args: any[];
749
- eventBus?: EventBus<any>;
750
- }
751
- type MaybePromise<T> = T | Promise<T>;
752
- type MiddlewareNext = () => MaybePromise<any>;
753
- type Middleware = (ctx: MiddlewareContext, next: MiddlewareNext) => MaybePromise<any>;
754
- declare class Pipeline {
755
- private middlewares;
756
- use(middleware: Middleware): void;
757
- execute(ctx: MiddlewareContext, finalOperation: () => MaybePromise<any>): MaybePromise<any>;
758
- wrap<T extends object>(target: T, baseContext: Partial<MiddlewareContext>): T;
759
- }
760
-
761
743
  interface MutexOptions {
762
744
  /**
763
745
  * Maximum number of pending requests allowed in the queue.
@@ -825,6 +807,76 @@ declare class Mutex {
825
807
  pending(): number;
826
808
  }
827
809
 
810
+ /**
811
+ * Interface defining the shape of the EventBus.
812
+ * @template TEventMap - A record mapping event names to their respective payload types.
813
+ */
814
+ interface EventBus<TEventMap extends Record<string, any>> {
815
+ /**
816
+ * Subscribes to a specific event by name.
817
+ * @param eventName - The name of the event to subscribe to.
818
+ * @param callback - The function to call when the event is emitted.
819
+ * @returns A function to unsubscribe from the event.
820
+ */
821
+ subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
822
+ /**
823
+ * Subscribes to an event and automatically unsubscribes after it fires once.
824
+ * @param eventName - The name of the event to subscribe to.
825
+ * @param callback - The function to call when the event is emitted.
826
+ * @returns A function to cancel the one-shot subscription before it fires.
827
+ */
828
+ once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
829
+ /**
830
+ * Emits an event with a payload to all subscribed listeners.
831
+ * @param event - An object containing the event name and payload.
832
+ */
833
+ emit: <TEventName extends keyof TEventMap>(event: {
834
+ name: TEventName;
835
+ payload: TEventMap[TEventName];
836
+ }) => void;
837
+ /**
838
+ * Retrieves metrics about event bus usage.
839
+ * @returns An object containing various metrics.
840
+ */
841
+ metrics: () => EventMetrics;
842
+ /**
843
+ * Clears all subscriptions and resets metrics.
844
+ * After calling clear(), the bus is fully reset and can be reused —
845
+ * cross-tab communication is re-established if it was previously enabled.
846
+ */
847
+ clear: () => void;
848
+ }
849
+ /**
850
+ * Interface defining the metrics tracked by the EventBus.
851
+ */
852
+ interface EventMetrics {
853
+ /** Total number of events emitted (both sync and deferred paths). */
854
+ totalEvents: number;
855
+ /** Number of active subscriptions across all event names. */
856
+ activeSubscriptions: number;
857
+ /** Map of event names to their emission counts. */
858
+ eventCounts: Map<string, number>;
859
+ /** Average duration of event dispatch in milliseconds. */
860
+ averageEmitDuration: number;
861
+ }
862
+
863
+ interface MiddlewareContext {
864
+ collection?: string;
865
+ documentId?: string;
866
+ operation: string;
867
+ args: any[];
868
+ eventBus?: EventBus<any>;
869
+ }
870
+ type MaybePromise<T> = T | Promise<T>;
871
+ type MiddlewareNext = () => MaybePromise<any>;
872
+ type Middleware = (ctx: MiddlewareContext, next: MiddlewareNext) => MaybePromise<any>;
873
+ declare class Pipeline {
874
+ private middlewares;
875
+ use(middleware: Middleware): void;
876
+ execute(ctx: MiddlewareContext, finalOperation: () => MaybePromise<any>): MaybePromise<any>;
877
+ wrap<T extends object>(target: T, baseContext: Partial<MiddlewareContext>): T;
878
+ }
879
+
828
880
  interface DocumentOptions<T extends Record<string, any>> {
829
881
  /**
830
882
  * The already-persisted initial state of the document.
package/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { QueryFilter, PaginationOptions } from '@asaidimu/query';
2
2
  import { IndexDefinition, SchemaDefinition, SchemaChange, DataTransform, PredicateMap } from '@asaidimu/anansi';
3
- import { EventBus } from '@core/events';
4
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
5
4
 
6
5
  /**
@@ -741,23 +740,6 @@ declare const createIndexedDbStore: <T extends Record<string, any>>(config: Stor
741
740
 
742
741
  declare function DatabaseConnection(config: Omit<DatabaseConfig, "keyPath">, createStore: <T extends Record<string, any>>(config: StoreConfig, indexes: IndexDefinition[]) => Store<T>): Promise<Database>;
743
742
 
744
- interface MiddlewareContext {
745
- collection?: string;
746
- documentId?: string;
747
- operation: string;
748
- args: any[];
749
- eventBus?: EventBus<any>;
750
- }
751
- type MaybePromise<T> = T | Promise<T>;
752
- type MiddlewareNext = () => MaybePromise<any>;
753
- type Middleware = (ctx: MiddlewareContext, next: MiddlewareNext) => MaybePromise<any>;
754
- declare class Pipeline {
755
- private middlewares;
756
- use(middleware: Middleware): void;
757
- execute(ctx: MiddlewareContext, finalOperation: () => MaybePromise<any>): MaybePromise<any>;
758
- wrap<T extends object>(target: T, baseContext: Partial<MiddlewareContext>): T;
759
- }
760
-
761
743
  interface MutexOptions {
762
744
  /**
763
745
  * Maximum number of pending requests allowed in the queue.
@@ -825,6 +807,76 @@ declare class Mutex {
825
807
  pending(): number;
826
808
  }
827
809
 
810
+ /**
811
+ * Interface defining the shape of the EventBus.
812
+ * @template TEventMap - A record mapping event names to their respective payload types.
813
+ */
814
+ interface EventBus<TEventMap extends Record<string, any>> {
815
+ /**
816
+ * Subscribes to a specific event by name.
817
+ * @param eventName - The name of the event to subscribe to.
818
+ * @param callback - The function to call when the event is emitted.
819
+ * @returns A function to unsubscribe from the event.
820
+ */
821
+ subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
822
+ /**
823
+ * Subscribes to an event and automatically unsubscribes after it fires once.
824
+ * @param eventName - The name of the event to subscribe to.
825
+ * @param callback - The function to call when the event is emitted.
826
+ * @returns A function to cancel the one-shot subscription before it fires.
827
+ */
828
+ once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
829
+ /**
830
+ * Emits an event with a payload to all subscribed listeners.
831
+ * @param event - An object containing the event name and payload.
832
+ */
833
+ emit: <TEventName extends keyof TEventMap>(event: {
834
+ name: TEventName;
835
+ payload: TEventMap[TEventName];
836
+ }) => void;
837
+ /**
838
+ * Retrieves metrics about event bus usage.
839
+ * @returns An object containing various metrics.
840
+ */
841
+ metrics: () => EventMetrics;
842
+ /**
843
+ * Clears all subscriptions and resets metrics.
844
+ * After calling clear(), the bus is fully reset and can be reused —
845
+ * cross-tab communication is re-established if it was previously enabled.
846
+ */
847
+ clear: () => void;
848
+ }
849
+ /**
850
+ * Interface defining the metrics tracked by the EventBus.
851
+ */
852
+ interface EventMetrics {
853
+ /** Total number of events emitted (both sync and deferred paths). */
854
+ totalEvents: number;
855
+ /** Number of active subscriptions across all event names. */
856
+ activeSubscriptions: number;
857
+ /** Map of event names to their emission counts. */
858
+ eventCounts: Map<string, number>;
859
+ /** Average duration of event dispatch in milliseconds. */
860
+ averageEmitDuration: number;
861
+ }
862
+
863
+ interface MiddlewareContext {
864
+ collection?: string;
865
+ documentId?: string;
866
+ operation: string;
867
+ args: any[];
868
+ eventBus?: EventBus<any>;
869
+ }
870
+ type MaybePromise<T> = T | Promise<T>;
871
+ type MiddlewareNext = () => MaybePromise<any>;
872
+ type Middleware = (ctx: MiddlewareContext, next: MiddlewareNext) => MaybePromise<any>;
873
+ declare class Pipeline {
874
+ private middlewares;
875
+ use(middleware: Middleware): void;
876
+ execute(ctx: MiddlewareContext, finalOperation: () => MaybePromise<any>): MaybePromise<any>;
877
+ wrap<T extends object>(target: T, baseContext: Partial<MiddlewareContext>): T;
878
+ }
879
+
828
880
  interface DocumentOptions<T extends Record<string, any>> {
829
881
  /**
830
882
  * The already-persisted initial state of the document.