Repository: parse-community/Parse-SDK-JS
Pull request #855 (2019-07-10 18:03:44)

Including read preference options in parse query
See https://github.com/parse-community/parse-server/issues/5515

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=h1) Report
> Merging [#855](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/309790cfcef6e6af449a498168ecd01855a08b70?src=pr&el=desc) will **decrease** coverage by `0.02%`.
> The diff coverage is `85.71%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855/graphs/tree.svg?width=650&token=8nCENM0QHj&height=150&src=pr)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #855      +/-   ##
==========================================
- Coverage    92.1%   92.07%   -0.03%     
==========================================
  Files          54       54              
  Lines        4953     4973      +20     
  Branches     1103     1109       +6     
==========================================
+ Hits         4562     4579      +17     
- Misses        391      394       +3
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseQuery.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855/diff?src=pr&el=tree#diff-c3JjL1BhcnNlUXVlcnkuanM=) | `95.64% <85.71%> (-0.41%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=footer). Last update [309790c...fb4706e](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/855?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/ParseQuery.js b/src/ParseQuery.js
index 9ba16c3..b3cd71e 100644
--- a/src/ParseQuery.js
+++ b/src/ParseQuery.js
@@ -36,6 +36,9 @@ export type QueryJSON = {
   order?: string;
   className?: string;
   count?: number;
+  readPreference?: string;
+  includeReadPreference?: string;
+  subqueryReadPreference?: string;
 };
 
 /**
@@ -220,6 +223,9 @@ class ParseQuery {
   _limit: number;
   _skip: number;
   _order: Array<string>;
+  _readPreference: string;
+  _includeReadPreference: string;
+  _subqueryReadPreference: string;
   _queriesLocalDatastore: boolean;
   _localDatastorePinName: any;
   _extraOptions: { [key: string]: mixed };
@@ -253,6 +259,9 @@ class ParseQuery {
     this._include = [];
     this._limit = -1; // negative limit is not sent in the server request
     this._skip = 0;
+    this._readPreference = null;
+    this._includeReadPreference = null;
+    this._subqueryReadPreference = null;
     this._queriesLocalDatastore = false;
     this._localDatastorePinName = null;
     this._extraOptions = {};
@@ -391,6 +400,15 @@ class ParseQuery {
     if (this._order) {
       params.order = this._order.join(',');
     }
+    if (this._readPreference) {
+      params.readPreference = this._readPreference;
+    }
+    if (this._includeReadPreference) {
+      params.includeReadPreference = this._includeReadPreference;
+    }
+    if (this._subqueryReadPreference) {
+      params.subqueryReadPreference = this._subqueryReadPreference;
+    }
     for (const key in this._extraOptions) {
       params[key] = this._extraOptions[key];
     }
@@ -444,9 +462,21 @@ class ParseQuery {
       this._order = json.order.split(",");
     }
 
+    if (json.readPreference) {
+      this._readPreference = json.readPreference;
+    }
+
+    if (json.includeReadPreference) {
+      this._includeReadPreference = json.includeReadPreference;
+    }
+
+    if (json.subqueryReadPreference) {
+      this._subqueryReadPreference = json.subqueryReadPreference;
+    }
+
     for (const key in json) {
       if (json.hasOwnProperty(key))  {
-        if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
+        if (["where", "include", "keys", "limit", "skip", "order", "readPreference", "includeReadPreference", "subqueryReadPreference"].indexOf(key) === -1) {
           this._extraOptions[key] = json[key];
         }
       }
@@ -1482,6 +1512,20 @@ class ParseQuery {
     return this;
   }
 
+  /**
+   * Changes the read preference that the backend will use when performing the query to the database.
+   * @param {String} readPreference The read preference for the main query.
+   * @param {String} includeReadPreference The read preference for the queries to include pointers.
+   * @param {String} subqueryReadPreference The read preference for the sub queries.
+   * @return {Parse.Query} Returns the query, so you can chain this call.
+   */
+  readPreference(readPreference: string, includeReadPreference?: string, subqueryReadPreference?: string): ParseQuery {
+    this._readPreference = readPreference;
+    this._includeReadPreference = includeReadPreference;
+    this._subqueryReadPreference = subqueryReadPreference;
+    return this;
+  }
+
   /**
    * Subscribe this query to get liveQuery updates
    *


Test patch:
diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js
index 58d4c71..606696c 100644
--- a/src/__tests__/ParseQuery-test.js
+++ b/src/__tests__/ParseQuery-test.js
@@ -1315,7 +1315,10 @@ describe('ParseQuery', () => {
             size: {
               $in: ['small', 'medium']
             }
-          }
+          },
+          readPreference: 'PRIMARY',
+          includeReadPreference: 'SECONDARY',
+          subqueryReadPreference: 'SECONDARY_PREFERRED',
         });
         expect(options).toEqual({});
         return Promise.resolve({
@@ -1333,6 +1336,7 @@ describe('ParseQuery', () => {
       .skip(8)
       .ascending('createdAt')
       .select('size', 'name')
+      .readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED')
       .find()
       .then((objs) => {
         expect(objs.length).toBe(2);