@@ -109,16 +109,13 @@ var Task = function(){
109
109
if ( ! isObject ( condition ) ) throw new Error ( "Invalid Condition" ) ;
110
110
if ( ! isObject ( data ) ) throw new Error ( "Invalid data" ) ;
111
111
112
- handle$Token ( condition ) ;
113
- handle$Token ( data ) ;
114
-
115
112
var updateObj = {
116
113
index : index
117
114
, type : UPDATE
118
115
, state : INITIAL
119
116
, name : getModelName ( model )
120
- , condition : condition
121
- , data : data
117
+ , condition : xcode ( condition )
118
+ , data : xcode ( data )
122
119
} ;
123
120
124
121
steps . push ( updateObj ) ;
@@ -148,14 +145,12 @@ var Task = function(){
148
145
if ( ! validModel ( model ) ) throw new Error ( "Invalid Model" ) ;
149
146
if ( ! isObject ( doc ) ) throw new Error ( "Invalid doc" ) ;
150
147
151
- handle$Token ( doc ) ;
152
-
153
148
var saveObj = {
154
149
index : index
155
150
, type : SAVE
156
151
, state : INITIAL
157
152
, name : getModelName ( model )
158
- , data : doc
153
+ , data : xcode ( doc )
159
154
} ;
160
155
161
156
steps . push ( saveObj ) ;
@@ -183,14 +178,12 @@ var Task = function(){
183
178
if ( ! validModel ( model ) ) throw new Error ( "Invalid Model" ) ;
184
179
if ( ! isObject ( condition ) ) throw new Error ( "Invalid Condition" ) ;
185
180
186
- handle$Token ( condition ) ;
187
-
188
181
var removeObj = {
189
182
index : index
190
183
, type : REMOVE
191
184
, state : INITIAL
192
185
, name : getModelName ( model )
193
- , condition : condition
186
+ , condition : xcode ( condition )
194
187
} ;
195
188
196
189
steps . push ( removeObj ) ;
@@ -231,10 +224,7 @@ var Task = function(){
231
224
*/
232
225
task . saveFile = function ( filePath , options ) {
233
226
if ( ! filePath ) throw new Error ( "File path is required and must be a string" ) ;
234
- if ( options ) {
235
- if ( ! isObject ( options ) ) throw new Error ( "options must be an object" ) ;
236
- handle$Token ( options ) ;
237
- }
227
+ if ( options && ! isObject ( options ) ) throw new Error ( "options must be an object" ) ;
238
228
239
229
var saveFileObj = {
240
230
type : FILE_SAVE
@@ -243,7 +233,7 @@ var Task = function(){
243
233
, data : {
244
234
file_path : filePath
245
235
}
246
- , options : options
236
+ , options : xcode ( options )
247
237
} ;
248
238
249
239
steps . push ( saveFileObj ) ;
@@ -263,13 +253,11 @@ var Task = function(){
263
253
task . removeFile = function ( options ) {
264
254
if ( ! isObject ( options ) ) throw new Error ( "options required. Must be an object" ) ;
265
255
266
- handle$Token ( options ) ;
267
-
268
256
var removeFileObj = {
269
257
type : FILE_REMOVE
270
258
, index : index
271
259
, state : INITIAL
272
- , options : options
260
+ , options : xcode ( options )
273
261
} ;
274
262
275
263
steps . push ( removeFileObj ) ;
@@ -349,27 +337,23 @@ function getResolveFunc(step){
349
337
*/
350
338
function performUpdate ( step , task , results ) {
351
339
var Collection = getModel ( step . name ) ;
340
+ var condition = xcode ( step . condition ) ;
341
+ var data = xcode ( step . data ) ;
352
342
353
- handle$Token ( step . condition , true ) ;
354
- handle$Token ( step . data , true ) ;
343
+ resolveFuture ( condition , results ) ;
344
+ resolveFuture ( data , results ) ;
355
345
356
- resolveFuture ( step . condition , results ) ;
357
- resolveFuture ( step . data , results ) ;
358
-
359
- // console.log(update);
360
- return storeOldData ( step )
361
- . then ( function ( ) {
362
- return updateState ( task , step . index , PENDING )
363
- . then ( function ( ) {
364
- return Collection . update ( step . condition , step . data , step . options )
365
- . exec ( )
366
- . then ( function ( result ) {
367
- results . push ( result ) ;
346
+ return storeOldData ( step , condition ) . then ( function ( ) {
347
+ return updateState ( task , step . index , PENDING ) . then ( function ( ) {
348
+ return Collection . update ( condition , data , step . options )
349
+ . exec ( )
350
+ . then ( function ( result ) {
351
+ results . push ( result ) ;
368
352
369
- return updateState ( task , step . index , DONE , results ) ;
370
- } ) ;
353
+ return updateState ( task , step . index , DONE , results ) ;
371
354
} ) ;
372
355
} ) ;
356
+ } ) ;
373
357
}
374
358
375
359
/**
@@ -383,11 +367,11 @@ function performUpdate(step, task, results) {
383
367
*/
384
368
function performSave ( step , task , results ) {
385
369
var Collection = getModel ( step . name ) ;
370
+ var data = xcode ( step . data ) ;
386
371
387
- handle$Token ( step . data , true ) ;
388
- resolveFuture ( step . data , results ) ;
372
+ resolveFuture ( data , results ) ;
389
373
390
- var doc = new Collection ( step . data ) ;
374
+ var doc = new Collection ( data ) ;
391
375
var dataStore = [ ] ;
392
376
393
377
doc . _id = doc . _id || utils . generateId ( ) ;
@@ -416,13 +400,13 @@ function performSave(step, task, results) {
416
400
*/
417
401
function performRemove ( step , task , results ) {
418
402
var Collection = getModel ( step . name ) ;
403
+ var condition = xcode ( step . condition ) ;
419
404
420
- handle$Token ( step . condition , true ) ;
421
- resolveFuture ( step . condition , results ) ;
405
+ resolveFuture ( condition , results ) ;
422
406
423
- return storeOldData ( step ) . then ( function ( ) {
407
+ return storeOldData ( step , condition ) . then ( function ( ) {
424
408
return updateState ( task , step . index , PENDING ) . then ( function ( ) {
425
- return Collection . remove ( step . condition ) . exec ( ) . then ( function ( result ) {
409
+ return Collection . remove ( condition ) . exec ( ) . then ( function ( result ) {
426
410
results . push ( result ) ;
427
411
428
412
return updateState ( task , step . index , DONE , results ) ;
@@ -441,12 +425,11 @@ function performRemove(step, task, results) {
441
425
* @returns {Promise|* }
442
426
*/
443
427
function performFileSave ( step , task , results ) {
444
- handle$Token ( step . options , true ) ;
445
- resolveFuture ( step . options , results ) ;
446
-
447
- var options = step . options || { } ;
428
+ var options = step . options ? xcode ( step . options ) : { } ;
448
429
var dataStore = [ ] ;
449
430
431
+ resolveFuture ( options , results ) ;
432
+
450
433
options . _id = options . _id || utils . generateId ( ) ;
451
434
dataStore . push ( { _id : options . _id } ) ;
452
435
@@ -488,10 +471,9 @@ function performFileSave(step, task, results) {
488
471
* @returns {Promise|* }
489
472
*/
490
473
function performFileRemove ( step , task , results ) {
491
- handle$Token ( step . options , true ) ;
492
- resolveFuture ( step . options , results ) ;
474
+ var options = xcode ( step . options ) ;
493
475
494
- var options = step . options ;
476
+ resolveFuture ( options , results ) ;
495
477
496
478
return storeOldFile ( step ) . then ( function ( file ) {
497
479
return updateState ( task , step . index , PENDING ) . then ( function ( ) {
@@ -527,8 +509,9 @@ function storeOldFile(step) {
527
509
return new Promise ( function ( resolve , reject ) {
528
510
var dataStore = [ ] ;
529
511
var gfs = Grid ( mongoose . connection . db ) ;
512
+ var options = xcode ( step . options ) ;
530
513
531
- gfs . findOne ( step . options , function ( err , file ) {
514
+ gfs . findOne ( options , function ( err , file ) {
532
515
if ( err ) return reject ( err ) ;
533
516
if ( ! file ) return resolve ( false ) ;
534
517
@@ -552,13 +535,16 @@ function storeOldFile(step) {
552
535
* changed by a step, for rollback purposes
553
536
*
554
537
* @param step the step
538
+ * @param condition literal obj rep of the step's condition
555
539
*
556
540
* @returns {Promise|* }
557
541
*/
558
- function storeOldData ( step ) {
542
+ function storeOldData ( step , condition ) {
559
543
var Collection = getModel ( step . name ) ;
560
- var options = step . options || step . type === REMOVE ? { multi : true } : null ;
561
- var query = Collection . find ( step . condition ) . lean ( ) ;
544
+ var options = step . options
545
+ ? xcode ( step . options )
546
+ : step . type === REMOVE ? { multi : true } : null ;
547
+ var query = Collection . find ( condition ) . lean ( ) ;
562
548
var searchQuery = options && options . multi === true
563
549
? query
564
550
: query . limit ( 1 ) ;
@@ -621,38 +607,15 @@ function getModelName(model){
621
607
}
622
608
623
609
/**
624
- * Handles the "$" token in queries because
625
- * MongoDB does not allow object keys to start with
626
- * "$"
610
+ * For encoding user provided conditions, options, and data as
611
+ * JSON strings for writes and decoding JSON strings to objects
612
+ * before operations.
627
613
*
628
- * @param obj the object in question
629
- * @param decode To decode or not to decode?
614
+ * @param item a JSON string or an object
615
+ * @returns object or JSON string
630
616
*/
631
- function handle$Token ( obj , decode ) {
632
- var ESCAPE_PREFIX = constants . ESCAPE_PREFIX ;
633
- var escapeLength = ESCAPE_PREFIX . length ;
634
- var keys = Object . keys ( obj ) ;
635
- var key ;
636
- var $key ;
637
-
638
- for ( var i = 0 ; i < keys . length ; i ++ ) {
639
- key = keys [ i ] ;
640
-
641
- if ( isObject ( obj [ key ] ) ) {
642
- handle$Token ( obj [ key ] , decode ) ;
643
- }
644
-
645
- if ( ! decode && key [ 0 ] === "$" ) {
646
- obj [ ESCAPE_PREFIX + key ] = obj [ key ] ;
647
- delete obj [ key ] ;
648
- }
649
-
650
- if ( decode && key . length >= escapeLength && key . substring ( 0 , escapeLength ) === ESCAPE_PREFIX ) {
651
- $key = key . replace ( ESCAPE_PREFIX , "" ) ;
652
- obj [ $key ] = obj [ key ] ;
653
- delete obj [ key ] ;
654
- }
655
- }
617
+ function xcode ( item ) {
618
+ return typeof item === "string" ? JSON . parse ( item ) : JSON . stringify ( item ) ;
656
619
}
657
620
658
621
/**
0 commit comments