Skip to content

Instantly share code, notes, and snippets.

@diogomachado
Created August 16, 2017 14:52
Show Gist options
  • Save diogomachado/91f7aceff221f1e46f2009bc7e7b77a4 to your computer and use it in GitHub Desktop.
Save diogomachado/91f7aceff221f1e46f2009bc7e7b77a4 to your computer and use it in GitHub Desktop.
Livro - Exemplo indexedDB
// Cria um objeto
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
// Abre (ou cria) o banco
var open = indexedDB.open("Freeburguer", 1);
// Cria o esquema
open.onupgradeneeded = function() {
var db = open.result;
var store = db.createObjectStore("lanches", {keyPath: "id"});
var index = store.createIndex("NomeIndex", ["nome.last", "nome.first"]);
};
open.onsuccess = function() {
// Inicia a transação
var db = open.result;
var tx = db.transaction("lanches", "readwrite");
var store = tx.objectStore("lanches");
var index = store.index("NomeIndex");
// Adiciona conteudo
store.put({id: 10001, nome: {first: "Big", last: "Bob"}, preco: 13.50});
store.put({id: 10002, nome: {first: "Bigg", last: "Salada"}, preco: 22.90});
// Consulta item por id
var pegarItemPorID = store.get(10001);
pegarItemPorID.onsuccess = function() {
console.log(getJohn.result.nome.first);
};
// Consulta item por index
var pegarItemPorIndex = index.get(["Salada", "Bigg"]);
pegarItemPorIndex.onsuccess = function() {
console.log(getBob.result.nome.first);
};
// Fecha o banco quando terminar
tx.oncomplete = function() {
db.close();
};
}
@duduindo
Copy link

duduindo commented Feb 8, 2018

Este é um exemplo de qual livro? Livro Javascript Guia Definitivo ?

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