@medyll/idae-db 0.2.0 → 0.4.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 +56 -0
- package/dist/@types/types.js +19 -0
- package/dist/IdaeDBModel.d.ts +2 -2
- package/dist/IdaeDbAdapter.d.ts +38 -21
- package/dist/IdaeDbAdapter.js +40 -18
- package/dist/IdaeDbConnection.js +1 -1
- package/dist/IdaeEventEmitter.d.ts +3 -10
- package/dist/IdaeEventEmitter.js +0 -1
- package/dist/adapters/ChromaDBAdapter.d.ts +20 -19
- package/dist/adapters/ChromaDBAdapter.js +36 -26
- package/dist/adapters/MongoDBAdapter.d.ts +7 -7
- package/dist/adapters/MongoDBAdapter.js +10 -8
- package/dist/adapters/MySQLAdapter.d.ts +17 -18
- package/dist/adapters/MySQLAdapter.js +106 -69
- package/dist/idaeDb.d.ts +11 -10
- package/dist/idaeDb.js +15 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/dist/types.d.ts +0 -33
- package/dist/types.js +0 -8
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Filter } from 'mongodb';
|
|
2
|
+
export type IdaeDbQueryFilter<T> = Filter<T>;
|
|
3
|
+
export type AdapterConstructor<CON> = new <T extends object>(collection: string, connection: CON, _constructor: () => IdaeDbAdapterInterface<T>) => Omit<IdaeDbAdapterInterface<T>, 'connect' | 'getDb' | 'close' | 'registerEvents'>;
|
|
4
|
+
export interface IdaeDbAdapterInterface<T extends object> {
|
|
5
|
+
createIndex<F, O>(fieldOrSpec: F, options?: O): Promise<any>;
|
|
6
|
+
create(data: Partial<T>): Promise<T>;
|
|
7
|
+
findById(id: string): Promise<T[]>;
|
|
8
|
+
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
9
|
+
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
10
|
+
update(id: string, updateData: Partial<T>): Promise<any>;
|
|
11
|
+
updateWhere<OPT = any>(params: IdaeDbParams<T>, updateData: Partial<T>, options?: OPT): Promise<any>;
|
|
12
|
+
deleteById(id: string): Promise<any>;
|
|
13
|
+
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
14
|
+
deletedCount?: number;
|
|
15
|
+
}>;
|
|
16
|
+
transaction<TResult>(callback: (session: any) => Promise<TResult>): Promise<TResult>;
|
|
17
|
+
}
|
|
18
|
+
export interface IdaeDbAdapterStaticMethods {
|
|
19
|
+
connect(uri: string): Promise<any>;
|
|
20
|
+
getDb(client: any, dbName: string): any;
|
|
21
|
+
close(client: any): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export interface IdaeDbParams<T extends object = Record<string, unknown>> {
|
|
24
|
+
id?: string;
|
|
25
|
+
query: IdaeDbQueryFilter<T>;
|
|
26
|
+
sortBy?: string;
|
|
27
|
+
limit?: number;
|
|
28
|
+
skip?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface IdaeDbParamsSortOptions {
|
|
31
|
+
[key: string]: 1 | -1;
|
|
32
|
+
}
|
|
33
|
+
export declare enum DbType {
|
|
34
|
+
MONGODB = "mongodb",
|
|
35
|
+
MYSQL = "mysql",
|
|
36
|
+
CHROMADB = "chromaDb"
|
|
37
|
+
}
|
|
38
|
+
export declare abstract class AbstractIdaeDbAdapter<T extends object> implements IdaeDbAdapterInterface<T> {
|
|
39
|
+
abstract createIndex(fieldOrSpec: any, options?: any): Promise<string>;
|
|
40
|
+
abstract create(data: Partial<T>): Promise<T>;
|
|
41
|
+
abstract findById(id: string): Promise<T[]>;
|
|
42
|
+
abstract find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
43
|
+
abstract findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
44
|
+
abstract update(id: string, updateData: Partial<T>): Promise<any>;
|
|
45
|
+
abstract updateWhere<OPT = any>(params: IdaeDbParams<T>, updateData: Partial<T>, options?: OPT): Promise<any>;
|
|
46
|
+
abstract deleteById(id: string): Promise<any>;
|
|
47
|
+
abstract deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
48
|
+
deletedCount?: number;
|
|
49
|
+
}>;
|
|
50
|
+
abstract transaction<TResult>(callback: (session: any) => Promise<TResult>): Promise<TResult>;
|
|
51
|
+
static connect(uri: string): Promise<any>;
|
|
52
|
+
static getDb(client: any, dbName: string): any;
|
|
53
|
+
static close(client: any): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export interface IdaeDbApiMethods<T extends object> extends AbstractIdaeDbAdapter<T> {
|
|
56
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// packages\idae-db\src\lib\adapters\types.ts
|
|
2
|
+
import {} from 'mongodb';
|
|
3
|
+
export var DbType;
|
|
4
|
+
(function (DbType) {
|
|
5
|
+
DbType["MONGODB"] = "mongodb";
|
|
6
|
+
DbType["MYSQL"] = "mysql";
|
|
7
|
+
DbType["CHROMADB"] = "chromaDb";
|
|
8
|
+
})(DbType || (DbType = {}));
|
|
9
|
+
export class AbstractIdaeDbAdapter {
|
|
10
|
+
static connect(uri) {
|
|
11
|
+
throw new Error('Method not implemented.');
|
|
12
|
+
}
|
|
13
|
+
static getDb(client, dbName) {
|
|
14
|
+
throw new Error('Method not implemented.');
|
|
15
|
+
}
|
|
16
|
+
static close(client) {
|
|
17
|
+
throw new Error('Method not implemented.');
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/IdaeDBModel.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { Collection
|
|
1
|
+
import type { Collection } from 'mongodb';
|
|
2
2
|
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
3
3
|
export interface IdaeModelOptions {
|
|
4
4
|
autoIncrementFormat?: (collection: string) => string;
|
|
5
5
|
autoIncrementDbCollection?: string;
|
|
6
6
|
}
|
|
7
|
-
export declare class IdaeDBModel<T extends
|
|
7
|
+
export declare class IdaeDBModel<T extends object> {
|
|
8
8
|
private connection;
|
|
9
9
|
private _collectionName;
|
|
10
10
|
private _collection;
|
package/dist/IdaeDbAdapter.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { DbType, type
|
|
1
|
+
import { AbstractIdaeDbAdapter, DbType, type AdapterConstructor, type IdaeDbParams } from './@types/types.js';
|
|
2
2
|
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
3
|
-
import { IdaeEventEmitter, type
|
|
4
|
-
export type
|
|
5
|
-
|
|
3
|
+
import { IdaeEventEmitter, type ErrorEventListener, type PostEventListener, type PreEventListener } from './IdaeEventEmitter.js';
|
|
4
|
+
export type EventListeners<T extends object> = {
|
|
5
|
+
[K in keyof IdaeDbAdapter<T> as IdaeDbAdapter<T>[K] extends (...args: unknown[]) => unknown ? K : never]?: {
|
|
6
|
+
pre?: PreEventListener<IdaeDbAdapter<T>[K] extends (...args: infer P) => unknown ? P : never>;
|
|
7
|
+
post?: PostEventListener<IdaeDbAdapter<T>[K] extends (...args: infer P) => unknown ? P : never, IdaeDbAdapter<T>[K] extends (...args: unknown[]) => infer R ? R : never>;
|
|
8
|
+
error?: ErrorEventListener;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare class IdaeDbAdapter<T extends object> extends IdaeEventEmitter implements AbstractIdaeDbAdapter<T> {
|
|
6
12
|
private dbType;
|
|
7
13
|
private adapter;
|
|
8
14
|
private static adapters;
|
|
@@ -11,26 +17,37 @@ export declare class IdaeDbAdapter<T extends Document> extends IdaeEventEmitter
|
|
|
11
17
|
* @param dbType The type of database for this adapter.
|
|
12
18
|
* @param adapterConstructor The constructor function for the adapter.
|
|
13
19
|
*/
|
|
14
|
-
static addAdapter<A>(dbType: DbType, adapterConstructor:
|
|
20
|
+
static addAdapter<A>(dbType: DbType, adapterConstructor: new (collection: string, connection: IdaeDbConnection) => A): void;
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves the adapter constructor for a specific database type.
|
|
23
|
+
* @param dbType The type of database for which to retrieve the adapter.
|
|
24
|
+
* @returns The adapter constructor for the specified database type, or undefined if not found.
|
|
25
|
+
*/
|
|
26
|
+
static getAdapterForDbType(dbType: DbType): AdapterConstructor<IdaeDbConnection> | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new instance of IdaeDbAdapter.
|
|
29
|
+
* @param collection The name of the collection or table to operate on.
|
|
30
|
+
* @param connection The database connection object.
|
|
31
|
+
* @param dbType The type of database being used (e.g., MongoDB, MySQL, ChromaDB).
|
|
32
|
+
* @throws {Error} If no adapter is found for the specified database type.
|
|
33
|
+
*/
|
|
15
34
|
constructor(collection: string, connection: IdaeDbConnection, dbType: DbType);
|
|
16
|
-
private applyAdapter;
|
|
17
35
|
/**
|
|
18
|
-
*
|
|
36
|
+
* Register event listeners for different operations.
|
|
19
37
|
* @param events An object containing event listeners for different operations.
|
|
20
38
|
*/
|
|
21
39
|
registerEvents(events: EventListeners<T>): void;
|
|
22
|
-
|
|
23
|
-
create(data: Partial<T>): Promise<T
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
transaction<TResult>(callback: (session: any) => Promise<TResult>): Promise<TResult>;
|
|
40
|
+
createIndex<F, O>(fieldOrSpec: F, options?: O): Promise<any>;
|
|
41
|
+
create(data: Partial<T>): Promise<T>;
|
|
42
|
+
findById(id: string): Promise<T[]>;
|
|
43
|
+
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
44
|
+
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
45
|
+
update(id: string, updateData: Partial<T>): Promise<any>;
|
|
46
|
+
updateWhere<OPT = any>(params: IdaeDbParams<T>, updateData: Partial<T>, options: OPT): Promise<any>;
|
|
47
|
+
deleteById(id: string): Promise<any>;
|
|
48
|
+
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
49
|
+
deletedCount?: number;
|
|
50
|
+
}>;
|
|
51
|
+
transaction<TResult>(callback: (session: unknown) => Promise<TResult>): Promise<TResult>;
|
|
52
|
+
private applyAdapter;
|
|
36
53
|
}
|
package/dist/IdaeDbAdapter.js
CHANGED
|
@@ -8,8 +8,8 @@ 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;
|
|
12
|
-
import { DbType } from '
|
|
11
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
12
|
+
import { AbstractIdaeDbAdapter, DbType } from './@types/types.js';
|
|
13
13
|
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
14
14
|
import { MongoDBAdapter } from './adapters/MongoDBAdapter.js';
|
|
15
15
|
import { MySQLAdapter } from './adapters/MySQLAdapter.js';
|
|
@@ -32,20 +32,28 @@ export class IdaeDbAdapter extends IdaeEventEmitter {
|
|
|
32
32
|
static addAdapter(dbType, adapterConstructor) {
|
|
33
33
|
IdaeDbAdapter.adapters.set(dbType, adapterConstructor);
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the adapter constructor for a specific database type.
|
|
37
|
+
* @param dbType The type of database for which to retrieve the adapter.
|
|
38
|
+
* @returns The adapter constructor for the specified database type, or undefined if not found.
|
|
39
|
+
*/
|
|
40
|
+
static getAdapterForDbType(dbType) {
|
|
41
|
+
return IdaeDbAdapter.adapters.get(dbType);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new instance of IdaeDbAdapter.
|
|
45
|
+
* @param collection The name of the collection or table to operate on.
|
|
46
|
+
* @param connection The database connection object.
|
|
47
|
+
* @param dbType The type of database being used (e.g., MongoDB, MySQL, ChromaDB).
|
|
48
|
+
* @throws {Error} If no adapter is found for the specified database type.
|
|
49
|
+
*/
|
|
35
50
|
constructor(collection, connection, dbType) {
|
|
36
51
|
super();
|
|
37
52
|
this.dbType = dbType;
|
|
38
53
|
this.applyAdapter(collection, connection, this.dbType);
|
|
39
54
|
}
|
|
40
|
-
applyAdapter(collection, connection, dbType) {
|
|
41
|
-
const AdapterConstructor = IdaeDbAdapter.adapters.get(dbType);
|
|
42
|
-
if (!AdapterConstructor) {
|
|
43
|
-
throw new Error(`No adapter found for database type: ${dbType}`);
|
|
44
|
-
}
|
|
45
|
-
this.adapter = new AdapterConstructor(collection, connection);
|
|
46
|
-
}
|
|
47
55
|
/**
|
|
48
|
-
*
|
|
56
|
+
* Register event listeners for different operations.
|
|
49
57
|
* @param events An object containing event listeners for different operations.
|
|
50
58
|
*/
|
|
51
59
|
registerEvents(events) {
|
|
@@ -61,8 +69,8 @@ export class IdaeDbAdapter extends IdaeEventEmitter {
|
|
|
61
69
|
}
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
|
-
|
|
65
|
-
return
|
|
72
|
+
async createIndex(fieldOrSpec, options) {
|
|
73
|
+
return this.adapter.createIndex(fieldOrSpec, options);
|
|
66
74
|
}
|
|
67
75
|
async create(data) {
|
|
68
76
|
return this.adapter.create(data);
|
|
@@ -79,8 +87,8 @@ export class IdaeDbAdapter extends IdaeEventEmitter {
|
|
|
79
87
|
async update(id, updateData) {
|
|
80
88
|
return this.adapter.update(id, updateData);
|
|
81
89
|
}
|
|
82
|
-
async updateWhere(params, updateData) {
|
|
83
|
-
return this.adapter.updateWhere(params, updateData);
|
|
90
|
+
async updateWhere(params, updateData, options) {
|
|
91
|
+
return this.adapter.updateWhere(params, updateData, options);
|
|
84
92
|
}
|
|
85
93
|
async deleteById(id) {
|
|
86
94
|
return this.adapter.deleteById(id);
|
|
@@ -91,11 +99,25 @@ export class IdaeDbAdapter extends IdaeEventEmitter {
|
|
|
91
99
|
async transaction(callback) {
|
|
92
100
|
return this.adapter.transaction(callback);
|
|
93
101
|
}
|
|
102
|
+
applyAdapter(collection, connection, dbType) {
|
|
103
|
+
const adapterConstructor = IdaeDbAdapter.adapters.get(dbType);
|
|
104
|
+
if (!adapterConstructor) {
|
|
105
|
+
throw new Error(`No adapter found for database type : ${dbType}`);
|
|
106
|
+
}
|
|
107
|
+
/** @ts-expect-error --- */
|
|
108
|
+
this.adapter = new adapterConstructor(collection, connection);
|
|
109
|
+
}
|
|
94
110
|
}
|
|
95
111
|
__decorate([
|
|
96
112
|
withEmitter(),
|
|
97
113
|
__metadata("design:type", Function),
|
|
98
|
-
__metadata("design:paramtypes", [typeof (_a = typeof
|
|
114
|
+
__metadata("design:paramtypes", [typeof (_a = typeof F !== "undefined" && F) === "function" ? _a : Object, typeof (_b = typeof O !== "undefined" && O) === "function" ? _b : Object]),
|
|
115
|
+
__metadata("design:returntype", Promise)
|
|
116
|
+
], IdaeDbAdapter.prototype, "createIndex", null);
|
|
117
|
+
__decorate([
|
|
118
|
+
withEmitter(),
|
|
119
|
+
__metadata("design:type", Function),
|
|
120
|
+
__metadata("design:paramtypes", [typeof (_c = typeof Partial !== "undefined" && Partial) === "function" ? _c : Object]),
|
|
99
121
|
__metadata("design:returntype", Promise)
|
|
100
122
|
], IdaeDbAdapter.prototype, "create", null);
|
|
101
123
|
__decorate([
|
|
@@ -119,13 +141,13 @@ __decorate([
|
|
|
119
141
|
__decorate([
|
|
120
142
|
withEmitter(),
|
|
121
143
|
__metadata("design:type", Function),
|
|
122
|
-
__metadata("design:paramtypes", [String, typeof (
|
|
144
|
+
__metadata("design:paramtypes", [String, typeof (_d = typeof Partial !== "undefined" && Partial) === "function" ? _d : Object]),
|
|
123
145
|
__metadata("design:returntype", Promise)
|
|
124
146
|
], IdaeDbAdapter.prototype, "update", null);
|
|
125
147
|
__decorate([
|
|
126
148
|
withEmitter(),
|
|
127
149
|
__metadata("design:type", Function),
|
|
128
|
-
__metadata("design:paramtypes", [Object, typeof (
|
|
150
|
+
__metadata("design:paramtypes", [Object, typeof (_e = typeof Partial !== "undefined" && Partial) === "function" ? _e : Object, typeof (_f = typeof OPT !== "undefined" && OPT) === "function" ? _f : Object]),
|
|
129
151
|
__metadata("design:returntype", Promise)
|
|
130
152
|
], IdaeDbAdapter.prototype, "updateWhere", null);
|
|
131
153
|
__decorate([
|
|
@@ -144,5 +166,5 @@ __decorate([
|
|
|
144
166
|
withEmitter(),
|
|
145
167
|
__metadata("design:type", Function),
|
|
146
168
|
__metadata("design:paramtypes", [Function]),
|
|
147
|
-
__metadata("design:returntype", typeof (
|
|
169
|
+
__metadata("design:returntype", typeof (_g = typeof Promise !== "undefined" && Promise) === "function" ? _g : Object)
|
|
148
170
|
], IdaeDbAdapter.prototype, "transaction", null);
|
package/dist/IdaeDbConnection.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import { IdaeDbAdapter } from './IdaeDbAdapter.js';
|
|
3
2
|
/**
|
|
4
3
|
* IdaeEventEmitter extends Node's EventEmitter to provide pre/post method hooks.
|
|
5
4
|
*/
|
|
@@ -20,11 +19,12 @@ export declare function withEmitter(): (target: object, propertyKey: string | sy
|
|
|
20
19
|
/**
|
|
21
20
|
* Type for pre-execution event listeners.
|
|
22
21
|
*/
|
|
23
|
-
export type PreEventListener<T extends
|
|
22
|
+
export type PreEventListener<T extends unknown[]> = (...args: T) => void | Promise<void>;
|
|
24
23
|
/**
|
|
25
24
|
* Type for post-execution event listeners.
|
|
26
25
|
*/
|
|
27
|
-
export type PostEventListener<T extends
|
|
26
|
+
export type PostEventListener<T extends unknown[], R> = (result: R, ...args: T) => void | Promise<void>;
|
|
27
|
+
export type EventListeners<T, R> = PreEventListener<T> & PostEventListener<T, R> & ErrorEventListener;
|
|
28
28
|
/**
|
|
29
29
|
* Type for error event listeners.
|
|
30
30
|
*/
|
|
@@ -43,10 +43,3 @@ export interface TypedIdaeEventEmitter<T> {
|
|
|
43
43
|
emit<K extends keyof T>(event: `post:${K & string}`, result: ReturnType<T[K]>, ...args: Parameters<T[K]>): boolean;
|
|
44
44
|
emit<K extends keyof T>(event: `error:${K & string}`, error: Error): boolean;
|
|
45
45
|
}
|
|
46
|
-
export type EventListeners<T> = {
|
|
47
|
-
[K in keyof IdaeDbAdapter<T>]?: {
|
|
48
|
-
pre?: PreEventListener<Parameters<IdaeDbAdapter<T>[K]>>;
|
|
49
|
-
post?: PostEventListener<Parameters<IdaeDbAdapter<T>[K]>, ReturnType<IdaeDbAdapter<T>[K]>>;
|
|
50
|
-
error?: ErrorEventListener;
|
|
51
|
-
};
|
|
52
|
-
};
|
package/dist/IdaeEventEmitter.js
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type { IdaeDbParams,
|
|
1
|
+
import { type AddParams, type Metadata, type UpdateParams, type Documents, type Embeddings, type ID } from 'chromadb';
|
|
2
|
+
import type { IdaeDbParams, AbstractIdaeDbAdapter } from '../@types/types.js';
|
|
3
3
|
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
4
|
-
|
|
4
|
+
type ChromaDBAdapterContent = {
|
|
5
|
+
id: ID;
|
|
6
|
+
embeddings: Embeddings;
|
|
7
|
+
metadatas: Metadata;
|
|
8
|
+
documents: Documents;
|
|
9
|
+
};
|
|
10
|
+
export declare class ChromaDBAdapter<T extends ChromaDBAdapterContent> implements AbstractIdaeDbAdapter<T> {
|
|
5
11
|
private collectionName;
|
|
6
12
|
private connection;
|
|
7
13
|
private collection;
|
|
8
14
|
private client;
|
|
9
15
|
constructor(collectionName: string, connection: IdaeDbConnection);
|
|
10
16
|
private _init;
|
|
11
|
-
create(data:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}>;
|
|
20
|
-
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>): Promise<void>;
|
|
21
|
-
deleteById(id: string): Promise<string[]>;
|
|
17
|
+
create(data: AddParams): Promise<T>;
|
|
18
|
+
findById(id: string): Promise<T[]>;
|
|
19
|
+
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
20
|
+
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
21
|
+
update(id: string, updateData: UpdateParams): Promise<T>;
|
|
22
|
+
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>): Promise<T[]>;
|
|
23
|
+
createIndex(field: unknown, opt: unknown): Promise<T[]>;
|
|
24
|
+
deleteById(id: string | string[]): Promise<boolean>;
|
|
22
25
|
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
23
|
-
deletedCount
|
|
24
|
-
}>;
|
|
25
|
-
addItem(id: string, vector: number[], metadata?: object): Promise<{
|
|
26
|
-
error: string;
|
|
26
|
+
deletedCount: number;
|
|
27
27
|
}>;
|
|
28
|
-
similaritySearch(vector: number[], k?: number): Promise<
|
|
28
|
+
similaritySearch(vector: number[], k?: number): Promise<T[]>;
|
|
29
29
|
}
|
|
30
|
+
export {};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// packages/idae-db/lib/adapters/ChromaDBAdapter.ts
|
|
2
1
|
import { Collection } from 'chromadb';
|
|
3
2
|
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
4
3
|
export class ChromaDBAdapter {
|
|
@@ -9,53 +8,64 @@ export class ChromaDBAdapter {
|
|
|
9
8
|
constructor(collectionName, connection) {
|
|
10
9
|
this.collectionName = collectionName;
|
|
11
10
|
this.connection = connection;
|
|
12
|
-
this.client = this.connection.getDb();
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
_init() {
|
|
16
|
-
this.collection = this.client
|
|
17
|
-
.getOrCreateCollection({ name: this.collectionName })
|
|
18
|
-
.then((collection) => {
|
|
19
|
-
return collection;
|
|
20
|
-
});
|
|
21
|
-
return this;
|
|
11
|
+
this.client = this.connection.getDb();
|
|
12
|
+
this._init();
|
|
13
|
+
}
|
|
14
|
+
async _init() {
|
|
15
|
+
this.collection = await this.client.getOrCreateCollection({ name: this.collectionName });
|
|
22
16
|
}
|
|
23
17
|
async create(data) {
|
|
24
|
-
const {
|
|
25
|
-
|
|
18
|
+
const { embeddings, metadatas, documents } = data;
|
|
19
|
+
const itemId = data.ids || crypto.randomUUID();
|
|
20
|
+
await this.collection.add({
|
|
21
|
+
ids: [itemId],
|
|
22
|
+
embeddings: [embeddings],
|
|
23
|
+
metadatas: [metadatas],
|
|
24
|
+
documents: [documents]
|
|
25
|
+
});
|
|
26
|
+
return { ...data, id: itemId };
|
|
26
27
|
}
|
|
27
28
|
async findById(id) {
|
|
28
|
-
|
|
29
|
+
const result = await this.collection.get([id]);
|
|
30
|
+
return result;
|
|
29
31
|
}
|
|
30
32
|
async find(params) {
|
|
31
33
|
const { query = {}, limit } = params;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return this.collection.query(query.vector || [], limit);
|
|
34
|
+
const results = await this.collection.query(query.vector || [], limit);
|
|
35
|
+
return results;
|
|
35
36
|
}
|
|
36
37
|
async findOne(params) {
|
|
37
38
|
const results = await this.find({ ...params, limit: 1 });
|
|
38
39
|
return results.length > 0 ? results[0] : null;
|
|
39
40
|
}
|
|
40
41
|
async update(id, updateData) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return this.collection.add(id, updateData.vector, updateData.metadata);
|
|
42
|
+
await this.collection.update(updateData);
|
|
43
|
+
return updateData;
|
|
44
44
|
}
|
|
45
45
|
async updateWhere(params, updateData) {
|
|
46
46
|
throw new Error('updateWhere not supported in ChromaDB');
|
|
47
47
|
}
|
|
48
|
+
async createIndex(field, opt) {
|
|
49
|
+
throw new Error(`updateWhere not supported in ChromaDB ${field} ${opt}`);
|
|
50
|
+
}
|
|
48
51
|
async deleteById(id) {
|
|
49
|
-
|
|
52
|
+
if (typeof id === 'string') {
|
|
53
|
+
await this.collection.delete({ ids: [id] });
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
await this.collection.delete({ ids: id });
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
50
59
|
}
|
|
51
60
|
async deleteWhere(params) {
|
|
52
61
|
throw new Error('deleteWhere not supported in ChromaDB');
|
|
53
62
|
}
|
|
54
|
-
//
|
|
55
|
-
async addItem(
|
|
56
|
-
|
|
57
|
-
}
|
|
63
|
+
// Specific methods for ChromaDB
|
|
64
|
+
/* async addItem(ids: string, vector: number[], metadata?: object): Promise<void> {
|
|
65
|
+
await this.collection.add({ ids, vector, metadata });
|
|
66
|
+
} */
|
|
58
67
|
async similaritySearch(vector, k = 10) {
|
|
59
|
-
|
|
68
|
+
const results = await this.collection.query(vector, k);
|
|
69
|
+
return results;
|
|
60
70
|
}
|
|
61
71
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type
|
|
1
|
+
import { type IdaeDbParams, AbstractIdaeDbAdapter } from '../@types/types.js';
|
|
2
|
+
import { type UpdateOptions, type IndexSpecification, type CreateIndexesOptions, type Document, ClientSession, MongoClient, Db } from 'mongodb';
|
|
3
3
|
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
4
|
-
export declare class MongoDBAdapter<T extends Document
|
|
4
|
+
export declare class MongoDBAdapter<T extends Document> implements AbstractIdaeDbAdapter<T> {
|
|
5
5
|
private model;
|
|
6
6
|
private connection;
|
|
7
7
|
private fieldId;
|
|
@@ -11,11 +11,11 @@ export declare class MongoDBAdapter<T extends Document = Document> implements Id
|
|
|
11
11
|
static close(client: MongoClient): Promise<void>;
|
|
12
12
|
createIndex(fieldOrSpec: IndexSpecification, options?: CreateIndexesOptions): Promise<string>;
|
|
13
13
|
findById(id: string): Promise<T[]>;
|
|
14
|
-
find(params: IdaeDbParams<T>): Promise<
|
|
15
|
-
findOne(params: IdaeDbParams<T>): Promise<
|
|
16
|
-
create(data: Partial<T>): Promise<
|
|
14
|
+
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
15
|
+
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
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(params: IdaeDbParams<T>, updateData: Partial<T>, options?: any): 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;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// packages\idae-db\src\lib\adapters\MongoDBAdapter.ts
|
|
2
2
|
import dotenv from 'dotenv';
|
|
3
|
+
import { AbstractIdaeDbAdapter } from '../@types/types.js';
|
|
3
4
|
import { ClientSession, MongoClient, Db } from 'mongodb';
|
|
4
5
|
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
5
6
|
import { IdaeDBModel } from '../IdaeDBModel.js';
|
|
@@ -16,13 +17,16 @@ export class MongoDBAdapter {
|
|
|
16
17
|
this.connection = connection;
|
|
17
18
|
this.model = this.connection.getModel(collection);
|
|
18
19
|
this.fieldId = this.model.fieldId;
|
|
20
|
+
console.log('MongoDBAdapter constructor', collection);
|
|
19
21
|
}
|
|
20
22
|
static async connect(uri) {
|
|
23
|
+
console.log('MongoDBAdapter connect', uri);
|
|
21
24
|
const client = new MongoClient(uri);
|
|
22
25
|
await client.connect();
|
|
23
26
|
return client;
|
|
24
27
|
}
|
|
25
28
|
static getDb(client, dbName) {
|
|
29
|
+
console.log('MongoDBAdapter getDb', dbName);
|
|
26
30
|
return client.db(dbName);
|
|
27
31
|
}
|
|
28
32
|
static async close(client) {
|
|
@@ -39,23 +43,21 @@ export class MongoDBAdapter {
|
|
|
39
43
|
async find(params) {
|
|
40
44
|
const { query = {}, sortBy, limit, skip } = params;
|
|
41
45
|
const sortOptions = this.parseSortOptions(sortBy);
|
|
42
|
-
return await this.model.collection
|
|
46
|
+
return (await this.model.collection
|
|
43
47
|
.find(query)
|
|
44
48
|
.sort(sortOptions)
|
|
45
49
|
.limit(Number(limit) || 0)
|
|
46
50
|
.skip(Number(skip) || 0)
|
|
47
|
-
.toArray();
|
|
51
|
+
.toArray());
|
|
48
52
|
}
|
|
49
53
|
async findOne(params) {
|
|
50
|
-
|
|
54
|
+
const result = await this.model.collection.findOne(params.query);
|
|
55
|
+
return result;
|
|
51
56
|
}
|
|
52
57
|
async create(data) {
|
|
53
|
-
// create auto_increment
|
|
54
58
|
const id = await this.model.getNextIncrement();
|
|
55
|
-
|
|
56
|
-
return
|
|
57
|
-
upsert: true
|
|
58
|
-
});
|
|
59
|
+
const result = await this.model.collection.findOneAndUpdate({ [this.fieldId]: id }, { $set: { ...data, [this.fieldId]: id } }, { upsert: true, returnDocument: 'after' });
|
|
60
|
+
return result.value;
|
|
59
61
|
}
|
|
60
62
|
async update(id, updateData, options) {
|
|
61
63
|
return this.model.collection.updateMany({ [this.fieldId]: id }, { $set: { ...updateData } }, options);
|
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[key: string]: any;
|
|
6
|
-
}
|
|
7
|
-
export declare class MySQLAdapter<T extends MySQLDocument> implements IdaeDbAdapterInterface<T> {
|
|
8
|
-
private sequelize;
|
|
1
|
+
import { AbstractIdaeDbAdapter, type IdaeDbParams } from '../@types/types.js';
|
|
2
|
+
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
3
|
+
import mysql from 'mysql2/promise';
|
|
4
|
+
export declare class MySQLAdapter<T extends Record<string, any>> implements AbstractIdaeDbAdapter<T> {
|
|
9
5
|
private model;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
private connection;
|
|
7
|
+
private fieldId;
|
|
8
|
+
private tableName;
|
|
9
|
+
constructor(collection: string, connection: IdaeDbConnection);
|
|
10
|
+
findById(id: string): Promise<T[]>;
|
|
11
|
+
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
12
|
+
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
13
|
+
create(data: Partial<T>): Promise<T>;
|
|
14
|
+
update(id: string, updateData: Partial<T>, options?: any): Promise<any>;
|
|
15
|
+
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>, options?: any): Promise<any>;
|
|
16
|
+
deleteById(id: string | number): Promise<any>;
|
|
17
|
+
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
19
18
|
deletedCount?: number;
|
|
20
19
|
}>;
|
|
20
|
+
transaction<TResult>(callback: (session: mysql.Connection) => Promise<TResult>): Promise<TResult>;
|
|
21
21
|
private parseSortOptions;
|
|
22
22
|
}
|
|
23
|
-
export {};
|
|
@@ -1,88 +1,125 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
1
|
+
import { AbstractIdaeDbAdapter } from '../@types/types.js';
|
|
2
|
+
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
3
|
+
import { IdaeDBModel } from '../IdaeDBModel.js';
|
|
4
|
+
import mysql from 'mysql2/promise';
|
|
5
5
|
export class MySQLAdapter {
|
|
6
|
-
sequelize;
|
|
7
6
|
model;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
this.model = this.sequelize.define(tableName, {
|
|
18
|
-
id: {
|
|
19
|
-
type: DataTypes.INTEGER.UNSIGNED,
|
|
20
|
-
autoIncrement: true,
|
|
21
|
-
primaryKey: true
|
|
22
|
-
}
|
|
23
|
-
// Define other fields here as needed
|
|
24
|
-
}, {
|
|
25
|
-
timestamps: false,
|
|
26
|
-
tableName
|
|
27
|
-
});
|
|
7
|
+
connection;
|
|
8
|
+
fieldId;
|
|
9
|
+
tableName;
|
|
10
|
+
constructor(collection, connection) {
|
|
11
|
+
this.connection = connection;
|
|
12
|
+
this.model = this.connection.getModel(collection);
|
|
13
|
+
this.fieldId = this.model.fieldId;
|
|
14
|
+
this.tableName = collection;
|
|
28
15
|
}
|
|
29
|
-
async
|
|
30
|
-
const
|
|
31
|
-
|
|
16
|
+
async findById(id) {
|
|
17
|
+
const [rows] = await this.connection
|
|
18
|
+
.getClient()
|
|
19
|
+
.execute(`SELECT * FROM ${this.tableName} WHERE ${this.fieldId} = ?`, [id]);
|
|
20
|
+
return rows;
|
|
32
21
|
}
|
|
33
22
|
async find(params) {
|
|
34
23
|
const { query = {}, sortBy, limit, skip } = params;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
24
|
+
let sql = `SELECT * FROM ${this.tableName}`;
|
|
25
|
+
const values = [];
|
|
26
|
+
if (Object.keys(query).length > 0) {
|
|
27
|
+
sql +=
|
|
28
|
+
' WHERE ' +
|
|
29
|
+
Object.keys(query)
|
|
30
|
+
.map((key) => `${key} = ?`)
|
|
31
|
+
.join(' AND ');
|
|
32
|
+
values.push(...Object.values(query));
|
|
33
|
+
}
|
|
34
|
+
if (sortBy) {
|
|
35
|
+
sql += ' ORDER BY ' + this.parseSortOptions(sortBy);
|
|
36
|
+
}
|
|
37
|
+
if (limit) {
|
|
38
|
+
sql += ' LIMIT ?';
|
|
39
|
+
values.push(Number(limit));
|
|
40
|
+
}
|
|
41
|
+
if (skip) {
|
|
42
|
+
sql += ' OFFSET ?';
|
|
43
|
+
values.push(Number(skip));
|
|
44
|
+
}
|
|
45
|
+
const [rows] = await this.connection.getClient().execute(sql, values);
|
|
46
|
+
return rows;
|
|
43
47
|
}
|
|
44
|
-
async
|
|
45
|
-
const
|
|
46
|
-
return
|
|
48
|
+
async findOne(params) {
|
|
49
|
+
const results = await this.find({ ...params, limit: 1 });
|
|
50
|
+
return results[0] || null;
|
|
47
51
|
}
|
|
48
|
-
async
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
+
async create(data) {
|
|
53
|
+
const columns = Object.keys(data);
|
|
54
|
+
const values = Object.values(data);
|
|
55
|
+
const placeholders = columns.map(() => '?').join(', ');
|
|
56
|
+
const [result] = await this.connection
|
|
57
|
+
.getClient()
|
|
58
|
+
.execute(`INSERT INTO ${this.tableName} (${columns.join(', ')}) VALUES (${placeholders})`, values);
|
|
59
|
+
const insertId = result.insertId;
|
|
60
|
+
return { ...data, [this.fieldId]: insertId };
|
|
52
61
|
}
|
|
53
|
-
async update(id, updateData) {
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
async update(id, updateData, options) {
|
|
63
|
+
const setClause = Object.keys(updateData)
|
|
64
|
+
.map((key) => `${key} = ?`)
|
|
65
|
+
.join(', ');
|
|
66
|
+
const values = [...Object.values(updateData), id];
|
|
67
|
+
const [result] = await this.connection
|
|
68
|
+
.getClient()
|
|
69
|
+
.execute(`UPDATE ${this.tableName} SET ${setClause} WHERE ${this.fieldId} = ?`, values);
|
|
70
|
+
return result;
|
|
59
71
|
}
|
|
60
|
-
async updateWhere(params, updateData) {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
async updateWhere(params, updateData, options = {}) {
|
|
73
|
+
const { query = {} } = params;
|
|
74
|
+
const setClause = Object.keys(updateData)
|
|
75
|
+
.map((key) => `${key} = ?`)
|
|
76
|
+
.join(', ');
|
|
77
|
+
const whereClause = Object.keys(query)
|
|
78
|
+
.map((key) => `${key} = ?`)
|
|
79
|
+
.join(' AND ');
|
|
80
|
+
const values = [...Object.values(updateData), ...Object.values(query)];
|
|
81
|
+
const [result] = await this.connection
|
|
82
|
+
.getClient()
|
|
83
|
+
.execute(`UPDATE ${this.tableName} SET ${setClause} WHERE ${whereClause}`, values);
|
|
84
|
+
return result;
|
|
66
85
|
}
|
|
67
86
|
async deleteById(id) {
|
|
68
|
-
const result = await this.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
return null;
|
|
87
|
+
const [result] = await this.connection
|
|
88
|
+
.getClient()
|
|
89
|
+
.execute(`DELETE FROM ${this.tableName} WHERE ${this.fieldId} = ?`, [id]);
|
|
90
|
+
return result;
|
|
74
91
|
}
|
|
75
|
-
async
|
|
92
|
+
async deleteWhere(params) {
|
|
76
93
|
const { query = {} } = params;
|
|
77
|
-
const
|
|
78
|
-
|
|
94
|
+
const whereClause = Object.keys(query)
|
|
95
|
+
.map((key) => `${key} = ?`)
|
|
96
|
+
.join(' AND ');
|
|
97
|
+
const values = Object.values(query);
|
|
98
|
+
const [result] = await this.connection
|
|
99
|
+
.getClient()
|
|
100
|
+
.execute(`DELETE FROM ${this.tableName} WHERE ${whereClause}`, values);
|
|
101
|
+
return { deletedCount: result.affectedRows };
|
|
102
|
+
}
|
|
103
|
+
async transaction(callback) {
|
|
104
|
+
const connection = await this.connection.getClient();
|
|
105
|
+
await connection.beginTransaction();
|
|
106
|
+
try {
|
|
107
|
+
const result = await callback(connection);
|
|
108
|
+
await connection.commit();
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
await connection.rollback();
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
79
115
|
}
|
|
80
116
|
parseSortOptions(sortBy) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
117
|
+
return sortBy
|
|
118
|
+
.split(',')
|
|
119
|
+
.map((field) => {
|
|
84
120
|
const [key, order] = field.split(':');
|
|
85
|
-
return
|
|
86
|
-
})
|
|
121
|
+
return `${key} ${order === 'desc' ? 'DESC' : 'ASC'}`;
|
|
122
|
+
})
|
|
123
|
+
.join(', ');
|
|
87
124
|
}
|
|
88
125
|
}
|
package/dist/idaeDb.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DbType } from '
|
|
1
|
+
import { DbType } from './@types/types.js';
|
|
2
2
|
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
3
3
|
import type { IdaeModelOptions } from './IdaeDBModel.js';
|
|
4
4
|
import type { Document } from 'mongodb';
|
|
@@ -6,7 +6,7 @@ import { IdaeDbAdapter } from './IdaeDbAdapter.js';
|
|
|
6
6
|
import type { EventListeners } from './IdaeEventEmitter.js';
|
|
7
7
|
type Uri = string;
|
|
8
8
|
type IdaeDbInstanceKey = `${DbType}:${Uri}`;
|
|
9
|
-
type
|
|
9
|
+
export type IdaeDbOptions = {
|
|
10
10
|
dbType: DbType;
|
|
11
11
|
dbScope: string | undefined;
|
|
12
12
|
dbScopeSeparator?: string;
|
|
@@ -15,21 +15,23 @@ type Options = {
|
|
|
15
15
|
export declare class IdaeDb {
|
|
16
16
|
#private;
|
|
17
17
|
private _uri;
|
|
18
|
-
private globalEvents?;
|
|
19
|
-
private static instances;
|
|
20
18
|
private constructor();
|
|
21
19
|
/**
|
|
22
20
|
* Initializes or retrieves an IdaeDb instance.
|
|
23
|
-
* @param dbType The type of database.
|
|
24
21
|
* @param uri The URI of the database.
|
|
22
|
+
* @param options options.
|
|
23
|
+
* @param options.dbType The type of database.
|
|
24
|
+
* @param options.dbScope The scope of the database.
|
|
25
|
+
* @param options.dbScopeSeparator The separator of the database scope.
|
|
26
|
+
* @param options.idaeModelOptions The options of the IdaeDBModel.
|
|
25
27
|
* @returns An IdaeDb instance.
|
|
26
28
|
*/
|
|
27
|
-
static init(uri: Uri, options?: Partial<
|
|
29
|
+
static init(uri: Uri, options?: Partial<IdaeDbOptions>): IdaeDb;
|
|
28
30
|
/**
|
|
29
31
|
* Registers event listeners for all collections.
|
|
30
32
|
* @param events An object containing event listeners for different operations.
|
|
31
33
|
*/
|
|
32
|
-
registerEvents<T extends
|
|
34
|
+
registerEvents<T extends object, R extends object>(events: EventListeners<T, R>): void;
|
|
33
35
|
/**
|
|
34
36
|
* Creates a new database connection.
|
|
35
37
|
* @param dbName The name of the database.
|
|
@@ -44,12 +46,11 @@ export declare class IdaeDb {
|
|
|
44
46
|
* @throws Error if the connection is not found or the database type is unsupported.
|
|
45
47
|
*/
|
|
46
48
|
collection<T extends Document>(collectionName: string): IdaeDbAdapter<T>;
|
|
47
|
-
private applyEvents;
|
|
48
49
|
closeConnection(): Promise<void>;
|
|
49
50
|
closeAllConnections(): Promise<void>;
|
|
50
|
-
get adapterClass(): import("
|
|
51
|
+
get adapterClass(): import("./@types/types.js").AdapterConstructor<IdaeDbConnection> | undefined;
|
|
51
52
|
get connectionKey(): IdaeDbInstanceKey;
|
|
52
53
|
get uri(): string;
|
|
53
|
-
get options():
|
|
54
|
+
get options(): IdaeDbOptions;
|
|
54
55
|
}
|
|
55
56
|
export {};
|
package/dist/idaeDb.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// packages\idae-db\lib\idaeDb.ts
|
|
2
|
-
import { DbType } from '
|
|
2
|
+
import { DbType } from './@types/types.js';
|
|
3
3
|
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
4
4
|
import { IdaeDbAdapter } from './IdaeDbAdapter.js';
|
|
5
5
|
export class IdaeDb {
|
|
6
6
|
_uri;
|
|
7
|
-
globalEvents;
|
|
8
|
-
static instances = new Map();
|
|
7
|
+
#globalEvents;
|
|
8
|
+
static #instances = new Map();
|
|
9
9
|
#adapterClass;
|
|
10
10
|
#connections = new Map();
|
|
11
11
|
#connection = undefined;
|
|
@@ -21,24 +21,28 @@ export class IdaeDb {
|
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Initializes or retrieves an IdaeDb instance.
|
|
24
|
-
* @param dbType The type of database.
|
|
25
24
|
* @param uri The URI of the database.
|
|
25
|
+
* @param options options.
|
|
26
|
+
* @param options.dbType The type of database.
|
|
27
|
+
* @param options.dbScope The scope of the database.
|
|
28
|
+
* @param options.dbScopeSeparator The separator of the database scope.
|
|
29
|
+
* @param options.idaeModelOptions The options of the IdaeDBModel.
|
|
26
30
|
* @returns An IdaeDb instance.
|
|
27
31
|
*/
|
|
28
32
|
static init(uri, options) {
|
|
29
33
|
const dbType = options?.dbType ?? DbType.MONGODB;
|
|
30
34
|
const instanceKey = `${dbType}:${uri}`;
|
|
31
|
-
if (!IdaeDb
|
|
32
|
-
IdaeDb
|
|
35
|
+
if (!IdaeDb.#instances.has(instanceKey)) {
|
|
36
|
+
IdaeDb.#instances.set(instanceKey, new IdaeDb(uri, options));
|
|
33
37
|
}
|
|
34
|
-
return IdaeDb
|
|
38
|
+
return IdaeDb.#instances.get(instanceKey);
|
|
35
39
|
}
|
|
36
40
|
/**
|
|
37
41
|
* Registers event listeners for all collections.
|
|
38
42
|
* @param events An object containing event listeners for different operations.
|
|
39
43
|
*/
|
|
40
44
|
registerEvents(events) {
|
|
41
|
-
this
|
|
45
|
+
this.#globalEvents = events;
|
|
42
46
|
}
|
|
43
47
|
/**
|
|
44
48
|
* Creates a new database connection.
|
|
@@ -66,12 +70,12 @@ export class IdaeDb {
|
|
|
66
70
|
throw new Error(`Connection for '${this.options.dbType}:${this._uri}' not found.`);
|
|
67
71
|
}
|
|
68
72
|
const adapter = new IdaeDbAdapter(collectionName, this.#connection, this.options.dbType);
|
|
69
|
-
if (this
|
|
70
|
-
this
|
|
73
|
+
if (this.#globalEvents) {
|
|
74
|
+
this.#applyEvents(adapter, this.#globalEvents);
|
|
71
75
|
}
|
|
72
76
|
return adapter;
|
|
73
77
|
}
|
|
74
|
-
applyEvents(adapter, events) {
|
|
78
|
+
#applyEvents(adapter, events) {
|
|
75
79
|
for (const [method, listeners] of Object.entries(events)) {
|
|
76
80
|
if (listeners.pre) {
|
|
77
81
|
adapter.on(`pre:${String(method)}`, listeners.pre);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export * from './types.js';
|
|
2
1
|
export * from './idaeDb.js';
|
|
3
2
|
export * from './IdaeEventEmitter.js';
|
|
4
3
|
export * from './IdaeDbConnection.js';
|
|
@@ -7,3 +6,4 @@ export * from './IdaeDBModel.js';
|
|
|
7
6
|
export * from './adapters/MySQLAdapter.js';
|
|
8
7
|
export * from './adapters/MongoDBAdapter.js';
|
|
9
8
|
export * from './adapters/ChromaDBAdapter.js';
|
|
9
|
+
export * from './@types/types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// auto exports of entry components
|
|
2
|
-
export * from './types.js';
|
|
3
2
|
export * from './idaeDb.js';
|
|
4
3
|
export * from './IdaeEventEmitter.js';
|
|
5
4
|
export * from './IdaeDbConnection.js';
|
|
@@ -8,3 +7,4 @@ export * from './IdaeDBModel.js';
|
|
|
8
7
|
export * from './adapters/MySQLAdapter.js';
|
|
9
8
|
export * from './adapters/MongoDBAdapter.js';
|
|
10
9
|
export * from './adapters/ChromaDBAdapter.js';
|
|
10
|
+
export * from './@types/types.js';
|
package/package.json
CHANGED
package/dist/types.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type Document, type Filter, type IndexSpecification, type CreateIndexesOptions } from 'mongodb';
|
|
2
|
-
export interface IdaeDbAdapterInterface<T extends Document> {
|
|
3
|
-
createIndex(fieldOrSpec: IndexSpecification, options?: CreateIndexesOptions): Promise<string>;
|
|
4
|
-
create(data: Partial<T>): Promise<T>;
|
|
5
|
-
findById(id: string): Promise<T[]>;
|
|
6
|
-
find(params: IdaeDbParams<T>): Promise<T[]>;
|
|
7
|
-
findOne(params: IdaeDbParams<T>): Promise<T | null>;
|
|
8
|
-
update(id: string, updateData: Partial<T>): Promise<any>;
|
|
9
|
-
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>): Promise<any>;
|
|
10
|
-
deleteById(id: string): Promise<any>;
|
|
11
|
-
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
12
|
-
deletedCount?: number;
|
|
13
|
-
}>;
|
|
14
|
-
transaction<TResult>(callback: (session: any) => Promise<TResult>): Promise<TResult>;
|
|
15
|
-
connect(uri: string): Promise<any>;
|
|
16
|
-
getDb(client: any, dbName: string): any;
|
|
17
|
-
close(client: any): Promise<void>;
|
|
18
|
-
}
|
|
19
|
-
export interface IdaeDbParams<T extends object = Record<string, unknown>> {
|
|
20
|
-
id?: string;
|
|
21
|
-
query: Filter<T>;
|
|
22
|
-
sortBy?: string;
|
|
23
|
-
limit?: number;
|
|
24
|
-
skip?: number;
|
|
25
|
-
}
|
|
26
|
-
export interface IdaeDbParamsSortOptions {
|
|
27
|
-
[key: string]: 1 | -1;
|
|
28
|
-
}
|
|
29
|
-
export declare enum DbType {
|
|
30
|
-
MONGODB = "mongodb",
|
|
31
|
-
MYSQL = "mysql",
|
|
32
|
-
CHROMADB = "chromaDb"
|
|
33
|
-
}
|
package/dist/types.js
DELETED