@medyll/idae-db 0.79.0 → 0.81.0

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.
@@ -1,4 +1,4 @@
1
- import type { Collection } from 'mongodb';
1
+ import { type Collection } from 'mongodb';
2
2
  import { IdaeDbConnection } from './IdaeDbConnection.js';
3
3
  export interface IdaeModelOptions {
4
4
  autoIncrementFormat?: (collection: string) => string;
@@ -1,3 +1,5 @@
1
+ // packages\idae-db\lib\IdaeDBModel.ts
2
+ import { ObjectId } from 'mongodb';
1
3
  import { IdaeDbConnection } from './IdaeDbConnection.js';
2
4
  import { IdaeDb } from './idaeDb.js';
3
5
  /**
@@ -43,8 +45,8 @@ export class IdaeDBModel {
43
45
  await idaeAuto.db(increment_name);
44
46
  const incrementCollection = idaeAuto.collection(this._autoIncrementDbCollection);
45
47
  // await incrementCollection.createIndex({ _id: 1 }, { unique: true });
46
- await incrementCollection.updateWhere({ query: { _id: this.fieldId } }, { $inc: { value: 1 } }, { upsert: true });
47
- const next = await incrementCollection.findOne({ query: { _id: this.fieldId } });
48
+ await incrementCollection.updateWhere({ query: { _id: new ObjectId(this.fieldId) } }, { $inc: { value: 1 } }, { upsert: true });
49
+ const next = await incrementCollection.findOne({ query: { _id: new ObjectId(this.fieldId) } });
48
50
  return next?.value;
49
51
  }
50
52
  }
@@ -5,23 +5,26 @@ import { EventEmitter } from 'events';
5
5
  export declare class IdaeEventEmitter extends EventEmitter {
6
6
  /**
7
7
  * Overrides the emit method to ensure type safety.
8
- * @param event The name of the event to emit.
9
- * @param args The arguments to pass to the event listeners.
8
+ * @param event - The name of the event to emit.
9
+ * @param args - The arguments to pass to the event listeners.
10
10
  * @returns Whether the event had listeners.
11
11
  */
12
- emit(event: string | symbol, ...args: any[]): boolean;
12
+ emit(event: string | symbol, ...args: unknown[]): boolean;
13
13
  }
14
14
  /**
15
15
  * Decorator factory to add pre/post hooks to a method.
16
16
  * @returns A method decorator that adds event emission for pre/post hooks.
17
17
  */
18
- export declare function withEmitter(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any> | void;
18
+ export declare function withEmitter(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<unknown>) => TypedPropertyDescriptor<unknown> | void;
19
19
  /**
20
20
  * Type for pre-execution event listeners.
21
+ * @template T - The tuple of arguments expected by the event.
21
22
  */
22
23
  export type PreEventListener<T extends unknown[]> = (...args: T) => void | Promise<void>;
23
24
  /**
24
25
  * Type for post-execution event listeners.
26
+ * @template T - The tuple of arguments expected by the event.
27
+ * @template R - The return type of the event.
25
28
  */
26
29
  export type PostEventListener<T extends unknown[], R> = (result: R, ...args: T) => void | Promise<void>;
27
30
  /**
@@ -30,8 +33,9 @@ export type PostEventListener<T extends unknown[], R> = (result: R, ...args: T)
30
33
  export type ErrorEventListener = (error: Error) => void | Promise<void>;
31
34
  /**
32
35
  * Interface to extend IdaeEventEmitter with strongly typed event listeners.
36
+ * @template T - The object containing methods to attach listeners to.
33
37
  */
34
- export interface TypedIdaeEventEmitter<T> {
38
+ export interface TypedIdaeEventEmitter<T extends Record<string, (...args: unknown[]) => unknown>> {
35
39
  on<K extends keyof T>(event: `pre:${K & string}`, listener: PreEventListener<Parameters<T[K]>>): this;
36
40
  on<K extends keyof T>(event: `post:${K & string}`, listener: PostEventListener<Parameters<T[K]>, ReturnType<T[K]>>): this;
37
41
  on<K extends keyof T>(event: `error:${K & string}`, listener: ErrorEventListener): this;
@@ -42,3 +46,15 @@ export interface TypedIdaeEventEmitter<T> {
42
46
  emit<K extends keyof T>(event: `post:${K & string}`, result: ReturnType<T[K]>, ...args: Parameters<T[K]>): boolean;
43
47
  emit<K extends keyof T>(event: `error:${K & string}`, error: Error): boolean;
44
48
  }
49
+ /**
50
+ * Type for event listeners.
51
+ * @template T - The object containing methods to attach listeners to.
52
+ * @template R - The object containing methods to attach listeners to (defaults to T).
53
+ */
54
+ export type EventListeners<T extends object, R extends object = T> = {
55
+ [K in keyof T as T[K] extends (...args: unknown[]) => unknown ? K : never]?: {
56
+ pre?: PreEventListener<T[K] extends (...args: infer P) => unknown ? P : never>;
57
+ post?: PostEventListener<T[K] extends (...args: infer P) => unknown ? P : never, T[K] extends (...args: unknown[]) => infer U ? U : never>;
58
+ error?: ErrorEventListener;
59
+ };
60
+ };
@@ -5,8 +5,8 @@ import { EventEmitter } from 'events';
5
5
  export class IdaeEventEmitter extends EventEmitter {
6
6
  /**
7
7
  * Overrides the emit method to ensure type safety.
8
- * @param event The name of the event to emit.
9
- * @param args The arguments to pass to the event listeners.
8
+ * @param event - The name of the event to emit.
9
+ * @param args - The arguments to pass to the event listeners.
10
10
  * @returns Whether the event had listeners.
11
11
  */
12
12
  emit(event, ...args) {
package/dist/idaeDb.js CHANGED
@@ -94,7 +94,7 @@ export class IdaeDb {
94
94
  */
95
95
  #applyEvents(adapter, events) {
96
96
  for (const [method, listeners] of Object.entries(events)) {
97
- if (listeners.pre) {
97
+ if (listeners?.pre) {
98
98
  adapter.on(`pre:${String(method)}`, listeners.pre);
99
99
  }
100
100
  if (listeners.post) {
@@ -120,7 +120,7 @@ export class IdaeDb {
120
120
  * @returns A Promise that resolves when all connections are closed.
121
121
  */
122
122
  async closeAllConnections() {
123
- for (const [connectionName, connection] of this.#connections) {
123
+ for (const [, connection] of this.#connections) {
124
124
  await connection.close();
125
125
  }
126
126
  this.#connections.clear();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medyll/idae-db",
3
- "version": "0.79.0",
3
+ "version": "0.81.0",
4
4
  "description": "@medyll/idae-db is a flexible and powerful library for interacting with various databases, with a particular focus on MongoDB support. It offers robust connection management, an intuitive API, and simplified CRUD operations.",
5
5
  "scripts": {
6
6
  "dev": "vite dev",