Skip to content

Instantly share code, notes, and snippets.

@RubaXa
Forked from Raynos/0example.js
Created April 24, 2012 06:13
Show Gist options
  • Select an option

  • Save RubaXa/2477008 to your computer and use it in GitHub Desktop.

Select an option

Save RubaXa/2477008 to your computer and use it in GitHub Desktop.

Revisions

  1. @Raynos Raynos revised this gist Mar 26, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions 0example.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // jsfiddle: http://jsfiddle.net/Pj7G4/1/
    var store = indexeddbStore("user")

    store.put({ name: "bob" }, "bob", function (err, result) {
  2. @Raynos Raynos created this gist Mar 26, 2012.
    7 changes: 7 additions & 0 deletions 0example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    var store = indexeddbStore("user")

    store.put({ name: "bob" }, "bob", function (err, result) {
    store.get("bob", function (err, user) {
    console.log(user.name === "bob")
    })
    })
    128 changes: 128 additions & 0 deletions 1indexeddb.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,128 @@
    (function () {

    var indexedDB = window.webkitIndexedDB || window.mozIndexedDB ||
    window.msIndexedDB || window.indexedDB,
    IDBObjectStore = (window.webkitIDBObjectStore || window.mozIDBObjectStore ||
    window.msIDBObjectStore || IDBObjectStore).prototype,
    IDBTransaction = (window.webkitIDBTransaction || window.mozIDBTransaction ||
    window.msIDBTransaction || IDBTransaction).prototype,
    cachedResults,
    callbackQueue

    function createDB(name, callback) {
    if (cachedResults) {
    return callback.apply(cachedResults[1], cachedResults)
    } else if (callbackQueue) {
    return callbackQueue.push(callback)
    }

    callbackQueue = [callback]

    var req = indexedDB.open(name || "DEFAULT")
    req.onupgradeneeded = function () {
    db.createObjectStore(storeName)
    }
    req.onsuccess = invokeCallbacks
    req.onerror = invokeCallbacks

    function invokeCallbacks(evt) {
    var callbackList = callbackQueue
    cachedResults = [evt.target.error, evt.target.result]
    callbackQueue = null
    for (var i = 0, len = callbackList.length; i < len; i++) {
    callbackList[i].apply(cachedResults[1], cachedResults)
    }
    }
    }

    function indexeddb(storeName, databaseName) {
    return getStore

    function getStore(callback) {
    createDB(databaseName, function (err, db) {
    if (!db.objectStoreNames.contains(storeName) &&
    db.setVersion
    ) {
    var req = db.setVersion("1.0")
    req.onerror = function () {
    callback(this.error)
    }
    req.onsuccess = function () {
    if (!db.objectStoreNames.contains(storeName)) {
    db.createObjectStore(storeName)
    }
    openStore()
    }
    } else {
    openStore()
    }

    function openStore() {
    var trans = db.transaction([storeName],
    IDBTransaction.READ_WRITE)
    callback(null, trans.objectStore(storeName))
    }
    })
    }
    }

    function store(storeName, databaseName) {
    return Object.create(Store).constructor(
    indexeddb(storeName, databaseName)
    )
    }

    var Store = {
    constructor: function (store) {
    store((function (err, store) {
    this._store = store
    }).bind(this))
    this.store = store
    return this
    }
    }

    function addToStore(methodName) {
    if (typeof IDBObjectStore[methodName] === "function" &&
    methodName !== "constructor"
    ) {
    Store[methodName] = tunnel(methodName)
    }
    }

    function tunnel(methodName) {
    return proxy

    function proxy() {
    var args = [].slice.call(arguments),
    callback = args.pop()

    this.store(invokeMethod)

    function invokeMethod(err, store) {
    if (err) {
    return (callback && callback(err))
    }
    var req = store[methodName].apply(store, args)
    req.onerror = function () {
    callback && callback(this.error)
    }
    req.onsuccess = function () {
    callback && callback(null, this.result)
    }
    }
    }
    }

    Object.keys(IDBObjectStore).forEach(addToStore)

    store.indexeddb = indexeddb
    store.Store = Store

    if (typeof module !== "undefined") {
    module.exports = store
    } else if (typeof window !== "undefined") {
    window.indexeddbStore = store
    }

    }())