@@ -3,7 +3,12 @@ import { MongoServer } from './mongoserver';
33import { ConnectionString } from 'mongodb-connection-string-url' ;
44import type { DownloadOptions } from '@mongodb-js/mongodb-downloader' ;
55import { downloadMongoDb } from '@mongodb-js/mongodb-downloader' ;
6- import type { Document , MongoClientOptions , TagSet } from 'mongodb' ;
6+ import type {
7+ Document ,
8+ MongoClientOptions ,
9+ TagSet ,
10+ WriteConcernSettings ,
11+ } from 'mongodb' ;
712import { MongoClient } from 'mongodb' ;
813import {
914 sleep ,
@@ -470,6 +475,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
470475 const admin = client . db ( 'admin' ) ;
471476 for ( const user of this . users ) {
472477 const { username, password, ...rest } = user ;
478+ debug ( 'adding new user' , { username, ...rest } , this . connectionString ) ;
473479 await admin . command ( { createUser : username , pwd : password , ...rest } ) ;
474480 }
475481 } ) ;
@@ -507,6 +513,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
507513 clientOptions : MongoClientOptions = { } ,
508514 ) : Promise < ReturnType < Fn > > {
509515 const client = await MongoClient . connect ( this . connectionString , {
516+ ...this . getFullWriteConcernOptions ( ) ,
510517 ...this . defaultConnectionOptions ,
511518 ...clientOptions ,
512519 } ) ;
@@ -524,4 +531,29 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
524531 unref ( ) : void {
525532 for ( const child of this . children ( ) ) child . unref ( ) ;
526533 }
534+
535+ // Return maximal write concern options based on topology
536+ getFullWriteConcernOptions ( ) : { writeConcern ?: WriteConcernSettings } {
537+ switch ( this . topology ) {
538+ case 'standalone' :
539+ return { } ;
540+ case 'replset' :
541+ return { writeConcern : { w : this . servers . length , j : true } } ;
542+ case 'sharded' :
543+ return {
544+ writeConcern : {
545+ w : Math . min (
546+ ...this . shards
547+ . map ( ( s ) => s . getFullWriteConcernOptions ( ) . writeConcern ?. w )
548+ . filter ( ( w ) => typeof w === 'number' ) ,
549+ ) ,
550+ j : true ,
551+ } ,
552+ } ;
553+ default :
554+ throw new Error (
555+ `Not implemented for topology ${ this . topology as string } ` ,
556+ ) ;
557+ }
558+ }
527559}
0 commit comments