1- import { Collection , Db } from "mongodb" ;
2- import { ConnectionManager } from "../connections/manager.class.js" ;
3- import { Message } from "amqplib" ;
4- import { hLog } from "./common_functions.js" ;
5- import { IAccount } from "../../interfaces/table-account.js" ;
6- import { IProposal } from "../../interfaces/table-proposal.js" ;
7- import { IVoter } from "../../interfaces/table-voter.js" ;
1+ import { BulkWriteResult , Collection , Db } from "mongodb" ;
2+ import { ConnectionManager } from "../connections/manager.class.js" ;
3+ import { Message } from "amqplib" ;
4+ import { hLog } from "./common_functions.js" ;
5+ import { IAccount } from "../../interfaces/table-account.js" ;
6+ import { IProposal } from "../../interfaces/table-proposal.js" ;
7+ import { IVoter } from "../../interfaces/table-voter.js" ;
88
99export class MongoRoutes {
1010
@@ -24,6 +24,7 @@ export class MongoRoutes {
2424 this . proposalsCollection = this . db . collection ( 'proposals' ) ;
2525 this . votersCollection = this . db . collection ( 'voters' ) ;
2626 this . addRoutes ( ) ;
27+ this . addDynamicRoutes ( ) ;
2728 }
2829 }
2930
@@ -62,7 +63,7 @@ export class MongoRoutes {
6263 }
6364 } ) ;
6465
65- this . accountsCollection ?. bulkWrite ( operations , { ordered : false } ) . catch ( reason => {
66+ this . accountsCollection ?. bulkWrite ( operations , { ordered : false } ) . catch ( reason => {
6667 hLog ( 'error' , 'mongo-routes' , 'table-accounts' , reason ) ;
6768 } ) . finally ( ( ) => {
6869 // TODO: ack
@@ -88,7 +89,7 @@ export class MongoRoutes {
8889 } ;
8990 } ) ;
9091
91- this . proposalsCollection ?. bulkWrite ( operations , { ordered : false } ) . catch ( reason => {
92+ this . proposalsCollection ?. bulkWrite ( operations , { ordered : false } ) . catch ( reason => {
9293 hLog ( 'error' , 'mongo-routes' , 'table-proposals' , reason ) ;
9394 } ) . finally ( ( ) => {
9495 callback ( payload . length ) ;
@@ -126,4 +127,66 @@ export class MongoRoutes {
126127 } ) ;
127128 } ;
128129 }
130+
131+ private addDynamicRoutes ( ) {
132+ this . routes [ 'dynamic-table' ] = ( payload : Message [ ] , callback : ( indexed_size ?: number ) => void ) => {
133+ const groupedOps = new Map < string , any [ ] > ( ) ;
134+ payload . forEach ( ( msg : Message ) => {
135+ const delta = JSON . parse ( msg . content . toString ( ) ) as any ;
136+ const headers = msg . properties . headers as any ;
137+
138+ const op = { } ;
139+
140+ if ( headers . present === 1 ) {
141+ op [ 'updateOne' ] = {
142+ filter : {
143+ '@scope' : delta . scope ,
144+ '@pk' : delta . primary_key
145+ } ,
146+ update : {
147+ $set : {
148+ '@scope' : delta . scope ,
149+ '@pk' : delta . primary_key ,
150+ '@payer' : delta . payer ,
151+ '@block_num' : delta . block_num ,
152+ '@block_time' : delta [ '@timestamp' ] ,
153+ '@block_id' : delta [ 'block_id' ] ,
154+ ...delta . data
155+ }
156+ } ,
157+ upsert : true
158+ } ;
159+ } else {
160+ op [ 'deleteOne' ] = {
161+ filter : {
162+ '@scope' : delta . scope ,
163+ '@pk' : delta . primary_key
164+ }
165+ } ;
166+ }
167+
168+ const collection = headers . code + '-' + headers . table ;
169+ const col = groupedOps . get ( collection ) ;
170+ if ( col ) {
171+ col . push ( op ) ;
172+ } else {
173+ groupedOps . set ( collection , [ op ] ) ;
174+ }
175+ // hLog(msg.properties.headers, delta);
176+ } ) ;
177+
178+ const promises : Promise < BulkWriteResult > [ ] = [ ] ;
179+
180+ groupedOps . forEach ( ( value , key ) => {
181+ if ( this . db ) {
182+ promises . push ( this . db . collection ( key ) . bulkWrite ( value , { ordered : false } ) )
183+ }
184+ } ) ;
185+
186+ Promise . all ( promises ) . finally ( ( ) => {
187+ callback ( payload . length ) ;
188+ } ) ;
189+ }
190+
191+ }
129192}
0 commit comments