Skip to content

adampoczatek/IDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 

Repository files navigation

IDB.js - indexedDB Wrapper

IDB.js is a simple wrapper for indexedDB API available in HTML5. The idea of this script is to help you organise your indexedDB connections and transactions, and to avoid code repetition.

Simple TODO Demo

Example

IndexedDB is asynchronous so most of the methods provide callbacks. To create/open your Database you need to call new IDB and provide 3 arguments: setup object, successHandler and errorHandler (optional). Here's an example:

var db;

new window.IDB({
    db: "Todo",
    version: 1,
    stores: [
        {
            name: "tasks",
            keyPath: "postDate",
            schema: {
                postDate: { unique: true },
                title: { unique: false },
                content: { unique: false },
                tags: { unique: false },
                due: { unique: false },
                users: { unique: false }
            }
        },
        {
            name: "tags",
            keyPath: "id",
            schema: {
                id: { unique: true },
                text: { unique: true },
                colour: { unique: false },
                priority: { unique: false }
            }
        }
    ]
}, function() {
    db = this;
    
    // Do more stuff...
});

API Reference

###.exportData() Back to top

Description: Export data.

.exportData(successHandler)

Parameter: successHandler

Type: function(data)

Description: A function to call when the request finishes. The function gets passed 1 parameter: data.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

###.getStoreInfo() Back to top

Description: Get information on a specific store.

.getStoreInfo(storeName, successHandler, errorHandler)

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.

==

###.importData() Back to top

Description: Import data.

.importData(importData, successHandler, errorHandler)

Parameter: importData

Type: Object

Description: A data object that will get inserted into the database.

==

Parameter: successHandler

Type: function(data)

Description: A function to call when the request finishes. The function gets passed 1 parameter: the Event.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.

==

###.insert()

Back to top

Description: Insert data into a specific store.

.insert(value, overwrite, storeName, successHandler, errorHandler)

Parameter: value

Type: Object / Array

Description: A data object (or an array of objects) that will get inserted into the database.

==

Parameter: overwrite

Type: Boolean

Description: If true, value will overwrite existing entry if already exists (only if the Store keyPath is not set to autoIncrement read more).

==

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.

==

###.openStore()

Back to top

Description: Open store and return IDBObjectStore.

.openStore(storeName, mode)

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: mode

Type: String

Description: Transaction mode: readwrite (default) and readonly read more.

==

###.query()

Back to top

Description: Query data from a specific store.

.query(keyRange, index, storeName, pageIndex, itemsPerPage, direction, successHandler, errorHandler)

Parameter: keyRange

Type: IDBKeyRange Read more

Description: A range of keys in a specific store read more. Set to null to query all keys.

==

Parameter: index

Type: String

Description: Query key range based on a specific index.

==

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: pageIndex

Type: Number

Description: Page index, works with itemsPerPage to help you setup a paging system.

==

Parameter: itemsPerPage

Type: Number

Description: Limit query to a specific number of items. If itemsPerPage is not set, query will return all records.

==

Parameter: direction

Type: String

Description: Cursor direction: next (default), nextunique, prev and prevunique read more.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.

==

###.queryMultipleKeys()

Back to top

Description: Query data from a specific store using an array of keys.

.queryMultipleKeys(keys, index, storeName, successHandler, errorHandler)

Parameter: keys

Type: Array

Description: An array of keys to query from a store.

==

Parameter: index

Type: String

Description: Query key range based on a specific index.

==

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.

==

###.remove()

Back to top

Description: Insert data into a specific store.

.remove(key, storeName, successHandler, errorHandler)

Parameter: key

Type: String|Number

Description: Key of an item in the store.

==

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.

==

###.update()

Back to top

Description: Insert data into a specific store.

.update(value, key, storeName, successHandler, errorHandler)

Parameter: value

Type: Object

Description: A data object that will get inserted into the database.

==

Parameter: key

Type: String|Number

Description: Key of an item in the store.

==

Parameter: storeName

Type: String

Description: Name of a Store in the Database.

==

Parameter: successHandler

Type: function(event, data) {}

Description: A function to call when the request finishes. The function gets passed 2 parameters: the Event and the Data.

==

Parameter: errorHandler

Type: function(event) {}

Description: A function to call when the request fails. The function gets passed 1 parameter: the Event.