Hi,
Let’s say I have a CMS collection of users and a CMS collection of projects.
How can I display eg.: “We have x amount of users and x amount of projects!” ?
Without having to update “x” myself all the time, what code / api should I use to have the page automatically update “x” ?
All the best,
X
You can use two options here:
Dataset
WixData Query
// For Dataset
$w("#dataser1").onReady(() => {
const totalCount = $w("#dataser1").getTotalCount();
$w("#text1").text = `Total ${totalCount} items are present`
)
1 Like
It depends.
You can have a simple database with 2 columns. User count, project count. Each time you update that number you can have it display on a text element on your page(s) via code.
Or where does the user count come from? Are these people registered as Members? If yes, you can do a hook, after signup update the count on a database and display with text via code on the appropriate page.
And where does the project count come from? Are they the number of total items in a specific database? If yes, again use a hook to update count each time a new item is added, etc, same logic as above.
I would, personally, avoid doing a live count via querying because as well all know Wix limits reaults to 1000. So it would be inefficient to loop a code around when all the calculations could have been done beforehand and a simple page code can help display the static numbers from a database. It will also prevent unnecessary code from slowing down your page(s).
1 Like
@codequeen As it will be over 1000 at some point I’ll probably go with your method.
I’m new to hooks, how would it work?
And thanks a lot
X
Thanks @Brian_Lewis — will this method be limited to 1000 items like @codequeen mentions?
X
No, the number will be lower. It will be limited to the number of items loaded on the dataset after the page loads only.
Query limits: https://www.wix.com/velo/reference/wix-data/wixdataquery/count
Here is the onMemberCreated event: onMemberCreated - Velo API Reference - Wix.com
Here is the data hooks: https://www.wix.com/velo/reference/wix-data/hooks
1 Like
To get the whole data (data-count) of a database…
Versio-1: hasNext-mode:
import wixData from 'wix-data'
$w.onReady(async()=>{
try{
const res = await wixData.query("MyDB").find();
let itemsTotal = res.items;
if(res.hasNext()){
while(res.hasNext()){
res = await res.next();
itemsTotal.concat(res.items);
}
}
console.log(itemsTotal);
}catch(err){console.error('Error: ', err);}
});
Versio-2: promiseAll-mode:
return Promise.all([
query('collection').limit(1000).find(),
query('collection').limit(1000).skip(1000).find(),
query('collection').limit(1000).skip(2000).find(),
query('collection').limit(1000).skip(3000).find(),
]).then((res) => {
const cache = { items: [] }
for (const result of res) {
cache.items = cache.items.concat(result.items);
}
})
An example to find ALL paid orders…
return wixData.query("Stores/Orders")
.limit(1000)
.eq("paymentStatus", "PAID")
.find(options)
.then(async (ordersData) => {
let totalPage = ordersData.totalPages;
let array = [];
let skip = 0;
for (let i = 0; i < totalPage; i++) {
array.push(wixData.query("Stores/Orders").limit(1000).eq("paymentStatus", "PAID").skip(skip).find(options))
skip = skip + 1000;
}
return Promise.all(array)
.then((res) => {
const cache = { items: [] }
for (const result of res) {
cache.items = cache.items.concat(result.items);
}
return cache;
})
})
Another method…
import wixData from 'wix-data';
$w.onReady(function () {
let r1;
wixData.query("CollectionName").limit(1000).find()
.then(r => {
r1 = r.items;
return r.totalCount;
})
.then(r => {
let numberOfQueries = Math.ceil(r/1000);
let queries = [];
for(let i = 1; i < numberOfQueries; i++){ queries.push(wixData.query("CollectionName").limit(1000).skip(i * 1000));
}
return Promise.all(queries.map(e => e.find()));
})
.then(r => {
r = r.map(e => e.items);
r.unshift(r1);
r = r.flat();
console.log(r.length);
//r is the results, you can use it.
}).catch(err => err);
});
And another one…
Hello, I had this problem for a while so I figured I’d share the solution. Query recursively increasing the index by 1000 and logging the results each time.
async function getAllItems ( db , index ) {
return wixData . query ( db )
. limit ( 1000 )
. skip ( index )
. find ()
. then ( async ( res ) => {
allItems . push ( res.items )
if ( res.items.length === 1000 ) {
index += 1000 ;
return await getAllItems ( db , index )
}
else {
return allItems ;
}
})
}
Then use it…
Surely more can be found.
The max limit is always → 1000 , exept DATABASES like for sure PRODUCTS and maybe BLOG-POSTS → LIMIT = 100 as i can remember.
1 Like
Putting this as the solution for now, and checking back later when I’m done with other to-do’s.
Thanks a lot, as always, russian-dima !
X X
1 Like