11import {
2- ObjectID , Db , Binary , InsertOneWriteOpResult , DeleteWriteOpResultObject , ObjectId , MapReduceOptions , CollectionInsertOneOptions , UpdateWriteOpResult , WriteOpResult , GridFSBucket
2+ ObjectID , Db , Binary , InsertOneWriteOpResult , DeleteWriteOpResultObject , ObjectId , MapReduceOptions , CollectionInsertOneOptions , UpdateWriteOpResult , WriteOpResult , GridFSBucket , ReadPreference
33} from "mongodb" ;
44import { MongoClient } from "mongodb" ;
55import { Base , Rights , WellknownIds } from "./base" ;
@@ -250,6 +250,12 @@ export class DatabaseConnection {
250250 q = this . getbasequery ( jwt , "_acl" , [ Rights . read ] ) ;
251251 }
252252
253+ if ( finalize != null && finalize != undefined ) {
254+ try {
255+ if ( ( ( finalize as any ) as string ) . trim ( ) == "" ) { ( finalize as any ) = null ; }
256+ } catch ( error ) {
257+ }
258+ }
253259 var inline : boolean = false ;
254260 var opt : MapReduceOptions = { query : q , out : { replace : "map_temp_res" } , finalize : finalize } ;
255261 var outcol : string = "map_temp_res" ;
@@ -266,14 +272,19 @@ export class DatabaseConnection {
266272 if ( out . hasOwnProperty ( "inline" ) ) { inline = true ; }
267273 }
268274 opt . scope = scope ;
275+ opt . readPreference = ReadPreference . PRIMARY_PREFERRED ;
269276
270277 // var result:T[] = await this.db.collection(collectionname).mapReduce(map, reduce, {query: q, out : {inline : 1}});
271- if ( inline ) {
272- var result : T [ ] = await this . db . collection ( collectionname ) . mapReduce ( map , reduce , opt ) ;
273- return result ;
274- } else {
275- await this . db . collection ( collectionname ) . mapReduce ( map , reduce , opt ) ;
276- return [ ] ;
278+ try {
279+ if ( inline ) {
280+ var result : T [ ] = await this . db . collection ( collectionname ) . mapReduce ( map , reduce , opt ) ;
281+ return result ;
282+ } else {
283+ await this . db . collection ( collectionname ) . mapReduce ( map , reduce , opt ) ;
284+ return [ ] ;
285+ }
286+ } catch ( error ) {
287+ throw error ;
277288 }
278289 // var result:T[] = await this.db.collection(outcol).find({}).toArray(); // .limit(top)
279290 // // this.db.collection("map_temp_res").deleteMany({});
0 commit comments