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 bracket support for db['collection'] #72

Open
wants to merge 2 commits 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
126 changes: 65 additions & 61 deletions dist/server/mongodb-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,84 +202,88 @@ function parseQuery(query, substitutions)
doc = {}
queryErrors = []

query = query.trim()
if (query.substring(0,3) != "db.")
{
queryErrors.push("Query must start with db.")
return null
}

// Query is of the form db.<collection>.aggregate or db.<collection>.find
// Split on the first ( after db.
var openBracketIndex = query.indexOf('(', 3)
if (openBracketIndex == -1)
{
queryErrors.push("Can't find opening bracket")
}
else
{
// Split the first bit - it's the collection name and operation ( must be aggregate )
var parts = query.substring(3, openBracketIndex).split('.')
// Collection names can have .s so last part is operation, rest is the collection name
if (parts.length >= 2)
try {
query = query.trim()
if (query.substring(0,3) != "db." && query.substring(0,3) != "db[")
{
doc.operation = parts.pop().trim()
doc.collection = parts.join('.')
queryErrors.push("Query must start with db.")
console.log("Query must start with db. or db[]")
return null
}
else
{
queryErrors.push("Invalid collection and operation syntax")
}

// Args is the rest up to the last bracket
var closeBracketIndex = query.indexOf(')', openBracketIndex)
if (closeBracketIndex == -1)

// Query is of the form db.<collection>.aggregate or db.<collection>.find
// Split on the first ( after db.
var openBracketIndex = query.indexOf('(', 3)
if (openBracketIndex == -1)
{
queryErrors.push("Can't find last bracket")
queryErrors.push("Can't find opening bracket")
}
else
{
var args = query.substring(openBracketIndex + 1, closeBracketIndex)
if ( doc.operation == 'aggregate')
// Split the first bit - it's the collection name and operation ( must be aggregate )
var parts = query.substring(3, openBracketIndex).split('.')
// Collection names can have .s so last part is operation, rest is the collection name
if (parts.length >= 2)
{
// Wrap args in array syntax so we can check for optional options arg
args = '[' + args + ']'
docs = JSON.parse(args)
// First Arg is pipeline
doc.pipeline = docs[0]
// If we have 2 top level args, second is agg options
if ( docs.length == 2 )
{
doc.agg_options = docs[1]
}
// Replace with substitutions
for ( var i = 0; i < doc.pipeline.length; i++)
{
var stage = doc.pipeline[i]
forIn(stage, function (obj, key, value)
{
if ( typeof(value) == "string" )
{
if ( value in substitutions )
{
obj[key] = substitutions[value]
}
}
})
}
doc.operation = parts.pop().trim()
doc.collection = parts.join('.').replace(/\'|\"|\[|\]/g, '')
}
else
{
queryErrors.push("Unknown operation " + doc.operation + ", only aggregate supported")
queryErrors.push("Invalid collection and operation syntax")
}

// Args is the rest up to the last bracket
var closeBracketIndex = query.indexOf(')', openBracketIndex)
if (closeBracketIndex == -1)
{
queryErrors.push("Can't find last bracket")
}
else
{
var args = query.substring(openBracketIndex + 1, closeBracketIndex)
if ( doc.operation == 'aggregate')
{
// Wrap args in array syntax so we can check for optional options arg
args = '[' + args + ']'
docs = JSON.parse(args)
// First Arg is pipeline
doc.pipeline = docs[0]
// If we have 2 top level args, second is agg options
if ( docs.length == 2 )
{
doc.agg_options = docs[1]
}
// Replace with substitutions
for ( var i = 0; i < doc.pipeline.length; i++)
{
var stage = doc.pipeline[i]
forIn(stage, function (obj, key, value)
{
if ( typeof(value) == "string" )
{
if ( value in substitutions )
{
obj[key] = substitutions[value]
}
}
})
}
}
else
{
queryErrors.push("Unknown operation " + doc.operation + ", only aggregate supported")
}
}
}
} catch (error) {
queryErrors.push(error.message)
}

if (queryErrors.length > 0 )
{
doc.err = new Error('Failed to parse query - ' + queryErrors.join(':'))
}

return doc
}

Expand Down
126 changes: 65 additions & 61 deletions server/mongodb-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,84 +202,88 @@ function parseQuery(query, substitutions)
doc = {}
queryErrors = []

query = query.trim()
if (query.substring(0,3) != "db.")
{
queryErrors.push("Query must start with db.")
return null
}

// Query is of the form db.<collection>.aggregate or db.<collection>.find
// Split on the first ( after db.
var openBracketIndex = query.indexOf('(', 3)
if (openBracketIndex == -1)
{
queryErrors.push("Can't find opening bracket")
}
else
{
// Split the first bit - it's the collection name and operation ( must be aggregate )
var parts = query.substring(3, openBracketIndex).split('.')
// Collection names can have .s so last part is operation, rest is the collection name
if (parts.length >= 2)
try {
query = query.trim()
if (query.substring(0,3) != "db." && query.substring(0,3) != "db[")
{
doc.operation = parts.pop().trim()
doc.collection = parts.join('.')
queryErrors.push("Query must start with db.")
console.log("Query must start with db. or db[]")
return null
}
else
{
queryErrors.push("Invalid collection and operation syntax")
}

// Args is the rest up to the last bracket
var closeBracketIndex = query.indexOf(')', openBracketIndex)
if (closeBracketIndex == -1)

// Query is of the form db.<collection>.aggregate or db.<collection>.find
// Split on the first ( after db.
var openBracketIndex = query.indexOf('(', 3)
if (openBracketIndex == -1)
{
queryErrors.push("Can't find last bracket")
queryErrors.push("Can't find opening bracket")
}
else
{
var args = query.substring(openBracketIndex + 1, closeBracketIndex)
if ( doc.operation == 'aggregate')
// Split the first bit - it's the collection name and operation ( must be aggregate )
var parts = query.substring(3, openBracketIndex).split('.')
// Collection names can have .s so last part is operation, rest is the collection name
if (parts.length >= 2)
{
// Wrap args in array syntax so we can check for optional options arg
args = '[' + args + ']'
docs = JSON.parse(args)
// First Arg is pipeline
doc.pipeline = docs[0]
// If we have 2 top level args, second is agg options
if ( docs.length == 2 )
{
doc.agg_options = docs[1]
}
// Replace with substitutions
for ( var i = 0; i < doc.pipeline.length; i++)
{
var stage = doc.pipeline[i]
forIn(stage, function (obj, key, value)
{
if ( typeof(value) == "string" )
{
if ( value in substitutions )
{
obj[key] = substitutions[value]
}
}
})
}
doc.operation = parts.pop().trim()
doc.collection = parts.join('.').replace(/\'|\"|\[|\]/g, '')
}
else
{
queryErrors.push("Unknown operation " + doc.operation + ", only aggregate supported")
queryErrors.push("Invalid collection and operation syntax")
}

// Args is the rest up to the last bracket
var closeBracketIndex = query.indexOf(')', openBracketIndex)
if (closeBracketIndex == -1)
{
queryErrors.push("Can't find last bracket")
}
else
{
var args = query.substring(openBracketIndex + 1, closeBracketIndex)
if ( doc.operation == 'aggregate')
{
// Wrap args in array syntax so we can check for optional options arg
args = '[' + args + ']'
docs = JSON.parse(args)
// First Arg is pipeline
doc.pipeline = docs[0]
// If we have 2 top level args, second is agg options
if ( docs.length == 2 )
{
doc.agg_options = docs[1]
}
// Replace with substitutions
for ( var i = 0; i < doc.pipeline.length; i++)
{
var stage = doc.pipeline[i]
forIn(stage, function (obj, key, value)
{
if ( typeof(value) == "string" )
{
if ( value in substitutions )
{
obj[key] = substitutions[value]
}
}
})
}
}
else
{
queryErrors.push("Unknown operation " + doc.operation + ", only aggregate supported")
}
}
}
} catch (error) {
queryErrors.push(error.message)
}

if (queryErrors.length > 0 )
{
doc.err = new Error('Failed to parse query - ' + queryErrors.join(':'))
}

return doc
}

Expand Down