Simplify same query on multiple database (collections) ?

Hello fellow coders,

I’m trying to simplify / refactor a query (the same) on multiple databases (collections).

At this moment, I’m using only 2 databases and my code is already very complex for a simple (same) query.

$w.onReady(function () {

let filtreFamille = $w("#dynamicDataset").getCurrentItem().filtreFamille;

    wixData.query("chaises")
      .contains("filtreFamille", filtreFamille)
      .eq("publier", true)
      .find()
      .then((results1) => {
 let results1Items = results1.items;
    wixData.query("lits")
          .contains("filtreFamille", filtreFamille)
          .find()
          .then((results2) => {
 let results2Items = results2.items;
 const allResults = [...results1Items, ...results2Items];
 $w(`#repeater1`).data = allResults;  
      $w(`#repeater1`).onItemReady(($w, itemData) => {
         $w(`#text9`).text = itemData.title;
            $w('#image1').src = itemData.image;
      });
      $w(`#repeater1`).show();
       $w(`#text11`).hide();
          })
      })
 })

I know that very soon I will have to make the same query in more than 20 databases and therefore I’m trying to find a solution to rewrite my code and make it clearer and more efficient.

I tried to use the Javascript Logical Operator “Or” || to simplify the code but it doesn’t seem to work.

$w.onReady(function () {

let filtreFamille = $w("#dynamicDataset").getCurrentItem().filtreFamille;
    wixData.query("chaises"|| "lits")
      .contains("filtreFamille", filtreFamille)
      .eq("publier", true)
      .find()
      .then((results) => {
 let resultsItems = results.items;
 $w(`#repeater1`).data = resultsItems;  
      $w(`#repeater1`).onItemReady(($w, itemData) => {
         $w(`#text9`).text = itemData.title;
            $w('#image1').src = itemData.image;
      });
      $w(`#repeater1`).show();
       $w(`#text11`).hide();
      })
 })

Is there a way to make the same query in multiple databases without copying the “wixData.query section” and concatenate the results ?

Thanks :grinning:

Try to use → RETURN and do something like this…

import wixData from 'wix-data';

const myDATABASES = ["chaises", "lits"]

$w.onReady(async function () {
    for (var i = 0; i < myDATABASES.length; i++) {
        let xxx = await myFunction(myDATABASES[i])
        console.log(xxx)
    }
});

async function myFunction(DATABASE) {       
    let filtreFamille = $w("#dynamicDataset").getCurrentItem().filtreFamille;
    await wixData.query(DATABASE)
    .contains("filtreFamille", filtreFamille)
    .eq("publier", true)
    .find()
    .then((results) => {
       let RESULT = results.items;
       return (RESULT)
    });
}
function query(collection){
return wixData.query(collection)
.contains("filtreFamille", filtreFamille)
      .eq("publier", true)
}
Promise.all([query("chaises").find(), query("lits").find()])
.then(r => {
 $w("#repeater1").data = r.map(e => e.items).flat();
});

This part is also new for me…

Promise.all([query("chaises").find(), query("lits").find()])

Where to find it in the API-dokumentation?

@russian-dima It’s pure javaScript. Not something that’s special for Velo.
So it’ll be documented here .
Basically you run several promises in parallel and once they’re resolved you receive an array of the results [res1, res1].
Since the queries here are independent there’s no point in waiting for one of them to get resolved before you run the other, so Promise.all() is ideal for this case.

@jonatandor35
Nice one. Thanks J.D.
I hope i can memorize it in my brain and use it in my future :grin:

@russian-dima You’re welcome. Promise.all() is a strong tool. There’s also Promise.race() that runs several promises in parallel and fulfills or rejects once one of them gets resolved or rejected. This is also useful in some cases.
Anyway when you use Promise.all() keep in mind that if there’re too many promises with too many results it might fail due to timeout, In that case chain the promises.

@jonatandor35 @russian-dima

Many thanks for your answers.

I’ll be using the last solution from @jonatandor35 as it’s even more compact than what I expected ^^
I learned Promise.all but never used it before. That’s a first for me and a good use case :wink: Thanks

The solution from @russian-dima was interesting too. I’ll keep it for another problem I might encounter. Thanks !:grinning:

You’re welcome :slight_smile: