fortune-indexeddb
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -34,3 +34,2 @@ 'use strict' | ||
var primaryKey = self.common.constants.primary | ||
var reduce = self.common.reduce | ||
var assign = self.common.assign | ||
@@ -102,4 +101,2 @@ var typesArray = Object.keys(self.recordTypes) | ||
var data = event.data | ||
var result = data.result | ||
var type | ||
@@ -111,5 +108,2 @@ if (data.id !== id) return null | ||
for (type in result) | ||
self.db[type] = reducer(type, result[type]) | ||
return resolve() | ||
@@ -127,11 +121,2 @@ } | ||
}) | ||
// Populating memory database with results from IndexedDB. | ||
function reducer (type, records) { | ||
return reduce(records, function (hash, record) { | ||
record = outputRecord.call(self, type, msgpack.decode(record)) | ||
hash[record[primaryKey]] = record | ||
return hash | ||
}, {}) | ||
} | ||
} | ||
@@ -146,2 +131,39 @@ | ||
IndexedDBAdapter.prototype.find = function (type, ids, options) { | ||
var self = this | ||
var primaryKey = self.common.constants.primary | ||
var id = self.common.generateId() | ||
return new Promise(function (resolve, reject) { | ||
self.worker.addEventListener('message', listener) | ||
self.worker.postMessage({ | ||
id: id, method: ids ? 'find' : 'findAll', | ||
type: type, ids: ids | ||
}) | ||
function listener (event) { | ||
var data = event.data | ||
if (data.id !== id) return null | ||
if (data.error) return reject(new Error(data.error)) | ||
self.worker.removeEventListener('message', listener) | ||
return resolve(data.result) | ||
} | ||
}) | ||
.then(function (records) { | ||
var record, i | ||
// Populating memory database with results from IndexedDB. | ||
for (i = 0; i < records.length; i++) { | ||
record = outputRecord.call(self, type, msgpack.decode(records[i])) | ||
self.db[type][record[primaryKey]] = record | ||
} | ||
return MemoryAdapter.prototype.find.call(self, type, ids, options) | ||
}) | ||
} | ||
IndexedDBAdapter.prototype.create = function (type, records, meta) { | ||
@@ -204,7 +226,2 @@ var self = this | ||
IndexedDBAdapter.prototype.find = function (type, ids, options) { | ||
return MemoryAdapter.prototype.find.call(this, type, ids, options) | ||
} | ||
IndexedDBAdapter.prototype.update = function (type, updates) { | ||
@@ -211,0 +228,0 @@ var self = this |
@@ -13,2 +13,4 @@ 'use strict' | ||
disconnect: disconnect, | ||
find: find, | ||
findAll: findAll, | ||
create: create, | ||
@@ -60,3 +62,3 @@ update: update, | ||
loadRecords() | ||
callback(null, null, []) | ||
} | ||
@@ -91,46 +93,73 @@ | ||
db = event.target.result | ||
loadRecords(db) | ||
callback(null, null, []) | ||
} | ||
} | ||
function loadRecords () { | ||
var counter = 0 | ||
var payload = {} | ||
var transfer = [] | ||
var i, j | ||
function errorConnection () { | ||
callback('The database connection could not be established.') | ||
} | ||
for (i = 0, j = typesArray.length; i < j; i++) | ||
loadType(typesArray[i]) | ||
function errorReconnection () { | ||
callback('An attempt to reconnect failed.') | ||
} | ||
} | ||
function loadType (type) { | ||
var transaction = db.transaction(type, 'readonly') | ||
var objectStore = transaction.objectStore(type) | ||
var cursor = objectStore.openCursor() | ||
payload[type] = [] | ||
cursor.onsuccess = function (event) { | ||
var iterator = event.target.result | ||
if (iterator) { | ||
payload[type].push(iterator.value[dataKey]) | ||
transfer.push(iterator.value[dataKey].buffer) | ||
iterator.continue() | ||
return | ||
} | ||
counter++ | ||
if (counter === typesArray.length) | ||
callback(null, payload, transfer) | ||
function find (data, callback) { | ||
var type = data.type | ||
var ids = data.ids | ||
var payload = [] | ||
var transfer = [] | ||
var transaction = db.transaction(type, 'readonly') | ||
var objectStore = transaction.objectStore(type) | ||
var counter = 0 | ||
ids.forEach(function (id) { | ||
var request = objectStore.get(type + delimiter + id) | ||
request.onsuccess = function () { | ||
var result = request.result | ||
if (result && result[dataKey]) { | ||
payload.push(result[dataKey]) | ||
transfer.push(result[dataKey].buffer) | ||
} | ||
cursor.onerror = errorIteration | ||
check() | ||
} | ||
} | ||
function errorConnection () { | ||
callback('The database connection could not be established.') | ||
request.onerror = function () { | ||
// Silently fail. | ||
check() | ||
} | ||
}) | ||
if (!ids.length) | ||
callback(null, payload, transfer) | ||
function check () { | ||
counter++ | ||
if (counter === ids.length) | ||
callback(null, payload, transfer) | ||
} | ||
} | ||
function errorReconnection () { | ||
callback('An attempt to reconnect failed.') | ||
function findAll (data, callback) { | ||
var type = data.type | ||
var payload = [] | ||
var transfer = [] | ||
var transaction = db.transaction(type, 'readonly') | ||
var objectStore = transaction.objectStore(type) | ||
var cursor = objectStore.openCursor() | ||
cursor.onsuccess = function (event) { | ||
var iterator = event.target.result | ||
if (iterator) { | ||
payload.push(iterator.value[dataKey]) | ||
transfer.push(iterator.value[dataKey].buffer) | ||
iterator.continue() | ||
return | ||
} | ||
callback(null, payload, transfer) | ||
} | ||
function errorIteration () { | ||
cursor.onerror = function () { | ||
callback('Failed to read record.') | ||
@@ -137,0 +166,0 @@ } |
{ | ||
"name": "fortune-indexeddb", | ||
"description": "IndexedDB adapter for Fortune.", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -21,8 +21,8 @@ "repository": { | ||
"devDependencies": { | ||
"browserify": "^16.2.2", | ||
"eslint": "^5.0.1", | ||
"browserify": "^16.2.3", | ||
"eslint": "^5.16.0", | ||
"eslint-config-boss": "^1.0.6", | ||
"fortune": "^5.4.3", | ||
"tapdance": "^5.1.0", | ||
"tape-run": "^4.0.0" | ||
"fortune": "^5.5.17", | ||
"tapdance": "^5.1.1", | ||
"tape-run": "^6.0.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "main": "lib/index.js", |
@@ -10,3 +10,3 @@ # Fortune IndexedDB | ||
- Runs in a Web Worker so that database operations don't [block the main thread](https://nolanlawson.com/2015/09/29/indexeddb-websql-localstorage-what-blocks-the-dom/), and uses [MessagePack](http://msgpack.org) for [messaging](https://developer.mozilla.org/en/docs/Web/API/Worker/postMessage). | ||
- Loads all records in memory so that unnecessary database reads are avoided. | ||
- Lazy loads records as they're requested and keeps them in memory. | ||
- Primary keys are universally unique, which solves some [compatibility problems](https://www.raymondcamden.com/2014/09/25/IndexedDB-on-iOS-8-Broken-Bad). | ||
@@ -13,0 +13,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23370
555