@friggframework/core 2.0.0--canary.490.dfd8df5.0 → 2.0.0--canary.490.1d4d68c.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/credential/repositories/credential-repository-factory.js +11 -13
- package/credential/repositories/{credential-repository-documentdb.js → credential-repository-mongodb-native.js} +4 -4
- package/database/repositories/health-check-repository-factory.js +10 -33
- package/database/repositories/{health-check-repository-documentdb.js → health-check-repository-mongodb-native.js} +4 -4
- package/integrations/repositories/integration-mapping-repository-factory.js +5 -30
- package/integrations/repositories/{integration-mapping-repository-documentdb.js → integration-mapping-repository-mongodb-native.js} +4 -4
- package/integrations/repositories/integration-repository-factory.js +11 -12
- package/integrations/repositories/{integration-repository-documentdb.js → integration-repository-mongodb-native.js} +12 -4
- package/integrations/repositories/process-repository-factory.js +5 -30
- package/integrations/repositories/{process-repository-documentdb.js → process-repository-mongodb-native.js} +4 -4
- package/modules/repositories/module-repository-factory.js +5 -11
- package/modules/repositories/{module-repository-documentdb.js → module-repository-mongodb-native.js} +4 -4
- package/package.json +5 -5
- package/syncs/repositories/sync-repository-factory.js +5 -11
- package/syncs/repositories/{sync-repository-documentdb.js → sync-repository-mongodb-native.js} +4 -4
- package/token/repositories/token-repository-factory.js +5 -11
- package/token/repositories/{token-repository-documentdb.js → token-repository-mongodb-native.js} +4 -4
- package/user/repositories/user-repository-factory.js +7 -32
- package/user/repositories/{user-repository-documentdb.js → user-repository-mongodb-native.js} +4 -4
- package/websocket/repositories/websocket-connection-repository-factory.js +5 -11
- package/websocket/repositories/{websocket-connection-repository-documentdb.js → websocket-connection-repository-mongodb-native.js} +4 -4
- package/credential/repositories/credential-repository-mongo.js +0 -311
- package/integrations/repositories/integration-mapping-repository-mongo.js +0 -165
- package/integrations/repositories/integration-repository-mongo.js +0 -307
- package/integrations/repositories/process-repository-mongo.js +0 -194
- package/modules/repositories/module-repository-mongo.js +0 -381
- package/syncs/repositories/sync-repository-mongo.js +0 -243
- package/token/repositories/token-repository-mongo.js +0 -216
- package/user/repositories/user-repository-mongo.js +0 -295
- package/websocket/repositories/websocket-connection-repository-mongo.js +0 -160
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const { ModuleRepositoryInterface } = require('./module-repository-interface');
|
|
3
|
-
const { BaseRepositoryMongoDB } = require('../../database/repositories/base-repository-mongodb');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* MongoDB Module Repository Adapter
|
|
7
|
-
* Handles Entity model operations for external service entities with MongoDB
|
|
8
|
-
*
|
|
9
|
-
* MongoDB-specific characteristics:
|
|
10
|
-
* - Uses String IDs (ObjectId)
|
|
11
|
-
* - No ID conversion needed (IDs are already strings)
|
|
12
|
-
*
|
|
13
|
-
* Prisma Migration Notes:
|
|
14
|
-
* - Mongoose discriminator (__t) → moduleName field (module type: salesforce, hubspot, etc.)
|
|
15
|
-
*/
|
|
16
|
-
class ModuleRepositoryMongo extends ModuleRepositoryInterface {
|
|
17
|
-
constructor() {
|
|
18
|
-
super();
|
|
19
|
-
|
|
20
|
-
// Use MongoDB base repository for DocumentDB compatibility
|
|
21
|
-
const mongoBase = new BaseRepositoryMongoDB({ prismaClient: prisma });
|
|
22
|
-
this.prisma = mongoBase.prisma;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Convert any value to string (handles null/undefined)
|
|
27
|
-
* @private
|
|
28
|
-
* @param {*} value - Value to convert
|
|
29
|
-
* @returns {string|null|undefined} String value or null/undefined
|
|
30
|
-
*/
|
|
31
|
-
_toString(value) {
|
|
32
|
-
if (value === null || value === undefined) return value;
|
|
33
|
-
return String(value);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Fetch credential by ID separately to ensure encryption extension processes it
|
|
38
|
-
* This fixes the bug where credentials fetched via include bypass decryption
|
|
39
|
-
* @private
|
|
40
|
-
* @param {string|null|undefined} credentialId - Credential ID
|
|
41
|
-
* @returns {Promise<Object|null>} Decrypted credential or null
|
|
42
|
-
*/
|
|
43
|
-
async _fetchCredential(credentialId) {
|
|
44
|
-
if (!credentialId) return null;
|
|
45
|
-
|
|
46
|
-
const credential = await this.prisma.credential.findUnique({
|
|
47
|
-
where: { id: credentialId },
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
return credential;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Fetch multiple credentials in bulk separately to ensure decryption
|
|
55
|
-
* More efficient than fetching one-by-one for arrays of entities
|
|
56
|
-
* @private
|
|
57
|
-
* @param {Array<string>} credentialIds - Array of credential IDs
|
|
58
|
-
* @returns {Promise<Map<string, Object>>} Map of credentialId -> credential object
|
|
59
|
-
*/
|
|
60
|
-
async _fetchCredentialsBulk(credentialIds) {
|
|
61
|
-
if (!credentialIds || credentialIds.length === 0) {
|
|
62
|
-
return new Map();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const validIds = credentialIds.filter(id => id !== null && id !== undefined);
|
|
66
|
-
|
|
67
|
-
if (validIds.length === 0) {
|
|
68
|
-
return new Map();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const credentials = await this.prisma.credential.findMany({
|
|
72
|
-
where: { id: { in: validIds } },
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const credentialMap = new Map();
|
|
76
|
-
for (const credential of credentials) {
|
|
77
|
-
credentialMap.set(credential.id, credential);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return credentialMap;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Find entity by ID with credential
|
|
85
|
-
* Replaces: Entity.findById(entityId).populate('credential')
|
|
86
|
-
*
|
|
87
|
-
* @param {string} entityId - Entity ID
|
|
88
|
-
* @returns {Promise<Object>} Entity object with string IDs
|
|
89
|
-
* @throws {Error} If entity not found
|
|
90
|
-
*/
|
|
91
|
-
async findEntityById(entityId) {
|
|
92
|
-
const entity = await this.prisma.entity.findUnique({
|
|
93
|
-
where: { id: entityId },
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
if (!entity) {
|
|
97
|
-
throw new Error(`Entity ${entityId} not found`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const credential = await this._fetchCredential(entity.credentialId);
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
id: entity.id,
|
|
104
|
-
accountId: entity.accountId,
|
|
105
|
-
credential,
|
|
106
|
-
userId: entity.userId,
|
|
107
|
-
name: entity.name,
|
|
108
|
-
externalId: entity.externalId,
|
|
109
|
-
moduleName: entity.moduleName,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Find all entities for a user
|
|
115
|
-
* Replaces: Entity.find({ user: userId }).populate('credential')
|
|
116
|
-
*
|
|
117
|
-
* @param {string} userId - User ID
|
|
118
|
-
* @returns {Promise<Array>} Array of entity objects with string IDs
|
|
119
|
-
*/
|
|
120
|
-
async findEntitiesByUserId(userId) {
|
|
121
|
-
const entities = await this.prisma.entity.findMany({
|
|
122
|
-
where: { userId },
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
|
|
126
|
-
const credentialMap = await this._fetchCredentialsBulk(credentialIds);
|
|
127
|
-
|
|
128
|
-
return entities.map((e) => ({
|
|
129
|
-
id: e.id,
|
|
130
|
-
accountId: e.accountId,
|
|
131
|
-
credential: credentialMap.get(e.credentialId) || null,
|
|
132
|
-
userId: e.userId,
|
|
133
|
-
name: e.name,
|
|
134
|
-
externalId: e.externalId,
|
|
135
|
-
moduleName: e.moduleName,
|
|
136
|
-
}));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Find entities by array of IDs
|
|
141
|
-
* Replaces: Entity.find({ _id: { $in: entitiesIds } }).populate('credential')
|
|
142
|
-
*
|
|
143
|
-
* @param {Array<string>} entitiesIds - Array of entity IDs
|
|
144
|
-
* @returns {Promise<Array>} Array of entity objects with string IDs
|
|
145
|
-
*/
|
|
146
|
-
async findEntitiesByIds(entitiesIds) {
|
|
147
|
-
const entities = await this.prisma.entity.findMany({
|
|
148
|
-
where: { id: { in: entitiesIds } },
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
|
|
152
|
-
const credentialMap = await this._fetchCredentialsBulk(credentialIds);
|
|
153
|
-
|
|
154
|
-
return entities.map((e) => ({
|
|
155
|
-
id: e.id,
|
|
156
|
-
accountId: e.accountId,
|
|
157
|
-
credential: credentialMap.get(e.credentialId) || null,
|
|
158
|
-
userId: e.userId,
|
|
159
|
-
name: e.name,
|
|
160
|
-
externalId: e.externalId,
|
|
161
|
-
moduleName: e.moduleName,
|
|
162
|
-
}));
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Find entities by user ID and module name
|
|
167
|
-
* Replaces: Entity.find({ user: userId, moduleName: moduleName }).populate('credential')
|
|
168
|
-
*
|
|
169
|
-
* @param {string} userId - User ID
|
|
170
|
-
* @param {string} moduleName - Module name
|
|
171
|
-
* @returns {Promise<Array>} Array of entity objects with string IDs
|
|
172
|
-
*/
|
|
173
|
-
async findEntitiesByUserIdAndModuleName(userId, moduleName) {
|
|
174
|
-
const entities = await this.prisma.entity.findMany({
|
|
175
|
-
where: {
|
|
176
|
-
userId,
|
|
177
|
-
moduleName,
|
|
178
|
-
},
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
|
|
182
|
-
const credentialMap = await this._fetchCredentialsBulk(credentialIds);
|
|
183
|
-
|
|
184
|
-
return entities.map((e) => ({
|
|
185
|
-
id: e.id,
|
|
186
|
-
accountId: e.accountId,
|
|
187
|
-
credential: credentialMap.get(e.credentialId) || null,
|
|
188
|
-
userId: e.userId,
|
|
189
|
-
name: e.name,
|
|
190
|
-
externalId: e.externalId,
|
|
191
|
-
moduleName: e.moduleName,
|
|
192
|
-
}));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Remove credential reference from entity
|
|
197
|
-
* Replaces: Entity.updateOne({ _id: entityId }, { $unset: { credential: "" } })
|
|
198
|
-
*
|
|
199
|
-
* @param {string} entityId - Entity ID
|
|
200
|
-
* @returns {Promise<boolean>} Success indicator
|
|
201
|
-
*/
|
|
202
|
-
async unsetCredential(entityId) {
|
|
203
|
-
await this.prisma.entity.update({
|
|
204
|
-
where: { id: entityId },
|
|
205
|
-
data: { credentialId: null },
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
return true;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Find entity by filter criteria
|
|
213
|
-
* Replaces: Entity.findOne(filter).populate('credential')
|
|
214
|
-
*
|
|
215
|
-
* @param {Object} filter - Filter criteria
|
|
216
|
-
* @returns {Promise<Object|null>} Entity object with string IDs or null
|
|
217
|
-
*/
|
|
218
|
-
async findEntity(filter) {
|
|
219
|
-
const where = this._convertFilterToWhere(filter);
|
|
220
|
-
const entity = await this.prisma.entity.findFirst({
|
|
221
|
-
where,
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
if (!entity) {
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const credential = await this._fetchCredential(entity.credentialId);
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
id: entity.id,
|
|
232
|
-
accountId: entity.accountId,
|
|
233
|
-
credential,
|
|
234
|
-
userId: entity.userId,
|
|
235
|
-
name: entity.name,
|
|
236
|
-
externalId: entity.externalId,
|
|
237
|
-
moduleName: entity.moduleName,
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Create a new entity
|
|
243
|
-
* Replaces: Entity.create(entityData)
|
|
244
|
-
*
|
|
245
|
-
* @param {Object} entityData - Entity data
|
|
246
|
-
* @returns {Promise<Object>} Created entity object with string IDs
|
|
247
|
-
*/
|
|
248
|
-
async createEntity(entityData) {
|
|
249
|
-
const data = {
|
|
250
|
-
userId: entityData.user || entityData.userId,
|
|
251
|
-
credentialId: entityData.credential || entityData.credentialId,
|
|
252
|
-
name: entityData.name,
|
|
253
|
-
moduleName: entityData.moduleName,
|
|
254
|
-
externalId: entityData.externalId,
|
|
255
|
-
accountId: entityData.accountId,
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
const entity = await this.prisma.entity.create({
|
|
259
|
-
data,
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
const credential = await this._fetchCredential(entity.credentialId);
|
|
263
|
-
|
|
264
|
-
return {
|
|
265
|
-
id: entity.id,
|
|
266
|
-
accountId: entity.accountId,
|
|
267
|
-
credential,
|
|
268
|
-
userId: entity.userId,
|
|
269
|
-
name: entity.name,
|
|
270
|
-
externalId: entity.externalId,
|
|
271
|
-
moduleName: entity.moduleName,
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Update an entity by ID
|
|
277
|
-
* Replaces: Entity.findByIdAndUpdate(entityId, updates, { new: true })
|
|
278
|
-
*
|
|
279
|
-
* @param {string} entityId - Entity ID to update
|
|
280
|
-
* @param {Object} updates - Fields to update
|
|
281
|
-
* @returns {Promise<Object|null>} Updated entity object with string IDs or null if not found
|
|
282
|
-
*/
|
|
283
|
-
async updateEntity(entityId, updates) {
|
|
284
|
-
const data = {};
|
|
285
|
-
if (updates.user !== undefined) data.userId = updates.user;
|
|
286
|
-
if (updates.userId !== undefined) data.userId = updates.userId;
|
|
287
|
-
if (updates.credential !== undefined)
|
|
288
|
-
data.credentialId = updates.credential;
|
|
289
|
-
if (updates.credentialId !== undefined)
|
|
290
|
-
data.credentialId = updates.credentialId;
|
|
291
|
-
if (updates.name !== undefined) data.name = updates.name;
|
|
292
|
-
if (updates.moduleName !== undefined)
|
|
293
|
-
data.moduleName = updates.moduleName;
|
|
294
|
-
if (updates.externalId !== undefined)
|
|
295
|
-
data.externalId = updates.externalId;
|
|
296
|
-
if (updates.accountId !== undefined) data.accountId = updates.accountId;
|
|
297
|
-
|
|
298
|
-
try {
|
|
299
|
-
const entity = await this.prisma.entity.update({
|
|
300
|
-
where: { id: entityId },
|
|
301
|
-
data,
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
const credential = await this._fetchCredential(entity.credentialId);
|
|
305
|
-
|
|
306
|
-
return {
|
|
307
|
-
id: entity.id,
|
|
308
|
-
accountId: entity.accountId,
|
|
309
|
-
credential,
|
|
310
|
-
userId: entity.userId,
|
|
311
|
-
name: entity.name,
|
|
312
|
-
externalId: entity.externalId,
|
|
313
|
-
moduleName: entity.moduleName,
|
|
314
|
-
};
|
|
315
|
-
} catch (error) {
|
|
316
|
-
if (error.code === 'P2025') {
|
|
317
|
-
return null;
|
|
318
|
-
}
|
|
319
|
-
throw error;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Delete an entity by ID
|
|
325
|
-
* Replaces: Entity.deleteOne({ _id: entityId })
|
|
326
|
-
*
|
|
327
|
-
* @param {string} entityId - Entity ID to delete
|
|
328
|
-
* @returns {Promise<boolean>} True if deleted successfully
|
|
329
|
-
*/
|
|
330
|
-
async deleteEntity(entityId) {
|
|
331
|
-
try {
|
|
332
|
-
await this.prisma.entity.delete({
|
|
333
|
-
where: { id: entityId },
|
|
334
|
-
});
|
|
335
|
-
return true;
|
|
336
|
-
} catch (error) {
|
|
337
|
-
if (error.code === 'P2025') {
|
|
338
|
-
// Record not found
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
throw error;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Convert Mongoose-style filter to Prisma where clause
|
|
347
|
-
* @private
|
|
348
|
-
* @param {Object} filter - Mongoose filter
|
|
349
|
-
* @returns {Object} Prisma where clause
|
|
350
|
-
*/
|
|
351
|
-
_convertFilterToWhere(filter) {
|
|
352
|
-
const where = {};
|
|
353
|
-
|
|
354
|
-
// Handle _id field (Mongoose uses _id, Prisma uses id)
|
|
355
|
-
if (filter._id) {
|
|
356
|
-
where.id = filter._id;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// Handle user field (Mongoose uses user, Prisma uses userId)
|
|
360
|
-
if (filter.user) {
|
|
361
|
-
where.userId = filter.user;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// Handle credential field (Mongoose uses credential, Prisma uses credentialId)
|
|
365
|
-
if (filter.credential) {
|
|
366
|
-
where.credentialId = filter.credential;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Copy other fields directly
|
|
370
|
-
if (filter.id) where.id = filter.id;
|
|
371
|
-
if (filter.userId) where.userId = filter.userId;
|
|
372
|
-
if (filter.credentialId) where.credentialId = filter.credentialId;
|
|
373
|
-
if (filter.name) where.name = filter.name;
|
|
374
|
-
if (filter.moduleName) where.moduleName = filter.moduleName;
|
|
375
|
-
if (filter.externalId) where.externalId = this._toString(filter.externalId);
|
|
376
|
-
|
|
377
|
-
return where;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
module.exports = { ModuleRepositoryMongo };
|
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const { SyncRepositoryInterface } = require('./sync-repository-interface');
|
|
3
|
-
const { BaseRepositoryMongoDB } = require('../../database/repositories/base-repository-mongodb');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* MongoDB Sync Repository Adapter
|
|
7
|
-
* Handles sync persistence using Prisma with MongoDB
|
|
8
|
-
*
|
|
9
|
-
* MongoDB-specific characteristics:
|
|
10
|
-
* - Uses scalar fields for entity relations (entityIds)
|
|
11
|
-
* - IDs are strings with @db.ObjectId
|
|
12
|
-
* - Arrays used for many-to-many relationships
|
|
13
|
-
*
|
|
14
|
-
* Migration from Mongoose:
|
|
15
|
-
* - Mongoose static methods → Repository instance methods
|
|
16
|
-
* - Mongoose populate() → Prisma include
|
|
17
|
-
* - Nested arrays → Separate DataIdentifier model
|
|
18
|
-
*/
|
|
19
|
-
class SyncRepositoryMongo extends SyncRepositoryInterface {
|
|
20
|
-
constructor() {
|
|
21
|
-
super();
|
|
22
|
-
|
|
23
|
-
// Use MongoDB base repository for DocumentDB compatibility
|
|
24
|
-
const mongoBase = new BaseRepositoryMongoDB({ prismaClient: prisma });
|
|
25
|
-
this.prisma = mongoBase.prisma;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get a sync object by name, data identifier, and entity
|
|
30
|
-
* Replaces: Sync.getSyncObject(name, dataIdentifier, entity)
|
|
31
|
-
*
|
|
32
|
-
* @param {string} name - The sync object name
|
|
33
|
-
* @param {Object} dataIdentifier - The data identifier object
|
|
34
|
-
* @param {string} entity - The entity ID (MongoDB ObjectId)
|
|
35
|
-
* @returns {Promise<Object|null>} The sync object or null
|
|
36
|
-
*/
|
|
37
|
-
async getSyncObject(name, dataIdentifier, entity) {
|
|
38
|
-
const syncList = await this.prisma.sync.findMany({
|
|
39
|
-
where: {
|
|
40
|
-
name,
|
|
41
|
-
dataIdentifiers: {
|
|
42
|
-
some: {
|
|
43
|
-
idData: dataIdentifier,
|
|
44
|
-
entityId: entity,
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
include: {
|
|
49
|
-
entities: true,
|
|
50
|
-
dataIdentifiers: {
|
|
51
|
-
include: {
|
|
52
|
-
entity: true,
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
if (syncList.length === 1) {
|
|
59
|
-
return syncList[0];
|
|
60
|
-
} else if (syncList.length === 0) {
|
|
61
|
-
return null;
|
|
62
|
-
} else {
|
|
63
|
-
throw new Error(
|
|
64
|
-
`There are multiple sync objects with the name ${name}, for entities [${syncList[0].entities}] [${syncList[1].entities}]`
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Create or update a sync object
|
|
71
|
-
* Replaces: Sync.upsert(filter, syncData)
|
|
72
|
-
*
|
|
73
|
-
* @param {Object} filter - Filter criteria for finding existing sync
|
|
74
|
-
* @param {Object} syncData - Sync data to create/update
|
|
75
|
-
* @returns {Promise<Object>} The created or updated sync object
|
|
76
|
-
*/
|
|
77
|
-
async upsertSync(filter, syncData) {
|
|
78
|
-
// Find existing sync
|
|
79
|
-
const where = this._convertFilterToWhere(filter);
|
|
80
|
-
const existing = await this.prisma.sync.findFirst({ where });
|
|
81
|
-
|
|
82
|
-
if (existing) {
|
|
83
|
-
// Update existing
|
|
84
|
-
return await this.prisma.sync.update({
|
|
85
|
-
where: { id: existing.id },
|
|
86
|
-
data: syncData,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Create new
|
|
91
|
-
return await this.prisma.sync.create({
|
|
92
|
-
data: syncData,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Update a sync object by ID
|
|
98
|
-
* Replaces: Sync.update({ _id: id }, updates)
|
|
99
|
-
*
|
|
100
|
-
* @param {string} id - The sync object ID
|
|
101
|
-
* @param {Object} updates - Updates to apply
|
|
102
|
-
* @returns {Promise<Object>} The updated sync object
|
|
103
|
-
*/
|
|
104
|
-
async updateSync(id, updates) {
|
|
105
|
-
return await this.prisma.sync.update({
|
|
106
|
-
where: { id },
|
|
107
|
-
data: updates,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Add a data identifier to a sync object
|
|
113
|
-
* Replaces: Sync.addDataIdentifier(syncId, dataIdentifier)
|
|
114
|
-
*
|
|
115
|
-
* @param {string} syncId - The sync object ID
|
|
116
|
-
* @param {Object} dataIdentifier - The data identifier to add
|
|
117
|
-
* @returns {Promise<Object>} The updated sync object
|
|
118
|
-
*/
|
|
119
|
-
async addDataIdentifier(syncId, dataIdentifier) {
|
|
120
|
-
// In Prisma, we create a new DataIdentifier record linked to the Sync
|
|
121
|
-
await this.prisma.dataIdentifier.create({
|
|
122
|
-
data: {
|
|
123
|
-
syncId,
|
|
124
|
-
entityId: dataIdentifier.entity,
|
|
125
|
-
idData: dataIdentifier.id,
|
|
126
|
-
hash: dataIdentifier.hash,
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
// Return updated sync object
|
|
131
|
-
return await this.prisma.sync.findUnique({
|
|
132
|
-
where: { id: syncId },
|
|
133
|
-
include: {
|
|
134
|
-
dataIdentifiers: true,
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Get entity object ID for entity ID from sync object
|
|
141
|
-
* Replaces: Sync.getEntityObjIdForEntityIdFromObject(syncObj, entityId)
|
|
142
|
-
*
|
|
143
|
-
* This is a pure helper method (no database access)
|
|
144
|
-
*
|
|
145
|
-
* @param {Object} syncObj - The sync object
|
|
146
|
-
* @param {string} entityId - The entity ID
|
|
147
|
-
* @returns {Object} The entity object ID
|
|
148
|
-
*/
|
|
149
|
-
getEntityObjIdForEntityIdFromObject(syncObj, entityId) {
|
|
150
|
-
if (!syncObj.dataIdentifiers) {
|
|
151
|
-
throw new Error('Sync object must include dataIdentifiers');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
for (let dataIdentifier of syncObj.dataIdentifiers) {
|
|
155
|
-
if (dataIdentifier.entityId === entityId) {
|
|
156
|
-
return dataIdentifier.idData;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
throw new Error(
|
|
161
|
-
`Sync object ${syncObj.id} does not contain a data identifier for entity ${entityId}`
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Find sync objects by filter
|
|
167
|
-
* Replaces: Sync.find(filter)
|
|
168
|
-
*
|
|
169
|
-
* @param {Object} filter - Filter criteria
|
|
170
|
-
* @returns {Promise<Array>} Array of sync objects
|
|
171
|
-
*/
|
|
172
|
-
async findSyncs(filter) {
|
|
173
|
-
const where = this._convertFilterToWhere(filter);
|
|
174
|
-
return await this.prisma.sync.findMany({
|
|
175
|
-
where,
|
|
176
|
-
include: {
|
|
177
|
-
entities: true,
|
|
178
|
-
dataIdentifiers: {
|
|
179
|
-
include: {
|
|
180
|
-
entity: true,
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Find one sync object by filter
|
|
189
|
-
* Replaces: Sync.findOne(filter)
|
|
190
|
-
*
|
|
191
|
-
* @param {Object} filter - Filter criteria
|
|
192
|
-
* @returns {Promise<Object|null>} The sync object or null
|
|
193
|
-
*/
|
|
194
|
-
async findOneSync(filter) {
|
|
195
|
-
const where = this._convertFilterToWhere(filter);
|
|
196
|
-
return await this.prisma.sync.findFirst({
|
|
197
|
-
where,
|
|
198
|
-
include: {
|
|
199
|
-
entities: true,
|
|
200
|
-
dataIdentifiers: {
|
|
201
|
-
include: {
|
|
202
|
-
entity: true,
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Delete a sync object by ID
|
|
211
|
-
* Replaces: Sync.deleteOne({ _id: id })
|
|
212
|
-
*
|
|
213
|
-
* @param {string} id - The sync object ID
|
|
214
|
-
* @returns {Promise<Object>} The deletion result
|
|
215
|
-
*/
|
|
216
|
-
async deleteSync(id) {
|
|
217
|
-
// Prisma will cascade delete dataIdentifiers automatically
|
|
218
|
-
return await this.prisma.sync.delete({
|
|
219
|
-
where: { id },
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Convert Mongoose-style filter to Prisma where clause
|
|
225
|
-
* @private
|
|
226
|
-
* @param {Object} filter - Mongoose filter
|
|
227
|
-
* @returns {Object} Prisma where clause
|
|
228
|
-
*/
|
|
229
|
-
_convertFilterToWhere(filter) {
|
|
230
|
-
const where = {};
|
|
231
|
-
|
|
232
|
-
// Handle _id field (Mongoose uses _id, Prisma uses id)
|
|
233
|
-
if (filter._id) {
|
|
234
|
-
where.id = filter._id;
|
|
235
|
-
delete filter._id;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Copy remaining filters
|
|
239
|
-
return { ...where, ...filter };
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
module.exports = { SyncRepositoryMongo };
|