for me as a javascript newbie this is quite hard to explain…
so I am giving my best and hope that you guys can follow…
I am searching for a way in javascript to dynamically build mongo-selectors.
In order to achieve this, I want to dynamically pass a “JSON configuration” as a parameter including a placeholder for the value.
For example dynamicallyBuildMyMongoSelector([surname: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}]) (WHICH does NOT work.)
Basically I want to pass a configuration-json to a function and then let it build a mongo-selector.
// Take JSON configuration
// ... fill theValuePlaceholder with real values
// return array containing real mongo-selectors (with real values)
dynamicallyBuildMyMongoSelector(configurationArray) {
var theQuery = [];
var exampleValue = 'example';
configurationArray.forEach(function(element, index) {
log(element.name);
// get selector and replace "theValuePlaceholder" with a "real" value
selectorWithValue = (element.selector, exampleValue); // HOW do I do this???
theQuery.push(selectorWithValue);
});
return theQuery;
}
// call function an pass selector layout as parameter
dynamicallyBuildMyMongoSelector(
[
// configure filters and set selector layout
{
name: 'filter-name',
selector: {
// PROBLEM: I am getting an error because "theValuePlaceholder" is NOT SET
// ... I DO NOT want to set it here but have it here as placeholder
$or: [
{surname: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
{forename: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
]
}
},
{
name: 'filter-country',
selector: {
country: theValuePlaceholder
}
}
]
);
I find it a bit strange to pass your “template” to the function, but not the variable part.
You could pass your “template” as a JSON string and do a replace on the string with your value, but that’s a bit hacky.
Could you please share your use case for doing this ?
Hi Vincent @vjau, thanks for your quick response! Right now I think that I’ll try to hack this using JSON-to-string and string-to-JSON, but I’m not sure if it’ll work.
This is the use-case: I am trying to build a configurable filter for aldeed:tabular using Blaze Components.
I`d like to pass the blaze component a dynamic list of input-fields with specific mongo-selectors via it’s constructor.
The Blaze component shall then read the user-input from the associated template
and put together the final mongo-db query to show the filtered data within aldeed:tabular.
So the componant has to read the value from the user input
and replace theValuePlaceholder with the input-value.
dynamicallyBuildMyMongoSelector(
[
// configure filters and set selector layout
{
name: 'filter-name',
selector: {
// PROBLEM: I am getting an error because "theValuePlaceholder" is NOT SET
// ... I DO NOT want to set it here but have it here as placeholder
$or: [
{surname: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
{forename: {$regex: '.*'+ theValuePlaceholder +'.*', $options: 'i'}},
]
}
},
{
name: 'filter-country',
selector: {
country: theValuePlaceholder
}
}
]
);