Skip to content

Commit 2b3fcc2

Browse files
committed
Change forIn to handle deep substitutions
Change forIn to handle deep substitutions to allow for changing of date values inside of a $date object
1 parent fc00bf1 commit 2b3fcc2

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

dist/server/mongodb-proxy.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,21 @@ function forIn(obj, processFunc)
188188
var key;
189189
for (key in obj)
190190
{
191-
var value = obj[key]
192-
processFunc(obj, key, value)
193-
if ( value != null && typeof(value) == "object")
191+
obj[key] = processFunc(obj, key, obj[key])
192+
if ( obj[key] != null && typeof(obj[key]) == "object")
194193
{
195-
forIn(value, processFunc)
194+
obj[key] = forIn(obj[key], processFunc)
196195
}
197196
}
197+
198+
if (
199+
!Array.isArray(obj) && Object.keys(obj).length === 1 &&
200+
typeof obj['$date'] !== 'undefined'
201+
) {
202+
return new Date(obj['$date']);
203+
}
204+
205+
return obj;
198206
}
199207

200208
function parseQuery(query, substitutions)
@@ -256,15 +264,17 @@ function parseQuery(query, substitutions)
256264
for ( var i = 0; i < doc.pipeline.length; i++)
257265
{
258266
var stage = doc.pipeline[i]
259-
forIn(stage, function (obj, key, value)
267+
stage = forIn(stage, function (obj, key, value)
260268
{
261269
if ( typeof(value) == "string" )
262270
{
263271
if ( value in substitutions )
264272
{
265-
obj[key] = substitutions[value]
273+
return substitutions[value]
266274
}
267275
}
276+
277+
return value;
268278
})
269279
}
270280
}
@@ -525,4 +535,4 @@ function getBucketCount(from, to, intervalMs)
525535
}
526536

527537
return count
528-
}
538+
}

server/mongodb-proxy.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,21 @@ function forIn(obj, processFunc)
188188
var key;
189189
for (key in obj)
190190
{
191-
var value = obj[key]
192-
processFunc(obj, key, value)
193-
if ( value != null && typeof(value) == "object")
191+
obj[key] = processFunc(obj, key, obj[key])
192+
if ( obj[key] != null && typeof(obj[key]) == "object")
194193
{
195-
forIn(value, processFunc)
194+
obj[key] = forIn(obj[key], processFunc)
196195
}
197196
}
197+
198+
if (
199+
!Array.isArray(obj) && Object.keys(obj).length === 1 &&
200+
typeof obj['$date'] !== 'undefined'
201+
) {
202+
return new Date(obj['$date']);
203+
}
204+
205+
return obj;
198206
}
199207

200208
function parseQuery(query, substitutions)
@@ -256,15 +264,17 @@ function parseQuery(query, substitutions)
256264
for ( var i = 0; i < doc.pipeline.length; i++)
257265
{
258266
var stage = doc.pipeline[i]
259-
forIn(stage, function (obj, key, value)
267+
stage = forIn(stage, function (obj, key, value)
260268
{
261269
if ( typeof(value) == "string" )
262270
{
263271
if ( value in substitutions )
264272
{
265-
obj[key] = substitutions[value]
273+
return substitutions[value]
266274
}
267275
}
276+
277+
return value;
268278
})
269279
}
270280
}
@@ -525,4 +535,4 @@ function getBucketCount(from, to, intervalMs)
525535
}
526536

527537
return count
528-
}
538+
}

0 commit comments

Comments
 (0)