streamby-core 0.20.2 → 0.21.1
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/adapters/database/connectionManager.d.ts +5 -1
- package/dist/adapters/database/connectionManager.js +66 -10
- package/dist/adapters/database/nosql.d.ts +1 -0
- package/dist/adapters/database/nosql.js +15 -1
- package/dist/adapters/database/sql.js +7 -3
- package/dist/middleware/createRouter.js +96 -14
- package/dist/models/Model.js +167 -36
- package/dist/services/auth.d.ts +1 -3
- package/dist/services/auth.js +0 -10
- package/dist/types/index.d.ts +1 -2
- package/package.json +1 -1
- package/dist/adapters/database/nosqlAdapter.d.ts +0 -8
- package/dist/adapters/database/nosqlAdapter.js +0 -30
- package/dist/adapters/database/sqlAdapter.d.ts +0 -8
- package/dist/adapters/database/sqlAdapter.js +0 -42
- package/dist/adapters/gcs.d.ts +0 -0
- package/dist/adapters/gcs.js +0 -1
- package/dist/adapters/local.d.ts +0 -0
- package/dist/adapters/local.js +0 -1
- package/dist/adapters/s3.d.ts +0 -11
- package/dist/adapters/s3.js +0 -65
- package/dist/database/Model.d.ts +0 -11
- package/dist/database/Model.js +0 -95
- package/dist/database/connectionManager.d.ts +0 -5
- package/dist/database/connectionManager.js +0 -32
- package/dist/database/index.d.ts +0 -3
- package/dist/database/index.js +0 -19
- package/dist/database/model.d.ts +0 -20
- package/dist/database/model.js +0 -2
- package/dist/database/modelManager.d.ts +0 -8
- package/dist/database/modelManager.js +0 -38
- package/dist/database/modelRegistry.d.ts +0 -3
- package/dist/database/modelRegistry.js +0 -17
- package/dist/database/mongoAdapter.d.ts +0 -11
- package/dist/database/mongoAdapter.js +0 -42
- package/dist/database/sqlAdapter.d.ts +0 -12
- package/dist/database/sqlAdapter.js +0 -68
- package/dist/models/modelManager.d.ts +0 -2
- package/dist/models/modelManager.js +0 -7
- package/dist/models/modelRegistry.d.ts +0 -3
- package/dist/models/modelRegistry.js +0 -17
- package/dist/models/registerModel.d.ts +0 -1
- package/dist/models/registerModel.js +0 -8
- package/dist/providers/createStorageProvider.d.ts +0 -2
- package/dist/providers/createStorageProvider.js +0 -12
package/dist/models/Model.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Model = void 0;
|
|
4
4
|
const connectionManager_1 = require("../adapters/database/connectionManager");
|
|
5
|
-
const pg_1 = require("pg");
|
|
6
5
|
const mongodb_1 = require("mongodb");
|
|
7
6
|
const sql_1 = require("../adapters/database/sql");
|
|
8
7
|
const nosql_1 = require("../adapters/database/nosql");
|
|
@@ -15,15 +14,55 @@ class Model {
|
|
|
15
14
|
const allResults = [];
|
|
16
15
|
const activeConnectionIds = this.connectionIds.filter(id => (0, connectionManager_1.getConnectedIds)().includes(id));
|
|
17
16
|
for (const connectionId of activeConnectionIds) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
const clientEntry = (0, connectionManager_1.getConnection)(connectionId);
|
|
18
|
+
const connection = clientEntry.client;
|
|
19
|
+
const dbType = clientEntry.type;
|
|
20
|
+
let processedFilter = { ...filter };
|
|
21
|
+
if (dbType === 'sql') {
|
|
22
|
+
if (processedFilter._id) {
|
|
23
|
+
processedFilter.id = processedFilter._id;
|
|
24
|
+
delete processedFilter._id;
|
|
25
|
+
}
|
|
26
|
+
let sqlResults = [];
|
|
27
|
+
if (this.tableName === 'projects' && processedFilter.members && processedFilter.members.$elemMatch) {
|
|
28
|
+
// Handle members filter for SQL projects
|
|
29
|
+
const userId = processedFilter.members.$elemMatch.userId;
|
|
30
|
+
const query = `
|
|
31
|
+
SELECT p.*, pm."userId" as memberUserId, pm.archived as memberArchived
|
|
32
|
+
FROM "projects" p
|
|
33
|
+
JOIN "project_members" pm ON p.id = pm."projectId"
|
|
34
|
+
WHERE pm."userId" = $1
|
|
35
|
+
`;
|
|
36
|
+
const result = await connection.query(query, [userId]);
|
|
37
|
+
sqlResults = result.rows.map(row => ({
|
|
38
|
+
...row,
|
|
39
|
+
members: [{
|
|
40
|
+
userId: row.memberUserId,
|
|
41
|
+
archived: row.memberArchived,
|
|
42
|
+
// Add other member fields if necessary
|
|
43
|
+
}]
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// General SQL find
|
|
48
|
+
sqlResults = await sql_1.sqlAdapter.find(connection, this.tableName, processedFilter);
|
|
49
|
+
}
|
|
21
50
|
for (const item of sqlResults) {
|
|
22
|
-
|
|
51
|
+
const transformedItem = this.transformResult(item);
|
|
52
|
+
if (this.tableName === 'projects' && !transformedItem.members) {
|
|
53
|
+
// For projects, if members not already populated by join, fetch from project_members table
|
|
54
|
+
const projectMembers = await sql_1.sqlAdapter.find(connection, 'project_members', { projectId: transformedItem.id });
|
|
55
|
+
transformedItem.members = projectMembers.map((member) => ({
|
|
56
|
+
userId: member.userId,
|
|
57
|
+
role: member.role, // Assuming role is also stored in project_members
|
|
58
|
+
archived: member.archived,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
allResults.push(transformedItem);
|
|
23
62
|
}
|
|
24
63
|
}
|
|
25
|
-
else if (
|
|
26
|
-
const nosqlResults = await nosql_1.nosqlAdapter.find(connection, this.tableName,
|
|
64
|
+
else if (dbType === 'nosql') {
|
|
65
|
+
const nosqlResults = await nosql_1.nosqlAdapter.find(connection, this.tableName, processedFilter);
|
|
27
66
|
for (const item of nosqlResults) {
|
|
28
67
|
allResults.push(this.transformResult(item));
|
|
29
68
|
}
|
|
@@ -41,18 +80,41 @@ class Model {
|
|
|
41
80
|
async findOne(filter) {
|
|
42
81
|
const activeConnectionIds = this.connectionIds.filter(id => (0, connectionManager_1.getConnectedIds)().includes(id));
|
|
43
82
|
for (const connectionId of activeConnectionIds) {
|
|
44
|
-
const
|
|
83
|
+
const clientEntry = (0, connectionManager_1.getConnection)(connectionId);
|
|
84
|
+
const connection = clientEntry.client;
|
|
85
|
+
const dbType = clientEntry.type;
|
|
45
86
|
let processedFilter = { ...filter };
|
|
46
|
-
if (
|
|
87
|
+
if (dbType === 'sql') {
|
|
47
88
|
if (processedFilter._id) {
|
|
48
89
|
processedFilter.id = processedFilter._id;
|
|
49
90
|
delete processedFilter._id;
|
|
50
91
|
}
|
|
51
92
|
const result = await sql_1.sqlAdapter.findOne(connection, this.tableName, processedFilter);
|
|
52
|
-
if (result)
|
|
53
|
-
|
|
93
|
+
if (result) {
|
|
94
|
+
const transformedResult = this.transformResult(result);
|
|
95
|
+
if (this.tableName === 'projects') {
|
|
96
|
+
// For projects, fetch members from project_members table
|
|
97
|
+
const projectMembers = await sql_1.sqlAdapter.find(connection, 'project_members', { projectId: transformedResult.id });
|
|
98
|
+
transformedResult.members = projectMembers.map((member) => ({
|
|
99
|
+
userId: member.userId,
|
|
100
|
+
role: member.role, // Assuming role is also stored in project_members
|
|
101
|
+
archived: member.archived,
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
return transformedResult;
|
|
105
|
+
}
|
|
54
106
|
}
|
|
55
|
-
else if (
|
|
107
|
+
else if (dbType === 'nosql') {
|
|
108
|
+
if (processedFilter._id && typeof processedFilter._id === 'string') {
|
|
109
|
+
try {
|
|
110
|
+
processedFilter._id = new mongodb_1.ObjectId(processedFilter._id);
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
// If it's not a valid ObjectId string, it might be a UUID from a SQL project.
|
|
114
|
+
// In this case, this NoSQL connection won't find it, so we can just continue.
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
56
118
|
const result = await nosql_1.nosqlAdapter.findOne(connection, this.tableName, processedFilter);
|
|
57
119
|
if (result)
|
|
58
120
|
return result;
|
|
@@ -62,12 +124,45 @@ class Model {
|
|
|
62
124
|
}
|
|
63
125
|
async create(data) {
|
|
64
126
|
const activeConnectionIds = this.connectionIds.filter(id => (0, connectionManager_1.getConnectedIds)().includes(id));
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
127
|
+
// Determine the target database type from the data payload
|
|
128
|
+
const targetDbType = data.dbType || 'nosql'; // Default to nosql if not specified
|
|
129
|
+
const allClientEntries = activeConnectionIds.map(id => (0, connectionManager_1.getConnection)(id));
|
|
130
|
+
// Find the connection that matches the targetDbType
|
|
131
|
+
const clientEntry = allClientEntries.find(entry => entry.type === targetDbType);
|
|
132
|
+
if (!clientEntry) {
|
|
133
|
+
throw new Error(`No active connection found for database type: ${targetDbType}`);
|
|
134
|
+
}
|
|
135
|
+
const connection = clientEntry.client;
|
|
136
|
+
const dbType = clientEntry.type;
|
|
137
|
+
if (dbType === 'sql') {
|
|
138
|
+
const dataToInsert = {};
|
|
139
|
+
let membersToInsert = [];
|
|
140
|
+
// Iterate over the keys of the incoming data
|
|
141
|
+
for (const key in data) {
|
|
142
|
+
if (this.tableName === 'projects' && key === 'members') {
|
|
143
|
+
membersToInsert = data[key];
|
|
144
|
+
}
|
|
145
|
+
else if (this.tableName === 'projects' && key.toLowerCase() === 'dbtype') {
|
|
146
|
+
dataToInsert["dbType"] = data[key];
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
dataToInsert[key] = data[key];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const created = await sql_1.sqlAdapter.create(connection, this.tableName, dataToInsert);
|
|
153
|
+
// Insert members into project_members table for SQL
|
|
154
|
+
if (this.tableName === 'projects' && membersToInsert.length > 0) {
|
|
155
|
+
for (const member of membersToInsert) {
|
|
156
|
+
await sql_1.sqlAdapter.create(connection, 'project_members', {
|
|
157
|
+
projectId: created.id,
|
|
158
|
+
userId: member.userId,
|
|
159
|
+
archived: member.archived || false,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
68
163
|
return created;
|
|
69
164
|
}
|
|
70
|
-
else if (
|
|
165
|
+
else if (dbType === 'nosql') {
|
|
71
166
|
const created = await nosql_1.nosqlAdapter.create(connection, this.tableName, data);
|
|
72
167
|
return created;
|
|
73
168
|
}
|
|
@@ -76,7 +171,9 @@ class Model {
|
|
|
76
171
|
async update(filter, data) {
|
|
77
172
|
const activeConnectionIds = this.connectionIds.filter(id => (0, connectionManager_1.getConnectedIds)().includes(id));
|
|
78
173
|
for (const connectionId of activeConnectionIds) {
|
|
79
|
-
const
|
|
174
|
+
const clientEntry = (0, connectionManager_1.getConnection)(connectionId);
|
|
175
|
+
const connection = clientEntry.client;
|
|
176
|
+
const dbType = clientEntry.type;
|
|
80
177
|
let processedFilter = { ...filter };
|
|
81
178
|
// Handle specific member archiving/unarchiving
|
|
82
179
|
if (data["members.$.archived"] !== undefined && processedFilter["_id"] && processedFilter["members.userId"]) {
|
|
@@ -85,25 +182,45 @@ class Model {
|
|
|
85
182
|
const newArchivedStatus = data["members.$.archived"];
|
|
86
183
|
const archivedBy = data["members.$.archivedBy"];
|
|
87
184
|
const archivedAt = data["members.$.archivedAt"];
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return { ...member, archived: newArchivedStatus, archivedBy: archivedBy, archivedAt: archivedAt };
|
|
185
|
+
if (dbType === 'sql') {
|
|
186
|
+
// For SQL, directly update the project_members table
|
|
187
|
+
const memberUpdateResult = await sql_1.sqlAdapter.update(connection, 'project_members', { projectId: projectId, userId: userIdToUpdate }, { archived: newArchivedStatus, archivedBy: archivedBy, archivedAt: archivedAt });
|
|
188
|
+
// After updating the member, fetch the full project to return
|
|
189
|
+
if (memberUpdateResult) {
|
|
190
|
+
const updatedProject = await this.findOne({ _id: projectId });
|
|
191
|
+
return updatedProject;
|
|
96
192
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
193
|
+
}
|
|
194
|
+
else if (dbType === 'nosql') {
|
|
195
|
+
// For NoSQL, fetch the project to modify its members array
|
|
196
|
+
const projectToUpdate = await this.findOne({ _id: projectId });
|
|
197
|
+
if (!projectToUpdate || !projectToUpdate.members) {
|
|
198
|
+
continue; // Project not found or has no members, try next connection
|
|
199
|
+
}
|
|
200
|
+
const updatedMembers = projectToUpdate.members.map((member) => {
|
|
201
|
+
if (member.userId === userIdToUpdate) {
|
|
202
|
+
return { ...member, archived: newArchivedStatus, archivedBy: archivedBy, archivedAt: archivedAt };
|
|
203
|
+
}
|
|
204
|
+
return member;
|
|
205
|
+
});
|
|
206
|
+
let objectIdProjectId;
|
|
207
|
+
try {
|
|
208
|
+
objectIdProjectId = new mongodb_1.ObjectId(projectId);
|
|
209
|
+
}
|
|
210
|
+
catch (e) {
|
|
211
|
+
// If it's not a valid ObjectId string, it might be a UUID from a SQL project.
|
|
212
|
+
// In this case, this NoSQL connection won't find it, so we can just continue.
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
// Update the project with the modified members array
|
|
216
|
+
const result = await nosql_1.nosqlAdapter.update(connection, this.tableName, { _id: objectIdProjectId }, { members: updatedMembers });
|
|
217
|
+
if (result)
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
103
220
|
}
|
|
104
221
|
else {
|
|
105
222
|
// General update logic
|
|
106
|
-
if (
|
|
223
|
+
if (dbType === 'sql') {
|
|
107
224
|
if (processedFilter._id) {
|
|
108
225
|
processedFilter.id = processedFilter._id;
|
|
109
226
|
delete processedFilter._id;
|
|
@@ -112,7 +229,7 @@ class Model {
|
|
|
112
229
|
if (result)
|
|
113
230
|
return result;
|
|
114
231
|
}
|
|
115
|
-
else if (
|
|
232
|
+
else if (dbType === 'nosql') {
|
|
116
233
|
const result = await nosql_1.nosqlAdapter.update(connection, this.tableName, processedFilter, data);
|
|
117
234
|
if (result)
|
|
118
235
|
return result;
|
|
@@ -125,17 +242,31 @@ class Model {
|
|
|
125
242
|
let deletedCount = 0;
|
|
126
243
|
const activeConnectionIds = this.connectionIds.filter(id => (0, connectionManager_1.getConnectedIds)().includes(id));
|
|
127
244
|
for (const connectionId of activeConnectionIds) {
|
|
128
|
-
const
|
|
245
|
+
const clientEntry = (0, connectionManager_1.getConnection)(connectionId);
|
|
246
|
+
const connection = clientEntry.client;
|
|
247
|
+
const dbType = clientEntry.type;
|
|
129
248
|
let processedFilter = { ...filter };
|
|
130
|
-
if (
|
|
249
|
+
if (dbType === 'sql') {
|
|
131
250
|
if (processedFilter._id) {
|
|
132
251
|
processedFilter.id = processedFilter._id;
|
|
133
252
|
delete processedFilter._id;
|
|
134
253
|
}
|
|
254
|
+
console.log(`SQL: Deleting from ${this.tableName} with filter:`, processedFilter);
|
|
135
255
|
const count = await sql_1.sqlAdapter.delete(connection, this.tableName, processedFilter);
|
|
256
|
+
console.log(`SQL: Deleted ${count} rows from ${this.tableName}`);
|
|
136
257
|
deletedCount += count;
|
|
137
258
|
}
|
|
138
|
-
else if (
|
|
259
|
+
else if (dbType === 'nosql') {
|
|
260
|
+
if (processedFilter._id && typeof processedFilter._id === 'string') {
|
|
261
|
+
try {
|
|
262
|
+
processedFilter._id = new mongodb_1.ObjectId(processedFilter._id);
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
// If it's not a valid ObjectId string, it might be a UUID from a SQL project.
|
|
266
|
+
// In this case, this NoSQL connection won't find it, so we can just continue.
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
139
270
|
const count = await nosql_1.nosqlAdapter.delete(connection, this.tableName, processedFilter);
|
|
140
271
|
deletedCount += count;
|
|
141
272
|
}
|
package/dist/services/auth.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { StreamByConfig, AuthContext } from '../types';
|
|
3
|
-
export declare function resolveAuth(config: StreamByConfig, req: Request): Promise<AuthContext>;
|
|
1
|
+
import { AuthContext } from '../types';
|
|
4
2
|
export declare function checkRole(auth: AuthContext, required: 'viewer' | 'editor' | 'admin'): void;
|
package/dist/services/auth.js
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveAuth = resolveAuth;
|
|
4
3
|
exports.checkRole = checkRole;
|
|
5
|
-
async function resolveAuth(config, req) {
|
|
6
|
-
const auth = await config.authProvider(req);
|
|
7
|
-
if (!auth ||
|
|
8
|
-
!auth.userId ||
|
|
9
|
-
!auth.role) {
|
|
10
|
-
throw new Error('Invalid or missing authentication context');
|
|
11
|
-
}
|
|
12
|
-
return auth;
|
|
13
|
-
}
|
|
14
4
|
function checkRole(auth, required) {
|
|
15
5
|
const roles = ['viewer', 'editor', 'admin'];
|
|
16
6
|
const userIdx = roles.indexOf(auth.role);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -61,10 +61,9 @@ export interface StreamByConfig {
|
|
|
61
61
|
adapter?: StorageAdapter;
|
|
62
62
|
}
|
|
63
63
|
export interface AuthContext {
|
|
64
|
-
role: 'viewer' | 'editor' | 'admin';
|
|
65
64
|
userId: string;
|
|
66
65
|
username: string;
|
|
67
|
-
|
|
66
|
+
role: 'viewer' | 'editor' | 'admin';
|
|
68
67
|
}
|
|
69
68
|
export type AuthProvider = (req: Request) => Promise<AuthContext>;
|
|
70
69
|
export interface Export {
|
package/package.json
CHANGED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { MongoClient, Document } from 'mongodb';
|
|
2
|
-
export declare const nosqlAdapter: {
|
|
3
|
-
find: <T extends Document>(connection: MongoClient, tableName: string, filter: any) => Promise<T[]>;
|
|
4
|
-
findOne: <T extends Document>(connection: MongoClient, tableName: string, filter: any) => Promise<T | null>;
|
|
5
|
-
create: <T extends Document>(connection: MongoClient, tableName: string, data: T) => Promise<T>;
|
|
6
|
-
update: <T extends Document>(connection: MongoClient, tableName: string, filter: any, data: Partial<T>) => Promise<number>;
|
|
7
|
-
delete: <T extends Document>(connection: MongoClient, tableName: string, filter: any) => Promise<number>;
|
|
8
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.nosqlAdapter = void 0;
|
|
4
|
-
exports.nosqlAdapter = {
|
|
5
|
-
find: async (connection, tableName, filter) => {
|
|
6
|
-
const db = connection.db();
|
|
7
|
-
const result = await db.collection(tableName).find(filter).toArray();
|
|
8
|
-
return result;
|
|
9
|
-
},
|
|
10
|
-
findOne: async (connection, tableName, filter) => {
|
|
11
|
-
const db = connection.db();
|
|
12
|
-
const result = await db.collection(tableName).findOne(filter);
|
|
13
|
-
return result;
|
|
14
|
-
},
|
|
15
|
-
create: async (connection, tableName, data) => {
|
|
16
|
-
const db = connection.db();
|
|
17
|
-
const result = await db.collection(tableName).insertOne(data);
|
|
18
|
-
return { ...data, _id: result.insertedId };
|
|
19
|
-
},
|
|
20
|
-
update: async (connection, tableName, filter, data) => {
|
|
21
|
-
const db = connection.db();
|
|
22
|
-
const result = await db.collection(tableName).updateMany(filter, { $set: data });
|
|
23
|
-
return result.modifiedCount;
|
|
24
|
-
},
|
|
25
|
-
delete: async (connection, tableName, filter) => {
|
|
26
|
-
const db = connection.db();
|
|
27
|
-
const result = await db.collection(tableName).deleteMany(filter);
|
|
28
|
-
return result.deletedCount;
|
|
29
|
-
},
|
|
30
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Pool } from 'pg';
|
|
2
|
-
export declare const sqlAdapter: {
|
|
3
|
-
find: <T>(connection: Pool, tableName: string, filter: any) => Promise<T[]>;
|
|
4
|
-
findOne: <T>(connection: Pool, tableName: string, filter: any) => Promise<T | null>;
|
|
5
|
-
create: <T>(connection: Pool, tableName: string, data: T) => Promise<T>;
|
|
6
|
-
update: <T>(connection: Pool, tableName: string, filter: any, data: Partial<T>) => Promise<number>;
|
|
7
|
-
delete: (connection: Pool, tableName: string, filter: any) => Promise<number>;
|
|
8
|
-
};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sqlAdapter = void 0;
|
|
4
|
-
exports.sqlAdapter = {
|
|
5
|
-
find: async (connection, tableName, filter) => {
|
|
6
|
-
const keys = Object.keys(filter);
|
|
7
|
-
const values = Object.values(filter);
|
|
8
|
-
const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
|
|
9
|
-
const result = await connection.query(`SELECT * FROM "${tableName}" WHERE ${where}`, values);
|
|
10
|
-
return result.rows;
|
|
11
|
-
},
|
|
12
|
-
findOne: async (connection, tableName, filter) => {
|
|
13
|
-
const keys = Object.keys(filter);
|
|
14
|
-
const values = Object.values(filter);
|
|
15
|
-
const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
|
|
16
|
-
const result = await connection.query(`SELECT * FROM "${tableName}" WHERE ${where} LIMIT 1`, values);
|
|
17
|
-
return result.rows[0] || null;
|
|
18
|
-
},
|
|
19
|
-
create: async (connection, tableName, data) => {
|
|
20
|
-
const keys = Object.keys(data).join(', ');
|
|
21
|
-
const values = Object.values(data);
|
|
22
|
-
const placeholders = values.map((_, i) => `$${i + 1}`).join(', ');
|
|
23
|
-
const result = await connection.query(`INSERT INTO "${tableName}" (${keys}) VALUES (${placeholders}) RETURNING *`, values);
|
|
24
|
-
return result.rows[0];
|
|
25
|
-
},
|
|
26
|
-
update: async (connection, tableName, filter, data) => {
|
|
27
|
-
const dataKeys = Object.keys(data);
|
|
28
|
-
const filterKeys = Object.keys(filter);
|
|
29
|
-
const values = [...Object.values(data), ...Object.values(filter)];
|
|
30
|
-
const setClause = dataKeys.map((key, i) => `"${key}" = $${i + 1}`).join(', ');
|
|
31
|
-
const whereClause = filterKeys.map((key, i) => `"${key}" = $${dataKeys.length + i + 1}`).join(' AND ');
|
|
32
|
-
const result = await connection.query(`UPDATE "${tableName}" SET ${setClause} WHERE ${whereClause}`, values);
|
|
33
|
-
return result.rowCount || 0;
|
|
34
|
-
},
|
|
35
|
-
delete: async (connection, tableName, filter) => {
|
|
36
|
-
const keys = Object.keys(filter);
|
|
37
|
-
const values = Object.values(filter);
|
|
38
|
-
const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
|
|
39
|
-
const result = await connection.query(`DELETE FROM "${tableName}" WHERE ${where}`, values);
|
|
40
|
-
return result.rowCount || 0;
|
|
41
|
-
},
|
|
42
|
-
};
|
package/dist/adapters/gcs.d.ts
DELETED
|
File without changes
|
package/dist/adapters/gcs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/adapters/local.d.ts
DELETED
|
File without changes
|
package/dist/adapters/local.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/adapters/s3.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/adapters/s3.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
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;
|
package/dist/database/Model.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/database/Model.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
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;
|
|
@@ -1,5 +0,0 @@
|
|
|
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;
|