@@ -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 ;
0 commit comments