Skip to content

Parse.Query.fromJSON method #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
});

});
});