Repository: parse-community/Parse-SDK-JS
Pull request #448 (2017-06-25 06:31:35)

Parse.Query.fromJSON method
Made mistake with previous PR. This is new one.
Method restores query from json gained from Parse.Query.toJSON()

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/448?src=pr&el=h1) Report
> Merging [#448](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/448?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/1b26adc1cb47d162c4cb2fa0ce0669c3acb1be49?src=pr&el=desc) will **increase** coverage by `0.04%`.
> The diff coverage is `94.11%`.

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

```diff
@@            Coverage Diff             @@
##           master     #448      +/-   ##
==========================================
+ Coverage   85.34%   85.38%   +0.04%     
==========================================
  Files          46       46              
  Lines        3704     3721      +17     
  Branches      846      854       +8     
==========================================
+ Hits         3161     3177      +16     
- Misses        543      544       +1
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/448?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/448?src=pr&el=footer). Last update [1b26adc...14c0ab5](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/448?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

kyptov: @flovilmart no problem, do you mean this?
* rename `fromJSON` -> `withJSON`,
* add new static method `fromJSON` which creates new Parse.Query and calling `withJSON` method
* some test to cover this

flovilmart: @kyptov, yes that’s what I had in mind. Does that makes sense for you?
kyptov: @flovilmart made some changes
flovilmart: @kyptov looking great! Thanks!

Patch:
diff --git a/src/ParseQuery.js b/src/ParseQuery.js
index 373bce0..1591869 100644
--- a/src/ParseQuery.js
+++ b/src/ParseQuery.js
@@ -261,6 +261,75 @@ export default class ParseQuery {
     return params;
   }
 
+  /**
+   * Return a query with conditions from json, can be useful to send query from server side to client
+   * Not static, all query conditions was set before calling this method will be deleted.
+   * For example on the server side we have
+   * var query = new Parse.Query("className");
+   * query.equalTo(key: value);
+   * query.limit(100);
+   * ... (others queries)
+   * Create JSON representation of Query Object
+   * var jsonFromServer = query.fromJSON();
+   *
+   * On client side getting query:
+   * var query = new Parse.Query("className");
+   * query.fromJSON(jsonFromServer);
+   *
+   * and continue to query...
+   * query.skip(100).find().then(...);
+   * @method withJSON
+   * @param {QueryJSON} json from Parse.Query.toJSON() method
+   * @return {Parse.Query} Returns the query, so you can chain this call.
+   */
+  withJSON(json: QueryJSON): ParseQuery {
+
+    if (json.where) {
+      this._where = json.where;
+    }
+
+    if (json.include) {
+      this._include = json.include.split(",");
+    }
+
+    if (json.keys) {
+      this._select = json.keys.split(",");
+    }
+
+    if (json.limit) {
+      this._limit  = json.limit;
+    }
+
+    if (json.skip) {
+      this._skip = json.skip;
+    }
+
+    if (json.order) {
+      this._order = json.order.split(",");
+    }
+
+    for (let key in json) if (json.hasOwnProperty(key))  {
+      if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
+        this._extraOptions[key] = json[key];
+      }
+    }
+
+    return this;
+
+  }
+
+    /**
+     * Static method to restore Parse.Query by json representation
+     * Internally calling Parse.Query.withJSON
+     * @param {String} className
+     * @param {QueryJSON} json from Parse.Query.toJSON() method
+     * @returns {Parse.Query} new created query
+     */
+  static fromJSON(className: string, json: QueryJSON): ParseQuery {
+    const query = new ParseQuery(className);
+    return query.withJSON(json);
+  }
+
   /**
    * Constructs a Parse.Object whose id is already known by fetching data from
    * the server.  Either options.success or options.error is called when the


Test patch:
diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js
index c33458f..61d3b6c 100644
--- a/src/__tests__/ParseQuery-test.js
+++ b/src/__tests__/ParseQuery-test.js
@@ -1555,4 +1555,33 @@ describe('ParseQuery', () => {
       done.fail(error);
     });
   });
+
+  it('restores queries from json representation', () => {
+    const q = new ParseQuery('Item');
+
+    q.include('manufacturer');
+    q.select('inStock', 'lastPurchase');
+    q.limit(10);
+    q.ascending(['a', 'b', 'c']);
+    q.skip(4);
+    q.equalTo('size', 'medium');
+
+    const json = q.toJSON();
+
+    const newQuery = ParseQuery.fromJSON('Item', json);
+
+    expect(newQuery.className).toBe('Item');
+
+    expect(newQuery.toJSON()).toEqual({
+      include: 'manufacturer',
+      keys: 'inStock,lastPurchase',
+      limit: 10,
+      order: 'a,b,c',
+      skip: 4,
+      where: {
+        size: 'medium'
+      }
+    });
+
+  });
 });