@medyll/idae-db 0.0.1 → 0.2.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/README.md +79 -35
- package/dist/IdaeDBModel.d.ts +13 -6
- package/dist/IdaeDBModel.js +27 -8
- package/dist/IdaeDbAdapter.d.ts +36 -0
- package/dist/IdaeDbAdapter.js +148 -0
- package/dist/IdaeDbConnection.d.ts +14 -13
- package/dist/IdaeDbConnection.js +56 -57
- package/dist/IdaeEventEmitter.d.ts +52 -0
- package/dist/IdaeEventEmitter.js +46 -0
- package/dist/adapters/ChromaDBAdapter.d.ts +29 -0
- package/dist/adapters/ChromaDBAdapter.js +61 -0
- package/dist/adapters/MongoDBAdapter.d.ts +14 -9
- package/dist/adapters/MongoDBAdapter.js +54 -20
- package/dist/adapters/MySQLAdapter.d.ts +7 -7
- package/dist/adapters/MySQLAdapter.js +25 -17
- package/dist/idaeDb.d.ts +55 -0
- package/dist/idaeDb.js +111 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/types.d.ts +16 -10
- package/dist/types.js +2 -1
- package/package.json +6 -3
- package/dist/idaDb.d.ts +0 -17
- package/dist/idaDb.js +0 -59
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Document } from 'chromadb';
|
|
2
|
+
import type { IdaeDbParams, IdaeDbAdapterInterface } from '../types.js';
|
|
3
|
+
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
4
|
+
export declare class ChromaDBAdapter<T extends Document = Document> implements IdaeDbAdapterInterface<T> {
|
|
5
|
+
private collectionName;
|
|
6
|
+
private connection;
|
|
7
|
+
private collection;
|
|
8
|
+
private client;
|
|
9
|
+
constructor(collectionName: string, connection: IdaeDbConnection);
|
|
10
|
+
private _init;
|
|
11
|
+
create(data: Partial<T>): Promise<{
|
|
12
|
+
error: string;
|
|
13
|
+
}>;
|
|
14
|
+
findById(id: string): Promise<import("chromadb").GetResponse>;
|
|
15
|
+
find(params: IdaeDbParams<T>): Promise<import("chromadb").QueryResponse>;
|
|
16
|
+
findOne(params: IdaeDbParams<T>): Promise<any>;
|
|
17
|
+
update(id: string, updateData: Partial<T>): Promise<{
|
|
18
|
+
error: string;
|
|
19
|
+
}>;
|
|
20
|
+
updateWhere(params: IdaeDbParams<T>, updateData: Partial<T>): Promise<void>;
|
|
21
|
+
deleteById(id: string): Promise<string[]>;
|
|
22
|
+
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
23
|
+
deletedCount?: number;
|
|
24
|
+
}>;
|
|
25
|
+
addItem(id: string, vector: number[], metadata?: object): Promise<{
|
|
26
|
+
error: string;
|
|
27
|
+
}>;
|
|
28
|
+
similaritySearch(vector: number[], k?: number): Promise<import("chromadb").QueryResponse>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// packages/idae-db/lib/adapters/ChromaDBAdapter.ts
|
|
2
|
+
import { Collection } from 'chromadb';
|
|
3
|
+
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
4
|
+
export class ChromaDBAdapter {
|
|
5
|
+
collectionName;
|
|
6
|
+
connection;
|
|
7
|
+
collection;
|
|
8
|
+
client;
|
|
9
|
+
constructor(collectionName, connection) {
|
|
10
|
+
this.collectionName = collectionName;
|
|
11
|
+
this.connection = connection;
|
|
12
|
+
this.client = this.connection.getDb(); // Assuming getDb() returns ChromaClient
|
|
13
|
+
return this._init();
|
|
14
|
+
}
|
|
15
|
+
_init() {
|
|
16
|
+
this.collection = this.client
|
|
17
|
+
.getOrCreateCollection({ name: this.collectionName })
|
|
18
|
+
.then((collection) => {
|
|
19
|
+
return collection;
|
|
20
|
+
});
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
async create(data) {
|
|
24
|
+
const { id, vector, metadata } = data;
|
|
25
|
+
return this.collection.add(id, vector, metadata);
|
|
26
|
+
}
|
|
27
|
+
async findById(id) {
|
|
28
|
+
return this.collection.get([id]);
|
|
29
|
+
}
|
|
30
|
+
async find(params) {
|
|
31
|
+
const { query = {}, limit } = params;
|
|
32
|
+
// ChromaDB uses vector similarity search, so we need to adapt the query
|
|
33
|
+
// This is a simplified example and may need to be adjusted based on your specific use case
|
|
34
|
+
return this.collection.query(query.vector || [], limit);
|
|
35
|
+
}
|
|
36
|
+
async findOne(params) {
|
|
37
|
+
const results = await this.find({ ...params, limit: 1 });
|
|
38
|
+
return results.length > 0 ? results[0] : null;
|
|
39
|
+
}
|
|
40
|
+
async update(id, updateData) {
|
|
41
|
+
// ChromaDB doesn't have a direct update method, so we need to delete and re-add
|
|
42
|
+
await this.deleteById(id);
|
|
43
|
+
return this.collection.add(id, updateData.vector, updateData.metadata);
|
|
44
|
+
}
|
|
45
|
+
async updateWhere(params, updateData) {
|
|
46
|
+
throw new Error('updateWhere not supported in ChromaDB');
|
|
47
|
+
}
|
|
48
|
+
async deleteById(id) {
|
|
49
|
+
return this.collection.delete([id]);
|
|
50
|
+
}
|
|
51
|
+
async deleteWhere(params) {
|
|
52
|
+
throw new Error('deleteWhere not supported in ChromaDB');
|
|
53
|
+
}
|
|
54
|
+
// Additional ChromaDB specific methods
|
|
55
|
+
async addItem(id, vector, metadata) {
|
|
56
|
+
return this.collection.add(id, vector, metadata);
|
|
57
|
+
}
|
|
58
|
+
async similaritySearch(vector, k = 10) {
|
|
59
|
+
return this.collection.query(vector, k);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
import type { IdaeDbParams,
|
|
2
|
-
import { Document, type IndexSpecification, type CreateIndexesOptions } from
|
|
3
|
-
import { IdaeDbConnection } from
|
|
4
|
-
export declare class MongoDBAdapter<T extends Document> implements
|
|
1
|
+
import type { IdaeDbParams, IdaeDbAdapterInterface } from '../types.js';
|
|
2
|
+
import { type Document, type UpdateOptions, type IndexSpecification, type CreateIndexesOptions, ClientSession, MongoClient, Db } from 'mongodb';
|
|
3
|
+
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
4
|
+
export declare class MongoDBAdapter<T extends Document = Document> implements IdaeDbAdapterInterface<T> {
|
|
5
5
|
private model;
|
|
6
6
|
private connection;
|
|
7
7
|
private fieldId;
|
|
8
8
|
constructor(collection: string, connection: IdaeDbConnection);
|
|
9
|
+
static connect(uri: string): Promise<MongoClient>;
|
|
10
|
+
static getDb(client: MongoClient, dbName: string): Db;
|
|
11
|
+
static close(client: MongoClient): Promise<void>;
|
|
9
12
|
createIndex(fieldOrSpec: IndexSpecification, options?: CreateIndexesOptions): Promise<string>;
|
|
10
|
-
findById(id: string): Promise<
|
|
11
|
-
find(params: IdaeDbParams<T>): Promise<import("mongodb").
|
|
13
|
+
findById(id: string): Promise<T[]>;
|
|
14
|
+
find(params: IdaeDbParams<T>): Promise<import("mongodb").WithId<T>[]>;
|
|
12
15
|
findOne(params: IdaeDbParams<T>): Promise<import("mongodb").WithId<T> | null>;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
create(data: Partial<T>): Promise<import("mongodb").UpdateResult<T>>;
|
|
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>>;
|
|
19
|
+
deleteById(id: string | number): Promise<import("mongodb").DeleteResult>;
|
|
16
20
|
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
17
21
|
deletedCount?: number;
|
|
18
22
|
}>;
|
|
23
|
+
transaction<TResult>(callback: (session: ClientSession) => Promise<TResult>): Promise<TResult>;
|
|
19
24
|
private parseSortOptions;
|
|
20
25
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// packages\idae-db\src\lib\adapters\MongoDBAdapter.ts
|
|
2
|
-
import dotenv from
|
|
3
|
-
import {
|
|
4
|
-
import { IdaeDbConnection } from
|
|
5
|
-
import { IdaeDBModel } from
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
import { ClientSession, MongoClient, Db } from 'mongodb';
|
|
4
|
+
import { IdaeDbConnection } from '../IdaeDbConnection.js';
|
|
5
|
+
import { IdaeDBModel } from '../IdaeDBModel.js';
|
|
6
6
|
// Load environment variables
|
|
7
7
|
dotenv.config();
|
|
8
8
|
// MongoDB Adapter
|
|
@@ -12,16 +12,29 @@ export class MongoDBAdapter {
|
|
|
12
12
|
fieldId;
|
|
13
13
|
constructor(collection, connection) {
|
|
14
14
|
// just in case
|
|
15
|
-
collection = collection.split(
|
|
15
|
+
collection = collection.split('.')[1] ?? collection.split('.')[0];
|
|
16
16
|
this.connection = connection;
|
|
17
|
-
this.model = this.connection.
|
|
17
|
+
this.model = this.connection.getModel(collection);
|
|
18
18
|
this.fieldId = this.model.fieldId;
|
|
19
19
|
}
|
|
20
|
+
static async connect(uri) {
|
|
21
|
+
const client = new MongoClient(uri);
|
|
22
|
+
await client.connect();
|
|
23
|
+
return client;
|
|
24
|
+
}
|
|
25
|
+
static getDb(client, dbName) {
|
|
26
|
+
return client.db(dbName);
|
|
27
|
+
}
|
|
28
|
+
static async close(client) {
|
|
29
|
+
return client.close();
|
|
30
|
+
}
|
|
20
31
|
async createIndex(fieldOrSpec, options) {
|
|
21
32
|
return this.model.collection.createIndex(fieldOrSpec, options);
|
|
22
33
|
}
|
|
23
34
|
async findById(id) {
|
|
24
|
-
return this.model.collection
|
|
35
|
+
return this.model.collection
|
|
36
|
+
.find({ [this.fieldId]: id }, { hint: this.fieldId })
|
|
37
|
+
.toArray();
|
|
25
38
|
}
|
|
26
39
|
async find(params) {
|
|
27
40
|
const { query = {}, sortBy, limit, skip } = params;
|
|
@@ -30,35 +43,56 @@ export class MongoDBAdapter {
|
|
|
30
43
|
.find(query)
|
|
31
44
|
.sort(sortOptions)
|
|
32
45
|
.limit(Number(limit) || 0)
|
|
33
|
-
.skip(Number(skip) || 0)
|
|
46
|
+
.skip(Number(skip) || 0)
|
|
47
|
+
.toArray();
|
|
34
48
|
}
|
|
35
|
-
findOne(params) {
|
|
49
|
+
async findOne(params) {
|
|
36
50
|
return this.model.collection.findOne(params.query);
|
|
37
51
|
}
|
|
38
|
-
async
|
|
39
|
-
|
|
40
|
-
|
|
52
|
+
async create(data) {
|
|
53
|
+
// create auto_increment
|
|
54
|
+
const id = await this.model.getNextIncrement();
|
|
55
|
+
// ensure Index i set
|
|
56
|
+
return this.model.collection.updateOne({ [this.fieldId]: id }, { $set: { ...data } }, {
|
|
57
|
+
upsert: true
|
|
41
58
|
});
|
|
42
59
|
}
|
|
43
|
-
async
|
|
44
|
-
return this.model.collection.updateMany(
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
async update(id, updateData, options) {
|
|
61
|
+
return this.model.collection.updateMany({ [this.fieldId]: id }, { $set: { ...updateData } }, options);
|
|
62
|
+
}
|
|
63
|
+
async updateWhere(params, updateData, options = {}) {
|
|
64
|
+
return this.model.collection.updateMany(params.query, updateData, options);
|
|
47
65
|
}
|
|
48
66
|
async deleteById(id) {
|
|
49
|
-
return this.model.collection.deleteMany({
|
|
67
|
+
return this.model.collection.deleteMany({ [this.fieldId]: id });
|
|
50
68
|
}
|
|
51
69
|
async deleteWhere(params) {
|
|
52
70
|
const result = await this.model.collection.deleteMany(params.query);
|
|
53
71
|
return { deletedCount: result.deletedCount };
|
|
54
72
|
}
|
|
73
|
+
async transaction(callback) {
|
|
74
|
+
const session = this.connection.getClient().startSession();
|
|
75
|
+
try {
|
|
76
|
+
session.startTransaction();
|
|
77
|
+
const result = await callback(session);
|
|
78
|
+
await session.commitTransaction();
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
await session.abortTransaction();
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
await session.endSession();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
55
89
|
parseSortOptions(sortBy) {
|
|
56
90
|
const sortOptions = {};
|
|
57
91
|
if (sortBy) {
|
|
58
|
-
const sortFields = sortBy.split(
|
|
92
|
+
const sortFields = sortBy.split(',');
|
|
59
93
|
sortFields.forEach((field) => {
|
|
60
|
-
const [key, order] = field.split(
|
|
61
|
-
sortOptions[key] = order ===
|
|
94
|
+
const [key, order] = field.split(':');
|
|
95
|
+
sortOptions[key] = order === 'desc' ? -1 : 1;
|
|
62
96
|
});
|
|
63
97
|
}
|
|
64
98
|
return sortOptions;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import type { DatabaseAdapter } from "../types";
|
|
1
|
+
import { Model } from 'sequelize';
|
|
2
|
+
import type { IdaeDbAdapterInterface, IdaeDbParams } from '../types.js';
|
|
4
3
|
interface MySQLDocument extends Model {
|
|
5
4
|
id: number;
|
|
6
5
|
[key: string]: any;
|
|
7
6
|
}
|
|
8
|
-
export declare class MySQLAdapter<T extends MySQLDocument> implements
|
|
7
|
+
export declare class MySQLAdapter<T extends MySQLDocument> implements IdaeDbAdapterInterface<T> {
|
|
9
8
|
private sequelize;
|
|
10
9
|
private model;
|
|
11
10
|
constructor(tableName: string);
|
|
12
11
|
create(document: Partial<T>): Promise<T>;
|
|
13
|
-
|
|
12
|
+
find(params: IdaeDbParams): Promise<T[]>;
|
|
14
13
|
findById(id: string): Promise<T | null>;
|
|
15
|
-
findOne(
|
|
14
|
+
findOne(): Promise<T | null>;
|
|
16
15
|
update(id: string, updateData: Partial<T>): Promise<T | null>;
|
|
16
|
+
updateWhere(params: IdaeDbParams, updateData: Partial<T>): Promise<T | null>;
|
|
17
17
|
deleteById(id: string): Promise<T | null>;
|
|
18
|
-
deleteManyByQuery(params:
|
|
18
|
+
deleteManyByQuery(params: IdaeDbParams): Promise<{
|
|
19
19
|
deletedCount?: number;
|
|
20
20
|
}>;
|
|
21
21
|
private parseSortOptions;
|
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
1
|
+
// packages\idae-api\src\lib\adapters\MySQLAdapter.ts
|
|
2
|
+
import { Sequelize, Model, DataTypes } from 'sequelize';
|
|
3
|
+
import dotenv from 'dotenv';
|
|
3
4
|
dotenv.config();
|
|
4
5
|
export class MySQLAdapter {
|
|
5
6
|
sequelize;
|
|
6
7
|
model;
|
|
7
8
|
constructor(tableName) {
|
|
8
9
|
this.sequelize = new Sequelize({
|
|
9
|
-
dialect:
|
|
10
|
-
host: process.env.MYSQL_HOST ||
|
|
10
|
+
dialect: 'mysql',
|
|
11
|
+
host: process.env.MYSQL_HOST || 'localhost',
|
|
11
12
|
port: Number(process.env.MYSQL_PORT) || 3306,
|
|
12
|
-
database: process.env.MYSQL_DATABASE ||
|
|
13
|
-
username: process.env.MYSQL_USER ||
|
|
14
|
-
password: process.env.MYSQL_PASSWORD ||
|
|
13
|
+
database: process.env.MYSQL_DATABASE || 'database',
|
|
14
|
+
username: process.env.MYSQL_USER || 'root',
|
|
15
|
+
password: process.env.MYSQL_PASSWORD || 'password'
|
|
15
16
|
});
|
|
16
17
|
this.model = this.sequelize.define(tableName, {
|
|
17
18
|
id: {
|
|
18
19
|
type: DataTypes.INTEGER.UNSIGNED,
|
|
19
20
|
autoIncrement: true,
|
|
20
|
-
primaryKey: true
|
|
21
|
-
}
|
|
21
|
+
primaryKey: true
|
|
22
|
+
}
|
|
22
23
|
// Define other fields here as needed
|
|
23
24
|
}, {
|
|
24
25
|
timestamps: false,
|
|
25
|
-
tableName
|
|
26
|
+
tableName
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
async create(document) {
|
|
29
30
|
const newDocument = await this.model.create(document);
|
|
30
31
|
return newDocument;
|
|
31
32
|
}
|
|
32
|
-
async
|
|
33
|
+
async find(params) {
|
|
33
34
|
const { query = {}, sortBy, limit, skip } = params;
|
|
34
35
|
const options = {
|
|
35
36
|
where: query,
|
|
36
37
|
order: this.parseSortOptions(sortBy),
|
|
37
38
|
limit: limit ? Number(limit) : undefined,
|
|
38
|
-
offset: skip ? Number(skip) : undefined
|
|
39
|
+
offset: skip ? Number(skip) : undefined
|
|
39
40
|
};
|
|
40
41
|
const results = await this.model.findAll(options);
|
|
41
42
|
return results;
|
|
@@ -44,7 +45,7 @@ export class MySQLAdapter {
|
|
|
44
45
|
const result = await this.model.findByPk(id);
|
|
45
46
|
return result ? result : null;
|
|
46
47
|
}
|
|
47
|
-
async findOne(
|
|
48
|
+
async findOne() {
|
|
48
49
|
const { query = {} } = params;
|
|
49
50
|
const result = await this.model.findOne({ where: query });
|
|
50
51
|
return result ? result : null;
|
|
@@ -52,7 +53,14 @@ export class MySQLAdapter {
|
|
|
52
53
|
async update(id, updateData) {
|
|
53
54
|
const [affectedCount, affectedRows] = await this.model.update(updateData, {
|
|
54
55
|
where: { id },
|
|
55
|
-
returning: true
|
|
56
|
+
returning: true
|
|
57
|
+
});
|
|
58
|
+
return affectedCount > 0 ? affectedRows[0] : null;
|
|
59
|
+
}
|
|
60
|
+
async updateWhere(params, updateData) {
|
|
61
|
+
const [affectedCount, affectedRows] = await this.model.update(updateData, {
|
|
62
|
+
where: { id },
|
|
63
|
+
returning: true
|
|
56
64
|
});
|
|
57
65
|
return affectedCount > 0 ? affectedRows[0] : null;
|
|
58
66
|
}
|
|
@@ -72,9 +80,9 @@ export class MySQLAdapter {
|
|
|
72
80
|
parseSortOptions(sortBy) {
|
|
73
81
|
if (!sortBy)
|
|
74
82
|
return [];
|
|
75
|
-
return sortBy.split(
|
|
76
|
-
const [key, order] = field.split(
|
|
77
|
-
return [key, order ===
|
|
83
|
+
return sortBy.split(',').map((field) => {
|
|
84
|
+
const [key, order] = field.split(':');
|
|
85
|
+
return [key, order === 'desc' ? 'DESC' : 'ASC'];
|
|
78
86
|
});
|
|
79
87
|
}
|
|
80
88
|
}
|
package/dist/idaeDb.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DbType } from './types.js';
|
|
2
|
+
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
3
|
+
import type { IdaeModelOptions } from './IdaeDBModel.js';
|
|
4
|
+
import type { Document } from 'mongodb';
|
|
5
|
+
import { IdaeDbAdapter } from './IdaeDbAdapter.js';
|
|
6
|
+
import type { EventListeners } from './IdaeEventEmitter.js';
|
|
7
|
+
type Uri = string;
|
|
8
|
+
type IdaeDbInstanceKey = `${DbType}:${Uri}`;
|
|
9
|
+
type Options = {
|
|
10
|
+
dbType: DbType;
|
|
11
|
+
dbScope: string | undefined;
|
|
12
|
+
dbScopeSeparator?: string;
|
|
13
|
+
idaeModelOptions?: IdaeModelOptions;
|
|
14
|
+
};
|
|
15
|
+
export declare class IdaeDb {
|
|
16
|
+
#private;
|
|
17
|
+
private _uri;
|
|
18
|
+
private globalEvents?;
|
|
19
|
+
private static instances;
|
|
20
|
+
private constructor();
|
|
21
|
+
/**
|
|
22
|
+
* Initializes or retrieves an IdaeDb instance.
|
|
23
|
+
* @param dbType The type of database.
|
|
24
|
+
* @param uri The URI of the database.
|
|
25
|
+
* @returns An IdaeDb instance.
|
|
26
|
+
*/
|
|
27
|
+
static init(uri: Uri, options?: Partial<Options>): IdaeDb;
|
|
28
|
+
/**
|
|
29
|
+
* Registers event listeners for all collections.
|
|
30
|
+
* @param events An object containing event listeners for different operations.
|
|
31
|
+
*/
|
|
32
|
+
registerEvents<T extends Document>(events: EventListeners<T>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new database connection.
|
|
35
|
+
* @param dbName The name of the database.
|
|
36
|
+
* @returns A Promise resolving to an IdaeDbConnection.
|
|
37
|
+
* @throws Error if the connection already exists.
|
|
38
|
+
*/
|
|
39
|
+
db(dbName: string): Promise<IdaeDbConnection>;
|
|
40
|
+
/**
|
|
41
|
+
* Gets an adapter for the current database type.
|
|
42
|
+
* @param collectionName The name of the collection or table.
|
|
43
|
+
* @returns An appropriate adapter for the database type.
|
|
44
|
+
* @throws Error if the connection is not found or the database type is unsupported.
|
|
45
|
+
*/
|
|
46
|
+
collection<T extends Document>(collectionName: string): IdaeDbAdapter<T>;
|
|
47
|
+
private applyEvents;
|
|
48
|
+
closeConnection(): Promise<void>;
|
|
49
|
+
closeAllConnections(): Promise<void>;
|
|
50
|
+
get adapterClass(): import("./IdaeDbAdapter.js").AdapterConstructor | undefined;
|
|
51
|
+
get connectionKey(): IdaeDbInstanceKey;
|
|
52
|
+
get uri(): string;
|
|
53
|
+
get options(): Options;
|
|
54
|
+
}
|
|
55
|
+
export {};
|
package/dist/idaeDb.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// packages\idae-db\lib\idaeDb.ts
|
|
2
|
+
import { DbType } from './types.js';
|
|
3
|
+
import { IdaeDbConnection } from './IdaeDbConnection.js';
|
|
4
|
+
import { IdaeDbAdapter } from './IdaeDbAdapter.js';
|
|
5
|
+
export class IdaeDb {
|
|
6
|
+
_uri;
|
|
7
|
+
globalEvents;
|
|
8
|
+
static instances = new Map();
|
|
9
|
+
#adapterClass;
|
|
10
|
+
#connections = new Map();
|
|
11
|
+
#connection = undefined;
|
|
12
|
+
#options = {
|
|
13
|
+
dbType: DbType.MONGODB,
|
|
14
|
+
dbScope: undefined,
|
|
15
|
+
idaeModelOptions: {}
|
|
16
|
+
};
|
|
17
|
+
constructor(_uri, options = {}) {
|
|
18
|
+
this._uri = _uri;
|
|
19
|
+
this.#options = { ...this.#options, ...options };
|
|
20
|
+
this.#adapterClass = IdaeDbAdapter.getAdapterForDbType(this.options.dbType);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Initializes or retrieves an IdaeDb instance.
|
|
24
|
+
* @param dbType The type of database.
|
|
25
|
+
* @param uri The URI of the database.
|
|
26
|
+
* @returns An IdaeDb instance.
|
|
27
|
+
*/
|
|
28
|
+
static init(uri, options) {
|
|
29
|
+
const dbType = options?.dbType ?? DbType.MONGODB;
|
|
30
|
+
const instanceKey = `${dbType}:${uri}`;
|
|
31
|
+
if (!IdaeDb.instances.has(instanceKey)) {
|
|
32
|
+
IdaeDb.instances.set(instanceKey, new IdaeDb(uri, options));
|
|
33
|
+
}
|
|
34
|
+
return IdaeDb.instances.get(instanceKey);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Registers event listeners for all collections.
|
|
38
|
+
* @param events An object containing event listeners for different operations.
|
|
39
|
+
*/
|
|
40
|
+
registerEvents(events) {
|
|
41
|
+
this.globalEvents = events;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new database connection.
|
|
45
|
+
* @param dbName The name of the database.
|
|
46
|
+
* @returns A Promise resolving to an IdaeDbConnection.
|
|
47
|
+
* @throws Error if the connection already exists.
|
|
48
|
+
*/
|
|
49
|
+
async db(dbName) {
|
|
50
|
+
if (this.#connections.has(this.connectionKey)) {
|
|
51
|
+
return this.#connections.get(this.connectionKey);
|
|
52
|
+
}
|
|
53
|
+
this.#connection = await new IdaeDbConnection(this, dbName).connect();
|
|
54
|
+
this.#connections.set(this.connectionKey, this.#connection);
|
|
55
|
+
return this.#connection;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets an adapter for the current database type.
|
|
59
|
+
* @param collectionName The name of the collection or table.
|
|
60
|
+
* @returns An appropriate adapter for the database type.
|
|
61
|
+
* @throws Error if the connection is not found or the database type is unsupported.
|
|
62
|
+
*/
|
|
63
|
+
// renamed from getAdapter to collection
|
|
64
|
+
collection(collectionName) {
|
|
65
|
+
if (!this.#connection) {
|
|
66
|
+
throw new Error(`Connection for '${this.options.dbType}:${this._uri}' not found.`);
|
|
67
|
+
}
|
|
68
|
+
const adapter = new IdaeDbAdapter(collectionName, this.#connection, this.options.dbType);
|
|
69
|
+
if (this.globalEvents) {
|
|
70
|
+
this.applyEvents(adapter, this.globalEvents);
|
|
71
|
+
}
|
|
72
|
+
return adapter;
|
|
73
|
+
}
|
|
74
|
+
applyEvents(adapter, events) {
|
|
75
|
+
for (const [method, listeners] of Object.entries(events)) {
|
|
76
|
+
if (listeners.pre) {
|
|
77
|
+
adapter.on(`pre:${String(method)}`, listeners.pre);
|
|
78
|
+
}
|
|
79
|
+
if (listeners.post) {
|
|
80
|
+
adapter.on(`post:${String(method)}`, listeners.post);
|
|
81
|
+
}
|
|
82
|
+
if (listeners.error) {
|
|
83
|
+
adapter.on(`error:${String(method)}`, listeners.error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async closeConnection() {
|
|
88
|
+
if (this.#connection) {
|
|
89
|
+
await this.#connection.close();
|
|
90
|
+
this.#connections.delete(this.connectionKey);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async closeAllConnections() {
|
|
94
|
+
for (const [connectionName, connection] of this.#connections) {
|
|
95
|
+
await connection.close();
|
|
96
|
+
}
|
|
97
|
+
this.#connections.clear();
|
|
98
|
+
}
|
|
99
|
+
get adapterClass() {
|
|
100
|
+
return this.#adapterClass;
|
|
101
|
+
}
|
|
102
|
+
get connectionKey() {
|
|
103
|
+
return `${this.options.dbType}:${this._uri}`;
|
|
104
|
+
}
|
|
105
|
+
get uri() {
|
|
106
|
+
return this._uri;
|
|
107
|
+
}
|
|
108
|
+
get options() {
|
|
109
|
+
return this.#options;
|
|
110
|
+
}
|
|
111
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export * from './types.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './idaeDb.js';
|
|
3
|
+
export * from './IdaeEventEmitter.js';
|
|
3
4
|
export * from './IdaeDbConnection.js';
|
|
5
|
+
export * from './IdaeDbAdapter.js';
|
|
4
6
|
export * from './IdaeDBModel.js';
|
|
5
7
|
export * from './adapters/MySQLAdapter.js';
|
|
6
8
|
export * from './adapters/MongoDBAdapter.js';
|
|
9
|
+
export * from './adapters/ChromaDBAdapter.js';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
// auto exports of entry components
|
|
2
2
|
export * from './types.js';
|
|
3
|
-
export * from './
|
|
3
|
+
export * from './idaeDb.js';
|
|
4
|
+
export * from './IdaeEventEmitter.js';
|
|
4
5
|
export * from './IdaeDbConnection.js';
|
|
6
|
+
export * from './IdaeDbAdapter.js';
|
|
5
7
|
export * from './IdaeDBModel.js';
|
|
6
8
|
export * from './adapters/MySQLAdapter.js';
|
|
7
9
|
export * from './adapters/MongoDBAdapter.js';
|
|
10
|
+
export * from './adapters/ChromaDBAdapter.js';
|
package/dist/types.d.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export interface
|
|
1
|
+
import { type Document, type Filter, type IndexSpecification, type CreateIndexesOptions } from 'mongodb';
|
|
2
|
+
export interface IdaeDbAdapterInterface<T extends Document> {
|
|
3
3
|
createIndex(fieldOrSpec: IndexSpecification, options?: CreateIndexesOptions): Promise<string>;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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>;
|
|
10
11
|
deleteWhere(params: IdaeDbParams<T>): Promise<{
|
|
11
12
|
deletedCount?: number;
|
|
12
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>;
|
|
13
18
|
}
|
|
14
|
-
export interface IdaeDbParams<T extends object
|
|
19
|
+
export interface IdaeDbParams<T extends object = Record<string, unknown>> {
|
|
15
20
|
id?: string;
|
|
16
21
|
query: Filter<T>;
|
|
17
22
|
sortBy?: string;
|
|
@@ -23,5 +28,6 @@ export interface IdaeDbParamsSortOptions {
|
|
|
23
28
|
}
|
|
24
29
|
export declare enum DbType {
|
|
25
30
|
MONGODB = "mongodb",
|
|
26
|
-
MYSQL = "mysql"
|
|
31
|
+
MYSQL = "mysql",
|
|
32
|
+
CHROMADB = "chromaDb"
|
|
27
33
|
}
|
package/dist/types.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// packages\idae-db\src\lib\adapters\types.ts
|
|
2
|
-
import {
|
|
2
|
+
import {} from 'mongodb';
|
|
3
3
|
export var DbType;
|
|
4
4
|
(function (DbType) {
|
|
5
5
|
DbType["MONGODB"] = "mongodb";
|
|
6
6
|
DbType["MYSQL"] = "mysql";
|
|
7
|
+
DbType["CHROMADB"] = "chromaDb";
|
|
7
8
|
})(DbType || (DbType = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-db",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "vite dev",
|
|
6
6
|
"build": "vite build && npm run package",
|
|
@@ -52,5 +52,8 @@
|
|
|
52
52
|
"svelte": "./dist/index.js",
|
|
53
53
|
"types": "./dist/index.d.ts",
|
|
54
54
|
"type": "module",
|
|
55
|
-
"scope": "@medyll"
|
|
56
|
-
|
|
55
|
+
"scope": "@medyll",
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"chromadb": "^1.8.1"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/dist/idaDb.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { DbType } from "./types.js";
|
|
2
|
-
import { IdaeDbConnection } from "./IdaeDbConnection";
|
|
3
|
-
import { MongoDBAdapter } from "./adapters/MongoDBAdapter";
|
|
4
|
-
import { MySQLAdapter } from "./adapters/MysqlAdapter";
|
|
5
|
-
import type { Document } from "mongodb";
|
|
6
|
-
export declare class IdaeDb {
|
|
7
|
-
private static instances;
|
|
8
|
-
private connections;
|
|
9
|
-
private dbType;
|
|
10
|
-
private constructor();
|
|
11
|
-
static getOrCreateInstance(dbType: DbType): IdaeDb;
|
|
12
|
-
createConnection(connectionName: string, uri: string, dbName: string): Promise<IdaeDbConnection>;
|
|
13
|
-
getConnection(connectionName: string): IdaeDbConnection;
|
|
14
|
-
getAdapter<T extends Document>(connectionName: string, collectionName: string): MongoDBAdapter<T> | MySQLAdapter<T>;
|
|
15
|
-
closeConnection(connectionName: string): Promise<void>;
|
|
16
|
-
closeAllConnections(): Promise<void>;
|
|
17
|
-
}
|