@friggframework/core 2.0.0-next.52 → 2.0.0-next.54
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/CLAUDE.md +2 -1
- package/application/commands/integration-commands.js +1 -1
- package/application/index.js +1 -1
- package/credential/repositories/credential-repository-documentdb.js +300 -0
- package/credential/repositories/credential-repository-factory.js +8 -1
- package/database/config.js +4 -4
- package/database/documentdb-encryption-service.js +330 -0
- package/database/documentdb-utils.js +136 -0
- package/database/encryption/README.md +50 -0
- package/database/encryption/documentdb-encryption-service.md +3270 -0
- package/database/encryption/encryption-schema-registry.js +46 -0
- package/database/prisma.js +7 -47
- package/database/repositories/health-check-repository-documentdb.js +134 -0
- package/database/repositories/health-check-repository-factory.js +6 -1
- package/database/repositories/health-check-repository-interface.js +29 -34
- package/database/repositories/health-check-repository-mongodb.js +1 -3
- package/database/use-cases/check-database-state-use-case.js +3 -3
- package/database/use-cases/run-database-migration-use-case.js +6 -4
- package/database/use-cases/trigger-database-migration-use-case.js +2 -2
- package/database/utils/mongodb-schema-init.js +5 -5
- package/database/utils/prisma-runner.js +15 -9
- package/generated/prisma-mongodb/edge.js +9 -9
- package/generated/prisma-mongodb/index-browser.js +4 -4
- package/generated/prisma-mongodb/index.d.ts +12 -7
- package/generated/prisma-mongodb/index.js +9 -9
- package/generated/prisma-mongodb/package.json +2 -2
- package/generated/prisma-mongodb/query-engine-debian-openssl-3.0.x +0 -0
- package/generated/prisma-mongodb/query-engine-rhel-openssl-3.0.x +0 -0
- package/generated/prisma-mongodb/runtime/binary.js +2 -2
- package/generated/prisma-mongodb/runtime/edge-esm.js +3 -3
- package/generated/prisma-mongodb/runtime/edge.js +3 -3
- package/generated/prisma-mongodb/runtime/library.d.ts +0 -5
- package/generated/prisma-mongodb/runtime/react-native.js +14 -14
- package/generated/prisma-mongodb/runtime/wasm-compiler-edge.js +18 -18
- package/generated/prisma-mongodb/runtime/wasm-engine-edge.js +11 -11
- package/generated/prisma-mongodb/schema.prisma +1 -3
- package/generated/prisma-mongodb/wasm.js +8 -8
- package/generated/prisma-postgresql/edge.js +9 -9
- package/generated/prisma-postgresql/index-browser.js +4 -4
- package/generated/prisma-postgresql/index.d.ts +12 -7
- package/generated/prisma-postgresql/index.js +9 -9
- package/generated/prisma-postgresql/package.json +2 -2
- package/generated/prisma-postgresql/query-engine-debian-openssl-3.0.x +0 -0
- package/generated/prisma-postgresql/query-engine-rhel-openssl-3.0.x +0 -0
- package/generated/prisma-postgresql/query_engine_bg.js +2 -2
- package/generated/prisma-postgresql/query_engine_bg.wasm +0 -0
- package/generated/prisma-postgresql/runtime/binary.js +2 -2
- package/generated/prisma-postgresql/runtime/edge-esm.js +3 -3
- package/generated/prisma-postgresql/runtime/edge.js +3 -3
- package/generated/prisma-postgresql/runtime/library.d.ts +0 -5
- package/generated/prisma-postgresql/runtime/react-native.js +14 -14
- package/generated/prisma-postgresql/runtime/wasm-compiler-edge.js +18 -18
- package/generated/prisma-postgresql/runtime/wasm-engine-edge.js +11 -11
- package/generated/prisma-postgresql/schema.prisma +1 -3
- package/generated/prisma-postgresql/wasm.js +8 -8
- package/handlers/routers/db-migration.js +2 -3
- package/handlers/routers/health.js +0 -3
- package/handlers/workers/db-migration.js +8 -8
- package/integrations/repositories/integration-mapping-repository-documentdb.js +135 -0
- package/integrations/repositories/integration-mapping-repository-factory.js +8 -1
- package/integrations/repositories/integration-repository-documentdb.js +189 -0
- package/integrations/repositories/integration-repository-factory.js +8 -1
- package/integrations/repositories/process-repository-documentdb.js +141 -0
- package/integrations/repositories/process-repository-factory.js +8 -1
- package/modules/repositories/module-repository-documentdb.js +307 -0
- package/modules/repositories/module-repository-factory.js +8 -1
- package/package.json +5 -5
- package/prisma-mongodb/schema.prisma +1 -3
- package/prisma-postgresql/migrations/20251112195422_update_user_unique_constraints/migration.sql +69 -0
- package/prisma-postgresql/schema.prisma +1 -3
- package/syncs/repositories/sync-repository-documentdb.js +240 -0
- package/syncs/repositories/sync-repository-factory.js +6 -1
- package/token/repositories/token-repository-documentdb.js +125 -0
- package/token/repositories/token-repository-factory.js +8 -1
- package/user/repositories/user-repository-documentdb.js +292 -0
- package/user/repositories/user-repository-factory.js +6 -1
- package/websocket/repositories/websocket-connection-repository-documentdb.js +119 -0
- package/websocket/repositories/websocket-connection-repository-factory.js +8 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const { prisma } = require('../../database/prisma');
|
|
2
|
+
const {
|
|
3
|
+
toObjectId,
|
|
4
|
+
fromObjectId,
|
|
5
|
+
findMany,
|
|
6
|
+
findOne,
|
|
7
|
+
insertOne,
|
|
8
|
+
updateOne,
|
|
9
|
+
deleteOne,
|
|
10
|
+
} = require('../../database/documentdb-utils');
|
|
11
|
+
const { ProcessRepositoryInterface } = require('./process-repository-interface');
|
|
12
|
+
|
|
13
|
+
class ProcessRepositoryDocumentDB extends ProcessRepositoryInterface {
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
this.prisma = prisma;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async create(processData) {
|
|
20
|
+
const now = new Date();
|
|
21
|
+
const document = {
|
|
22
|
+
userId: toObjectId(processData.userId),
|
|
23
|
+
integrationId: toObjectId(processData.integrationId),
|
|
24
|
+
name: processData.name,
|
|
25
|
+
type: processData.type,
|
|
26
|
+
state: processData.state || 'INITIALIZING',
|
|
27
|
+
context: processData.context || {},
|
|
28
|
+
results: processData.results || {},
|
|
29
|
+
childProcesses: (processData.childProcesses || []).map((id) => toObjectId(id)).filter(Boolean),
|
|
30
|
+
parentProcessId: processData.parentProcessId ? toObjectId(processData.parentProcessId) : null,
|
|
31
|
+
createdAt: now,
|
|
32
|
+
updatedAt: now,
|
|
33
|
+
};
|
|
34
|
+
const insertedId = await insertOne(this.prisma, 'Process', document);
|
|
35
|
+
const created = await findOne(this.prisma, 'Process', { _id: insertedId });
|
|
36
|
+
return this._mapProcess(created);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async findById(processId) {
|
|
40
|
+
const objectId = toObjectId(processId);
|
|
41
|
+
if (!objectId) return null;
|
|
42
|
+
const doc = await findOne(this.prisma, 'Process', { _id: objectId });
|
|
43
|
+
return doc ? this._mapProcess(doc) : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async update(processId, updates) {
|
|
47
|
+
const objectId = toObjectId(processId);
|
|
48
|
+
if (!objectId) return null;
|
|
49
|
+
const updatePayload = {};
|
|
50
|
+
if (updates.state !== undefined) updatePayload.state = updates.state;
|
|
51
|
+
if (updates.context !== undefined) updatePayload.context = updates.context;
|
|
52
|
+
if (updates.results !== undefined) updatePayload.results = updates.results;
|
|
53
|
+
if (updates.childProcesses !== undefined) {
|
|
54
|
+
updatePayload.childProcesses = (updates.childProcesses || []).map((id) => toObjectId(id)).filter(Boolean);
|
|
55
|
+
}
|
|
56
|
+
if (updates.parentProcessId !== undefined) {
|
|
57
|
+
updatePayload.parentProcessId = updates.parentProcessId ? toObjectId(updates.parentProcessId) : null;
|
|
58
|
+
}
|
|
59
|
+
updatePayload.updatedAt = new Date();
|
|
60
|
+
await updateOne(
|
|
61
|
+
this.prisma,
|
|
62
|
+
'Process',
|
|
63
|
+
{ _id: objectId },
|
|
64
|
+
{ $set: updatePayload }
|
|
65
|
+
);
|
|
66
|
+
const updated = await findOne(this.prisma, 'Process', { _id: objectId });
|
|
67
|
+
return updated ? this._mapProcess(updated) : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async findByIntegrationAndType(integrationId, type) {
|
|
71
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
72
|
+
const filter = {
|
|
73
|
+
integrationId: integrationObjectId,
|
|
74
|
+
type,
|
|
75
|
+
};
|
|
76
|
+
const docs = await findMany(this.prisma, 'Process', filter, {
|
|
77
|
+
sort: { createdAt: -1 },
|
|
78
|
+
});
|
|
79
|
+
return docs.map((doc) => this._mapProcess(doc));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async findActiveProcesses(integrationId, excludeStates = ['COMPLETED', 'ERROR']) {
|
|
83
|
+
const integrationObjectId = toObjectId(integrationId);
|
|
84
|
+
const pipeline = [
|
|
85
|
+
{
|
|
86
|
+
$match: {
|
|
87
|
+
integrationId: integrationObjectId,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
$match: {
|
|
92
|
+
state: { $nin: excludeStates },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{ $sort: { createdAt: -1 } },
|
|
96
|
+
];
|
|
97
|
+
const docs = await this.prisma.$runCommandRaw({
|
|
98
|
+
aggregate: 'Process',
|
|
99
|
+
pipeline,
|
|
100
|
+
cursor: {},
|
|
101
|
+
}).then((res) => res?.cursor?.firstBatch || []);
|
|
102
|
+
return docs.map((doc) => this._mapProcess(doc));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async findByName(name) {
|
|
106
|
+
const doc = await findOne(
|
|
107
|
+
this.prisma,
|
|
108
|
+
'Process',
|
|
109
|
+
{ name },
|
|
110
|
+
{ sort: { createdAt: -1 } }
|
|
111
|
+
);
|
|
112
|
+
return doc ? this._mapProcess(doc) : null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async deleteById(processId) {
|
|
116
|
+
const objectId = toObjectId(processId);
|
|
117
|
+
if (!objectId) return;
|
|
118
|
+
await deleteOne(this.prisma, 'Process', { _id: objectId });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_mapProcess(doc) {
|
|
122
|
+
return {
|
|
123
|
+
id: fromObjectId(doc?._id),
|
|
124
|
+
userId: fromObjectId(doc?.userId),
|
|
125
|
+
integrationId: fromObjectId(doc?.integrationId),
|
|
126
|
+
name: doc?.name ?? null,
|
|
127
|
+
type: doc?.type ?? null,
|
|
128
|
+
state: doc?.state ?? null,
|
|
129
|
+
context: doc?.context ?? {},
|
|
130
|
+
results: doc?.results ?? {},
|
|
131
|
+
childProcesses: (doc?.childProcesses || []).map((id) => fromObjectId(id)),
|
|
132
|
+
parentProcessId: doc?.parentProcessId ? fromObjectId(doc.parentProcessId) : null,
|
|
133
|
+
createdAt: doc?.createdAt ? new Date(doc.createdAt) : null,
|
|
134
|
+
updatedAt: doc?.updatedAt ? new Date(doc.updatedAt) : null,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = { ProcessRepositoryDocumentDB };
|
|
140
|
+
|
|
141
|
+
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { ProcessRepositoryMongo } = require('./process-repository-mongo');
|
|
2
2
|
const { ProcessRepositoryPostgres } = require('./process-repository-postgres');
|
|
3
|
+
const {
|
|
4
|
+
ProcessRepositoryDocumentDB,
|
|
5
|
+
} = require('./process-repository-documentdb');
|
|
3
6
|
const config = require('../../database/config');
|
|
4
7
|
|
|
5
8
|
/**
|
|
@@ -30,9 +33,12 @@ function createProcessRepository() {
|
|
|
30
33
|
case 'postgresql':
|
|
31
34
|
return new ProcessRepositoryPostgres();
|
|
32
35
|
|
|
36
|
+
case 'documentdb':
|
|
37
|
+
return new ProcessRepositoryDocumentDB();
|
|
38
|
+
|
|
33
39
|
default:
|
|
34
40
|
throw new Error(
|
|
35
|
-
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'postgresql'`
|
|
41
|
+
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'documentdb', 'postgresql'`
|
|
36
42
|
);
|
|
37
43
|
}
|
|
38
44
|
}
|
|
@@ -42,5 +48,6 @@ module.exports = {
|
|
|
42
48
|
// Export adapters for direct testing
|
|
43
49
|
ProcessRepositoryMongo,
|
|
44
50
|
ProcessRepositoryPostgres,
|
|
51
|
+
ProcessRepositoryDocumentDB,
|
|
45
52
|
};
|
|
46
53
|
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
const { prisma } = require('../../database/prisma');
|
|
2
|
+
const {
|
|
3
|
+
toObjectId,
|
|
4
|
+
fromObjectId,
|
|
5
|
+
findMany,
|
|
6
|
+
findOne,
|
|
7
|
+
insertOne,
|
|
8
|
+
updateOne,
|
|
9
|
+
deleteOne,
|
|
10
|
+
} = require('../../database/documentdb-utils');
|
|
11
|
+
const { ModuleRepositoryInterface } = require('./module-repository-interface');
|
|
12
|
+
const { DocumentDBEncryptionService } = require('../../database/documentdb-encryption-service');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Module/Entity repository for DocumentDB.
|
|
16
|
+
* Uses DocumentDBEncryptionService for credential decryption.
|
|
17
|
+
*
|
|
18
|
+
* Encrypted fields: Credential.data.*
|
|
19
|
+
*
|
|
20
|
+
* Note: This repository only reads credentials. CredentialRepository
|
|
21
|
+
* handles credential creation/updates with encryption.
|
|
22
|
+
*
|
|
23
|
+
* @see DocumentDBEncryptionService
|
|
24
|
+
* @see CredentialRepositoryDocumentDB
|
|
25
|
+
*/
|
|
26
|
+
class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
|
|
27
|
+
constructor() {
|
|
28
|
+
super();
|
|
29
|
+
this.prisma = prisma;
|
|
30
|
+
this.encryptionService = new DocumentDBEncryptionService();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async findEntityById(entityId) {
|
|
34
|
+
const objectId = toObjectId(entityId);
|
|
35
|
+
if (!objectId) {
|
|
36
|
+
throw new Error(`Entity ${entityId} not found`);
|
|
37
|
+
}
|
|
38
|
+
const doc = await findOne(this.prisma, 'Entity', { _id: objectId });
|
|
39
|
+
if (!doc) {
|
|
40
|
+
throw new Error(`Entity ${entityId} not found`);
|
|
41
|
+
}
|
|
42
|
+
const credential = await this._fetchCredential(doc.credentialId);
|
|
43
|
+
return this._mapEntity(doc, credential);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async findEntitiesByUserId(userId) {
|
|
47
|
+
const objectId = toObjectId(userId);
|
|
48
|
+
if (!objectId) {
|
|
49
|
+
throw new Error(`Invalid userId: ${userId}`);
|
|
50
|
+
}
|
|
51
|
+
const filter = { userId: objectId };
|
|
52
|
+
const docs = await findMany(this.prisma, 'Entity', filter);
|
|
53
|
+
const credentialMap = await this._fetchCredentialsBulk(docs.map((doc) => doc.credentialId));
|
|
54
|
+
return docs.map((doc) => this._mapEntity(doc, credentialMap.get(fromObjectId(doc.credentialId)) || null));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async findEntitiesByIds(entitiesIds) {
|
|
58
|
+
const ids = (entitiesIds || []).map((id) => toObjectId(id)).filter(Boolean);
|
|
59
|
+
if (ids.length === 0) return [];
|
|
60
|
+
const docs = await findMany(this.prisma, 'Entity', { _id: { $in: ids } });
|
|
61
|
+
const credentialMap = await this._fetchCredentialsBulk(docs.map((doc) => doc.credentialId));
|
|
62
|
+
return docs.map((doc) => this._mapEntity(doc, credentialMap.get(fromObjectId(doc.credentialId)) || null));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async findEntitiesByUserIdAndModuleName(userId, moduleName) {
|
|
66
|
+
const objectId = toObjectId(userId);
|
|
67
|
+
if (!objectId) {
|
|
68
|
+
throw new Error(`Invalid userId: ${userId}`);
|
|
69
|
+
}
|
|
70
|
+
const filter = {
|
|
71
|
+
userId: objectId,
|
|
72
|
+
moduleName,
|
|
73
|
+
};
|
|
74
|
+
const docs = await findMany(this.prisma, 'Entity', filter);
|
|
75
|
+
const credentialMap = await this._fetchCredentialsBulk(docs.map((doc) => doc.credentialId));
|
|
76
|
+
return docs.map((doc) => this._mapEntity(doc, credentialMap.get(fromObjectId(doc.credentialId)) || null));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async unsetCredential(entityId) {
|
|
80
|
+
const objectId = toObjectId(entityId);
|
|
81
|
+
if (!objectId) return false;
|
|
82
|
+
await updateOne(
|
|
83
|
+
this.prisma,
|
|
84
|
+
'Entity',
|
|
85
|
+
{ _id: objectId },
|
|
86
|
+
{
|
|
87
|
+
$set: {
|
|
88
|
+
credentialId: null,
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async findEntity(filter) {
|
|
96
|
+
const query = this._buildFilter(filter);
|
|
97
|
+
const doc = await findOne(this.prisma, 'Entity', query);
|
|
98
|
+
if (!doc) return null;
|
|
99
|
+
const credential = await this._fetchCredential(doc.credentialId);
|
|
100
|
+
return this._mapEntity(doc, credential);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async createEntity(entityData) {
|
|
104
|
+
const document = {
|
|
105
|
+
userId: toObjectId(entityData.user || entityData.userId),
|
|
106
|
+
credentialId: toObjectId(entityData.credential || entityData.credentialId) || null,
|
|
107
|
+
name: entityData.name ?? null,
|
|
108
|
+
moduleName: entityData.moduleName ?? null,
|
|
109
|
+
externalId: entityData.externalId ?? null,
|
|
110
|
+
accountId: entityData.accountId ?? null,
|
|
111
|
+
};
|
|
112
|
+
const insertedId = await insertOne(this.prisma, 'Entity', document);
|
|
113
|
+
const created = await findOne(this.prisma, 'Entity', { _id: insertedId });
|
|
114
|
+
const credential = await this._fetchCredential(created?.credentialId);
|
|
115
|
+
return this._mapEntity(created, credential);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async updateEntity(entityId, updates) {
|
|
119
|
+
const objectId = toObjectId(entityId);
|
|
120
|
+
if (!objectId) return null;
|
|
121
|
+
const updatePayload = {};
|
|
122
|
+
if (updates.user !== undefined || updates.userId !== undefined) {
|
|
123
|
+
const userVal = updates.user !== undefined ? updates.user : updates.userId;
|
|
124
|
+
updatePayload.userId = toObjectId(userVal) || null;
|
|
125
|
+
}
|
|
126
|
+
if (updates.credential !== undefined || updates.credentialId !== undefined) {
|
|
127
|
+
const credVal = updates.credential !== undefined ? updates.credential : updates.credentialId;
|
|
128
|
+
updatePayload.credentialId = toObjectId(credVal) || null;
|
|
129
|
+
}
|
|
130
|
+
if (updates.name !== undefined) updatePayload.name = updates.name;
|
|
131
|
+
if (updates.moduleName !== undefined) updatePayload.moduleName = updates.moduleName;
|
|
132
|
+
if (updates.externalId !== undefined) updatePayload.externalId = updates.externalId;
|
|
133
|
+
if (updates.accountId !== undefined) updatePayload.accountId = updates.accountId;
|
|
134
|
+
const result = await updateOne(
|
|
135
|
+
this.prisma,
|
|
136
|
+
'Entity',
|
|
137
|
+
{ _id: objectId },
|
|
138
|
+
{ $set: updatePayload }
|
|
139
|
+
);
|
|
140
|
+
const modified = result?.nModified ?? result?.n ?? 0;
|
|
141
|
+
if (modified === 0) return null;
|
|
142
|
+
const updated = await findOne(this.prisma, 'Entity', { _id: objectId });
|
|
143
|
+
const credential = await this._fetchCredential(updated?.credentialId);
|
|
144
|
+
return this._mapEntity(updated, credential);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async deleteEntity(entityId) {
|
|
148
|
+
const objectId = toObjectId(entityId);
|
|
149
|
+
if (!objectId) return false;
|
|
150
|
+
const result = await deleteOne(this.prisma, 'Entity', { _id: objectId });
|
|
151
|
+
const deleted = result?.n ?? 0;
|
|
152
|
+
return deleted > 0;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async _fetchCredential(credentialId) {
|
|
156
|
+
const id = fromObjectId(credentialId);
|
|
157
|
+
if (!id) return null;
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
// Convert to ObjectId for raw query
|
|
161
|
+
const objectId = toObjectId(id);
|
|
162
|
+
if (!objectId) return null;
|
|
163
|
+
|
|
164
|
+
// Use raw findOne to bypass Prisma encryption extension
|
|
165
|
+
const rawCredential = await findOne(this.prisma, 'Credential', {
|
|
166
|
+
_id: objectId
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
if (!rawCredential) return null;
|
|
170
|
+
|
|
171
|
+
// Decrypt sensitive fields using service
|
|
172
|
+
const decryptedCredential = await this.encryptionService.decryptFields('Credential', rawCredential);
|
|
173
|
+
|
|
174
|
+
// Return in same format
|
|
175
|
+
const credential = {
|
|
176
|
+
id: fromObjectId(decryptedCredential._id),
|
|
177
|
+
userId: fromObjectId(decryptedCredential.userId),
|
|
178
|
+
externalId: decryptedCredential.externalId ?? null,
|
|
179
|
+
authIsValid: decryptedCredential.authIsValid ?? null,
|
|
180
|
+
createdAt: decryptedCredential.createdAt,
|
|
181
|
+
updatedAt: decryptedCredential.updatedAt,
|
|
182
|
+
data: decryptedCredential.data
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return this._convertCredentialIds(credential);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error(`Failed to fetch/decrypt credential ${id}:`, error.message);
|
|
188
|
+
// Return null instead of throwing to allow graceful degradation
|
|
189
|
+
// This repository is read-only (doesn't create/update credentials)
|
|
190
|
+
// Entities can still be loaded even if their credential is corrupted/unreadable
|
|
191
|
+
// The entity will have null credential, which calling code must handle
|
|
192
|
+
// This is intentional behavior: prefer partial data over complete failure
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async _fetchCredentialsBulk(credentialIds) {
|
|
198
|
+
const ids = (credentialIds || [])
|
|
199
|
+
.map((value) => fromObjectId(value))
|
|
200
|
+
.filter((value) => value !== null && value !== undefined);
|
|
201
|
+
if (ids.length === 0) return new Map();
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
// Convert string IDs to ObjectIds for bulk query
|
|
205
|
+
const objectIds = ids.map(id => toObjectId(id)).filter(Boolean);
|
|
206
|
+
if (objectIds.length === 0) return new Map();
|
|
207
|
+
|
|
208
|
+
// Use raw findMany to bypass Prisma encryption extension
|
|
209
|
+
const rawCredentials = await findMany(this.prisma, 'Credential', {
|
|
210
|
+
_id: { $in: objectIds }
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Decrypt all credentials in parallel
|
|
214
|
+
const decryptionPromises = rawCredentials.map(async (rawCredential) => {
|
|
215
|
+
try {
|
|
216
|
+
// Decrypt sensitive fields using service
|
|
217
|
+
const decryptedCredential = await this.encryptionService.decryptFields('Credential', rawCredential);
|
|
218
|
+
|
|
219
|
+
// Build credential object in same format as Prisma would return
|
|
220
|
+
const credential = {
|
|
221
|
+
id: fromObjectId(decryptedCredential._id),
|
|
222
|
+
userId: fromObjectId(decryptedCredential.userId),
|
|
223
|
+
externalId: decryptedCredential.externalId ?? null,
|
|
224
|
+
authIsValid: decryptedCredential.authIsValid ?? null,
|
|
225
|
+
createdAt: decryptedCredential.createdAt,
|
|
226
|
+
updatedAt: decryptedCredential.updatedAt,
|
|
227
|
+
data: decryptedCredential.data
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
return this._convertCredentialIds(credential);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
const credId = fromObjectId(rawCredential._id);
|
|
233
|
+
console.error(`Failed to decrypt credential ${credId}:`, error.message);
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// Wait for all decryptions to complete
|
|
239
|
+
const decryptedCredentials = await Promise.all(decryptionPromises);
|
|
240
|
+
|
|
241
|
+
// Build Map from results, filtering out nulls
|
|
242
|
+
const map = new Map();
|
|
243
|
+
decryptedCredentials.forEach(credential => {
|
|
244
|
+
if (credential) {
|
|
245
|
+
map.set(credential.id, credential);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
return map;
|
|
250
|
+
} catch (error) {
|
|
251
|
+
console.error('Failed to fetch credentials bulk:', error.message);
|
|
252
|
+
return new Map();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Convert credential object IDs to strings for application layer
|
|
258
|
+
* Ensures consistent credential format across database adapters
|
|
259
|
+
* @private
|
|
260
|
+
* @param {Object|null} credential - Credential object from database
|
|
261
|
+
* @returns {Object|null} Credential with properly formatted IDs
|
|
262
|
+
*/
|
|
263
|
+
_convertCredentialIds(credential) {
|
|
264
|
+
if (!credential) return credential;
|
|
265
|
+
return {
|
|
266
|
+
...credential,
|
|
267
|
+
id: credential.id ? String(credential.id) : null,
|
|
268
|
+
userId: credential.userId ? String(credential.userId) : null,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
_buildFilter(filter) {
|
|
273
|
+
const query = {};
|
|
274
|
+
if (!filter) return query;
|
|
275
|
+
if (filter._id || filter.id) {
|
|
276
|
+
const idObj = toObjectId(filter._id || filter.id);
|
|
277
|
+
if (idObj) query._id = idObj;
|
|
278
|
+
}
|
|
279
|
+
if (filter.user || filter.userId) {
|
|
280
|
+
const userObj = toObjectId(filter.user || filter.userId);
|
|
281
|
+
if (userObj) query.userId = userObj;
|
|
282
|
+
}
|
|
283
|
+
if (filter.credential || filter.credentialId) {
|
|
284
|
+
const credObj = toObjectId(filter.credential || filter.credentialId);
|
|
285
|
+
if (credObj) query.credentialId = credObj;
|
|
286
|
+
}
|
|
287
|
+
if (filter.name) query.name = filter.name;
|
|
288
|
+
if (filter.moduleName) query.moduleName = filter.moduleName;
|
|
289
|
+
if (filter.externalId) query.externalId = filter.externalId;
|
|
290
|
+
return query;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
_mapEntity(doc, credential) {
|
|
294
|
+
return {
|
|
295
|
+
id: fromObjectId(doc?._id),
|
|
296
|
+
accountId: doc?.accountId ?? null,
|
|
297
|
+
credential,
|
|
298
|
+
userId: fromObjectId(doc?.userId),
|
|
299
|
+
name: doc?.name ?? null,
|
|
300
|
+
externalId: doc?.externalId ?? null,
|
|
301
|
+
moduleName: doc?.moduleName ?? null,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
module.exports = { ModuleRepositoryDocumentDB };
|
|
307
|
+
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { ModuleRepositoryMongo } = require('./module-repository-mongo');
|
|
2
2
|
const { ModuleRepositoryPostgres } = require('./module-repository-postgres');
|
|
3
|
+
const {
|
|
4
|
+
ModuleRepositoryDocumentDB,
|
|
5
|
+
} = require('./module-repository-documentdb');
|
|
3
6
|
const config = require('../../database/config');
|
|
4
7
|
|
|
5
8
|
/**
|
|
@@ -18,9 +21,12 @@ function createModuleRepository() {
|
|
|
18
21
|
case 'postgresql':
|
|
19
22
|
return new ModuleRepositoryPostgres();
|
|
20
23
|
|
|
24
|
+
case 'documentdb':
|
|
25
|
+
return new ModuleRepositoryDocumentDB();
|
|
26
|
+
|
|
21
27
|
default:
|
|
22
28
|
throw new Error(
|
|
23
|
-
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'postgresql'`
|
|
29
|
+
`Unsupported database type: ${dbType}. Supported values: 'mongodb', 'documentdb', 'postgresql'`
|
|
24
30
|
);
|
|
25
31
|
}
|
|
26
32
|
}
|
|
@@ -30,4 +36,5 @@ module.exports = {
|
|
|
30
36
|
// Export adapters for direct testing
|
|
31
37
|
ModuleRepositoryMongo,
|
|
32
38
|
ModuleRepositoryPostgres,
|
|
39
|
+
ModuleRepositoryDocumentDB,
|
|
33
40
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0-next.
|
|
4
|
+
"version": "2.0.0-next.54",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@friggframework/eslint-config": "2.0.0-next.
|
|
42
|
-
"@friggframework/prettier-config": "2.0.0-next.
|
|
43
|
-
"@friggframework/test": "2.0.0-next.
|
|
41
|
+
"@friggframework/eslint-config": "2.0.0-next.54",
|
|
42
|
+
"@friggframework/prettier-config": "2.0.0-next.54",
|
|
43
|
+
"@friggframework/test": "2.0.0-next.54",
|
|
44
44
|
"@prisma/client": "^6.17.0",
|
|
45
45
|
"@types/lodash": "4.17.15",
|
|
46
46
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"publishConfig": {
|
|
81
81
|
"access": "public"
|
|
82
82
|
},
|
|
83
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "d72f0af6966a5701fe2a4257139d40292972f92a"
|
|
84
84
|
}
|
package/prisma-postgresql/migrations/20251112195422_update_user_unique_constraints/migration.sql
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Warnings:
|
|
3
|
+
|
|
4
|
+
- You are about to drop the column `subType` on the `Credential` table. All the data in the column will be lost.
|
|
5
|
+
- You are about to drop the column `subType` on the `Entity` table. All the data in the column will be lost.
|
|
6
|
+
- A unique constraint covering the columns `[username,appUserId]` on the table `User` will be added. If there are existing duplicate values, this will fail.
|
|
7
|
+
|
|
8
|
+
*/
|
|
9
|
+
-- DropIndex
|
|
10
|
+
DROP INDEX "User_appOrgId_key";
|
|
11
|
+
|
|
12
|
+
-- DropIndex
|
|
13
|
+
DROP INDEX "User_email_key";
|
|
14
|
+
|
|
15
|
+
-- DropIndex
|
|
16
|
+
DROP INDEX "User_username_key";
|
|
17
|
+
|
|
18
|
+
-- AlterTable
|
|
19
|
+
ALTER TABLE "Credential" DROP COLUMN "subType";
|
|
20
|
+
|
|
21
|
+
-- AlterTable
|
|
22
|
+
ALTER TABLE "Entity" DROP COLUMN "subType";
|
|
23
|
+
|
|
24
|
+
-- CreateTable
|
|
25
|
+
CREATE TABLE "Process" (
|
|
26
|
+
"id" SERIAL NOT NULL,
|
|
27
|
+
"userId" INTEGER NOT NULL,
|
|
28
|
+
"integrationId" INTEGER NOT NULL,
|
|
29
|
+
"name" TEXT NOT NULL,
|
|
30
|
+
"type" TEXT NOT NULL,
|
|
31
|
+
"state" TEXT NOT NULL,
|
|
32
|
+
"context" JSONB NOT NULL DEFAULT '{}',
|
|
33
|
+
"results" JSONB NOT NULL DEFAULT '{}',
|
|
34
|
+
"parentProcessId" INTEGER,
|
|
35
|
+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
36
|
+
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
37
|
+
|
|
38
|
+
CONSTRAINT "Process_pkey" PRIMARY KEY ("id")
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
-- CreateIndex
|
|
42
|
+
CREATE INDEX "Process_userId_idx" ON "Process"("userId");
|
|
43
|
+
|
|
44
|
+
-- CreateIndex
|
|
45
|
+
CREATE INDEX "Process_integrationId_idx" ON "Process"("integrationId");
|
|
46
|
+
|
|
47
|
+
-- CreateIndex
|
|
48
|
+
CREATE INDEX "Process_type_idx" ON "Process"("type");
|
|
49
|
+
|
|
50
|
+
-- CreateIndex
|
|
51
|
+
CREATE INDEX "Process_state_idx" ON "Process"("state");
|
|
52
|
+
|
|
53
|
+
-- CreateIndex
|
|
54
|
+
CREATE INDEX "Process_name_idx" ON "Process"("name");
|
|
55
|
+
|
|
56
|
+
-- CreateIndex
|
|
57
|
+
CREATE INDEX "Process_parentProcessId_idx" ON "Process"("parentProcessId");
|
|
58
|
+
|
|
59
|
+
-- CreateIndex
|
|
60
|
+
CREATE UNIQUE INDEX "User_username_appUserId_key" ON "User"("username", "appUserId");
|
|
61
|
+
|
|
62
|
+
-- AddForeignKey
|
|
63
|
+
ALTER TABLE "Process" ADD CONSTRAINT "Process_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
64
|
+
|
|
65
|
+
-- AddForeignKey
|
|
66
|
+
ALTER TABLE "Process" ADD CONSTRAINT "Process_integrationId_fkey" FOREIGN KEY ("integrationId") REFERENCES "Integration"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
67
|
+
|
|
68
|
+
-- AddForeignKey
|
|
69
|
+
ALTER TABLE "Process" ADD CONSTRAINT "Process_parentProcessId_fkey" FOREIGN KEY ("parentProcessId") REFERENCES "Process"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|