Last active
December 15, 2015 18:58
-
-
Save leepfrog/5307321 to your computer and use it in GitHub Desktop.
IndexDB Notes / sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| indexdb notes | |
| dbName = 'someName' | |
| # obtain non-vendored versions of these | |
| # Vars used to open / work with our db instance | |
| window.db = null | |
| request = indexedDB.open(dbName) | |
| # request = indexedDB.open('testdb', versionNum) | |
| # These should typically be handled and implemented per request since everything in indexeddb is async | |
| # request.onerror = (e) -> # do event stuff | |
| # request.onsuccess = (e) -> # do event stuff | |
| # | |
| # A common error is VER_ERR which means DB on disk is later than the verison specified in open | |
| request.onerror = (evt) -> console.error 'dberror: #{evt.target.errorCode}' | |
| request.onsuccess = (evt) -> | |
| db = request.result | |
| # Error events bubble up, so if we set this on our db object, we will have this fire if we have a | |
| # request error -> transaction error -> db error | |
| db.onerror = (e) -> console.error 'dberror: #{evt.target.errorCode}' | |
| # Occurs when a newer version of the db schema is required (new indices / object stores / etc) | |
| # this is specific to the open db request | |
| request.onupgradeneeded = (evt) -> | |
| # Creating an object store (entity) | |
| objectStore = db.createObjectStore('users', { keyPath: 'id' }) | |
| # Creating an index (used for searching) | |
| # indexName, keyPath, options IDBIndexParameters | |
| objectStore.createIndex('name', 'name', { unique: false }) | |
| objectStore.createIndex('email', 'email', { unique: true }) | |
| # Seeding data into the store | |
| # for person in peopleData | |
| # objectStore.add(person) | |
| # Transaction example | |
| window.addUser = (user) -> | |
| # Name of the object store, type of transaction | |
| # <[objectStoreName]>, [<readonly|readwrite|versionchange>], | |
| transaction = db.transaction(['users'], 'readwrite') | |
| # Possible callbacks | |
| # transaction.onerror = (e) -> this will also bubble up to db.onerror, use preventDefault() to stop propagation/rolling back transaction | |
| # transaction.oncomplete = (e) -> | |
| # transaction.onabort = (e) -> fired when transaction is rolled back | |
| # To add data, select the object store from the transaction | |
| objectStore = transaction.objectStore('users') | |
| # Add the data | |
| request = objectStore.add(user) | |
| # Update the data | |
| # request = objectStore.put(user) | |
| # Remove the data | |
| # request = objectStore.delete(<keyValue>) | |
| # Register callbacks | |
| # request.onsuccess = (e) -> do somethin! | |
| # Getting a user - chained example | |
| # db.transaction('users').objectStore('users').get(1).onsuccess = (e) -> console.log e.toString() | |
| # Getting many users | |
| # objectStore = db.transaction('customers').objectStore('customers') | |
| # Simple example - findAll | |
| # objectStore.openCursor().onsuccess = (e) -> | |
| # # Get our cursor | |
| # cursor = e.target.result | |
| # if cursor | |
| # console.log "Current record is: #{cursor}" | |
| # # move to the next record | |
| # cursor.continue() | |
| # else | |
| # console.log 'no more entries' | |
| ### | |
| # Searching by index examples | |
| ### | |
| # Obtain first object for a search | |
| # index = objectStore.index('name') | |
| # index.get('Donna').onsuccess = (e) -> console.log "This is the first donna we have: #{e.target.result}" | |
| # Obtain all matches objects for a search | |
| # index.openCursor().onsuccess = (e) -> | |
| # Get cursor | |
| # cursor = e.target.result | |
| # if cursor | |
| # console.log "key is: #{cursor.key} whole object is: #{cursor.value}" | |
| # cursor.continue() | |
| # Only obtain the keys for a search | |
| # index.openKeyCursor().onsuccess = (e) -> | |
| # Get cursor | |
| # cursor = e.target.result | |
| # if cursor | |
| # console.log "key is: #{cursor.key} search value is: #{cursor.value}" | |
| # cursor.continue() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for the record, this code doesn't actually work or do anything (scoping issues,etc..) but is a reference for how to do some things.