Skip to content

Instantly share code, notes, and snippets.

@marc-queiroz
Created February 17, 2021 19:10
Show Gist options
  • Save marc-queiroz/3b7f6764a876fb2a5aaa746ee157fb4b to your computer and use it in GitHub Desktop.
Save marc-queiroz/3b7f6764a876fb2a5aaa746ee157fb4b to your computer and use it in GitHub Desktop.
How to use Dexie from Chrome devtools. This snippet allow the user to query directly into IndexedDB database, very useful to developed or debug queries.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment