Skip to content

Instantly share code, notes, and snippets.

@JamesMessinger
Last active October 16, 2024 09:40
Show Gist options
  • Save JamesMessinger/a0d6389a5d0e3a24814b to your computer and use it in GitHub Desktop.
Save JamesMessinger/a0d6389a5d0e3a24814b to your computer and use it in GitHub Desktop.
Very Simple IndexedDB Example
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
// Open (or create) the database
var open = indexedDB.open("MyDatabase", 1);
// Create the schema
open.onupgradeneeded = function() {
var db = open.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
};
open.onsuccess = function() {
// Start a new transaction
var db = open.result;
var tx = db.transaction("MyObjectStore", "readwrite");
var store = tx.objectStore("MyObjectStore");
var index = store.index("NameIndex");
// Add some data
store.put({id: 12345, name: {first: "John", last: "Doe"}, age: 42});
store.put({id: 67890, name: {first: "Bob", last: "Smith"}, age: 35});
// Query the data
var getJohn = store.get(12345);
var getBob = index.get(["Smith", "Bob"]);
getJohn.onsuccess = function() {
console.log(getJohn.result.name.first); // => "John"
};
getBob.onsuccess = function() {
console.log(getBob.result.name.first); // => "Bob"
};
// Close the db when the transaction is done
tx.oncomplete = function() {
db.close();
};
}
@tundeiness
Copy link

hi I have a small challenge. can you please explain how to apply this to a situation where you are fetching data from an API? I mean the "add some data" part.

@thesparrow
Copy link

Thank you!! :D

@speir-wang
Copy link

Thanks✌️

@luxwarp
Copy link

luxwarp commented Jul 10, 2019

Amazing! Thnx! :D

@jvdi
Copy link

jvdi commented Aug 27, 2021

Thank you very much

@alanmtk
Copy link

alanmtk commented Oct 7, 2021

Awesome :)

@vladimir-kovalenko
Copy link

vladimir-kovalenko commented Jul 11, 2022

I have a comment, which is more of a question. You execute four operations against the store (two writes and two reads). The database is closed when the transaction is completed. From what I there is a high risk that the database will be closed before one of the operations is actually completed. Am I right or am I missing something?

@vslipchenko
Copy link

I have a comment, which is more of a question. You execute four operations against the store (two writes and two reads). The database is closed when the transaction is completed. From what I there is a high risk that the database will be closed before one of the operations is actually completed. Am I right or am I missing something?

This one could answer your question:
https://stackoverflow.com/questions/34915581/indexeddb-when-to-close-a-connection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment