Skip to content

Instantly share code, notes, and snippets.

@MalucoMarinero
Created November 20, 2012 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MalucoMarinero/5d34635659a79014cec3 to your computer and use it in GitHub Desktop.
Save MalucoMarinero/5d34635659a79014cec3 to your computer and use it in GitHub Desktop.
IndexedDB Rough Library
'use strict'
root = window ? global
root.JohoDB = (db) ->
return {
db: db
store: (storeName) ->
return {
db: this.db
store: storeName
add: (obj, callback) ->
req = this.db.transaction(this.store, IDBTransaction.READ_WRITE)
.objectStore(this.store).add(obj)
req.onsuccess = (e) ->
callback req.result
deleteByID: (index, key, callback) ->
req = this.db.transaction(this.store, IDBTransaction.READ_WRITE)
.objectStore(this.store)
.index(index).openCursor(IDBKeyRange.only(key))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
ureq = cursor.delete()
ureq.onsuccess = (e) ->
callback true
else
callback null
get: (key, callback) ->
req = this.db.transaction(this.store).objectStore(this.store)
.get(key)
req.onsuccess = (e) ->
callback req.result
getAll: (callback) ->
results = []
req = this.db.transaction(this.store).objectStore(this.store)
.openCursor()
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
results.push cursor.value
cursor.continue()
else
callback results
updateObj: (index, key, obj, callback) ->
req = this.db.transaction(this.store, IDBTransaction.READ_WRITE)
.objectStore(this.store)
.index(index).openCursor(IDBKeyRange.only(key))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
ureq = cursor.update obj
ureq.onsuccess = (e) ->
callback obj
else
callback null
getIndexOrNull: (index, key, callback) ->
req = this.db.transaction(this.store).objectStore(this.store)
.index(index).openCursor(IDBKeyRange.only(key))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
callback req.result
else
callback null
}
complexQuery: (func, returnFunc) ->
queryManager =
db: this.db
openCalls: 0
result: null
store: (storeName) ->
return {
db: this.db
that: this
store: storeName
add: (obj, callback) ->
this.that.openCalls += 1
that = this.that
req = this.db.transaction(this.store, IDBTransaction.READ_WRITE)
.objectStore(this.store).add(obj)
req.onsuccess = (e) ->
callback req.result
that.openCalls -= 1
get: (key, callback) ->
this.that.openCalls += 1
that = this.that
req = this.db.transaction(this.store).objectStore(this.store)
.get(key)
req.onsuccess = (e) ->
callback req.result
that.openCalls -= 1
getAll: (callback) ->
this.that.openCalls += 1
that = this.that
results = []
req = this.db.transaction(this.store).objectStore(this.store)
.openCursor()
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
results.push cursor.value
cursor.continue()
else
callback results
that.openCalls -= 1
getIndexOrNull: (index, key, callback) ->
this.that.openCalls += 1
that = this.that
req = this.db.transaction(this.store).objectStore(this.store)
.index(index).openCursor(IDBKeyRange.only(key))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
callback req.result
that.openCalls -= 1
else
callback null
that.openCalls -= 1
deleteByID: (index, key, callback) ->
this.that.openCalls += 1
that = this.that
req = this.db.transaction(this.store, IDBTransaction.READ_WRITE)
.objectStore(this.store)
.index(index).openCursor(IDBKeyRange.only(key))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
that.openCalls += 1
ureq = cursor.delete()
ureq.onsuccess = (e) ->
callback true
that.openCalls -= 1
that.openCalls -= 1
else
callback null
that.openCalls -= 1
updateObj: (index, key, obj, callback) ->
this.that.openCalls += 1
that = this.that
req = this.db.transaction(this.store, IDBTransaction.READ_WRITE)
.objectStore(this.store)
.index(index).openCursor(IDBKeyRange.only(key))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
that.openCalls += 1
ureq = cursor.update obj
ureq.onsuccess = (e) ->
callback obj
that.openCalls -= 1
that.openCalls -= 1
else
callback null
that.openCalls -= 1
filterByIndex: (index, values, callback) ->
this.that.openCalls += 1
that = this.that
results = []
stillCollecting = values.length
for val in values
req = this.db.transaction(this.store)
.objectStore(this.store).index(index)
.openCursor(IDBKeyRange.only(val))
req.onsuccess = (e) ->
cursor = e.target.result
if cursor
results.push cursor.value
cursor.continue()
else
stillCollecting -= 1
req.onerror = (e) ->
stillCollecting -= 1
waitingOn = () ->
if stillCollecting == 0
callback results
that.openCalls -= 1
else
setTimeout waitingOn, 10
waitingOn()
}
if func
func queryManager
returnQuery = () ->
if queryManager.openCalls == 0
returnFunc queryManager.result
else
setTimeout returnQuery, 10
returnQuery()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment