Skip to content

Commit b3a6a0c

Browse files
kyptovflovilmart
authored andcommitted
Parse.Query.fromJSON method (#448)
* Parse.Query.fromJSON method * Parse.Query.fromJSON to static, Parse.Query.withJSON method
1 parent 1b26adc commit b3a6a0c

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/ParseQuery.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,75 @@ export default class ParseQuery {
261261
return params;
262262
}
263263

264+
/**
265+
* Return a query with conditions from json, can be useful to send query from server side to client
266+
* Not static, all query conditions was set before calling this method will be deleted.
267+
* For example on the server side we have
268+
* var query = new Parse.Query("className");
269+
* query.equalTo(key: value);
270+
* query.limit(100);
271+
* ... (others queries)
272+
* Create JSON representation of Query Object
273+
* var jsonFromServer = query.fromJSON();
274+
*
275+
* On client side getting query:
276+
* var query = new Parse.Query("className");
277+
* query.fromJSON(jsonFromServer);
278+
*
279+
* and continue to query...
280+
* query.skip(100).find().then(...);
281+
* @method withJSON
282+
* @param {QueryJSON} json from Parse.Query.toJSON() method
283+
* @return {Parse.Query} Returns the query, so you can chain this call.
284+
*/
285+
withJSON(json: QueryJSON): ParseQuery {
286+
287+
if (json.where) {
288+
this._where = json.where;
289+
}
290+
291+
if (json.include) {
292+
this._include = json.include.split(",");
293+
}
294+
295+
if (json.keys) {
296+
this._select = json.keys.split(",");
297+
}
298+
299+
if (json.limit) {
300+
this._limit = json.limit;
301+
}
302+
303+
if (json.skip) {
304+
this._skip = json.skip;
305+
}
306+
307+
if (json.order) {
308+
this._order = json.order.split(",");
309+
}
310+
311+
for (let key in json) if (json.hasOwnProperty(key)) {
312+
if (["where", "include", "keys", "limit", "skip", "order"].indexOf(key) === -1) {
313+
this._extraOptions[key] = json[key];
314+
}
315+
}
316+
317+
return this;
318+
319+
}
320+
321+
/**
322+
* Static method to restore Parse.Query by json representation
323+
* Internally calling Parse.Query.withJSON
324+
* @param {String} className
325+
* @param {QueryJSON} json from Parse.Query.toJSON() method
326+
* @returns {Parse.Query} new created query
327+
*/
328+
static fromJSON(className: string, json: QueryJSON): ParseQuery {
329+
const query = new ParseQuery(className);
330+
return query.withJSON(json);
331+
}
332+
264333
/**
265334
* Constructs a Parse.Object whose id is already known by fetching data from
266335
* the server. Either options.success or options.error is called when the

src/__tests__/ParseQuery-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,4 +1555,33 @@ describe('ParseQuery', () => {
15551555
done.fail(error);
15561556
});
15571557
});
1558+
1559+
it('restores queries from json representation', () => {
1560+
const q = new ParseQuery('Item');
1561+
1562+
q.include('manufacturer');
1563+
q.select('inStock', 'lastPurchase');
1564+
q.limit(10);
1565+
q.ascending(['a', 'b', 'c']);
1566+
q.skip(4);
1567+
q.equalTo('size', 'medium');
1568+
1569+
const json = q.toJSON();
1570+
1571+
const newQuery = ParseQuery.fromJSON('Item', json);
1572+
1573+
expect(newQuery.className).toBe('Item');
1574+
1575+
expect(newQuery.toJSON()).toEqual({
1576+
include: 'manufacturer',
1577+
keys: 'inStock,lastPurchase',
1578+
limit: 10,
1579+
order: 'a,b,c',
1580+
skip: 4,
1581+
where: {
1582+
size: 'medium'
1583+
}
1584+
});
1585+
1586+
});
15581587
});

0 commit comments

Comments
 (0)