2024 04 27 modern storage solutionindexeddb for structured data efficiency in browsers - bizkite-co/handterm GitHub Wiki


original file name: Codeium Chat - uyeaP7b9mBE0zlLEKTtSmz3t62tIOBAF.md summary: The user asked about using a more modern data storage solution with indexing capability in the browser instead of localStorage for storing structured data efficiently. I suggested using the IndexedDB API, which is a powerful client-side storage solution for handling structured data and performing efficient data operations. I provided an example of how to use IndexedDB to create a database, object store, index, add data, and query data. I emphasized that IndexedDB is well-suited for storing WPM stats per character and recommended considering fallback options for older browsers. date: 2024-04-27T22:14

Codeium Chat Conversation

Note: This is purely the output of the chat conversation and does not contain any raw data, codebase snippets, etc. used to generate the output.

User at 10:14 pm, Apr 27, 2024

I am using localStorage to store WPM stats per character for users who play my web-based cli-based typing game.

Is there a more modern data storage that allows indexing in the browser storage so I can more efficient story structured data in indexed records that I can loop through or list out or enumerate?


Codeium at 10:14 pm, Apr 27, 2024

Yes, a more modern approach to storing structured data in the browser that allows for indexing and efficient data operations is the IndexedDB API. IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs. It lets you create, read, navigate, and write to a user's local database. It is more powerful than localStorage and is suitable for applications that need to handle various types of data as objects and search through this data using indexes.

Here's a simple example of how to use IndexedDB:

// Open (or create) the database
var open = indexedDB.open('MyDatabase', 1);

// Create the schema
open.onupgradeneeded = function() {
    var db = open.result;
    var store = db.createObjectStore('MyObjectStore', {keyPath: 'id'});
    var index = store.createIndex('NameIndex', ['name.last', 'name.first']);
};

open.onsuccess = function() {
    // Start a new transaction
    var db = open.result;
    var tx = db.transaction('MyObjectStore', 'readwrite');
    var store = tx.objectStore('MyObjectStore');
    var index = store.index('NameIndex');

    // Add some data
    store.put({id: 12345, name: {first: 'John', last: 'Doe'}, age: 42});
    store.put({id: 67890, name: {first: 'Bob', last: 'Smith'}, age: 35});

    // Query the data
    var getJohn = store.get(12345);
    var getBob = index.get(['Smith', 'Bob']);

    getJohn.onsuccess = function() {
        console.log(getJohn.result.name.first);  // => "John"
    };

    getBob.onsuccess = function() {
        console.log(getBob.result.name.first);   // => "Bob"
    };

    // Close the db when the transaction is done
    tx.oncomplete = function() {
        db.close();
    };
};

This example shows how to open a database, create an object store, and an index within that store. It also demonstrates how to add data to the store and query it using both the store's key and the index.

IndexedDB is asynchronous and works well within the event-driven model of the web. It allows for significant amounts of data to be stored and efficiently retrieved through indexing, which would suit your use case of storing WPM stats per character. It's supported in all modern browsers, but you should always consider the need for a fallback or polyfill for older browsers if necessary.

⚠️ **GitHub.com Fallback** ⚠️