3
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
* Code distributed by Google as part of the polymer project is also
8
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11
// From https://gist.github.com/inexorabletash/c8069c042b734519680c (Joshua Bell)
14
var SECRET = Object.create(null);
15
var DB_PREFIX = '$SimpleDB$';
18
// Chrome iOS has window.indexeDB, but it is null.
19
if (!(global.indexedDB && indexedDB.open)) {
23
function SimpleDBFactory(secret) {
24
if (secret !== SECRET) throw TypeError('Invalid constructor');
26
SimpleDBFactory.prototype = {
27
open: function(name) {
28
return new Promise(function(resolve, reject) {
29
var request = indexedDB.open(DB_PREFIX + name);
30
request.onupgradeneeded = function() {
31
var db = request.result;
32
db.createObjectStore(STORE);
34
request.onsuccess = function() {
35
var db = request.result;
36
resolve(new SimpleDB(SECRET, name, db));
38
request.onerror = function() {
39
reject(request.error);
43
delete: function(name) {
44
return new Promise(function(resolve, reject) {
45
var request = indexedDB.deleteDatabase(DB_PREFIX + name);
46
request.onsuccess = function() {
49
request.onerror = function() {
50
reject(request.error);
56
function SimpleDB(secret, name, db) {
57
if (secret !== SECRET) throw TypeError('Invalid constructor');
61
SimpleDB.cmp = indexedDB.cmp;
62
SimpleDB.prototype = {
68
return new Promise(function(resolve, reject) {
69
var tx = that._db.transaction(STORE, 'readwrite');
70
var store = tx.objectStore(STORE);
71
var req = store.get(key);
72
// NOTE: Could also use req.onsuccess/onerror
73
tx.oncomplete = function() { resolve(req.result); };
74
tx.onabort = function() { reject(tx.error); };
77
set: function(key, value) {
79
return new Promise(function(resolve, reject) {
80
var tx = that._db.transaction(STORE, 'readwrite');
81
var store = tx.objectStore(STORE);
82
var req = store.put(value, key);
83
tx.oncomplete = function() { resolve(undefined); };
84
tx.onabort = function() { reject(tx.error); };
87
delete: function(key) {
89
return new Promise(function(resolve, reject) {
90
var tx = that._db.transaction(STORE, 'readwrite');
91
var store = tx.objectStore(STORE);
92
var req = store.delete(key);
93
tx.oncomplete = function() { resolve(undefined); };
94
tx.onabort = function() { reject(tx.error); };
99
return new Promise(function(resolve, reject) {
100
var tx = that._db.transaction(STORE, 'readwrite');
101
var store = tx.objectStore(STORE);
102
var request = store.clear();
103
tx.oncomplete = function() { resolve(undefined); };
104
tx.onabort = function() { reject(tx.error); };
107
forEach: function(callback, options) {
109
return new Promise(function(resolve, reject) {
110
options = options || {};
111
var tx = that._db.transaction(STORE, 'readwrite');
112
var store = tx.objectStore(STORE);
113
var request = store.openCursor(
115
options.direction === 'reverse' ? 'prev' : 'next');
116
request.onsuccess = function() {
117
var cursor = request.result;
120
var terminate = callback(cursor.key, cursor.value);
121
if (!terminate) cursor.continue();
126
tx.oncomplete = function() { resolve(undefined); };
127
tx.onabort = function() { reject(tx.error); };
130
getMany: function(keys) {
132
return new Promise(function(resolve, reject) {
133
var tx = that._db.transaction(STORE, 'readwrite');
134
var store = tx.objectStore(STORE);
136
keys.forEach(function(key) {
137
store.get(key).onsuccess(function(result) {
138
results.push(result);
141
tx.oncomplete = function() { resolve(results); };
142
tx.onabort = function() { reject(tx.error); };
145
setMany: function(entries) {
147
return new Promise(function(resolve, reject) {
148
var tx = that._db.transaction(STORE, 'readwrite');
149
var store = tx.objectStore(STORE);
150
entries.forEach(function(entry) {
151
store.put(entry.value, entry.key);
153
tx.oncomplete = function() { resolve(undefined); };
154
tx.onabort = function() { reject(tx.error); };
157
deleteMany: function(keys) {
159
return new Promise(function(resolve, reject) {
160
var tx = that._db.transaction(STORE, 'readwrite');
161
var store = tx.objectStore(STORE);
162
keys.forEach(function(key) {
165
tx.oncomplete = function() { resolve(undefined); };
166
tx.onabort = function() { reject(tx.error); };
171
global.simpleDB = new SimpleDBFactory(SECRET);
172
global.SimpleDBKeyRange = IDBKeyRange;