@@ -358,7 +358,7 @@ export class DatabaseConnection {
358358 * @returns Promise<T[]> Array of results
359359 */
360360 // tslint:disable-next-line: max-line-length
361- async query < T extends Base > ( query : any , projection : Object , top : number , skip : number , orderby : Object | string , collectionname : string , jwt : string , queryas : string = null , hint : Object | string = null , parent : Span = undefined ) : Promise < T [ ] > {
361+ async query < T extends Base > ( query : any , projection : Object , top : number , skip : number , orderby : Object | string , collectionname : string , jwt : string , queryas : string = null , hint : Object | string = null , parent : Span ) : Promise < T [ ] > {
362362 const span : Span = otel . startSubSpan ( "db.query" , parent ) ;
363363 try {
364364 await this . connect ( span ) ;
@@ -501,32 +501,38 @@ export class DatabaseConnection {
501501 otel . endSpan ( span ) ;
502502 }
503503 }
504- async GetDocumentVersion < T extends Base > ( collectionname : string , id : string , version : number , jwt : string ) : Promise < T > {
504+ async GetDocumentVersion < T extends Base > ( collectionname : string , id : string , version : number , jwt : string , parent : Span ) : Promise < T > {
505505 const roundDown = function ( num , precision ) : number {
506506 num = parseFloat ( num ) ;
507507 if ( ! precision ) return num ;
508508 return ( Math . floor ( num / precision ) * precision ) ;
509509 } ;
510+ const span : Span = otel . startSubSpan ( "db.GetDocumentVersion" , parent ) ;
511+ try {
510512
511- let result : T = await this . getbyid < T > ( id , collectionname , jwt ) ;
512- if ( result == null ) return result ;
513- if ( result . _version > version ) {
514- const rootjwt = Crypt . rootToken ( )
515- // const baseversion = roundDown(version, Config.history_delta_count);
516- const basehist = await this . query < any > ( { id : id , item : { $exists : true , $ne : null } , "_version" : { $lte : version } } , null , 1 , 0 , { _version : - 1 } , collectionname + "_hist" , rootjwt ) ;
517- result = basehist [ 0 ] . item ;
518- const baseversion = basehist [ 0 ] . _version ;
519-
520- const history = await this . query < T > ( { id : id , "_version" : { $gt : baseversion , $lte : version } } , null , Config . history_delta_count , 0 , { _version : 1 } , collectionname + "_hist" , rootjwt ) ;
521-
522- for ( let i = 0 ; i < history . length ; i ++ ) {
523- const delta = ( history [ i ] as any ) . delta ;
524- if ( delta != null ) {
525- result = jsondiffpatch . patch ( result , delta ) ;
513+ let result : T = await this . getbyid < T > ( id , collectionname , jwt , span ) ;
514+ if ( result == null ) return result ;
515+ if ( result . _version > version ) {
516+ const rootjwt = Crypt . rootToken ( )
517+ // const baseversion = roundDown(version, Config.history_delta_count);
518+ const basehist = await this . query < any > ( { id : id , item : { $exists : true , $ne : null } , "_version" : { $lte : version } } , null , 1 , 0 , { _version : - 1 } , collectionname + "_hist" , rootjwt , undefined , undefined , span ) ;
519+ result = basehist [ 0 ] . item ;
520+ const baseversion = basehist [ 0 ] . _version ;
521+ const history = await this . query < T > ( { id : id , "_version" : { $gt : baseversion , $lte : version } } , null , Config . history_delta_count , 0 , { _version : 1 } , collectionname + "_hist" , rootjwt , undefined , undefined , span ) ;
522+ for ( let i = 0 ; i < history . length ; i ++ ) {
523+ const delta = ( history [ i ] as any ) . delta ;
524+ if ( delta != null ) {
525+ result = jsondiffpatch . patch ( result , delta ) ;
526+ }
526527 }
527528 }
529+ return result ;
530+ } catch ( error ) {
531+ span . recordException ( error ) ;
532+ throw error ;
533+ } finally {
534+ otel . endSpan ( span ) ;
528535 }
529- return result ;
530536 }
531537
532538 /**
@@ -536,11 +542,19 @@ export class DatabaseConnection {
536542 * @param {string } jwt JWT of user who is making the query, to limit results based on permissions
537543 * @returns Promise<T>
538544 */
539- async getbyid < T extends Base > ( id : string , collectionname : string , jwt : string ) : Promise < T > {
540- if ( id === null || id === undefined ) { throw Error ( "Id cannot be null" ) ; }
541- const arr : T [ ] = await this . query < T > ( { _id : id } , null , 1 , 0 , null , collectionname , jwt ) ;
542- if ( arr === null || arr . length === 0 ) { return null ; }
543- return arr [ 0 ] ;
545+ async getbyid < T extends Base > ( id : string , collectionname : string , jwt : string , parent : Span ) : Promise < T > {
546+ const span : Span = otel . startSubSpan ( "db.getbyid" , parent ) ;
547+ try {
548+ if ( id === null || id === undefined ) { throw Error ( "Id cannot be null" ) ; }
549+ const arr : T [ ] = await this . query < T > ( { _id : id } , null , 1 , 0 , null , collectionname , jwt , undefined , undefined , span ) ;
550+ if ( arr === null || arr . length === 0 ) { return null ; }
551+ return arr [ 0 ] ;
552+ } catch ( error ) {
553+ span . recordException ( error ) ;
554+ throw error ;
555+ } finally {
556+ otel . endSpan ( span ) ;
557+ }
544558 }
545559 /**
546560 * Do MongoDB aggregation
@@ -967,8 +981,7 @@ export class DatabaseConnection {
967981 let name = q . item . name ;
968982 if ( NoderedUtil . IsNullEmpty ( name ) ) name = ( q . item as any ) . _name ;
969983 if ( NoderedUtil . IsNullEmpty ( name ) ) name = "Unknown" ;
970-
971- original = await this . getbyid < T > ( q . item . _id , q . collectionname , q . jwt ) ;
984+ original = await this . getbyid < T > ( q . item . _id , q . collectionname , q . jwt , span ) ;
972985 if ( ! original ) { throw Error ( "item not found!" ) ; }
973986 if ( ! this . hasAuthorization ( user , original , Rights . update ) ) {
974987 const again = this . hasAuthorization ( user , original , Rights . update ) ;
@@ -1309,7 +1322,7 @@ export class DatabaseConnection {
13091322 let exists : Base [ ] = [ ] ;
13101323 if ( query != null ) {
13111324 // exists = await this.query(query, { name: 1 }, 2, 0, null, q.collectionname, q.jwt);
1312- exists = await this . query ( query , null , 2 , 0 , null , q . collectionname , q . jwt ) ;
1325+ exists = await this . query ( query , null , 2 , 0 , null , q . collectionname , q . jwt , undefined , undefined , span ) ;
13131326 }
13141327 if ( exists . length == 1 ) {
13151328 q . item . _id = exists [ 0 ] . _id ;
@@ -1790,9 +1803,15 @@ export class DatabaseConnection {
17901803 }
17911804 const ot_end = otel . startTimer ( ) ;
17921805 const mongodbspan : Span = otel . startSubSpan ( "mongodb.insertOne" , span ) ;
1793- await this . db . collection ( q . collectionname + '_hist' ) . insertOne ( updatehist ) ;
1794- otel . endSpan ( mongodbspan ) ;
1795- otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : q . collectionname + '_hist' } ) ;
1806+ // await this.db.collection(q.collectionname + '_hist').insertOne(updatehist);
1807+ this . db . collection ( q . collectionname + '_hist' ) . insertOne ( updatehist ) . then ( ( ) => {
1808+ otel . endSpan ( mongodbspan ) ;
1809+ otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : q . collectionname + '_hist' } ) ;
1810+ } ) . catch ( err => {
1811+ mongodbspan . recordException ( err ) ;
1812+ otel . endSpan ( mongodbspan ) ;
1813+ otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : q . collectionname + '_hist' } ) ;
1814+ } ) ;
17961815 } catch ( error ) {
17971816 span . recordException ( error ) ;
17981817 this . _logger . error ( error ) ;
@@ -1888,9 +1907,15 @@ export class DatabaseConnection {
18881907 }
18891908 const ot_end = otel . startTimer ( ) ;
18901909 const mongodbspan : Span = otel . startSubSpan ( "mongodb.insertOne" , span ) ;
1891- await this . db . collection ( collectionname + '_hist' ) . insertOne ( deltahist ) ;
1892- otel . endSpan ( mongodbspan ) ;
1893- otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : collectionname + '_hist' } ) ;
1910+ // await this.db.collection(collectionname + '_hist').insertOne(deltahist);
1911+ this . db . collection ( collectionname + '_hist' ) . insertOne ( deltahist ) . then ( ( ) => {
1912+ otel . endSpan ( mongodbspan ) ;
1913+ otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : collectionname + '_hist' } ) ;
1914+ } ) . catch ( err => {
1915+ mongodbspan . recordException ( err ) ;
1916+ otel . endSpan ( mongodbspan ) ;
1917+ otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : collectionname + '_hist' } ) ;
1918+ } ) ;
18941919 }
18951920 } else {
18961921 const fullhist = {
@@ -1910,9 +1935,15 @@ export class DatabaseConnection {
19101935 }
19111936 const ot_end = otel . startTimer ( ) ;
19121937 const mongodbspan : Span = otel . startSubSpan ( "mongodb.insertOne" , span ) ;
1913- await this . db . collection ( collectionname + '_hist' ) . insertOne ( fullhist ) ;
1914- otel . endSpan ( mongodbspan ) ;
1915- otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : collectionname + '_hist' } ) ;
1938+ // await this.db.collection(collectionname + '_hist').insertOne(fullhist);
1939+ this . db . collection ( collectionname + '_hist' ) . insertOne ( fullhist ) . then ( ( ) => {
1940+ otel . endSpan ( mongodbspan ) ;
1941+ otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : collectionname + '_hist' } ) ;
1942+ } ) . catch ( err => {
1943+ mongodbspan . recordException ( err ) ;
1944+ otel . endSpan ( mongodbspan ) ;
1945+ otel . endTimer ( ot_end , DatabaseConnection . mongodb_insert , { collection : collectionname + '_hist' } ) ;
1946+ } ) ;
19161947 }
19171948 item . _modifiedby = _modifiedby ;
19181949 item . _modifiedbyid = _modifiedbyid ;
0 commit comments