Created
February 17, 2021 19:10
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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