1
1
import { ChainSyncEventType } from '@cardano-sdk/core' ;
2
- import { TransactionEntity } from '../entity' ;
2
+ import { CredentialEntity , TransactionEntity } from '../entity' ;
3
3
import { WithTxCredentials } from './storeCredentials' ;
4
4
import { typeormOperator } from './util' ;
5
5
import { WithBlock } from '@cardano-sdk/projection' ;
@@ -17,14 +17,25 @@ export const storeTransactions = typeormOperator<WithTxCredentials>(async (evt)
17
17
// produced txs will be automatically deleted via block cascade
18
18
if ( txs . length === 0 || eventType !== ChainSyncEventType . RollForward ) return ;
19
19
20
- const transactionEntities = txs . map (
21
- ( tx ) : TransactionEntity => ( {
20
+ const credentialEntities = new Set < CredentialEntity > ( ) ;
21
+ const transactionEntities = new Array < TransactionEntity > ( ) ;
22
+ for ( const tx of txs ) {
23
+ const credentials = credentialsByTx [ tx . id ] || [ ] ;
24
+ const txEntity : TransactionEntity = {
22
25
block : header ,
23
26
cbor : tx . cbor ,
24
- credentials : credentialsByTx [ tx . id ] || [ ] ,
27
+ credentials,
25
28
txId : tx . id
26
- } )
27
- ) ;
29
+ } ;
30
+ transactionEntities . push ( txEntity ) ;
31
+ credentials . forEach ( ( credential ) => {
32
+ if ( ! credential . transactions ) {
33
+ credential . transactions = [ ] ;
34
+ }
35
+ credential . transactions . push ( txEntity ) ;
36
+ credentialEntities . add ( credential ) ; // Collect credentials to be re-saved
37
+ } ) ;
38
+ }
28
39
29
40
await queryRunner . manager
30
41
. createQueryBuilder ( )
@@ -33,4 +44,7 @@ export const storeTransactions = typeormOperator<WithTxCredentials>(async (evt)
33
44
. values ( transactionEntities )
34
45
. orIgnore ( )
35
46
. execute ( ) ;
47
+
48
+ // Re-save credential entities to ensure relationships are persisted
49
+ await queryRunner . manager . save ( Array . from ( credentialEntities ) ) ;
36
50
} ) ;
0 commit comments