@@ -69,25 +69,50 @@ Store.prototype.flush = function(err) {
69
69
}
70
70
}
71
71
72
- Store . prototype . execute = function ( ) {
72
+ var primaryOptions = [ 'primary' , 'primaryPreferred' , 'nearest' , 'secondaryPreferred' ] ;
73
+ var secondaryOptions = [ 'secondary' , 'secondaryPreferred' ] ;
74
+
75
+ Store . prototype . execute = function ( options ) {
76
+ options = options || { } ;
73
77
// Get current ops
74
78
var ops = this . s . storedOps ;
75
79
// Reset the ops
76
80
this . s . storedOps = [ ] ;
77
81
82
+ // Unpack options
83
+ var executePrimary = typeof options . executePrimary === 'boolean'
84
+ ? options . executePrimary : true ;
85
+ var executeSecondary = typeof options . executeSecondary === 'boolean'
86
+ ? options . executeSecondary : true ;
87
+
78
88
// Execute all the stored ops
79
89
while ( ops . length > 0 ) {
80
90
var op = ops . shift ( ) ;
81
91
82
- // console.log("======= execute op")
83
- // console.dir(op)
84
-
85
92
if ( op . t == 'cursor' ) {
86
- op . o [ op . m ] . apply ( op . o , op . p ) ;
93
+ if ( executePrimary && executeSecondary ) {
94
+ op . o [ op . m ] . apply ( op . o , op . p ) ;
95
+ } else if ( executePrimary && op . o . options
96
+ && op . o . options . readPreference
97
+ && primaryOptions . indexOf ( op . o . options . readPreference . mode ) != - 1 ) {
98
+ op . o [ op . m ] . apply ( op . o , op . p ) ;
99
+ } else if ( ! executePrimary && executeSecondary && op . o . options
100
+ && op . o . options . readPreference
101
+ && secondaryOptions . indexOf ( op . o . options . readPreference . mode ) != - 1 ) {
102
+ op . o [ op . m ] . apply ( op . o , op . p ) ;
103
+ }
87
104
} else if ( op . t == 'auth' ) {
88
105
this . s . topology [ op . t ] . apply ( this . s . topology , op . o ) ;
89
106
} else {
90
- this . s . topology [ op . t ] ( op . n , op . o , op . op , op . c ) ;
107
+ if ( executePrimary && executeSecondary ) {
108
+ this . s . topology [ op . t ] ( op . n , op . o , op . op , op . c ) ;
109
+ } else if ( executePrimary && op . op && op . op . readPreference
110
+ && primaryOptions . indexOf ( op . op . readPreference . mode ) != - 1 ) {
111
+ this . s . topology [ op . t ] ( op . n , op . o , op . op , op . c ) ;
112
+ } else if ( ! executePrimary && executeSecondary && op . op && op . op . readPreference
113
+ && secondaryOptions . indexOf ( op . op . readPreference . mode ) != - 1 ) {
114
+ this . s . topology [ op . t ] ( op . n , op . o , op . op , op . c ) ;
115
+ }
91
116
}
92
117
}
93
118
}
0 commit comments