Skip to content

Commit b88dbd4

Browse files
committed
Handle store execution based on readPreference
1 parent b6b8290 commit b88dbd4

File tree

3 files changed

+464
-6
lines changed

3 files changed

+464
-6
lines changed

lib/topology_base.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,50 @@ Store.prototype.flush = function(err) {
6969
}
7070
}
7171

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 || {};
7377
// Get current ops
7478
var ops = this.s.storedOps;
7579
// Reset the ops
7680
this.s.storedOps = [];
7781

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+
7888
// Execute all the stored ops
7989
while(ops.length > 0) {
8090
var op = ops.shift();
8191

82-
// console.log("======= execute op")
83-
// console.dir(op)
84-
8592
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+
}
87104
} else if(op.t == 'auth') {
88105
this.s.topology[op.t].apply(this.s.topology, op.o);
89106
} 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+
}
91116
}
92117
}
93118
}

0 commit comments

Comments
 (0)