How many calls does this function consume?

We have a cap on the total number of wix-data calls a site can make per minute, right?

Suppose I have a database of ~50,000 products. How many calls does the following function consume?

async function checkForProduct(stockCode) {
 let result = await wixData.query('products').eq('stockCode', stockCode).limit(1000).find(options);
 let allItems = result.items;
 while (result.hasNext()) {
        result = await result.next();
        allItems = allItems.concat(result.items);
    }
 return allItems;
}

Lets assume that there are only 10 products with the queried stockCode - The above number of calls should be 2, right? 1 query + 1 hasNext() function

Now how many calls will the below function consume?

async function checkForProduct(stockCode) {
 let result = await wixData.query('products').limit(1000).find(options);
 let allItems = result.items;
 while (result.hasNext()) {
        result = await result.next();
        allItems = allItems.concat(result.items);
    }
 return allItems;
}

The answer to the above should be 100, right? because the database has 50,000 products so 50 queries and 50 hasNext() functions.

Another question, without the .eq() will the 50 consecutive queries even be possible? I tried but it never console.logs the 50,000 items array when queried on a backend file.

Hi, Shan,

to answer your questions:

  1. 1 DB call, today hasNext() is free.
  2. 50 as hasNext() is free, so only initial find() and next() calls.

Another question, without the .eq() will the 50 consecutive queries even be possible?

It depends on how big a collection is. In a really large collection it may be impossible to get to the last page as the query will timeout before being able to be executed.

It is also possible that the application does not have enough memory to keep all of the records in an array.

Overall, it’s best to not store the results for each batch in memory and attempt to do any processing per batch. Also it’s best to limit those batches, say by adding constraints on _createdDate , so that each processing is a separate call. Say, one call does all elements 2020-02, next 2020-03, etc.