@medyll/idae-db 0.82.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.
- package/dist/@types/types.d.ts +2 -2
- package/dist/IdaeDbAdapter.d.ts +1 -1
- package/dist/IdaeDbAdapter.js +8 -7
- package/dist/IdaeDbConnection.d.ts +2 -1
- package/dist/IdaeDbConnection.js +2 -1
- package/dist/IdaeEventEmitter.d.ts +3 -2
- package/dist/IdaeEventEmitter.js +6 -5
- package/dist/adapters/MongoDBAdapter.d.ts +1 -1
- package/dist/adapters/MongoDBAdapter.js +1 -4
- package/dist/adapters/MySQLAdapter.d.ts +5 -4
- package/dist/adapters/MySQLAdapter.js +10 -1
- package/dist/idaeDb.d.ts +1 -1
- package/package.json +1 -1
package/dist/@types/types.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface IdaeDbAdapterInterface<T extends object> {
|
|
|
8
8
|
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
9
9
|
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
10
10
|
update(id: string, updateData: Partial<T>): Promise<unknown>;
|
|
11
|
-
updateWhere<OPT =
|
|
11
|
+
updateWhere<OPT = never>(params: IdaeDbParams<T>, updateData: Partial<T>, options?: OPT): Promise<unknown>;
|
|
12
12
|
deleteById(id: string): Promise<unknown>;
|
|
13
13
|
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
14
14
|
deletedCount?: number;
|
|
@@ -42,7 +42,7 @@ export declare abstract class AbstractIdaeDbAdapter<T extends object> implements
|
|
|
42
42
|
abstract find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
43
43
|
abstract findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
44
44
|
abstract update(id: string, updateData: Partial<T>): Promise<unknown>;
|
|
45
|
-
abstract updateWhere<OPT =
|
|
45
|
+
abstract updateWhere<OPT = never>(params: IdaeDbParams<T>, updateData: Partial<T>, options?: OPT): Promise<unknown>;
|
|
46
46
|
abstract deleteById(id: string): Promise<unknown>;
|
|
47
47
|
abstract deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
48
48
|
deletedCount?: number;
|
package/dist/IdaeDbAdapter.d.ts
CHANGED
|
@@ -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<
|
|
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[]>;
|
package/dist/IdaeDbAdapter.js
CHANGED
|
@@ -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
|
-
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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
|
*/
|
|
@@ -23,7 +24,7 @@ export declare class IdaeDbConnection {
|
|
|
23
24
|
* @returns The database instance.
|
|
24
25
|
* @throws Error if the database is not connected.
|
|
25
26
|
*/
|
|
26
|
-
getDb():
|
|
27
|
+
getDb(): {};
|
|
27
28
|
/**
|
|
28
29
|
* Gets the model for a collection.
|
|
29
30
|
* @param collectionName The name of the collection.
|
package/dist/IdaeDbConnection.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// packages\idae-db\lib\IdaeDbConnection.ts
|
|
2
|
-
import { DbType } from './@types/types.js';
|
|
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<
|
|
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
|
|
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>;
|
package/dist/IdaeEventEmitter.js
CHANGED
|
@@ -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
|
|
23
|
-
|
|
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 =
|
|
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 =
|
|
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?:
|
|
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) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbstractIdaeDbAdapter, type IdaeDbParams } from '../@types/types.js';
|
|
2
2
|
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
3
3
|
import mysql from 'mysql2/promise';
|
|
4
|
-
export declare class MySQLAdapter<T extends Record<string,
|
|
4
|
+
export declare class MySQLAdapter<T extends Record<string, unknown>> implements AbstractIdaeDbAdapter<T> {
|
|
5
5
|
private model;
|
|
6
6
|
private connection;
|
|
7
7
|
private fieldId;
|
|
@@ -11,12 +11,13 @@ export declare class MySQLAdapter<T extends Record<string, any>> implements Abst
|
|
|
11
11
|
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
12
12
|
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
13
13
|
create(data: Partial<T>): Promise<T>;
|
|
14
|
-
update(id: string, updateData: Partial<T>, options?:
|
|
15
|
-
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>, options?:
|
|
16
|
-
deleteById(id: string | number): Promise<
|
|
14
|
+
update(id: string, updateData: Partial<T>, options?: unknown): Promise<unknown>;
|
|
15
|
+
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>, options?: unknown): Promise<unknown>;
|
|
16
|
+
deleteById(id: string | number): Promise<unknown>;
|
|
17
17
|
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
18
18
|
deletedCount?: number;
|
|
19
19
|
}>;
|
|
20
20
|
transaction<TResult>(callback: (session: mysql.Connection) => Promise<TResult>): Promise<TResult>;
|
|
21
|
+
createIndex(fieldOrSpec: string, options?: unknown): Promise<string>;
|
|
21
22
|
private parseSortOptions;
|
|
22
23
|
}
|
|
@@ -59,6 +59,7 @@ export class MySQLAdapter {
|
|
|
59
59
|
const insertId = result.insertId;
|
|
60
60
|
return { ...data, [this.fieldId]: insertId };
|
|
61
61
|
}
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
62
63
|
async update(id, updateData, options) {
|
|
63
64
|
const setClause = Object.keys(updateData)
|
|
64
65
|
.map((key) => `${key} = ?`)
|
|
@@ -69,7 +70,9 @@ export class MySQLAdapter {
|
|
|
69
70
|
.execute(`UPDATE ${this.tableName} SET ${setClause} WHERE ${this.fieldId} = ?`, values);
|
|
70
71
|
return result;
|
|
71
72
|
}
|
|
72
|
-
async updateWhere(params, updateData,
|
|
73
|
+
async updateWhere(params, updateData,
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
75
|
+
options = {}) {
|
|
73
76
|
const { query = {} } = params;
|
|
74
77
|
const setClause = Object.keys(updateData)
|
|
75
78
|
.map((key) => `${key} = ?`)
|
|
@@ -113,6 +116,12 @@ export class MySQLAdapter {
|
|
|
113
116
|
throw error;
|
|
114
117
|
}
|
|
115
118
|
}
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
120
|
+
async createIndex(fieldOrSpec, options) {
|
|
121
|
+
const sql = `CREATE INDEX idx_${fieldOrSpec} ON ${this.tableName} (${fieldOrSpec})`;
|
|
122
|
+
await this.connection.getClient().execute(sql);
|
|
123
|
+
return `idx_${fieldOrSpec}`;
|
|
124
|
+
}
|
|
116
125
|
parseSortOptions(sortBy) {
|
|
117
126
|
return sortBy
|
|
118
127
|
.split(',')
|
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
|
|
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.
|
|
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",
|