streamby-core 0.17.0 → 0.19.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 +206 -114
- package/dist/adapters/database/connectionManager.d.ts +6 -0
- package/dist/adapters/database/connectionManager.js +47 -0
- package/dist/adapters/database/nosql.d.ts +8 -0
- package/dist/adapters/database/nosql.js +46 -0
- package/dist/adapters/database/nosqlAdapter.d.ts +8 -0
- package/dist/adapters/database/nosqlAdapter.js +30 -0
- package/dist/adapters/database/sql.d.ts +8 -0
- package/dist/adapters/database/sql.js +42 -0
- package/dist/adapters/database/sqlAdapter.d.ts +8 -0
- package/dist/adapters/database/sqlAdapter.js +42 -0
- package/dist/adapters/s3.d.ts +11 -2
- package/dist/adapters/s3.js +59 -76
- package/dist/adapters/storage/gcs.d.ts +0 -0
- package/dist/adapters/storage/gcs.js +1 -0
- package/dist/adapters/storage/local.d.ts +0 -0
- package/dist/adapters/storage/local.js +1 -0
- package/dist/adapters/storage/s3.d.ts +11 -0
- package/dist/adapters/storage/s3.js +65 -0
- package/dist/database/Model.d.ts +11 -0
- package/dist/database/Model.js +95 -0
- package/dist/database/connectionManager.d.ts +5 -0
- package/dist/database/connectionManager.js +32 -0
- package/dist/database/index.d.ts +3 -0
- package/dist/database/index.js +19 -0
- package/dist/database/model.d.ts +20 -0
- package/dist/database/model.js +2 -0
- package/dist/database/modelManager.d.ts +8 -0
- package/dist/database/modelManager.js +38 -0
- package/dist/database/modelRegistry.d.ts +3 -0
- package/dist/database/modelRegistry.js +17 -0
- package/dist/database/mongoAdapter.d.ts +11 -0
- package/dist/database/mongoAdapter.js +42 -0
- package/dist/database/sqlAdapter.d.ts +12 -0
- package/dist/database/sqlAdapter.js +68 -0
- package/dist/middleware/createRouter.js +76 -140
- package/dist/models/Model.d.ts +12 -0
- package/dist/models/Model.js +146 -0
- package/dist/models/manager.d.ts +2 -0
- package/dist/models/manager.js +7 -0
- package/dist/models/modelManager.d.ts +2 -0
- package/dist/models/modelManager.js +7 -0
- package/dist/models/modelRegistry.d.ts +3 -0
- package/dist/models/modelRegistry.js +17 -0
- package/dist/models/register.d.ts +1 -0
- package/dist/models/register.js +8 -0
- package/dist/models/registerModel.d.ts +1 -0
- package/dist/models/registerModel.js +8 -0
- package/dist/models/registry.d.ts +3 -0
- package/dist/models/registry.js +17 -0
- package/dist/providers/createStorageProvider.d.ts +1 -1
- package/dist/providers/createStorageProvider.js +9 -13
- package/dist/providers/storage.d.ts +2 -0
- package/dist/providers/storage.js +12 -0
- package/dist/services/auth.d.ts +0 -1
- package/dist/services/auth.js +0 -11
- package/dist/types/index.d.ts +4 -95
- package/package.json +5 -1
- package/dist/config.d.ts +0 -18
- package/dist/config.js +0 -26
- package/dist/db/initExportModel.d.ts +0 -60
- package/dist/db/initExportModel.js +0 -18
- package/dist/db/initProjectModel.d.ts +0 -2502
- package/dist/db/initProjectModel.js +0 -33
- package/dist/db/initUserModel.d.ts +0 -48
- package/dist/db/initUserModel.js +0 -19
- package/dist/models/ExportCollection.d.ts +0 -54
- package/dist/models/ExportCollection.js +0 -19
- package/dist/mongoose_models/initExportModel.d.ts +0 -61
- package/dist/mongoose_models/initExportModel.js +0 -19
- package/dist/mongoose_models/initProjectModel.d.ts +0 -2503
- package/dist/mongoose_models/initProjectModel.js +0 -34
- package/dist/providers/createDatabaseProvider.d.ts +0 -2
- package/dist/providers/createDatabaseProvider.js +0 -36
- package/dist/providers/createMongooseProvider.d.ts +0 -8
- package/dist/providers/createMongooseProvider.js +0 -33
- package/dist/providers/createPrismaProvider.d.ts +0 -14
- package/dist/providers/createPrismaProvider.js +0 -29
- package/dist/providers/mongoExportProvider.d.ts +0 -3
- package/dist/providers/mongoExportProvider.js +0 -13
- package/dist/providers/mongoProjectProvider.d.ts +0 -3
- package/dist/providers/mongoProjectProvider.js +0 -107
- package/dist/providers/mongooseExportProvider.d.ts +0 -3
- package/dist/providers/mongooseExportProvider.js +0 -13
- package/dist/providers/mongooseProjectProvider.d.ts +0 -3
- package/dist/providers/mongooseProjectProvider.js +0 -114
- package/dist/providers/prismaExportCollectionProvider.d.ts +0 -61
- package/dist/providers/prismaExportCollectionProvider.js +0 -45
- package/dist/providers/prismaExportProvider.d.ts +0 -3
- package/dist/providers/prismaExportProvider.js +0 -13
- package/dist/providers/prismaProjectProvider.d.ts +0 -3
- package/dist/providers/prismaProjectProvider.js +0 -148
- package/dist/utils/pushPrismaSchema.d.ts +0 -5
- package/dist/utils/pushPrismaSchema.js +0 -35
package/dist/adapters/s3.js
CHANGED
|
@@ -1,82 +1,65 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.S3Adapter = void 0;
|
|
4
4
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
5
5
|
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Key: key,
|
|
41
|
-
ContentType: contentType,
|
|
42
|
-
});
|
|
43
|
-
const url = await (0, s3_request_presigner_1.getSignedUrl)(s3, command, { expiresIn: 60 });
|
|
44
|
-
const publicUrl = `https://${config.bucket}.s3.${config.region}.amazonaws.com/${encodeURIComponent(key)}`;
|
|
45
|
-
return { url, publicUrl };
|
|
46
|
-
},
|
|
47
|
-
async listFiles(projectId) {
|
|
48
|
-
const command = new client_s3_1.ListObjectsV2Command({
|
|
49
|
-
Bucket: config.bucket,
|
|
50
|
-
Prefix: `${projectId}/`,
|
|
51
|
-
});
|
|
52
|
-
const result = await s3.send(command);
|
|
53
|
-
return result.Contents || [];
|
|
54
|
-
},
|
|
55
|
-
async deleteProjectDirectory(projectId) {
|
|
56
|
-
const prefix = `${projectId}/`;
|
|
57
|
-
let continuationToken = undefined;
|
|
58
|
-
while (true) {
|
|
59
|
-
const listCommand = new client_s3_1.ListObjectsV2Command({
|
|
60
|
-
Bucket: config.bucket,
|
|
61
|
-
Prefix: prefix,
|
|
62
|
-
ContinuationToken: continuationToken
|
|
63
|
-
});
|
|
64
|
-
const listedObjects = await s3.send(listCommand);
|
|
65
|
-
const contents = listedObjects.Contents || [];
|
|
66
|
-
if (contents.length > 0) {
|
|
67
|
-
const objectsToDelete = contents.map((item) => ({
|
|
68
|
-
Key: item.Key
|
|
69
|
-
}));
|
|
70
|
-
const deleteCommand = new client_s3_1.DeleteObjectsCommand({
|
|
71
|
-
Bucket: config.bucket,
|
|
72
|
-
Delete: { Objects: objectsToDelete },
|
|
73
|
-
});
|
|
74
|
-
await s3.send(deleteCommand);
|
|
75
|
-
}
|
|
76
|
-
if (!listedObjects.IsTruncated)
|
|
77
|
-
break;
|
|
78
|
-
continuationToken = listedObjects.NextContinuationToken;
|
|
79
|
-
}
|
|
6
|
+
class S3Adapter {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.s3 = new client_s3_1.S3Client({
|
|
9
|
+
region: config.region,
|
|
10
|
+
credentials: {
|
|
11
|
+
accessKeyId: config.accessKeyId,
|
|
12
|
+
secretAccessKey: config.secretAccessKey,
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
this.bucket = config.bucket;
|
|
16
|
+
}
|
|
17
|
+
async listFiles(projectId) {
|
|
18
|
+
const command = new client_s3_1.ListObjectsV2Command({
|
|
19
|
+
Bucket: this.bucket,
|
|
20
|
+
Prefix: `${projectId}/`,
|
|
21
|
+
});
|
|
22
|
+
const result = await this.s3.send(command);
|
|
23
|
+
return result.Contents || [];
|
|
24
|
+
}
|
|
25
|
+
async deleteProjectImage(projectId) {
|
|
26
|
+
const command = new client_s3_1.DeleteObjectCommand({
|
|
27
|
+
Bucket: this.bucket,
|
|
28
|
+
Key: `${projectId}/project-image`,
|
|
29
|
+
});
|
|
30
|
+
return await this.s3.send(command);
|
|
31
|
+
}
|
|
32
|
+
async deleteProjectDirectory(projectId) {
|
|
33
|
+
const listCommand = new client_s3_1.ListObjectsV2Command({
|
|
34
|
+
Bucket: this.bucket,
|
|
35
|
+
Prefix: `${projectId}/`,
|
|
36
|
+
});
|
|
37
|
+
const listedObjects = await this.s3.send(listCommand);
|
|
38
|
+
if (!listedObjects.Contents || listedObjects.Contents.length === 0) {
|
|
39
|
+
return;
|
|
80
40
|
}
|
|
81
|
-
|
|
41
|
+
const deleteParams = {
|
|
42
|
+
Bucket: this.bucket,
|
|
43
|
+
Delete: { Objects: listedObjects.Contents.map(({ Key }) => ({ Key })) },
|
|
44
|
+
};
|
|
45
|
+
const deleteCommand = new client_s3_1.DeleteObjectCommand(deleteParams);
|
|
46
|
+
return await this.s3.send(deleteCommand);
|
|
47
|
+
}
|
|
48
|
+
async getPresignedUrl(contentType, projectId) {
|
|
49
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
50
|
+
Bucket: this.bucket,
|
|
51
|
+
Key: `${projectId}/${Date.now()}`,
|
|
52
|
+
ContentType: contentType,
|
|
53
|
+
});
|
|
54
|
+
return await (0, s3_request_presigner_1.getSignedUrl)(this.s3, command, { expiresIn: 3600 });
|
|
55
|
+
}
|
|
56
|
+
async getPresignedProjectImageUrl(projectId) {
|
|
57
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
58
|
+
Bucket: this.bucket,
|
|
59
|
+
Key: `${projectId}/project-image`,
|
|
60
|
+
ContentType: 'image/jpeg',
|
|
61
|
+
});
|
|
62
|
+
return await (0, s3_request_presigner_1.getSignedUrl)(this.s3, command, { expiresIn: 3600 });
|
|
63
|
+
}
|
|
82
64
|
}
|
|
65
|
+
exports.S3Adapter = S3Adapter;
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { S3Config, StorageAdapter } from '../../types';
|
|
2
|
+
export declare class S3Adapter implements StorageAdapter {
|
|
3
|
+
private s3;
|
|
4
|
+
private bucket;
|
|
5
|
+
constructor(config: S3Config);
|
|
6
|
+
listFiles(projectId: string): Promise<any[]>;
|
|
7
|
+
deleteProjectImage(projectId: string): Promise<any>;
|
|
8
|
+
deleteProjectDirectory(projectId: string): Promise<any>;
|
|
9
|
+
getPresignedUrl(contentType: string, projectId: string): Promise<any>;
|
|
10
|
+
getPresignedProjectImageUrl(projectId: string): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.S3Adapter = void 0;
|
|
4
|
+
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
5
|
+
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
6
|
+
class S3Adapter {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.s3 = new client_s3_1.S3Client({
|
|
9
|
+
region: config.region,
|
|
10
|
+
credentials: {
|
|
11
|
+
accessKeyId: config.accessKeyId,
|
|
12
|
+
secretAccessKey: config.secretAccessKey,
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
this.bucket = config.bucket;
|
|
16
|
+
}
|
|
17
|
+
async listFiles(projectId) {
|
|
18
|
+
const command = new client_s3_1.ListObjectsV2Command({
|
|
19
|
+
Bucket: this.bucket,
|
|
20
|
+
Prefix: `${projectId}/`,
|
|
21
|
+
});
|
|
22
|
+
const result = await this.s3.send(command);
|
|
23
|
+
return result.Contents || [];
|
|
24
|
+
}
|
|
25
|
+
async deleteProjectImage(projectId) {
|
|
26
|
+
const command = new client_s3_1.DeleteObjectCommand({
|
|
27
|
+
Bucket: this.bucket,
|
|
28
|
+
Key: `${projectId}/project-image`,
|
|
29
|
+
});
|
|
30
|
+
return await this.s3.send(command);
|
|
31
|
+
}
|
|
32
|
+
async deleteProjectDirectory(projectId) {
|
|
33
|
+
const listCommand = new client_s3_1.ListObjectsV2Command({
|
|
34
|
+
Bucket: this.bucket,
|
|
35
|
+
Prefix: `${projectId}/`,
|
|
36
|
+
});
|
|
37
|
+
const listedObjects = await this.s3.send(listCommand);
|
|
38
|
+
if (!listedObjects.Contents || listedObjects.Contents.length === 0) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const deleteParams = {
|
|
42
|
+
Bucket: this.bucket,
|
|
43
|
+
Delete: { Objects: listedObjects.Contents.map(({ Key }) => ({ Key })) },
|
|
44
|
+
};
|
|
45
|
+
const deleteCommand = new client_s3_1.DeleteObjectCommand(deleteParams);
|
|
46
|
+
return await this.s3.send(deleteCommand);
|
|
47
|
+
}
|
|
48
|
+
async getPresignedUrl(contentType, projectId) {
|
|
49
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
50
|
+
Bucket: this.bucket,
|
|
51
|
+
Key: `${projectId}/${Date.now()}`,
|
|
52
|
+
ContentType: contentType,
|
|
53
|
+
});
|
|
54
|
+
return await (0, s3_request_presigner_1.getSignedUrl)(this.s3, command, { expiresIn: 3600 });
|
|
55
|
+
}
|
|
56
|
+
async getPresignedProjectImageUrl(projectId) {
|
|
57
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
58
|
+
Bucket: this.bucket,
|
|
59
|
+
Key: `${projectId}/project-image`,
|
|
60
|
+
ContentType: 'image/jpeg',
|
|
61
|
+
});
|
|
62
|
+
return await (0, s3_request_presigner_1.getSignedUrl)(this.s3, command, { expiresIn: 3600 });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.S3Adapter = S3Adapter;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Document } from 'mongodb';
|
|
2
|
+
export declare class Model<T extends Document> {
|
|
3
|
+
private connectionId;
|
|
4
|
+
private tableName;
|
|
5
|
+
constructor(connectionId: string, tableName: string);
|
|
6
|
+
find(filter: any): Promise<T[]>;
|
|
7
|
+
findOne(filter: any): Promise<T | null>;
|
|
8
|
+
create(data: T): Promise<T>;
|
|
9
|
+
update(filter: any, data: Partial<T>): Promise<number>;
|
|
10
|
+
delete(filter: any): Promise<number>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Model = void 0;
|
|
4
|
+
const connectionManager_1 = require("./connectionManager");
|
|
5
|
+
const pg_1 = require("pg");
|
|
6
|
+
const mongodb_1 = require("mongodb");
|
|
7
|
+
class Model {
|
|
8
|
+
constructor(connectionId, tableName) {
|
|
9
|
+
this.connectionId = connectionId;
|
|
10
|
+
this.tableName = tableName;
|
|
11
|
+
}
|
|
12
|
+
async find(filter) {
|
|
13
|
+
const connection = (0, connectionManager_1.getConnection)(this.connectionId);
|
|
14
|
+
if (connection instanceof pg_1.Pool) {
|
|
15
|
+
const keys = Object.keys(filter);
|
|
16
|
+
const values = Object.values(filter);
|
|
17
|
+
const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
|
|
18
|
+
const result = await connection.query(`SELECT * FROM "${this.tableName}" WHERE ${where}`, values);
|
|
19
|
+
return result.rows;
|
|
20
|
+
}
|
|
21
|
+
else if (connection instanceof mongodb_1.MongoClient) {
|
|
22
|
+
const db = connection.db();
|
|
23
|
+
const result = await db.collection(this.tableName).find(filter).toArray();
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
async findOne(filter) {
|
|
29
|
+
const connection = (0, connectionManager_1.getConnection)(this.connectionId);
|
|
30
|
+
if (connection instanceof pg_1.Pool) {
|
|
31
|
+
const keys = Object.keys(filter);
|
|
32
|
+
const values = Object.values(filter);
|
|
33
|
+
const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
|
|
34
|
+
const result = await connection.query(`SELECT * FROM "${this.tableName}" WHERE ${where} LIMIT 1`, values);
|
|
35
|
+
return result.rows[0] || null;
|
|
36
|
+
}
|
|
37
|
+
else if (connection instanceof mongodb_1.MongoClient) {
|
|
38
|
+
const db = connection.db();
|
|
39
|
+
const result = await db.collection(this.tableName).findOne(filter);
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
async create(data) {
|
|
45
|
+
const connection = (0, connectionManager_1.getConnection)(this.connectionId);
|
|
46
|
+
if (connection instanceof pg_1.Pool) {
|
|
47
|
+
const keys = Object.keys(data).join(', ');
|
|
48
|
+
const values = Object.values(data);
|
|
49
|
+
const placeholders = values.map((_, i) => `$${i + 1}`).join(', ');
|
|
50
|
+
const result = await connection.query(`INSERT INTO "${this.tableName}" (${keys}) VALUES (${placeholders}) RETURNING *`, values);
|
|
51
|
+
return result.rows[0];
|
|
52
|
+
}
|
|
53
|
+
else if (connection instanceof mongodb_1.MongoClient) {
|
|
54
|
+
const db = connection.db();
|
|
55
|
+
const result = await db.collection(this.tableName).insertOne(data);
|
|
56
|
+
return { ...data, _id: result.insertedId };
|
|
57
|
+
}
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
async update(filter, data) {
|
|
61
|
+
const connection = (0, connectionManager_1.getConnection)(this.connectionId);
|
|
62
|
+
if (connection instanceof pg_1.Pool) {
|
|
63
|
+
const dataKeys = Object.keys(data);
|
|
64
|
+
const filterKeys = Object.keys(filter);
|
|
65
|
+
const values = [...Object.values(data), ...Object.values(filter)];
|
|
66
|
+
const setClause = dataKeys.map((key, i) => `"${key}" = $${i + 1}`).join(', ');
|
|
67
|
+
const whereClause = filterKeys.map((key, i) => `"${key}" = $${dataKeys.length + i + 1}`).join(' AND ');
|
|
68
|
+
const result = await connection.query(`UPDATE "${this.tableName}" SET ${setClause} WHERE ${whereClause}`, values);
|
|
69
|
+
return result.rowCount || 0;
|
|
70
|
+
}
|
|
71
|
+
else if (connection instanceof mongodb_1.MongoClient) {
|
|
72
|
+
const db = connection.db();
|
|
73
|
+
const result = await db.collection(this.tableName).updateMany(filter, { $set: data });
|
|
74
|
+
return result.modifiedCount;
|
|
75
|
+
}
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
async delete(filter) {
|
|
79
|
+
const connection = (0, connectionManager_1.getConnection)(this.connectionId);
|
|
80
|
+
if (connection instanceof pg_1.Pool) {
|
|
81
|
+
const keys = Object.keys(filter);
|
|
82
|
+
const values = Object.values(filter);
|
|
83
|
+
const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
|
|
84
|
+
const result = await connection.query(`DELETE FROM "${this.tableName}" WHERE ${where}`, values);
|
|
85
|
+
return result.rowCount || 0;
|
|
86
|
+
}
|
|
87
|
+
else if (connection instanceof mongodb_1.MongoClient) {
|
|
88
|
+
const db = connection.db();
|
|
89
|
+
const result = await db.collection(this.tableName).deleteMany(filter);
|
|
90
|
+
return result.deletedCount;
|
|
91
|
+
}
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.Model = Model;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { MongoClient } from 'mongodb';
|
|
3
|
+
import { DatabaseCredential } from '../types';
|
|
4
|
+
export declare const initConnections: (configs: DatabaseCredential[]) => Promise<void>;
|
|
5
|
+
export declare const getConnection: (id: string) => Pool | MongoClient;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getConnection = exports.initConnections = void 0;
|
|
4
|
+
const pg_1 = require("pg");
|
|
5
|
+
const mongodb_1 = require("mongodb");
|
|
6
|
+
const clients = {};
|
|
7
|
+
const initConnections = async (configs) => {
|
|
8
|
+
for (const config of configs) {
|
|
9
|
+
if (!config.connectionString) {
|
|
10
|
+
throw new Error(`Connection string not provided for database config with id ${config.id}`);
|
|
11
|
+
}
|
|
12
|
+
if (config.type === 'sql') {
|
|
13
|
+
const pool = new pg_1.Pool({ connectionString: config.connectionString });
|
|
14
|
+
await pool.connect();
|
|
15
|
+
clients[config.id] = pool;
|
|
16
|
+
}
|
|
17
|
+
else if (config.type === 'nosql') {
|
|
18
|
+
const client = new mongodb_1.MongoClient(config.connectionString);
|
|
19
|
+
await client.connect();
|
|
20
|
+
clients[config.id] = client;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
exports.initConnections = initConnections;
|
|
25
|
+
const getConnection = (id) => {
|
|
26
|
+
const client = clients[id];
|
|
27
|
+
if (!client) {
|
|
28
|
+
throw new Error(`Connection with id ${id} not found.`);
|
|
29
|
+
}
|
|
30
|
+
return client;
|
|
31
|
+
};
|
|
32
|
+
exports.getConnection = getConnection;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./connectionManager"), exports);
|
|
18
|
+
__exportStar(require("./Model"), exports);
|
|
19
|
+
__exportStar(require("./modelRegistry"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface BaseModel {
|
|
2
|
+
id?: string;
|
|
3
|
+
}
|
|
4
|
+
export interface ModelDefinition<T extends BaseModel> {
|
|
5
|
+
name: string;
|
|
6
|
+
connectionName: string;
|
|
7
|
+
}
|
|
8
|
+
export interface QueryOptions {
|
|
9
|
+
filter?: Record<string, any>;
|
|
10
|
+
limit?: number;
|
|
11
|
+
offset?: number;
|
|
12
|
+
sort?: Record<string, 1 | -1>;
|
|
13
|
+
}
|
|
14
|
+
export interface ModelOperations<T extends BaseModel> {
|
|
15
|
+
find(options?: QueryOptions): Promise<T[]>;
|
|
16
|
+
findById(id: string): Promise<T | null>;
|
|
17
|
+
create(data: Omit<T, 'id'>): Promise<T>;
|
|
18
|
+
update(id: string, data: Partial<Omit<T, 'id'>>): Promise<T | null>;
|
|
19
|
+
delete(id: string): Promise<boolean>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BaseModel, ModelDefinition, ModelOperations } from './model';
|
|
2
|
+
declare class ModelManager {
|
|
3
|
+
private models;
|
|
4
|
+
registerModel<T extends BaseModel>(definition: ModelDefinition<T>): void;
|
|
5
|
+
getModel<T extends BaseModel>(name: string): ModelOperations<T>;
|
|
6
|
+
}
|
|
7
|
+
export declare const modelManager: ModelManager;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelManager = void 0;
|
|
4
|
+
const connectionManager_1 = require("./connectionManager");
|
|
5
|
+
const sqlAdapter_1 = require("./sqlAdapter");
|
|
6
|
+
const mongoAdapter_1 = require("./mongoAdapter");
|
|
7
|
+
const mongodb_1 = require("mongodb");
|
|
8
|
+
class ModelManager {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.models = new Map();
|
|
11
|
+
}
|
|
12
|
+
registerModel(definition) {
|
|
13
|
+
const client = connectionManager_1.connectionManager.getClient(definition.connectionName);
|
|
14
|
+
if (!client) {
|
|
15
|
+
throw new Error(`Connection '${definition.connectionName}' not found for model '${definition.name}'`);
|
|
16
|
+
}
|
|
17
|
+
let adapter;
|
|
18
|
+
if (client instanceof mongodb_1.Pool) {
|
|
19
|
+
adapter = new sqlAdapter_1.SqlAdapter(client, definition.name.toLowerCase() + 's'); // Assuming table name is plural of model name
|
|
20
|
+
}
|
|
21
|
+
else if (client instanceof mongodb_1.MongoClient) {
|
|
22
|
+
adapter = new mongoAdapter_1.MongoAdapter(client, definition.name.toLowerCase() + 's'); // Assuming collection name is plural of model name
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new Error(`Unsupported client type for connection '${definition.connectionName}'`);
|
|
26
|
+
}
|
|
27
|
+
this.models.set(definition.name, adapter);
|
|
28
|
+
console.log(`Model '${definition.name}' registered with connection '${definition.connectionName}'`);
|
|
29
|
+
}
|
|
30
|
+
getModel(name) {
|
|
31
|
+
const model = this.models.get(name);
|
|
32
|
+
if (!model) {
|
|
33
|
+
throw new Error(`Model '${name}' not found. Make sure it is registered.`);
|
|
34
|
+
}
|
|
35
|
+
return model;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.modelManager = new ModelManager();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getModel = exports.defineModel = void 0;
|
|
4
|
+
const Model_1 = require("./Model");
|
|
5
|
+
const models = {};
|
|
6
|
+
const defineModel = (name, connectionId, tableName) => {
|
|
7
|
+
models[name] = new Model_1.Model(connectionId, tableName);
|
|
8
|
+
};
|
|
9
|
+
exports.defineModel = defineModel;
|
|
10
|
+
const getModel = (name) => {
|
|
11
|
+
const model = models[name];
|
|
12
|
+
if (!model) {
|
|
13
|
+
throw new Error(`Model ${name} not defined.`);
|
|
14
|
+
}
|
|
15
|
+
return model;
|
|
16
|
+
};
|
|
17
|
+
exports.getModel = getModel;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MongoClient } from 'mongodb';
|
|
2
|
+
import { BaseModel, ModelOperations, QueryOptions } from './model';
|
|
3
|
+
export declare class MongoAdapter<T extends BaseModel> implements ModelOperations<T> {
|
|
4
|
+
private collection;
|
|
5
|
+
constructor(client: MongoClient, collectionName: string);
|
|
6
|
+
find(options?: QueryOptions): Promise<T[]>;
|
|
7
|
+
findById(id: string): Promise<T | null>;
|
|
8
|
+
create(data: Omit<T, 'id'>): Promise<T>;
|
|
9
|
+
update(id: string, data: Partial<Omit<T, 'id'>>): Promise<T | null>;
|
|
10
|
+
delete(id: string): Promise<boolean>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongoAdapter = void 0;
|
|
4
|
+
const mongodb_1 = require("mongodb");
|
|
5
|
+
class MongoAdapter {
|
|
6
|
+
constructor(client, collectionName) {
|
|
7
|
+
// Assuming a default database name for now, or it could be passed in config
|
|
8
|
+
const db = client.db();
|
|
9
|
+
this.collection = db.collection(collectionName);
|
|
10
|
+
}
|
|
11
|
+
async find(options) {
|
|
12
|
+
let cursor = this.collection.find(options?.filter || {});
|
|
13
|
+
if (options?.sort) {
|
|
14
|
+
cursor = cursor.sort(options.sort);
|
|
15
|
+
}
|
|
16
|
+
if (options?.limit) {
|
|
17
|
+
cursor = cursor.limit(options.limit);
|
|
18
|
+
}
|
|
19
|
+
if (options?.offset) {
|
|
20
|
+
cursor = cursor.skip(options.offset);
|
|
21
|
+
}
|
|
22
|
+
const result = await cursor.toArray();
|
|
23
|
+
return result.map(doc => ({ ...doc, id: doc._id?.toString() }));
|
|
24
|
+
}
|
|
25
|
+
async findById(id) {
|
|
26
|
+
const doc = await this.collection.findOne({ _id: new mongodb_1.ObjectId(id) });
|
|
27
|
+
return doc ? { ...doc, id: doc._id?.toString() } : null;
|
|
28
|
+
}
|
|
29
|
+
async create(data) {
|
|
30
|
+
const result = await this.collection.insertOne(data);
|
|
31
|
+
return { ...data, id: result.insertedId.toString() };
|
|
32
|
+
}
|
|
33
|
+
async update(id, data) {
|
|
34
|
+
const result = await this.collection.findOneAndUpdate({ _id: new mongodb_1.ObjectId(id) }, { $set: data }, { returnDocument: 'after' });
|
|
35
|
+
return result.value ? { ...result.value, id: result.value._id?.toString() } : null;
|
|
36
|
+
}
|
|
37
|
+
async delete(id) {
|
|
38
|
+
const result = await this.collection.deleteOne({ _id: new mongodb_1.ObjectId(id) });
|
|
39
|
+
return result.deletedCount > 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.MongoAdapter = MongoAdapter;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { BaseModel, ModelOperations, QueryOptions } from './model';
|
|
3
|
+
export declare class SqlAdapter<T extends BaseModel> implements ModelOperations<T> {
|
|
4
|
+
private client;
|
|
5
|
+
private tableName;
|
|
6
|
+
constructor(client: Pool, tableName: string);
|
|
7
|
+
find(options?: QueryOptions): Promise<T[]>;
|
|
8
|
+
findById(id: string): Promise<T | null>;
|
|
9
|
+
create(data: Omit<T, 'id'>): Promise<T>;
|
|
10
|
+
update(id: string, data: Partial<Omit<T, 'id'>>): Promise<T | null>;
|
|
11
|
+
delete(id: string): Promise<boolean>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlAdapter = void 0;
|
|
4
|
+
class SqlAdapter {
|
|
5
|
+
constructor(client, tableName) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.tableName = tableName;
|
|
8
|
+
}
|
|
9
|
+
async find(options) {
|
|
10
|
+
let query = `SELECT * FROM ${this.tableName}`;
|
|
11
|
+
const values = [];
|
|
12
|
+
const conditions = [];
|
|
13
|
+
let paramIndex = 1;
|
|
14
|
+
if (options?.filter) {
|
|
15
|
+
for (const key in options.filter) {
|
|
16
|
+
conditions.push(`${key} = $${paramIndex++}`);
|
|
17
|
+
values.push(options.filter[key]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (conditions.length > 0) {
|
|
21
|
+
query += ` WHERE ${conditions.join(' AND ')}`;
|
|
22
|
+
}
|
|
23
|
+
if (options?.sort) {
|
|
24
|
+
const sortKeys = Object.keys(options.sort);
|
|
25
|
+
if (sortKeys.length > 0) {
|
|
26
|
+
const orderBy = sortKeys.map(key => `${key} ${options.sort[key] === 1 ? 'ASC' : 'DESC'}`).join(', ');
|
|
27
|
+
query += ` ORDER BY ${orderBy}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (options?.limit) {
|
|
31
|
+
query += ` LIMIT $${paramIndex++}`;
|
|
32
|
+
values.push(options.limit);
|
|
33
|
+
}
|
|
34
|
+
if (options?.offset) {
|
|
35
|
+
query += ` OFFSET $${paramIndex++}`;
|
|
36
|
+
values.push(options.offset);
|
|
37
|
+
}
|
|
38
|
+
const result = await this.client.query(query, values);
|
|
39
|
+
return result.rows;
|
|
40
|
+
}
|
|
41
|
+
async findById(id) {
|
|
42
|
+
const query = `SELECT * FROM ${this.tableName} WHERE id = $1`;
|
|
43
|
+
const result = await this.client.query(query, [id]);
|
|
44
|
+
return result.rows[0] || null;
|
|
45
|
+
}
|
|
46
|
+
async create(data) {
|
|
47
|
+
const keys = Object.keys(data);
|
|
48
|
+
const values = Object.values(data);
|
|
49
|
+
const placeholders = keys.map((_, i) => `$${i + 1}`).join(', ');
|
|
50
|
+
const query = `INSERT INTO ${this.tableName} (${keys.join(', ')}) VALUES (${placeholders}) RETURNING *`;
|
|
51
|
+
const result = await this.client.query(query, values);
|
|
52
|
+
return result.rows[0];
|
|
53
|
+
}
|
|
54
|
+
async update(id, data) {
|
|
55
|
+
const keys = Object.keys(data);
|
|
56
|
+
const values = Object.values(data);
|
|
57
|
+
const setClauses = keys.map((key, i) => `${key} = $${i + 1}`).join(', ');
|
|
58
|
+
const query = `UPDATE ${this.tableName} SET ${setClauses} WHERE id = $${keys.length + 1} RETURNING *`;
|
|
59
|
+
const result = await this.client.query(query, [...values, id]);
|
|
60
|
+
return result.rows[0] || null;
|
|
61
|
+
}
|
|
62
|
+
async delete(id) {
|
|
63
|
+
const query = `DELETE FROM ${this.tableName} WHERE id = $1`;
|
|
64
|
+
const result = await this.client.query(query, [id]);
|
|
65
|
+
return result.rowCount > 0;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.SqlAdapter = SqlAdapter;
|