Hello, it's not often that i write here, but it seems i have a problem, which makes me crazy!
What i am talking about?
I am trying to generate a specific code to get UNIQUE-DATA out of DATABASE using Wix-Data.
It works!
async function create_UniqueDropdown(items, DBFIELDS, dropdown) {console.log(items)
const uniqueTitles = await getUniqueTitles(items); console.log("Unique-Titles: ", uniqueTitles);
$w(`#${dropdown}`).options = buildOptions(uniqueTitles); //console.log("OPTIONS: ", buildOptions(uniqueTitles));
async function getUniqueTitles(items) {
let titlesOnly = await items.map(item => item[DBFIELDS]); //console.log("Titles-Only: ", titlesOnly);
return [...new Set(titlesOnly)];
}
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
return {label:curr, value:curr};
});
}
}
But my aim is to modify it the way, that the user can choose the exact DB-field to be unified and get all results of the selected field(s).
At least DBFIELDS must be an ARRAY, or even an OBJECT.
The shown code runs good for just one DB-Field, but would quickly get very slow when working for several DB-fields.
So my idea was to use promiseAll(), but this is exactly my issue, no matter what i tried, i could not get a satisfying result.
Maybe one of you have a good idea?
Aim-1: --> eleminating all --> asynchronity (async-await)
Aim2: ---> modifiying this function into a multi-DB-FIELD one.
So the Input for DBFIELDS would be an ARRAY for example...
DBFIELDS = ["dbField1", "dbField2", "dbField3", ......]
And as result would be expected all UNIQUE-ITEMS for each of the DB-FIELDS --> compressed into an ARRAY or Object.
Before I answer, do you need the unicity to be on all fields combined or unique within each field? In other words is an item unique if valueField1 or valueField2 is unique or if the combination of valueField1+valueField2 is unique? If it's the latter you can do something like
function getUniqueItemsForField(items, fields){ const itemsByKey = {} items.map(item => { const key = fields.reduce((compoundKey, field) => compoundKey+field+":"+item[field]+"-", ""); if(!itemsByKey[key]) itemsByKey[key] = item }) return Object.values(itemsByKey) }
For the first question, yes - I was thinking perhaps one query then allowing users to filter rather than trigger a new query each time would be faster.
For indexes, I'm workign on a video for that now but it will be based on this session that Justas created. Just after 18min he talks about indexes but you may find the entire session interesting
Support Article on Data Indexing
Also, something else to try potentially that could speed up your queries is adding a few index values to commonly searched columns, still thinking. Let me know what you have come up with
My first thought is not running a query each time for this and rather once you pull the data, allowing the user to filter on the data you have already queried during their session. Would something like that potentially work for your use case?