Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get GroupBy count in Dexie

I have an IndexedDB table that follows accepts following structured JSON as a row:

{
    id : 1,
    name : 'doc1',
    createdDate : '2018-08-08'
}

I want to get count for each available date in the table. ie: groupby:date then count. Expected example output is in the format of:

{
    '2018-08-08' : 5,
    '2018-08-18' : 19,
    ...
}

Table contains large number of records. So, how can efficiently achieve this requirement using Dexie?

like image 726
Nilanka Manoj Avatar asked Nov 28 '25 00:11

Nilanka Manoj


1 Answers

If you index createdDate,

const db = new Dexie("yourDB");
db.version(1).stores({
  yourTable: "id, name, createdDate"
});

Then you could do the following:

const result = {}
await db.yourTable.orderBy('createdDate').eachKey(date => {
  result[date] = (result[date] || 0) + 1;
});
like image 140
David Fahlander Avatar answered Nov 30 '25 12:11

David Fahlander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!