Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An empty database in everybody's pocket at NG-Poland

Phil Nash
November 22, 2016

An empty database in everybody's pocket at NG-Poland

IndexedDB is not new. It is supported in almost every browser and can power the most modern of client side applications. But there is not a lot of love for it and that needs to change.
We'll investigate why we should love IndexedDB and the kind of apps you can create when you have the power of a database in the browser. Then we'll dive into how to use IndexedDB with a look at both the terrifying API and the friendly libraries and Angular integrations that will help us along the way. Together we'll discover a new love for IndexedDB.

--

Links:

Angular2-indexedDB: https://github.com/gilf/angular2-indexeddb
Angular-indexedDB: https://github.com/bramski/angular-indexedDB

idb-keyval: https://github.com/jakearchibald/idb-keyval
localForage: https://github.com/localForage/localForage
idb: https://github.com/jakearchibald/idb
Dexie: http://dexie.org/
PouchDB: https://pouchdb.com/

Twilio Programmable Chat (from the demo): https://www.twilio.com/chat

Phil Nash

November 22, 2016
Tweet

More Decks by Phil Nash

Other Decks in Programming

Transcript

  1. Open the database let db; const request = indexedDB.open("messagesTest", 1);

    request.onerror = (err) => console.error(err); request.onupgradeneeded = (event) => { const db = event.target.result; db.createObjectStore("messages", { keyPath: "sid" }); }; request.onsuccess = (event) => { // do other stuff } 01. 02. 03. 04. 05. 06. 07. 08.
  2. Add messages to the database const transaction = db.transaction(["messages"], "readwrite");

    const objectStore = transaction.objectStore("messages"); const request = objectStore.put(message); request.onsuccess = (event) => console.log(event); 01. 02. 03. 04.
  3. Read messages from the database request.onsuccess = (event) => {

    db = event.target.result; const transaction = db.transaction(["messages"]); const objectStore = transaction.objectStore("messages"); objectStore.openCursor().onsuccess = (event) => { const cursor = event.target.result; if (cursor) { messageList.addMessage(cursor.value); cursor.continue(); } else { renderMessages(messageList); } }; }; 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14.
  4. Indexes request.onupgradeneeded = (event) => { const db = event.target.result;

    const objectStore = db.createObjectStore('messages', { keyPath: 'sid' }); objectStore.createIndex('dateUpdated', 'dateUpdated'); }; 01. 02. 03. 04. 05. 06. 07.
  5. Indexes const transaction = db.transaction(['messages']); const objectStore = transaction.objectStore('messages'); const

    index = objectStore.index('dateUpdated'); index.openCursor().onsuccess = (event) => { // do stuff } 01. 02. 03. 04. 05. 06.
  6. Upgrades request.onupgradeneeded = (event) => { const db = event.target.result;

    db.createObjectStore("messages", { keyPath: "sid" }); }; 01. 02. 03. 04.
  7. Upgrades request.onupgradeneeded = (event) => { const db = event.target.result;

    if (event.oldVersion < 1) { db.createObjectStore("messages", { keyPath: "sid" }); } if (event.oldVersion < 2) { db.createObjectStore("channels", { keyPath: "sid" }); } }; 01. 02. 03. 04. 05. 06. 07. 08. 09.
  8. angular2-indexedDB import {AngularIndexedDB} from 'angular2-indexeddb'; const db = new AngularIndexedDB('messageStore',

    1); db.createStore(1, (event) => { const objectStore = event.currentTarget.result; objectStore.createObjectStore('messages', { keyPath: "sid"}); }); 01. 02. 03. 04. 05. 06. 07. 08.
  9. angular2-indexedDB db.openCursor('messages', (event) => { const cursor = event.target.result; if(cursor)

    { // do something with cursor.value cursor.continue(); } else { // render } }); 01. 02. 03. 04. 05. 06. 07. 08. 09.