Odin Hørthe Omdal, Opera Software
Persistently store values and javascript objects in the browser.
Get them back as needed.
open
takes the database name and the version.
open("things")
open("things", 3)
var open_request = indexedDB.open("mydb")
open_request.onupgradeneeded = function() { // create }
open_request.onsuccess = function() { // opened }
var open_request = indexedDB.open("mydb")
open_request.onupgradeneeded = function() { // notrun }
open_request.onsuccess = function() { // opened }
I have this data I want to store:
Name | Gender | Since | Role |
---|---|---|---|
Princess Zelda | Female | 1986 | goodie |
Ganon | Male | 1986 | baddie |
Saria | Female | 1998 | goodie |
Navi | Female | 1998 | goodie |
zelda_people = [
{ name: 'Princess Zelda', gender: 'f',
since: 1986, baddie: false },
{ name: 'Ganon', gender: 'm',
since: 1986, baddie: true },
{ name: 'Saria', gender: 'f',
since: 1998, baddie: false },
{ name: 'Navi', gender: 'f',
since: 1998, baddie: false }
]
Two main ways to identify a record, in-line and out-of-line key:
add({ name: "Odin" })
add({ name: "Odin" }, "My Custom Key")
add("Odin", 1337)
keyPath
var obj = {
name: "Odin",
infoz: {
height: 172
}
}
keyPath | Key |
---|---|
"name" | "Odin" |
"infoz.height" | 172 |
"whatever" | undefined |
"" | Full object (invalid) |
var open_request = indexedDB.open("zelda")
open_request.onupgradeneeded = function() { // create }
open_request.onsuccess = function() { // opened }
open_request.onupgradeneeded = function(e) {
db = e.target.result
var s = db.createObjectStore("people",
{ keyPath: "name" })
s.createIndex("first_apperance", "since")
s.createIndex("sex", "gender")
}
keyPath
createObjectStore("people", { keyPath: "name" })
createIndex("first_apperance", "since")
var open_request = indexedDB.open("zelda")
open_request.onupgradeneeded = function() { // the code }
open_request.onsuccess = function() {
db = e.target.result
// Set data event handlers etc
}
button.onclick = function(e) {
var txn = db.transaction("people", "readwrite")
var store = txn.objectStore("people")
store.add(getValue())
}
button.onclick = function(e) {
var txn = db.transaction("people", "readwrite")
var store = txn.objectStore("people")
store.add(getValue())
}
<html lang="en-US">
<head>
<title>Shower</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="s/screen.css">
<script src="j/jquery.js"></script>
</head>
<html lang="en-US">
May or may not crash your browser
<meta charset="UTF-8">
Lock up your machine
This tool is provided without warranty, guarantee, or much in the way of explanation. Note that use of this tool may or may not crash your browser, lock up your machine, erase your hard drive.
Vadim Makeev, Opera Software
Shower: github.com/pepelsbey/shower