function dynamicallyLoadScript(url) { var script = document.createElement("script"); // create a script DOM node script.src = url; // set its src to the provided URL document.head.appendChild(script); // add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead) } dynamicallyLoadScript('https://npmcdn.com/dexie/dist/dexie.js') let database = new Dexie('database') database.version(26) .stores({ login: '', markets: null, groupsCatComp: '[category+competitionSlug], category, competitionSlug', groups: '[category+competitionSlug+nameSlug]', orders: 'id', statements: 'id', hotCompetitions: '++' }) .upgrade((transaction) => { transaction.db.tables.forEach((table) => table.clear()) }) database.open().catch(function(e) { console.error('Could not open database:', e) }) async function quantity(db) { // count category by competition const result = {} await db.groupsCatComp .orderBy('[category+competitionSlug]') .each((item, cursor) => { result[cursor.primaryKey[0]] = // This works because ~~ will turn many non-number things into 0 after coercing to Number. ~~result[cursor.primaryKey[0]] + 1 result[cursor.primaryKey[1]] = item.groups.size }) console.log('quantity', result) } quantity(database)