Skip to content

Commit efc91e0

Browse files
committed
feat(query): add .fromJSON and a new #toJSON
BREAKING CHANGE: The original #toJSON is renamed to #_getParams
1 parent d700c21 commit efc91e0

File tree

8 files changed

+99
-30
lines changed

8 files changed

+99
-30
lines changed

src/live-query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ module.exports = AV => {
127127
AV._config.realtime
128128
.createLiveQueryClient(subscriptionId)
129129
.then(liveQueryClient => {
130-
const { where, keys, returnACL } = query.toJSON();
130+
const { where, keys, returnACL } = query._getParams();
131131
const queryJSON = {
132132
where,
133133
keys,

src/object.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,11 @@ module.exports = function(AV) {
10421042
}
10431043

10441044
if (options.query) {
1045-
var queryJSON;
1046-
if (typeof options.query.toJSON === 'function') {
1047-
queryJSON = options.query.toJSON();
1048-
if (queryJSON) {
1049-
query.where = queryJSON.where;
1045+
var queryParams;
1046+
if (typeof options.query._getParams === 'function') {
1047+
queryParams = options.query._getParams();
1048+
if (queryParams) {
1049+
query.where = queryParams.where;
10501050
}
10511051
}
10521052
if (!query.where) {

src/push.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = function(AV) {
2727
*/
2828
AV.Push.send = function(data, options) {
2929
if (data.where) {
30-
data.where = data.where.toJSON().where;
30+
data.where = data.where._getParams().where;
3131
}
3232

3333
if (data.where && data.cql) {

src/query.js

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ module.exports = function(AV) {
7070
this._select = [];
7171
this._limit = -1; // negative limit means, do not send a limit
7272
this._skip = 0;
73-
this._extraOptions = {};
7473
};
7574

7675
/**
@@ -164,6 +163,39 @@ module.exports = function(AV) {
164163
});
165164
};
166165

166+
/**
167+
* Return a query with conditions from json.
168+
* This can be useful to send a query from server side to client side.
169+
* @since 4.0.0
170+
* @param {Object} json from {@link AV.Query#toJSON}
171+
* @return {AV.Query}
172+
*/
173+
AV.Query.fromJSON = ({
174+
className,
175+
where,
176+
include,
177+
select,
178+
includeACL,
179+
limit,
180+
skip,
181+
order,
182+
}) => {
183+
if (typeof className !== 'string') {
184+
throw new TypeError('Invalid Query JSON, className must be a String.');
185+
}
186+
const query = new AV.Query(className);
187+
_.extend(query, {
188+
_where: where,
189+
_include: include,
190+
_select: select,
191+
_includeACL: includeACL,
192+
_limit: limit,
193+
_skip: skip,
194+
_order: order,
195+
});
196+
return query;
197+
};
198+
167199
AV.Query._extend = AV._extend;
168200

169201
_.extend(
@@ -195,7 +227,7 @@ module.exports = function(AV) {
195227
var obj = this._newObject();
196228
obj.id = objectId;
197229

198-
var queryJSON = this.toJSON();
230+
var queryJSON = this._getParams();
199231
var fetchOptions = {};
200232

201233
if (queryJSON.keys) fetchOptions.keys = queryJSON.keys;
@@ -222,7 +254,30 @@ module.exports = function(AV) {
222254
* Returns a JSON representation of this query.
223255
* @return {Object}
224256
*/
225-
toJSON: function() {
257+
toJSON() {
258+
const {
259+
className,
260+
_where: where,
261+
_include: include,
262+
_select: select,
263+
_includeACL: includeACL,
264+
_limit: limit,
265+
_skip: skip,
266+
_order: order,
267+
} = this;
268+
return {
269+
className,
270+
where,
271+
include,
272+
select,
273+
includeACL,
274+
limit,
275+
skip,
276+
order,
277+
};
278+
},
279+
280+
_getParams: function() {
226281
var params = {
227282
where: this._where,
228283
};
@@ -246,10 +301,6 @@ module.exports = function(AV) {
246301
params.order = this._order;
247302
}
248303

249-
AV._objectEach(this._extraOptions, function(v, k) {
250-
params[k] = v;
251-
});
252-
253304
return params;
254305
},
255306

@@ -263,7 +314,7 @@ module.exports = function(AV) {
263314
return obj;
264315
},
265316
_createRequest(
266-
params = this.toJSON(),
317+
params = this._getParams(),
267318
options,
268319
path = `/classes/${this.className}`
269320
) {
@@ -348,7 +399,7 @@ module.exports = function(AV) {
348399
* });
349400
*/
350401
scan({ orderedBy, batchSize } = {}, authOptions) {
351-
const condition = this.toJSON();
402+
const condition = this._getParams();
352403
debug('scan %O', condition);
353404
if (condition.order) {
354405
console.warn(
@@ -434,7 +485,7 @@ module.exports = function(AV) {
434485
* the query completes.
435486
*/
436487
count: function(options) {
437-
var params = this.toJSON();
488+
var params = this._getParams();
438489
params.limit = 0;
439490
params.count = 1;
440491
var request = this._createRequest(params, options);
@@ -454,7 +505,7 @@ module.exports = function(AV) {
454505
first: function(options) {
455506
var self = this;
456507

457-
var params = this.toJSON();
508+
var params = this._getParams();
458509
params.limit = 1;
459510
var request = this._createRequest(params, options);
460511

@@ -691,7 +742,7 @@ module.exports = function(AV) {
691742
* @return {AV.Query} Returns the query, so you can chain this call.
692743
*/
693744
matchesQuery: function(key, query) {
694-
var queryJSON = query.toJSON();
745+
var queryJSON = query._getParams();
695746
queryJSON.className = query.className;
696747
this._addCondition(key, '$inQuery', queryJSON);
697748
return this;
@@ -706,7 +757,7 @@ module.exports = function(AV) {
706757
* @return {AV.Query} Returns the query, so you can chain this call.
707758
*/
708759
doesNotMatchQuery: function(key, query) {
709-
var queryJSON = query.toJSON();
760+
var queryJSON = query._getParams();
710761
queryJSON.className = query.className;
711762
this._addCondition(key, '$notInQuery', queryJSON);
712763
return this;
@@ -723,7 +774,7 @@ module.exports = function(AV) {
723774
* @return {AV.Query} Returns the query, so you can chain this call.
724775
*/
725776
matchesKeyInQuery: function(key, queryKey, query) {
726-
var queryJSON = query.toJSON();
777+
var queryJSON = query._getParams();
727778
queryJSON.className = query.className;
728779
this._addCondition(key, '$select', { key: queryKey, query: queryJSON });
729780
return this;
@@ -740,7 +791,7 @@ module.exports = function(AV) {
740791
* @return {AV.Query} Returns the query, so you can chain this call.
741792
*/
742793
doesNotMatchKeyInQuery: function(key, queryKey, query) {
743-
var queryJSON = query.toJSON();
794+
var queryJSON = query._getParams();
744795
queryJSON.className = query.className;
745796
this._addCondition(key, '$dontSelect', {
746797
key: queryKey,
@@ -757,7 +808,7 @@ module.exports = function(AV) {
757808
*/
758809
_orQuery: function(queries) {
759810
var queryJSON = _.map(queries, function(q) {
760-
return q.toJSON().where;
811+
return q._getParams().where;
761812
});
762813

763814
this._where.$or = queryJSON;
@@ -772,7 +823,7 @@ module.exports = function(AV) {
772823
*/
773824
_andQuery: function(queries) {
774825
var queryJSON = _.map(queries, function(q) {
775-
return q.toJSON().where;
826+
return q._getParams().where;
776827
});
777828

778829
this._where.$and = queryJSON;

src/search.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ module.exports = function(AV) {
124124
null,
125125
null,
126126
'GET',
127-
params || this.toJSON(),
127+
params || this._getParams(),
128128
options
129129
);
130130
},
@@ -257,8 +257,8 @@ module.exports = function(AV) {
257257
});
258258
},
259259

260-
toJSON: function() {
261-
var params = AV.SearchQuery.__super__.toJSON.call(this);
260+
_getParams: function() {
261+
var params = AV.SearchQuery.__super__._getParams.call(this);
262262
delete params.where;
263263
if (this.className) {
264264
params.clazz = this.className;

src/status.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = function(AV) {
109109

110110
return getUserPointer(options)
111111
.then(currUser => {
112-
var query = this.query.toJSON();
112+
var query = this.query._getParams();
113113
query.className = this.query.className;
114114
var data = {};
115115
data.query = query;
@@ -391,8 +391,8 @@ module.exports = function(AV) {
391391
this._inboxType = type;
392392
return this;
393393
},
394-
toJSON: function() {
395-
var params = AV.InboxQuery.__super__.toJSON.call(this);
394+
_getParams: function() {
395+
var params = AV.InboxQuery.__super__._getParams.call(this);
396396
params.owner = AV._encode(this._owner);
397397
params.inboxType = AV._encode(this._inboxType);
398398
params.sinceId = AV._encode(this._sinceId);

storage.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ export class Query<T extends Queriable> extends BaseQuery<T> {
531531
pvalues?: any,
532532
options?: AuthOptions
533533
): Promise<U>;
534+
static fromJSON<U extends Queriable>(json: object): Query<U>;
534535

535536
containedIn(key: string, values: any[]): this;
536537
contains(key: string, substring: string): this;

test/query.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ var query = new AV.Query(GameScore);
1010
describe('Queries', function() {
1111
setupPolly();
1212

13+
it('serialize and parse', () => {
14+
const json = new AV.Query(GameScore)
15+
.equalTo('a', 1)
16+
.lessThan('b', 2)
17+
.contains('c', 'c')
18+
.limit(1)
19+
.select('z')
20+
.select('y')
21+
.include('z')
22+
.includeACL(true)
23+
.addAscending('a')
24+
.addAscending('b')
25+
.toJSON();
26+
const newQuery = AV.Query.fromJSON(json);
27+
newQuery.toJSON().should.eql(json);
28+
});
29+
1330
describe('#Basic Queries', function() {
1431
it('should return Class Array', function() {
1532
query = new AV.Query(GameScore);

0 commit comments

Comments
 (0)