Skip to content

compactIDB

Sai Raj edited this page May 26, 2023 · 1 revision

Compact IndexedDB operations

compactIDB operations can be used to perform basic IndexedDB operations such as add, read/write, modify and remove. These functions are asynchronous and return a promise. Contains the following operations.

NOTE: Compact IndexedDB operations have all been promisified. All output needs to be handled using .then and .catch

Example:

compactIDB.someFunction(parameters)
.then(result => process_resolve(result))
.catch(error => process_reject(error))

Default Database

Default database is always use if database name argument (dbName) is not passed to database operators

Setting default database

Default database can be set using either of the following:

compactIDB.setDefaultDB(dbName);

(or)

compactIDB.default = dbName;
  1. dbName (optional) - This is the name of default database to be used.
  • Note: this operation is neither promisified nor returns a value. It just sets the default DB

NOTE: Configured default database name can be returned using getter compactIDB.default. Example:

var defaultDB = compactIDB.default;

Initializing Database

compactIDB.initDB(dbName, objectStores = {})

Initializes new IndexedDB.

  1. dbName (optional) - Specifies database to be initialized.
  2. objectStores *(optional)() - Accepts an object containing various objectStores (or) array of objectStore names to be initiazed when creating an IDB.
  • Resolves: Status (string) | Rejects: error

Example:

compactIDB.initDB('sampleDB1', ['employee', 'student'])

compactIDB.initDB('sampleDB2', {
    employee: {}, 
    student: {}
})

compactIDB.initDB('sampleDB3', {
    'employee': {
        options: <objectStoreOptions>, 
        indexes: {
            name: {}, 
            age: {}, 
            address: <indexesOptions>
        }
    }
})

sampleDB1 and sampleDB2 are equivalent databases

Database operations

Opening a database

compactIDB.openDB(dbName)

Opens and resolves the database.

  1. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: database (IDB) | Rejects: error

Deleting a database

compactIDB.deleteDB(dbName)

Deletes the specified database.

  1. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: Status (string) | Rejects: error

Data operations

Write (insert or update) data

compactIDB.writeData(obsName, data, key, dbName)

Writes specified data into the database if data doesn't exists or updates it when data is already present.

  1. obsName - object store name in which the data is to be written.
  2. data - data that has to be written in specified object store.
  3. key (optional) - Primary key of the data (null or false can be used for objectstores with autoincrement keys) (DEFAULT: false)
  4. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: Status (string) | Rejects: error

Insert data

compactIDB.addData(obsName, data, key, dbName)

Adds a new data into object store. If data already exists, it will return an error.

  1. obsName - Object store name in which has to be stored.
  2. data - The data which has to be added to obeject store.
  3. key (optional) - Primary key of the data (null or false can be used for objectstores with autoincrement keys) (DEFAULT: false)
  4. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: Status (string) | Rejects: error

Remove data

compactDB.removeData(obsName, key, dbName)

Deletes data from specified object store using primary key.

  1. obsName - Name of object store from which the data has to be removed.
  2. key - Primary key of the data.
  3. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: Status (string) | Rejects: error

Clear all data

compactDB.clearData(obsName, dbName)

Clears all data in the objectStore.

  1. obsName - Name of object store from which the data has to be removed.
  2. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: Status (string) | Rejects: error

Read single data

compactDB.readData(obsName, key, dbName)

Reads the data from object store associated with specified key.

  1. obsName - Name of object store from which the data has to be retrieved.
  2. key - Primary key of the data to read.
  3. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: data (Object) | Rejects: error

Read all data

compactDB.readAllData(obsName, dbName)

Reads all the data from specified object store using IndexedDB openCursor method.

  1. obsName - Name of object store from which the data has to be retrieved.
  2. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: data (Object) | Rejects: error

Search data

compactIDB.searchData(obsName, options, dbName)

Search and resolve data that satisfy the conditions.

  1. obsName - Name of object store from which the data has to be retrieved.
  2. options (optional) - Accepts an object of options. Available options:
    • lowerKey: records after given key (inclusive)
    • upperKey: records before given key (inclusive)
    • atKey: record at given key
    • limit: max number of records to resolve
    • lastOnly: resolve only the last record matching the search
    • reverse: search and resolve the records in reverse order
    • patternEval: a function to check record key and value for match. Example: (key, value) => value > 10 will resolve all records with value greater than 10.
  3. dbName (optional) - Name of the database (uses defaultDB if not specified)
  • Resolves: data (Object) | Rejects: error