Skip to content
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

Add ejson support for mongodb query #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion dist/server/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"server":
{
"port": 3333,
"host": "127.0.0.1",
"logRequests": false,
"logQueries": false,
"logTimings": false
}
}


24 changes: 16 additions & 8 deletions dist/server/mongodb-proxy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var parser = require('mongodb-query-parser');
var escapeStringRegexp = require('escape-string-regexp');
var express = require('express');
var bodyParser = require('body-parser');
var _ = require('lodash');
Expand Down Expand Up @@ -128,8 +130,8 @@ app.all('/query', function(req, res, next)
setCORSHeaders(res);

// Parse query string in target
substitutions = { "$from" : new Date(req.body.range.from),
"$to" : new Date(req.body.range.to),
substitutions = { "$from" : req.body.range.from,
"$to" : req.body.range.to,
"$dateBucketCount" : getBucketCount(req.body.range.from, req.body.range.to, req.body.intervalMs)
}

Expand Down Expand Up @@ -172,7 +174,7 @@ app.use(function(error, req, res, next)
// Get config from server/default.json
var serverConfig = config.get('server');

app.listen(serverConfig.port);
app.listen(serverConfig.port, serverConfig.host);

console.log("Server is listening on port " + serverConfig.port);

Expand Down Expand Up @@ -232,7 +234,7 @@ function parseQuery(query, substitutions)
}

// Args is the rest up to the last bracket
var closeBracketIndex = query.indexOf(')', openBracketIndex)
var closeBracketIndex = query.lastIndexOf(')')
if (closeBracketIndex == -1)
{
queryErrors.push("Can't find last bracket")
Expand All @@ -244,7 +246,13 @@ function parseQuery(query, substitutions)
{
// Wrap args in array syntax so we can check for optional options arg
args = '[' + args + ']'
docs = JSON.parse(args)
//docs = JSON.parse(args)
// Replace with substitutions
for (var key in substitutions) {
var regex = new RegExp(escapeStringRegexp(key), 'g')
args = args.replace(regex, substitutions[key])
}
docs = parser(args)
// First Arg is pipeline
doc.pipeline = docs[0]
// If we have 2 top level args, second is agg options
Expand All @@ -253,7 +261,7 @@ function parseQuery(query, substitutions)
doc.agg_options = docs[1]
}
// Replace with substitutions
for ( var i = 0; i < doc.pipeline.length; i++)
/*for ( var i = 0; i < doc.pipeline.length; i++)
{
var stage = doc.pipeline[i]
forIn(stage, function (obj, key, value)
Expand All @@ -266,7 +274,7 @@ function parseQuery(query, substitutions)
}
}
})
}
}*/
}
else
{
Expand Down Expand Up @@ -525,4 +533,4 @@ function getBucketCount(from, to, intervalMs)
}

return count
}
}
Loading