@medyll/idae-db 0.83.0 → 0.84.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.
@@ -40,7 +40,7 @@ export declare class IdaeDbAdapter<T extends object> extends IdaeEventEmitter im
40
40
  * @param events An object containing event listeners for different operations.
41
41
  */
42
42
  registerEvents(events: EventListeners<T>): void;
43
- createIndex<F, O>(fieldOrSpec: F, options?: O): Promise<unknown>;
43
+ createIndex<F, O>(fieldOrSpec: F, options?: O): Promise<string>;
44
44
  create(data: Partial<T>): Promise<T>;
45
45
  findById(id: string): Promise<T[]>;
46
46
  find(params: IdaeDbParams<T>): Promise<T[]>;
@@ -8,7 +8,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var _a, _b, _c, _d, _e, _f, _g;
11
+ var _a, _b, _c, _d, _e, _f, _g, _h;
12
12
  import { AbstractIdaeDbAdapter, DbType } from './@types/types.js';
13
13
  import { IdaeDbConnection } from './IdaeDbConnection.js';
14
14
  import { MongoDBAdapter } from './adapters/MongoDBAdapter.js';
@@ -73,7 +73,8 @@ export class IdaeDbAdapter extends IdaeEventEmitter {
73
73
  }
74
74
  }
75
75
  async createIndex(fieldOrSpec, options) {
76
- return this.adapter.createIndex(fieldOrSpec, options);
76
+ const result = await this.adapter.createIndex(fieldOrSpec, options);
77
+ return String(result);
77
78
  }
78
79
  async create(data) {
79
80
  return this.adapter.create(data);
@@ -122,12 +123,12 @@ __decorate([
122
123
  withEmitter(),
123
124
  __metadata("design:type", Function),
124
125
  __metadata("design:paramtypes", [typeof (_a = typeof F !== "undefined" && F) === "function" ? _a : Object, typeof (_b = typeof O !== "undefined" && O) === "function" ? _b : Object]),
125
- __metadata("design:returntype", Promise)
126
+ __metadata("design:returntype", typeof (_c = typeof Promise !== "undefined" && Promise) === "function" ? _c : Object)
126
127
  ], IdaeDbAdapter.prototype, "createIndex", null);
127
128
  __decorate([
128
129
  withEmitter(),
129
130
  __metadata("design:type", Function),
130
- __metadata("design:paramtypes", [typeof (_c = typeof Partial !== "undefined" && Partial) === "function" ? _c : Object]),
131
+ __metadata("design:paramtypes", [typeof (_d = typeof Partial !== "undefined" && Partial) === "function" ? _d : Object]),
131
132
  __metadata("design:returntype", Promise)
132
133
  ], IdaeDbAdapter.prototype, "create", null);
133
134
  __decorate([
@@ -151,13 +152,13 @@ __decorate([
151
152
  __decorate([
152
153
  withEmitter(),
153
154
  __metadata("design:type", Function),
154
- __metadata("design:paramtypes", [String, typeof (_d = typeof Partial !== "undefined" && Partial) === "function" ? _d : Object]),
155
+ __metadata("design:paramtypes", [String, typeof (_e = typeof Partial !== "undefined" && Partial) === "function" ? _e : Object]),
155
156
  __metadata("design:returntype", Promise)
156
157
  ], IdaeDbAdapter.prototype, "update", null);
157
158
  __decorate([
158
159
  withEmitter(),
159
160
  __metadata("design:type", Function),
160
- __metadata("design:paramtypes", [Object, typeof (_e = typeof Partial !== "undefined" && Partial) === "function" ? _e : Object, typeof (_f = typeof OPT !== "undefined" && OPT) === "function" ? _f : Object]),
161
+ __metadata("design:paramtypes", [Object, typeof (_f = typeof Partial !== "undefined" && Partial) === "function" ? _f : Object, typeof (_g = typeof OPT !== "undefined" && OPT) === "function" ? _g : Object]),
161
162
  __metadata("design:returntype", Promise)
162
163
  ], IdaeDbAdapter.prototype, "updateWhere", null);
163
164
  __decorate([
@@ -176,5 +177,5 @@ __decorate([
176
177
  withEmitter(),
177
178
  __metadata("design:type", Function),
178
179
  __metadata("design:paramtypes", [Function]),
179
- __metadata("design:returntype", typeof (_g = typeof Promise !== "undefined" && Promise) === "function" ? _g : Object)
180
+ __metadata("design:returntype", typeof (_h = typeof Promise !== "undefined" && Promise) === "function" ? _h : Object)
180
181
  ], IdaeDbAdapter.prototype, "transaction", null);
@@ -1,5 +1,6 @@
1
1
  import { IdaeDBModel } from './IdaeDBModel.js';
2
2
  import { IdaeDb } from './idaeDb.js';
3
+ import { type Document } from 'mongodb';
3
4
  /**
4
5
  * Represents a database connection.
5
6
  */
@@ -2,6 +2,7 @@
2
2
  import { DbType, IdaeDbAdapterStaticMethods } from './@types/types.js';
3
3
  import { IdaeDBModel } from './IdaeDBModel.js';
4
4
  import { IdaeDb } from './idaeDb.js';
5
+ import {} from 'mongodb';
5
6
  /**
6
7
  * Represents a database connection.
7
8
  */
@@ -13,9 +13,10 @@ export declare class IdaeEventEmitter extends EventEmitter {
13
13
  }
14
14
  /**
15
15
  * Decorator factory to add pre/post hooks to a method.
16
+ * @template T - The type of the method being decorated.
16
17
  * @returns A method decorator that adds event emission for pre/post hooks.
17
18
  */
18
- export declare function withEmitter(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<unknown>) => TypedPropertyDescriptor<unknown> | void;
19
+ export declare function withEmitter<T extends (...args: any[]) => any>(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => void;
19
20
  /**
20
21
  * Type for pre-execution event listeners.
21
22
  * @template T - The tuple of arguments expected by the event.
@@ -51,7 +52,7 @@ export interface TypedIdaeEventEmitter<T extends Record<string, (...args: unknow
51
52
  * @template T - The object containing methods to attach listeners to.
52
53
  * @template R - The object containing methods to attach listeners to (defaults to T).
53
54
  */
54
- export type EventListeners<T extends object, R extends object = T> = {
55
+ export type EventListeners<T extends object, R = unknown> = {
55
56
  [K in keyof T as T[K] extends (...args: unknown[]) => unknown ? K : never]?: {
56
57
  pre?: PreEventListener<T[K] extends (...args: infer P) => unknown ? P : never>;
57
58
  post?: PostEventListener<T[K] extends (...args: infer P) => unknown ? P : never, T[K] extends (...args: unknown[]) => infer U ? U : never>;
@@ -15,21 +15,23 @@ export class IdaeEventEmitter extends EventEmitter {
15
15
  }
16
16
  /**
17
17
  * Decorator factory to add pre/post hooks to a method.
18
+ * @template T - The type of the method being decorated.
18
19
  * @returns A method decorator that adds event emission for pre/post hooks.
19
20
  */
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
22
  export function withEmitter() {
21
23
  return function (target, propertyKey, descriptor) {
22
- if (descriptor === undefined) {
23
- descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
24
+ if (!descriptor || !descriptor.value) {
25
+ throw new Error('Descriptor or method value is undefined');
24
26
  }
25
27
  const originalMethod = descriptor.value;
26
- descriptor.value = async function (...args) {
28
+ descriptor.value = function (...args) {
27
29
  // Emit the pre-execution event
28
30
  this.emit(`pre:${String(propertyKey)}`, ...args);
29
31
  let result;
30
32
  try {
31
33
  // Execute the original method
32
- result = await originalMethod.apply(this, args);
34
+ result = originalMethod.apply(this, args);
33
35
  }
34
36
  catch (error) {
35
37
  // Emit the error event if an exception occurs
@@ -40,6 +42,5 @@ export function withEmitter() {
40
42
  this.emit(`post:${String(propertyKey)}`, result, ...args);
41
43
  return result;
42
44
  };
43
- return descriptor;
44
45
  };
45
46
  }
@@ -15,7 +15,7 @@ export declare class MongoDBAdapter<T extends Document> implements AbstractIdaeD
15
15
  findOne(params: IdaeDbParams<T>): Promise<T | null>;
16
16
  create(data: Partial<T>): Promise<T>;
17
17
  update(id: string, updateData: Partial<T>, options?: UpdateOptions): Promise<import("mongodb").UpdateResult<T>>;
18
- updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>, options?: UpdateOptions): Promise<import("mongodb").UpdateResult<T>>;
18
+ updateWhere<OPT = never>(params: IdaeDbParams<T>, updateData: Partial<T>, options?: OPT): Promise<import("mongodb").UpdateResult<T>>;
19
19
  deleteById(id: string | number): Promise<import("mongodb").DeleteResult>;
20
20
  deleteWhere(params: IdaeDbParams<T>): Promise<{
21
21
  deletedCount?: number;
@@ -17,16 +17,13 @@ export class MongoDBAdapter {
17
17
  this.connection = connection;
18
18
  this.model = this.connection.getModel(collection);
19
19
  this.fieldId = this.model.fieldId;
20
- console.log('MongoDBAdapter constructor', collection);
21
20
  }
22
21
  static async connect(uri) {
23
- console.log('MongoDBAdapter connect', uri);
24
22
  const client = new MongoClient(uri);
25
23
  await client.connect();
26
24
  return client;
27
25
  }
28
26
  static getDb(client, dbName) {
29
- console.log('MongoDBAdapter getDb', dbName);
30
27
  return client.db(dbName);
31
28
  }
32
29
  static async close(client) {
@@ -62,7 +59,7 @@ export class MongoDBAdapter {
62
59
  async update(id, updateData, options) {
63
60
  return this.model.collection.updateMany({ [this.fieldId]: id }, { $set: { ...updateData } }, options);
64
61
  }
65
- async updateWhere(params, updateData, options = {}) {
62
+ async updateWhere(params, updateData, options) {
66
63
  return this.model.collection.updateMany(params.query, updateData, options);
67
64
  }
68
65
  async deleteById(id) {
package/dist/idaeDb.d.ts CHANGED
@@ -11,7 +11,7 @@ export type IdaeDbOptions = {
11
11
  dbScope: string | undefined;
12
12
  dbScopeSeparator?: string;
13
13
  idaeModelOptions?: IdaeModelOptions;
14
- dbEvents?: EventListeners<object, object>;
14
+ dbEvents?: EventListeners<object>;
15
15
  };
16
16
  /**
17
17
  * Represents the IdaeDb class.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medyll/idae-db",
3
- "version": "0.83.0",
3
+ "version": "0.84.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",