Skip to content

Commit 174d108

Browse files
authored
Including read preference options in parse query (#855)
* Including read preference options in parse query * Turning includeReadPreference and subqueryReadPreference optional when using query.readPreference() method
1 parent 309790c commit 174d108

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/ParseQuery.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export type QueryJSON = {
3636
order?: string;
3737
className?: string;
3838
count?: number;
39+
readPreference?: string;
40+
includeReadPreference?: string;
41+
subqueryReadPreference?: string;
3942
};
4043

4144
/**
@@ -220,6 +223,9 @@ class ParseQuery {
220223
_limit: number;
221224
_skip: number;
222225
_order: Array<string>;
226+
_readPreference: string;
227+
_includeReadPreference: string;
228+
_subqueryReadPreference: string;
223229
_queriesLocalDatastore: boolean;
224230
_localDatastorePinName: any;
225231
_extraOptions: { [key: string]: mixed };
@@ -253,6 +259,9 @@ class ParseQuery {
253259
this._include = [];
254260
this._limit = -1; // negative limit is not sent in the server request
255261
this._skip = 0;
262+
this._readPreference = null;
263+
this._includeReadPreference = null;
264+
this._subqueryReadPreference = null;
256265
this._queriesLocalDatastore = false;
257266
this._localDatastorePinName = null;
258267
this._extraOptions = {};
@@ -391,6 +400,15 @@ class ParseQuery {
391400
if (this._order) {
392401
params.order = this._order.join(',');
393402
}
403+
if (this._readPreference) {
404+
params.readPreference = this._readPreference;
405+
}
406+
if (this._includeReadPreference) {
407+
params.includeReadPreference = this._includeReadPreference;
408+
}
409+
if (this._subqueryReadPreference) {
410+
params.subqueryReadPreference = this._subqueryReadPreference;
411+
}
394412
for (const key in this._extraOptions) {
395413
params[key] = this._extraOptions[key];
396414
}
@@ -444,9 +462,21 @@ class ParseQuery {
444462
this._order = json.order.split(",");
445463
}
446464

465+
if (json.readPreference) {
466+
this._readPreference = json.readPreference;
467+
}
468+
469+
if (json.includeReadPreference) {
470+
this._includeReadPreference = json.includeReadPreference;
471+
}
472+
473+
if (json.subqueryReadPreference) {
474+
this._subqueryReadPreference = json.subqueryReadPreference;
475+
}
476+
447477
for (const key in json) {
448478
if (json.hasOwnProperty(key)) {
449-
if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
479+
if (["where", "include", "keys", "limit", "skip", "order", "readPreference", "includeReadPreference", "subqueryReadPreference"].indexOf(key) === -1) {
450480
this._extraOptions[key] = json[key];
451481
}
452482
}
@@ -1482,6 +1512,20 @@ class ParseQuery {
14821512
return this;
14831513
}
14841514

1515+
/**
1516+
* Changes the read preference that the backend will use when performing the query to the database.
1517+
* @param {String} readPreference The read preference for the main query.
1518+
* @param {String} includeReadPreference The read preference for the queries to include pointers.
1519+
* @param {String} subqueryReadPreference The read preference for the sub queries.
1520+
* @return {Parse.Query} Returns the query, so you can chain this call.
1521+
*/
1522+
readPreference(readPreference: string, includeReadPreference?: string, subqueryReadPreference?: string): ParseQuery {
1523+
this._readPreference = readPreference;
1524+
this._includeReadPreference = includeReadPreference;
1525+
this._subqueryReadPreference = subqueryReadPreference;
1526+
return this;
1527+
}
1528+
14851529
/**
14861530
* Subscribe this query to get liveQuery updates
14871531
*

src/__tests__/ParseQuery-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,10 @@ describe('ParseQuery', () => {
13151315
size: {
13161316
$in: ['small', 'medium']
13171317
}
1318-
}
1318+
},
1319+
readPreference: 'PRIMARY',
1320+
includeReadPreference: 'SECONDARY',
1321+
subqueryReadPreference: 'SECONDARY_PREFERRED',
13191322
});
13201323
expect(options).toEqual({});
13211324
return Promise.resolve({
@@ -1333,6 +1336,7 @@ describe('ParseQuery', () => {
13331336
.skip(8)
13341337
.ascending('createdAt')
13351338
.select('size', 'name')
1339+
.readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED')
13361340
.find()
13371341
.then((objs) => {
13381342
expect(objs.length).toBe(2);

0 commit comments

Comments
 (0)