@@ -16,11 +16,14 @@ import {
16
16
* with proper type handling and access control.
17
17
*/
18
18
export class DbService {
19
+ private driver : Driver ;
20
+
19
21
/**
20
22
* Creates a new instance of the DbService.
21
- * @param driver - The Neo4j driver instance
22
23
*/
23
- constructor ( private driver : Driver ) { }
24
+ constructor ( driver : Driver ) {
25
+ this . driver = driver ;
26
+ }
24
27
25
28
/**
26
29
* Executes a Cypher query with the given parameters.
@@ -174,6 +177,59 @@ export class DbService {
174
177
} ) ;
175
178
}
176
179
180
+ /**
181
+ * Finds multiple meta-envelopes by an array of IDs.
182
+ * @param ids - Array of MetaEnvelope IDs
183
+ * @returns Array of meta-envelopes with envelopes and parsed payload
184
+ */
185
+ async findMetaEnvelopesByIds <
186
+ T extends Record < string , any > = Record < string , any > ,
187
+ > ( ids : string [ ] ) : Promise < MetaEnvelopeResult < T > [ ] > {
188
+ if ( ! ids . length ) return [ ] ;
189
+
190
+ const result = await this . runQuery (
191
+ `
192
+ MATCH (m:MetaEnvelope)-[:LINKS_TO]->(e:Envelope)
193
+ WHERE m.id IN $ids
194
+ RETURN m.id AS id, m.ontology AS ontology, m.acl AS acl, collect(e) AS envelopes
195
+ ` ,
196
+ { ids } ,
197
+ ) ;
198
+
199
+ return result . records . map ( ( record ) : MetaEnvelopeResult < T > => {
200
+ const envelopes = record
201
+ . get ( "envelopes" )
202
+ . map ( ( node : any ) : Envelope < T [ keyof T ] > => {
203
+ const props = node . properties ;
204
+ return {
205
+ id : props . id ,
206
+ ontology : props . ontology ,
207
+ value : deserializeValue (
208
+ props . value ,
209
+ props . valueType ,
210
+ ) as T [ keyof T ] ,
211
+ valueType : props . valueType ,
212
+ } ;
213
+ } ) ;
214
+
215
+ const parsed = envelopes . reduce (
216
+ ( acc : T , env : Envelope < T [ keyof T ] > ) => {
217
+ ( acc as any ) [ env . ontology ] = env . value ;
218
+ return acc ;
219
+ } ,
220
+ { } as T ,
221
+ ) ;
222
+
223
+ return {
224
+ id : record . get ( "id" ) ,
225
+ ontology : record . get ( "ontology" ) ,
226
+ acl : record . get ( "acl" ) ,
227
+ envelopes,
228
+ parsed,
229
+ } ;
230
+ } ) ;
231
+ }
232
+
177
233
/**
178
234
* Finds a meta-envelope by its ID.
179
235
* @param id - The ID of the meta-envelope to find
@@ -226,20 +282,53 @@ export class DbService {
226
282
}
227
283
228
284
/**
229
- * Finds all meta-envelope IDs for a given ontology .
285
+ * Finds all meta-envelopes by ontology with their envelopes and parsed payload .
230
286
* @param ontology - The ontology to search for
231
- * @returns Array of meta-envelope IDs
287
+ * @returns Array of meta-envelopes
232
288
*/
233
- async findMetaEnvelopesByOntology ( ontology : string ) : Promise < string [ ] > {
289
+ async findMetaEnvelopesByOntology <
290
+ T extends Record < string , any > = Record < string , any > ,
291
+ > ( ontology : string ) : Promise < MetaEnvelopeResult < T > [ ] > {
234
292
const result = await this . runQuery (
235
293
`
236
- MATCH (m:MetaEnvelope { ontology: $ontology })
237
- RETURN m.id AS id
238
- ` ,
294
+ MATCH (m:MetaEnvelope { ontology: $ontology })-[:LINKS_TO]->(e:Envelope )
295
+ RETURN m.id AS id, m.ontology AS ontology, m.acl AS acl, collect(e) AS envelopes
296
+ ` ,
239
297
{ ontology } ,
240
298
) ;
241
299
242
- return result . records . map ( ( r ) => r . get ( "id" ) ) ;
300
+ return result . records . map ( ( record ) => {
301
+ const envelopes = record
302
+ . get ( "envelopes" )
303
+ . map ( ( node : any ) : Envelope < T [ keyof T ] > => {
304
+ const properties = node . properties ;
305
+ return {
306
+ id : properties . id ,
307
+ ontology : properties . ontology ,
308
+ value : deserializeValue (
309
+ properties . value ,
310
+ properties . valueType ,
311
+ ) as T [ keyof T ] ,
312
+ valueType : properties . valueType ,
313
+ } ;
314
+ } ) ;
315
+
316
+ const parsed = envelopes . reduce (
317
+ ( acc : T , envelope : Envelope < T [ keyof T ] > ) => {
318
+ ( acc as any ) [ envelope . ontology ] = envelope . value ;
319
+ return acc ;
320
+ } ,
321
+ { } as T ,
322
+ ) ;
323
+
324
+ return {
325
+ id : record . get ( "id" ) ,
326
+ ontology : record . get ( "ontology" ) ,
327
+ acl : record . get ( "acl" ) ,
328
+ envelopes,
329
+ parsed,
330
+ } ;
331
+ } ) ;
243
332
}
244
333
245
334
/**
0 commit comments