www.damionmullins.com Damion Mullins, MS, MBA

IndexedDB Manager

Version 1.0, developed in 2021



IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs. The indexedDB Manager simplifies managing and maintaining indexedDB datastores in one JavaScript file. This utility includes functions for creating, reading, updating, and deleting indexedDB datastore records. It also reduces complexity in initializing indexedDB datastores and helps with version control. Additionally, this utility is written in JavaScript, with no reliances on any libraries or third-party technologies, making it easy to update and customize for your specific needs.

Example Workflow

1. Example indexedDB database configuration
                         
    "exampleOneDb": {
        "version": 1, //required version (int)
        "storeName": "objectStore", //required database name (string)
        "optionalParameters": {keyPath: "id", autoIncrement: true}, //can be {} or keypath name with options
        "indexes": [
            ["dateTime", "dateTime", {unique: true}],
            ["record", "record", {unique: false}]
        ]
    } 
                        
                    
2. Insert records in exampleOneDb
Save
                         
    indexedDbManager.saveRecord(
        "exampleOneDb", // Name of the database
        {record: exampleInputField, dateTime: indexedDbUtility.getDateTime()}, // Your record being saved
        indexedDbUtility.afterSave, // Callback function after save process
        {"msg": "Record saved!"}, // Optional parameters to pass in your callback function
        "System error, please try again" // Optional error msg, if the save process failed
    );
                        
                    
3. View all records in exampleOneDb
                         
    // getAllRecords returns all records in an array as the first param in your callback function

    indexedDbManager.getAllRecords(
        "exampleOneDb", // Name of the database
        "next", // Sort direction: next = asc, prev = desc
        indexedDbUtility.populateListbox, // Callback function after get all process
        {} // Optional parameters to pass in your callback function
    );
                        
                    
4. Count records in exampleOneDb
                        
    // countAllRecords returns the count as the first param in your callback function                            
                         
    indexedDbManager.countAllRecords(
        "exampleOneDb", // Name of the database 
        indexedDbUtility.afterGetCount, // Callback function after get count process
        {} // Optional parameters to pass in your callback function
    );
                        
                    
5. Get a record from exampleOneDb
Click on an option in the listbox to get the record:
                        
    // getRecordByKeypath returns your record in a cursor as the first param in your callback function
                        
    indexedDbManager.getRecordByKeypath(
        "exampleOneDb", // Name of the database  
        key,// Keypath key of the record
        indexedDbUtility.afterGetARecord, // Callback function after get record process
        {} // Optional parameters to pass in your callback function
    );
                        
                    
6. Update a record in exampleOneDb
                         
    // getRecordByKeypath returns your record in a cursor as the first param in your callback function
    // First, get the record being updated
                        
    indexedDbManager.getRecordByKeypath(
        "exampleOneDb", // Name of the database  
        key, // Keypath key of record 
        indexedDbUtility.afterGetRecord, // Callback function after getting a record 
        {newValue: value} // Parameter with updated value to be saved
    );

    // Second, update the record in the database

    indexedDbManager.updateRecord(
        "exampleOneDb", // Name of the database    
        cursor.value, // cursor's value, which is the old record's entire value e.g. {record: "old value", dateTime: "11/01/2020 10:30:05"} 
        newRec, // New record with updated value e.g. {record: "new value", dateTime: "11/01/2020 10:30:05"} 
        indexedDbUtility.afterUpdate, // Callback function after getting a record 
        {id: cursor.key} // Optional parameters to pass in your callback function
    );
                        
                    
7. Delete a record in exampleOneDb
                         
    indexedDbManager.deleteRecordByKey(
        "exampleOneDb", // Name of the database
        key, // Keypath key of record
        indexedDbUtility.afterDelete, // Callback function after delete process
        {id: "row_" + key} // Optional parameters to pass in your callback function
    );
                        
                    
8. Delete all records in exampleOneDb
Delete All
                         
    indexedDbManager.deleteAllRecords(
        "exampleOneDb", // Name of the database
        indexedDbUtility.afterAllDeleted, // Callback function after delete process
        {} // Optional parameters to pass in your callback function
    );
                        
                    
9. Destroy exampleOneDb
Destroy Database
                         
    indexedDbManager.destroyDatabase(
        "exampleOneDb", // Name of the database
        indexedDbUtility.afterDestroy, // Callback function after the destroy process
        {} // Optional parameters to pass in your callback function
    );
                        
                    
Additional Functions:
                         
    // Get the first record in the database 
    // returns your record in a cursor as the first param in your callback function                           
                        
    indexedDbManager.getFirstRecord(
        "exampleOneDb", // Name of the database
        indexedDbUtility.afterGetFirst, // Callback function
        {} // Optional parameters to pass in your callback function
    );
                        
                    
                         
    // Get the last record in the database
    // returns your record in a cursor as the first param in your callback function                            
                        
    indexedDbManager.getLastRecord(
        "exampleOneDb", // Name of the database
        indexedDbUtility.afterGetLast, // Callback function
        {} // Optional parameters to pass in your callback function
    );
                         
                    
                         
    // Get all records matching a keyword
    // returns your records in an array                           
                        
    indexedDbManager.getRecordsByWildcardSearch(
        "exampleOneDb", // Name of the database
        "Your keyword",
        "A keypath in the value",
        indexedDbUtility.afterSearchByWildcard, // Callback function
        {} // Optional parameters to pass in your callback function
    );
                         
                    
Download More Projects!