Comparing version 1.5.10 to 1.5.11
{ | ||
"name": "lokijs", | ||
"version": "1.5.10", | ||
"version": "1.5.11", | ||
"description": "Fast document oriented javascript in-memory database", | ||
@@ -5,0 +5,0 @@ "homepage": "https://techfort.github.io/LokiJS/", |
@@ -16,2 +16,5 @@ (function(root, factory) { | ||
/* jshint -W030 */ | ||
var DEBUG = typeof window !== 'undefined' && !!window.__loki_incremental_idb_debug; | ||
/** | ||
@@ -34,4 +37,12 @@ * An improved Loki persistence adapter for IndexedDB (not compatible with LokiIndexedAdapter) | ||
* @param {object=} options Configuration options for the adapter | ||
* @param {boolean} options.onversionchange Function to call on `IDBDatabase.onversionchange` event | ||
* @param {function} options.onversionchange Function to call on `IDBDatabase.onversionchange` event | ||
* (most likely database deleted from another browser tab) | ||
* @param {function} options.onFetchStart Function to call once IDB load has begun. | ||
* Use this as an opportunity to execute code concurrently while IDB does work on a separate thread | ||
* @param {function} options.serializeChunk Called with a chunk (array of Loki documents) before | ||
* it's saved to IndexedDB. You can use it to manually compress on-disk representation | ||
* for faster database loads. Hint: Hand-written conversion of objects to arrays is very | ||
* profitable for performance. If you use this, you must also pass options.deserializeChunk. | ||
* @param {function} options.deserializeChunk Called with a chunk serialized with options.serializeChunk | ||
* Expects an array of Loki documents as the return value | ||
*/ | ||
@@ -52,2 +63,3 @@ function IncrementalIndexedDBAdapter(options) { | ||
// use idIndex to find first collection.data position within the $loki range | ||
collection.ensureId(); | ||
var idIndex = collection.idIndex; | ||
@@ -132,8 +144,9 @@ | ||
var that = this; | ||
console.log("exportDatabase - begin"); | ||
console.time("exportDatabase"); | ||
DEBUG && console.log("exportDatabase - begin"); | ||
DEBUG && console.time("exportDatabase"); | ||
var chunksToSave = []; | ||
var savedLength = 0; | ||
loki.collections.forEach(function(collection, i) { | ||
var prepareCollection = function (collection, i) { | ||
// Find dirty chunk ids | ||
@@ -148,25 +161,38 @@ var dirtyChunks = new Set(); | ||
// Serialize chunks to save | ||
dirtyChunks.forEach(function(chunkId) { | ||
var prepareChunk = function (chunkId) { | ||
var chunkData = that._getChunk(collection, chunkId); | ||
if (that.options.serializeChunk) { | ||
chunkData = that.options.serializeChunk(collection.name, chunkData); | ||
} | ||
// we must stringify now, because IDB is asynchronous, and underlying objects are mutable | ||
// (and it's faster for some reason) | ||
chunkData = JSON.stringify(chunkData); | ||
savedLength += chunkData.length; | ||
chunksToSave.push({ | ||
key: collection.name + ".chunk." + chunkId, | ||
value: JSON.stringify(chunkData), | ||
value: chunkData, | ||
}); | ||
}); | ||
}; | ||
dirtyChunks.forEach(prepareChunk); | ||
collection.data = []; | ||
// this is recreated on load anyway, so we can make metadata smaller | ||
collection.isIndex = []; | ||
// save collection metadata as separate chunk (but only if changed) | ||
if (collection.dirty) { | ||
collection.idIndex = []; // this is recreated lazily | ||
collection.data = []; | ||
// save collection metadata as separate chunk, leave only names in loki | ||
// TODO: To reduce IO, we should only save this chunk when it has changed | ||
chunksToSave.push({ | ||
key: collection.name + ".metadata", | ||
value: JSON.stringify(collection), | ||
}); | ||
var metadataChunk = JSON.stringify(collection); | ||
savedLength += metadataChunk.length; | ||
chunksToSave.push({ | ||
key: collection.name + ".metadata", | ||
value: metadataChunk, | ||
}); | ||
} | ||
// leave only names in the loki chunk | ||
loki.collections[i] = { name: collection.name }; | ||
}); | ||
}; | ||
loki.collections.forEach(prepareCollection); | ||
var serializedMetadata = JSON.stringify(loki); | ||
savedLength += serializedMetadata.length; | ||
loki = null; // allow GC of the DB copy | ||
@@ -176,2 +202,3 @@ | ||
DEBUG && console.log("saved size: " + savedLength); | ||
that._saveChunks(dbname, chunksToSave, callback); | ||
@@ -197,8 +224,8 @@ }; | ||
var that = this; | ||
console.log("loadDatabase - begin"); | ||
console.time("loadDatabase"); | ||
DEBUG && console.log("loadDatabase - begin"); | ||
DEBUG && console.time("loadDatabase"); | ||
this._getAllChunks(dbname, function(chunks) { | ||
if (!Array.isArray(chunks)) { | ||
// we got an error | ||
console.timeEnd("loadDatabase"); | ||
DEBUG && console.timeEnd("loadDatabase"); | ||
callback(chunks); | ||
@@ -208,3 +235,3 @@ } | ||
if (!chunks.length) { | ||
console.timeEnd("loadDatabase"); | ||
DEBUG && console.timeEnd("loadDatabase"); | ||
callback(null); | ||
@@ -214,3 +241,3 @@ return; | ||
console.log("Found chunks:", chunks.length); | ||
DEBUG && console.log("Found chunks:", chunks.length); | ||
@@ -269,3 +296,3 @@ that._sortChunksInPlace(chunks); | ||
console.timeEnd("loadDatabase"); | ||
DEBUG && console.timeEnd("loadDatabase"); | ||
callback(loki); | ||
@@ -299,2 +326,3 @@ }); | ||
IncrementalIndexedDBAdapter.prototype._populate = function(loki, chunkCollections) { | ||
var that = this; | ||
loki.collections.forEach(function(collectionStub, i) { | ||
@@ -316,2 +344,6 @@ var chunkCollection = chunkCollections[collectionStub.name]; | ||
if (that.options.deserializeChunk) { | ||
chunk = that.options.deserializeChunk(collection.name, chunk); | ||
} | ||
chunk.forEach(function(doc) { | ||
@@ -327,3 +359,3 @@ collection.data.push(doc); | ||
var that = this; | ||
console.log("initializing idb"); | ||
DEBUG && console.log("initializing idb"); | ||
@@ -339,3 +371,3 @@ if (this.idbInitInProgress) { | ||
var db = e.target.result; | ||
console.log('onupgradeneeded, old version: ' + e.oldVersion); | ||
DEBUG && console.log('onupgradeneeded, old version: ' + e.oldVersion); | ||
@@ -362,6 +394,6 @@ if (e.oldVersion < 1) { | ||
console.log("init success"); | ||
DEBUG && console.log("init success"); | ||
that.idb.onversionchange = function(versionChangeEvent) { | ||
console.log('IDB version change', versionChangeEvent); | ||
DEBUG && console.log('IDB version change', versionChangeEvent); | ||
// This function will be called if another connection changed DB version | ||
@@ -412,3 +444,3 @@ // (Most likely database was deleted from another browser tab, unless there's a new version | ||
that.operationInProgress = false; | ||
console.timeEnd("exportDatabase"); | ||
DEBUG && console.timeEnd("exportDatabase"); | ||
callback(); | ||
@@ -462,2 +494,6 @@ }; | ||
}; | ||
if (this.options.onFetchStart) { | ||
this.options.onFetchStart(); | ||
} | ||
}; | ||
@@ -487,4 +523,4 @@ | ||
var that = this; | ||
console.log("deleteDatabase - begin"); | ||
console.time("deleteDatabase"); | ||
DEBUG && console.log("deleteDatabase - begin"); | ||
DEBUG && console.time("deleteDatabase"); | ||
@@ -500,3 +536,3 @@ if (this.idb) { | ||
that.operationInProgress = false; | ||
console.timeEnd("deleteDatabase"); | ||
DEBUG && console.timeEnd("deleteDatabase"); | ||
callback({ success: true }); | ||
@@ -503,0 +539,0 @@ }; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
3060949
9737