idb-wrapper
Advanced tools
Comparing version 1.3.0 to 1.4.0
177
idbstore.js
@@ -45,3 +45,3 @@ /*global window:false, self:false, define:false, module:false */ | ||
* @name IDBStore | ||
* @version 1.1.0 | ||
* @version 1.4.0 | ||
* | ||
@@ -101,3 +101,10 @@ * @param {Object} [kwArgs] An options object used to configure the store and | ||
for(var key in defaults){ | ||
if (typeof onStoreReady == 'undefined' && typeof kwArgs == 'function') { | ||
onStoreReady = kwArgs; | ||
} | ||
if (Object.prototype.toString.call(kwArgs) != '[object Object]') { | ||
kwArgs = {}; | ||
} | ||
for (var key in defaults) { | ||
this[key] = typeof kwArgs[key] != 'undefined' ? kwArgs[key] : defaults[key]; | ||
@@ -107,3 +114,3 @@ } | ||
this.dbName = this.storePrefix + this.storeName; | ||
this.dbVersion = parseInt(this.dbVersion, 10); | ||
this.dbVersion = parseInt(this.dbVersion, 10) || 1; | ||
@@ -136,2 +143,9 @@ onStoreReady && (this.onStoreReady = onStoreReady); | ||
/** | ||
* A pointer to the IDBStore ctor | ||
* | ||
* @type IDBStore | ||
*/ | ||
constructor: IDBStore, | ||
/** | ||
* The version of IDBStore | ||
@@ -141,3 +155,3 @@ * | ||
*/ | ||
version: '1.3.0', | ||
version: '1.4.0', | ||
@@ -389,2 +403,3 @@ /** | ||
* failed. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
* @example | ||
@@ -440,2 +455,4 @@ // Storing an object, using inline keys (the default scenario): | ||
putRequest.onerror = onError; | ||
return putTransaction; | ||
}, | ||
@@ -452,2 +469,3 @@ | ||
* occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -474,2 +492,4 @@ get: function (key, onSuccess, onError) { | ||
getRequest.onerror = onError; | ||
return getTransaction; | ||
}, | ||
@@ -485,2 +505,3 @@ | ||
* occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -508,2 +529,4 @@ remove: function (key, onSuccess, onError) { | ||
deleteRequest.onerror = onError; | ||
return removeTransaction; | ||
}, | ||
@@ -520,2 +543,3 @@ | ||
* occurred during one of the operations. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -578,2 +602,4 @@ batch: function (dataArray, onSuccess, onError) { | ||
}, this); | ||
return batchTransaction; | ||
}, | ||
@@ -589,2 +615,3 @@ | ||
* occurred during one of the operations. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -596,3 +623,3 @@ putBatch: function (dataArray, onSuccess, onError) { | ||
this.batch(batchData, onSuccess, onError); | ||
return this.batch(batchData, onSuccess, onError); | ||
}, | ||
@@ -609,2 +636,3 @@ | ||
* occurred during one of the operations. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -616,6 +644,117 @@ removeBatch: function (keyArray, onSuccess, onError) { | ||
this.batch(batchData, onSuccess, onError); | ||
return this.batch(batchData, onSuccess, onError); | ||
}, | ||
/** | ||
* Takes an array of keys and fetches matching objects | ||
* | ||
* @param {Array} keyArray An array of keys identifying the objects to fetch | ||
* @param {Function} [onSuccess] A callback that is called if all operations | ||
* were successful. | ||
* @param {Function} [onError] A callback that is called if an error | ||
* occurred during one of the operations. | ||
* @param {String} [arrayType='sparse'] The type of array to pass to the | ||
* success handler. May be one of 'sparse', 'dense' or 'skip'. Defaults to | ||
* 'sparse'. This parameter specifies how to handle the situation if a get | ||
* operation did not throw an error, but there was no matching object in | ||
* the database. In most cases, 'sparse' provides the most desired | ||
* behavior. See the examples for details. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
* @example | ||
// given that there are two objects in the database with the keypath | ||
// values 1 and 2, and the call looks like this: | ||
myStore.getBatch([1, 5, 2], onError, function (data) { … }, arrayType); | ||
// this is what the `data` array will be like: | ||
// arrayType == 'sparse': | ||
// data is a sparse array containing two entries and having a length of 3: | ||
[Object, 2: Object] | ||
0: Object | ||
2: Object | ||
length: 3 | ||
__proto__: Array[0] | ||
// calling forEach on data will result in the callback being called two | ||
// times, with the index parameter matching the index of the key in the | ||
// keyArray. | ||
// arrayType == 'dense': | ||
// data is a dense array containing three entries and having a length of 3, | ||
// where data[1] is of type undefined: | ||
[Object, undefined, Object] | ||
0: Object | ||
1: undefined | ||
2: Object | ||
length: 3 | ||
__proto__: Array[0] | ||
// calling forEach on data will result in the callback being called three | ||
// times, with the index parameter matching the index of the key in the | ||
// keyArray, but the second call will have undefined as first argument. | ||
// arrayType == 'skip': | ||
// data is a dense array containing two entries and having a length of 2: | ||
[Object, Object] | ||
0: Object | ||
1: Object | ||
length: 2 | ||
__proto__: Array[0] | ||
// calling forEach on data will result in the callback being called two | ||
// times, with the index parameter not matching the index of the key in the | ||
// keyArray. | ||
*/ | ||
getBatch: function (keyArray, onSuccess, onError, arrayType) { | ||
onError || (onError = defaultErrorHandler); | ||
onSuccess || (onSuccess = noop); | ||
arrayType || (arrayType = 'sparse'); | ||
if(Object.prototype.toString.call(keyArray) != '[object Array]'){ | ||
onError(new Error('keyArray argument must be of type Array.')); | ||
} | ||
var batchTransaction = this.db.transaction([this.storeName] , this.consts.READ_ONLY); | ||
batchTransaction.oncomplete = function () { | ||
var callback = hasSuccess ? onSuccess : onError; | ||
callback(result); | ||
}; | ||
batchTransaction.onabort = onError; | ||
batchTransaction.onerror = onError; | ||
var data = []; | ||
var count = keyArray.length; | ||
var called = false; | ||
var hasSuccess = false; | ||
var result = null; | ||
var onItemSuccess = function (event) { | ||
if (event.target.result || arrayType == 'dense') { | ||
data.push(event.target.result); | ||
} else if (arrayType == 'sparse') { | ||
data.length++; | ||
} | ||
count--; | ||
if (count === 0) { | ||
called = true; | ||
hasSuccess = true; | ||
result = data; | ||
} | ||
}; | ||
keyArray.forEach(function (key) { | ||
var onItemError = function (err) { | ||
called = true; | ||
result = err; | ||
onError(err); | ||
batchTransaction.abort(); | ||
}; | ||
var getRequest = batchTransaction.objectStore(this.storeName).get(key); | ||
getRequest.onsuccess = onItemSuccess; | ||
getRequest.onerror = onItemError; | ||
}, this); | ||
return batchTransaction; | ||
}, | ||
/** | ||
* Fetches all entries in the store. | ||
@@ -627,2 +766,3 @@ * | ||
* occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -639,2 +779,4 @@ getAll: function (onSuccess, onError) { | ||
} | ||
return getAllTransaction; | ||
}, | ||
@@ -719,2 +861,3 @@ | ||
* error occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -742,2 +885,4 @@ clear: function (onSuccess, onError) { | ||
clearRequest.onerror = onError; | ||
return clearTransaction; | ||
}, | ||
@@ -839,2 +984,3 @@ | ||
* if an error occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -892,2 +1038,4 @@ iterate: function (onItem, options) { | ||
}; | ||
return cursorTransaction; | ||
}, | ||
@@ -910,2 +1058,3 @@ | ||
* occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -918,3 +1067,3 @@ query: function (onSuccess, options) { | ||
}; | ||
this.iterate(function (item) { | ||
return this.iterate(function (item) { | ||
result.push(item); | ||
@@ -936,2 +1085,3 @@ }, options); | ||
* occurred during the operation. | ||
* @returns {IDBTransaction} The transaction used for this operation. | ||
*/ | ||
@@ -968,2 +1118,4 @@ count: function (onSuccess, options) { | ||
countRequest.onError = onError; | ||
return cursorTransaction; | ||
}, | ||
@@ -988,2 +1140,5 @@ | ||
* bound passed in options.upper from the key range | ||
* @param {*} [options.only] A single key value. Use this if you need a key | ||
* range that only includes one value for a key. Providing this | ||
* property invalidates all other properties. | ||
* @return {Object} The IDBKeyRange representing the specified options | ||
@@ -995,5 +1150,9 @@ */ | ||
hasLower = typeof options.lower != 'undefined', | ||
hasUpper = typeof options.upper != 'undefined'; | ||
hasUpper = typeof options.upper != 'undefined', | ||
isOnly = typeof options.only != 'undefined'; | ||
switch(true){ | ||
case isOnly: | ||
keyRange = this.keyRange.only(options.only); | ||
break; | ||
case hasLower && hasUpper: | ||
@@ -1009,3 +1168,3 @@ keyRange = this.keyRange.bound(options.lower, options.upper, options.excludeLower, options.excludeUpper); | ||
default: | ||
throw new Error('Cannot create KeyRange. Provide one or both of "lower" or "upper" value.'); | ||
throw new Error('Cannot create KeyRange. Provide one or both of "lower" or "upper" value, or an "only" value.'); | ||
} | ||
@@ -1012,0 +1171,0 @@ |
@@ -8,18 +8,19 @@ /* | ||
*/ | ||
(function(h,i,j){"function"===typeof define?define(i):"undefined"!==typeof module&&module.exports?module.exports=i():j[h]=i()})("IDBStore",function(){var h=function(a){throw a;},i={storeName:"Store",storePrefix:"IDBWrapper-",dbVersion:1,keyPath:"id",autoIncrement:!0,onStoreReady:function(){},onError:h,indexes:[]},j=function(a,c){for(var b in i)this[b]="undefined"!=typeof a[b]?a[b]:i[b];this.dbName=this.storePrefix+this.storeName;this.dbVersion=parseInt(this.dbVersion,10);c&&(this.onStoreReady=c); | ||
b="object"==typeof window?window:self;this.idb=b.indexedDB||b.webkitIndexedDB||b.mozIndexedDB;this.keyRange=b.IDBKeyRange||b.webkitIDBKeyRange||b.mozIDBKeyRange;this.features={hasAutoIncrement:!b.mozIndexedDB};this.consts={READ_ONLY:"readonly",READ_WRITE:"readwrite",VERSION_CHANGE:"versionchange",NEXT:"next",NEXT_NO_DUPLICATE:"nextunique",PREV:"prev",PREV_NO_DUPLICATE:"prevunique"};this.openDB()};j.prototype={version:"1.3.0",db:null,dbName:null,dbVersion:null,store:null,storeName:null,keyPath:null, | ||
autoIncrement:null,indexes:null,features:null,onStoreReady:null,onError:null,_insertIdCount:0,openDB:function(){var a=this.idb.open(this.dbName,this.dbVersion),c=!1;a.onerror=function(a){var c=!1;"error"in a.target?c="VersionError"==a.target.error.name:"errorCode"in a.target&&(c=12==a.target.errorCode);if(c)this.onError(Error("The version number provided is lower than the existing one."));else this.onError(a)}.bind(this);a.onsuccess=function(a){if(!c)if(this.db)this.onStoreReady();else if(this.db= | ||
a.target.result,"string"==typeof this.db.version)this.onError(Error("The IndexedDB implementation in this browser is outdated. Please upgrade your browser."));else if(this.db.objectStoreNames.contains(this.storeName))this.store=this.db.transaction([this.storeName],this.consts.READ_ONLY).objectStore(this.storeName),this.indexes.forEach(function(a){var b=a.name;b?(this.normalizeIndexData(a),this.hasIndex(b)?this.indexComplies(this.store.index(b),a)||(c=!0,this.onError(Error('Cannot modify index "'+ | ||
b+'" for current version. Please bump version number to '+(this.dbVersion+1)+"."))):(c=!0,this.onError(Error('Cannot create new index "'+b+'" for current version. Please bump version number to '+(this.dbVersion+1)+".")))):(c=!0,this.onError(Error("Cannot create index: No index name given.")))},this),c||this.onStoreReady();else this.onError(Error("Something is wrong with the IndexedDB implementation in this browser. Please upgrade your browser."))}.bind(this);a.onupgradeneeded=function(a){this.db= | ||
a.target.result;this.store=this.db.objectStoreNames.contains(this.storeName)?a.target.transaction.objectStore(this.storeName):this.db.createObjectStore(this.storeName,{keyPath:this.keyPath,autoIncrement:this.autoIncrement});this.indexes.forEach(function(a){var b=a.name;b||(c=!0,this.onError(Error("Cannot create index: No index name given.")));this.normalizeIndexData(a);this.hasIndex(b)?this.indexComplies(this.store.index(b),a)||(this.store.deleteIndex(b),this.store.createIndex(b,a.keyPath,{unique:a.unique, | ||
multiEntry:a.multiEntry})):this.store.createIndex(b,a.keyPath,{unique:a.unique,multiEntry:a.multiEntry})},this)}.bind(this)},deleteDatabase:function(){this.idb.deleteDatabase&&this.idb.deleteDatabase(this.dbName)},put:function(a,c,b,d){null!==this.keyPath&&(d=b,b=c,c=a);d||(d=h);b||(b=k);var f=!1,e=null,g=this.db.transaction([this.storeName],this.consts.READ_WRITE);g.oncomplete=function(){(f?b:d)(e)};g.onabort=d;g.onerror=d;null!==this.keyPath?(this._addIdPropertyIfNeeded(c),a=g.objectStore(this.storeName).put(c)): | ||
a=g.objectStore(this.storeName).put(c,a);a.onsuccess=function(a){f=!0;e=a.target.result};a.onerror=d},get:function(a,c,b){b||(b=h);c||(c=k);var d=!1,f=null,e=this.db.transaction([this.storeName],this.consts.READ_ONLY);e.oncomplete=function(){(d?c:b)(f)};e.onabort=b;e.onerror=b;a=e.objectStore(this.storeName).get(a);a.onsuccess=function(a){d=!0;f=a.target.result};a.onerror=b},remove:function(a,c,b){b||(b=h);c||(c=k);var d=!1,f=null,e=this.db.transaction([this.storeName],this.consts.READ_WRITE);e.oncomplete= | ||
function(){(d?c:b)(f)};e.onabort=b;e.onerror=b;a=e.objectStore(this.storeName)["delete"](a);a.onsuccess=function(a){d=!0;f=a.target.result};a.onerror=b},batch:function(a,c,b){b||(b=h);c||(c=k);"[object Array]"!=Object.prototype.toString.call(a)&&b(Error("dataArray argument must be of type Array."));var d=this.db.transaction([this.storeName],this.consts.READ_WRITE);d.oncomplete=function(){(g?c:b)(g)};d.onabort=b;d.onerror=b;var f=a.length,e=!1,g=!1,l=function(){f--;0===f&&!e&&(g=e=!0)};a.forEach(function(a){var c= | ||
a.type,f=a.key,g=a.value,a=function(a){d.abort();e||(e=!0,b(a,c,f))};if("remove"==c)g=d.objectStore(this.storeName)["delete"](f),g.onsuccess=l,g.onerror=a;else if("put"==c)null!==this.keyPath?(this._addIdPropertyIfNeeded(g),g=d.objectStore(this.storeName).put(g)):g=d.objectStore(this.storeName).put(g,f),g.onsuccess=l,g.onerror=a},this)},putBatch:function(a,c,b){this.batch(a.map(function(a){return{type:"put",value:a}}),c,b)},removeBatch:function(a,c,b){this.batch(a.map(function(a){return{type:"remove", | ||
key:a}}),c,b)},getAll:function(a,c){c||(c=h);a||(a=k);var b=this.db.transaction([this.storeName],this.consts.READ_ONLY),d=b.objectStore(this.storeName);d.getAll?this._getAllNative(b,d,a,c):this._getAllCursor(b,d,a,c)},_getAllNative:function(a,c,b,d){var f=!1,e=null;a.oncomplete=function(){(f?b:d)(e)};a.onabort=d;a.onerror=d;a=c.getAll();a.onsuccess=function(a){f=!0;e=a.target.result};a.onerror=d},_getAllCursor:function(a,c,b,d){var f=[],e=!1,g=null;a.oncomplete=function(){(e?b:d)(g)};a.onabort=d; | ||
a.onerror=d;a=c.openCursor();a.onsuccess=function(a){(a=a.target.result)?(f.push(a.value),a["continue"]()):(e=!0,g=f)};a.onError=d},clear:function(a,c){c||(c=h);a||(a=k);var b=!1,d=null,f=this.db.transaction([this.storeName],this.consts.READ_WRITE);f.oncomplete=function(){(b?a:c)(d)};f.onabort=c;f.onerror=c;f=f.objectStore(this.storeName).clear();f.onsuccess=function(a){b=!0;d=a.target.result};f.onerror=c},_addIdPropertyIfNeeded:function(a){!this.features.hasAutoIncrement&&"undefined"==typeof a[this.keyPath]&& | ||
(a[this.keyPath]=this._insertIdCount++ +Date.now())},getIndexList:function(){return this.store.indexNames},hasIndex:function(a){return this.store.indexNames.contains(a)},normalizeIndexData:function(a){a.keyPath=a.keyPath||a.name;a.unique=!!a.unique;a.multiEntry=!!a.multiEntry},indexComplies:function(a,c){return["keyPath","unique","multiEntry"].every(function(b){return"multiEntry"==b&&void 0===a[b]&&!1===c[b]?!0:c[b]==a[b]})},iterate:function(a,c){var c=m({index:null,order:"ASC",autoContinue:!0,filterDuplicates:!1, | ||
keyRange:null,writeAccess:!1,onEnd:null,onError:h},c||{}),b="desc"==c.order.toLowerCase()?"PREV":"NEXT";c.filterDuplicates&&(b+="_NO_DUPLICATE");var d=!1,f=this.db.transaction([this.storeName],this.consts[c.writeAccess?"READ_WRITE":"READ_ONLY"]),e=f.objectStore(this.storeName);c.index&&(e=e.index(c.index));f.oncomplete=function(){if(d)if(c.onEnd)c.onEnd();else a(null);else c.onError(null)};f.onabort=c.onError;f.onerror=c.onError;b=e.openCursor(c.keyRange,this.consts[b]);b.onerror=c.onError;b.onsuccess= | ||
function(b){if(b=b.target.result){if(a(b.value,b,f),c.autoContinue)b["continue"]()}else d=!0}},query:function(a,c){var b=[],c=c||{};c.onEnd=function(){a(b)};this.iterate(function(a){b.push(a)},c)},count:function(a,c){var c=m({index:null,keyRange:null},c||{}),b=c.onError||h,d=!1,f=null,e=this.db.transaction([this.storeName],this.consts.READ_ONLY);e.oncomplete=function(){(d?a:b)(f)};e.onabort=b;e.onerror=b;e=e.objectStore(this.storeName);c.index&&(e=e.index(c.index));e=e.count(c.keyRange);e.onsuccess= | ||
function(a){d=!0;f=a.target.result};e.onError=b},makeKeyRange:function(a){var c="undefined"!=typeof a.lower,b="undefined"!=typeof a.upper;switch(!0){case c&&b:a=this.keyRange.bound(a.lower,a.upper,a.excludeLower,a.excludeUpper);break;case c:a=this.keyRange.lowerBound(a.lower,a.excludeLower);break;case b:a=this.keyRange.upperBound(a.upper,a.excludeUpper);break;default:throw Error('Cannot create KeyRange. Provide one or both of "lower" or "upper" value.');}return a}};var k=function(){},n={},m=function(a, | ||
c){var b,d;for(b in c)d=c[b],d!==n[b]&&d!==a[b]&&(a[b]=d);return a};j.version=j.prototype.version;return j},this); | ||
(function(h,j,i){"function"===typeof define?define(j):"undefined"!==typeof module&&module.exports?module.exports=j():i[h]=j()})("IDBStore",function(){var h=function(a){throw a;},j={storeName:"Store",storePrefix:"IDBWrapper-",dbVersion:1,keyPath:"id",autoIncrement:!0,onStoreReady:function(){},onError:h,indexes:[]},i=function(a,c){"undefined"==typeof c&&"function"==typeof a&&(c=a);"[object Object]"!=Object.prototype.toString.call(a)&&(a={});for(var b in j)this[b]="undefined"!=typeof a[b]?a[b]:j[b]; | ||
this.dbName=this.storePrefix+this.storeName;this.dbVersion=parseInt(this.dbVersion,10)||1;c&&(this.onStoreReady=c);b="object"==typeof window?window:self;this.idb=b.indexedDB||b.webkitIndexedDB||b.mozIndexedDB;this.keyRange=b.IDBKeyRange||b.webkitIDBKeyRange||b.mozIDBKeyRange;this.features={hasAutoIncrement:!b.mozIndexedDB};this.consts={READ_ONLY:"readonly",READ_WRITE:"readwrite",VERSION_CHANGE:"versionchange",NEXT:"next",NEXT_NO_DUPLICATE:"nextunique",PREV:"prev",PREV_NO_DUPLICATE:"prevunique"};this.openDB()}; | ||
i.prototype={constructor:i,version:"1.4.0",db:null,dbName:null,dbVersion:null,store:null,storeName:null,keyPath:null,autoIncrement:null,indexes:null,features:null,onStoreReady:null,onError:null,_insertIdCount:0,openDB:function(){var a=this.idb.open(this.dbName,this.dbVersion),c=!1;a.onerror=function(a){var c=!1;"error"in a.target?c="VersionError"==a.target.error.name:"errorCode"in a.target&&(c=12==a.target.errorCode);if(c)this.onError(Error("The version number provided is lower than the existing one.")); | ||
else this.onError(a)}.bind(this);a.onsuccess=function(a){if(!c)if(this.db)this.onStoreReady();else if(this.db=a.target.result,"string"==typeof this.db.version)this.onError(Error("The IndexedDB implementation in this browser is outdated. Please upgrade your browser."));else if(this.db.objectStoreNames.contains(this.storeName))this.store=this.db.transaction([this.storeName],this.consts.READ_ONLY).objectStore(this.storeName),this.indexes.forEach(function(a){var b=a.name;b?(this.normalizeIndexData(a), | ||
this.hasIndex(b)?this.indexComplies(this.store.index(b),a)||(c=!0,this.onError(Error('Cannot modify index "'+b+'" for current version. Please bump version number to '+(this.dbVersion+1)+"."))):(c=!0,this.onError(Error('Cannot create new index "'+b+'" for current version. Please bump version number to '+(this.dbVersion+1)+".")))):(c=!0,this.onError(Error("Cannot create index: No index name given.")))},this),c||this.onStoreReady();else this.onError(Error("Something is wrong with the IndexedDB implementation in this browser. Please upgrade your browser."))}.bind(this); | ||
a.onupgradeneeded=function(a){this.db=a.target.result;this.store=this.db.objectStoreNames.contains(this.storeName)?a.target.transaction.objectStore(this.storeName):this.db.createObjectStore(this.storeName,{keyPath:this.keyPath,autoIncrement:this.autoIncrement});this.indexes.forEach(function(a){var b=a.name;b||(c=!0,this.onError(Error("Cannot create index: No index name given.")));this.normalizeIndexData(a);this.hasIndex(b)?this.indexComplies(this.store.index(b),a)||(this.store.deleteIndex(b),this.store.createIndex(b, | ||
a.keyPath,{unique:a.unique,multiEntry:a.multiEntry})):this.store.createIndex(b,a.keyPath,{unique:a.unique,multiEntry:a.multiEntry})},this)}.bind(this)},deleteDatabase:function(){this.idb.deleteDatabase&&this.idb.deleteDatabase(this.dbName)},put:function(a,c,b,d){null!==this.keyPath&&(d=b,b=c,c=a);d||(d=h);b||(b=k);var e=!1,f=null,g=this.db.transaction([this.storeName],this.consts.READ_WRITE);g.oncomplete=function(){(e?b:d)(f)};g.onabort=d;g.onerror=d;null!==this.keyPath?(this._addIdPropertyIfNeeded(c), | ||
a=g.objectStore(this.storeName).put(c)):a=g.objectStore(this.storeName).put(c,a);a.onsuccess=function(a){e=!0;f=a.target.result};a.onerror=d;return g},get:function(a,c,b){b||(b=h);c||(c=k);var d=!1,e=null,f=this.db.transaction([this.storeName],this.consts.READ_ONLY);f.oncomplete=function(){(d?c:b)(e)};f.onabort=b;f.onerror=b;a=f.objectStore(this.storeName).get(a);a.onsuccess=function(a){d=!0;e=a.target.result};a.onerror=b;return f},remove:function(a,c,b){b||(b=h);c||(c=k);var d=!1,e=null,f=this.db.transaction([this.storeName], | ||
this.consts.READ_WRITE);f.oncomplete=function(){(d?c:b)(e)};f.onabort=b;f.onerror=b;a=f.objectStore(this.storeName)["delete"](a);a.onsuccess=function(a){d=!0;e=a.target.result};a.onerror=b;return f},batch:function(a,c,b){b||(b=h);c||(c=k);"[object Array]"!=Object.prototype.toString.call(a)&&b(Error("dataArray argument must be of type Array."));var d=this.db.transaction([this.storeName],this.consts.READ_WRITE);d.oncomplete=function(){(g?c:b)(g)};d.onabort=b;d.onerror=b;var e=a.length,f=!1,g=!1,l=function(){e--; | ||
0===e&&!f&&(g=f=!0)};a.forEach(function(a){var c=a.type,e=a.key,g=a.value,a=function(a){d.abort();f||(f=!0,b(a,c,e))};if("remove"==c)g=d.objectStore(this.storeName)["delete"](e),g.onsuccess=l,g.onerror=a;else if("put"==c)null!==this.keyPath?(this._addIdPropertyIfNeeded(g),g=d.objectStore(this.storeName).put(g)):g=d.objectStore(this.storeName).put(g,e),g.onsuccess=l,g.onerror=a},this);return d},putBatch:function(a,c,b){return this.batch(a.map(function(a){return{type:"put",value:a}}),c,b)},removeBatch:function(a, | ||
c,b){return this.batch(a.map(function(a){return{type:"remove",key:a}}),c,b)},getBatch:function(a,c,b,d){b||(b=h);c||(c=k);d||(d="sparse");"[object Array]"!=Object.prototype.toString.call(a)&&b(Error("keyArray argument must be of type Array."));var e=this.db.transaction([this.storeName],this.consts.READ_ONLY);e.oncomplete=function(){(l?c:b)(i)};e.onabort=b;e.onerror=b;var f=[],g=a.length,l=!1,i=null,j=function(a){a.target.result||"dense"==d?f.push(a.target.result):"sparse"==d&&f.length++;g--;0===g&& | ||
(l=!0,i=f)};a.forEach(function(a){a=e.objectStore(this.storeName).get(a);a.onsuccess=j;a.onerror=function(a){i=a;b(a);e.abort()}},this);return e},getAll:function(a,c){c||(c=h);a||(a=k);var b=this.db.transaction([this.storeName],this.consts.READ_ONLY),d=b.objectStore(this.storeName);d.getAll?this._getAllNative(b,d,a,c):this._getAllCursor(b,d,a,c);return b},_getAllNative:function(a,c,b,d){var e=!1,f=null;a.oncomplete=function(){(e?b:d)(f)};a.onabort=d;a.onerror=d;a=c.getAll();a.onsuccess=function(a){e= | ||
!0;f=a.target.result};a.onerror=d},_getAllCursor:function(a,c,b,d){var e=[],f=!1,g=null;a.oncomplete=function(){(f?b:d)(g)};a.onabort=d;a.onerror=d;a=c.openCursor();a.onsuccess=function(a){(a=a.target.result)?(e.push(a.value),a["continue"]()):(f=!0,g=e)};a.onError=d},clear:function(a,c){c||(c=h);a||(a=k);var b=!1,d=null,e=this.db.transaction([this.storeName],this.consts.READ_WRITE);e.oncomplete=function(){(b?a:c)(d)};e.onabort=c;e.onerror=c;var f=e.objectStore(this.storeName).clear();f.onsuccess= | ||
function(a){b=!0;d=a.target.result};f.onerror=c;return e},_addIdPropertyIfNeeded:function(a){!this.features.hasAutoIncrement&&"undefined"==typeof a[this.keyPath]&&(a[this.keyPath]=this._insertIdCount++ +Date.now())},getIndexList:function(){return this.store.indexNames},hasIndex:function(a){return this.store.indexNames.contains(a)},normalizeIndexData:function(a){a.keyPath=a.keyPath||a.name;a.unique=!!a.unique;a.multiEntry=!!a.multiEntry},indexComplies:function(a,c){return["keyPath","unique","multiEntry"].every(function(b){return"multiEntry"== | ||
b&&void 0===a[b]&&!1===c[b]?!0:c[b]==a[b]})},iterate:function(a,c){var c=m({index:null,order:"ASC",autoContinue:!0,filterDuplicates:!1,keyRange:null,writeAccess:!1,onEnd:null,onError:h},c||{}),b="desc"==c.order.toLowerCase()?"PREV":"NEXT";c.filterDuplicates&&(b+="_NO_DUPLICATE");var d=!1,e=this.db.transaction([this.storeName],this.consts[c.writeAccess?"READ_WRITE":"READ_ONLY"]),f=e.objectStore(this.storeName);c.index&&(f=f.index(c.index));e.oncomplete=function(){if(d)if(c.onEnd)c.onEnd();else a(null); | ||
else c.onError(null)};e.onabort=c.onError;e.onerror=c.onError;b=f.openCursor(c.keyRange,this.consts[b]);b.onerror=c.onError;b.onsuccess=function(b){if(b=b.target.result){if(a(b.value,b,e),c.autoContinue)b["continue"]()}else d=!0};return e},query:function(a,c){var b=[],c=c||{};c.onEnd=function(){a(b)};return this.iterate(function(a){b.push(a)},c)},count:function(a,c){var c=m({index:null,keyRange:null},c||{}),b=c.onError||h,d=!1,e=null,f=this.db.transaction([this.storeName],this.consts.READ_ONLY);f.oncomplete= | ||
function(){(d?a:b)(e)};f.onabort=b;f.onerror=b;var g=f.objectStore(this.storeName);c.index&&(g=g.index(c.index));g=g.count(c.keyRange);g.onsuccess=function(a){d=!0;e=a.target.result};g.onError=b;return f},makeKeyRange:function(a){var c="undefined"!=typeof a.lower,b="undefined"!=typeof a.upper,d="undefined"!=typeof a.only;switch(!0){case d:a=this.keyRange.only(a.only);break;case c&&b:a=this.keyRange.bound(a.lower,a.upper,a.excludeLower,a.excludeUpper);break;case c:a=this.keyRange.lowerBound(a.lower, | ||
a.excludeLower);break;case b:a=this.keyRange.upperBound(a.upper,a.excludeUpper);break;default:throw Error('Cannot create KeyRange. Provide one or both of "lower" or "upper" value, or an "only" value.');}return a}};var k=function(){},n={},m=function(a,c){var b,d;for(b in c)d=c[b],d!==n[b]&&d!==a[b]&&(a[b]=d);return a};i.version=i.prototype.version;return i},this); |
{ | ||
"name": "idb-wrapper", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A cross-browser wrapper for IndexedDB", | ||
"keywords": [], | ||
"keywords": [ | ||
"storage", "offline", "IndexedDB" | ||
], | ||
"author": "jensarps <mail@jensarps.de> (http://jensarps.de/)", | ||
@@ -14,3 +16,4 @@ "repository": "git://github.com/jensarps/IDBWrapper.git", | ||
"Chad Engler <chad@pantherdev.com> (http://chad.pantherdev.com)", | ||
"Max Ogden <max+ogden@maxogden.com> (http://www.maxogden.com)" | ||
"Max Ogden <max+ogden@maxogden.com> (http://www.maxogden.com)", | ||
"Asa Ayers <asa.ayers@gmail.com>" | ||
], | ||
@@ -17,0 +20,0 @@ "bugs": { |
165
README.md
About | ||
===== | ||
This is a wrapper for indexedDB. It is meant to | ||
IDBWrapper is a cross-browser wrapper for the HTML5 IndexedDB API. While this | ||
API is the future of offline storage, it is not very intuitive to use. | ||
IDBWrapper is there to provide easy access to IndexedDB's features. | ||
a) ease the use of indexedDB and abstract away the differences between the | ||
existing impls in Chrome, Firefox, IE10 and Opera 15 (yes, it works in all four), and | ||
##Browser Support | ||
b) show how IDB works. The code is split up into short methods, so that it's | ||
easy to see what happens in what method. | ||
IDBWrapper works on all browsers supperting the IndexedDB API, which are: | ||
The code in idbstore.js is not optimized for anything, nor minified or anything. | ||
It is meant to be read and easy to understand. So, please, go ahead and check out | ||
the source! | ||
**Desktop** | ||
* Chrome | ||
* Firefox | ||
* Opera 15+ | ||
* IE 10+ | ||
**Mobile** | ||
* Chrome for Android | ||
* Firefox for Android | ||
* Opera for Android | ||
* IE10 for WP8 | ||
**Worker** IDBWrapper runs inside of a worker on following browsers: | ||
* Chrome | ||
* Chrome for Android | ||
* Firefox | ||
* Opera | ||
* Opera for Android | ||
* IE10 | ||
* IE10 for WP8 | ||
##Tutorials | ||
There are two tutorials to get you up and running: | ||
@@ -52,3 +74,3 @@ | ||
``` | ||
//cdnjs.cloudflare.com/ajax/libs/idbwrapper/1.2.0/idbstore.min.js | ||
//cdnjs.cloudflare.com/ajax/libs/idbwrapper/1.3.0/idbstore.min.js | ||
``` | ||
@@ -151,3 +173,3 @@ | ||
Use the following methods to read and write data: | ||
Use the following methods to read and write data (all methods in this section return the `IDBTransaction` they open): | ||
@@ -239,12 +261,101 @@ ___ | ||
##Batch Operations | ||
IDBWrapper allows to run a single method when dealing with multiple objects. All methods in this section return the `IDBTransaction` they open. | ||
1) The getBatch method. | ||
```javascript | ||
getBatch: function (/*Array*/keyArray, /*Function?*/onSuccess, /*Function?*/onError, /*String?*/arrayType) | ||
``` | ||
This method takes an array of keys and fetches matching objects. | ||
`keyArray` must be an array of keys identifying the objects to fetch. | ||
`arrayType` defines the type of array to pass to the success handler. May be one of 'sparse', 'dense' or 'skip'. Defaults to 'sparse'. This parameter specifies how to handle the situation if a get operation did not throw an error, but there was no matching object in the database. In most cases, 'sparse' provides the most desired behavior. See the examples for details: | ||
```javascript | ||
// given that there are two objects in the database with the keypath | ||
// values 1 and 2, and the call looks like this: | ||
myStore.getBatch([1, 5, 2], onError, function (data) { … }, arrayType); | ||
// this is what the `data` array will be like: | ||
// arrayType == 'sparse': | ||
// data is a sparse array containing two entries and having a length of 3: | ||
[Object, 2: Object] | ||
0: Object | ||
2: Object | ||
length: 3 | ||
__proto__: Array[0] | ||
// calling forEach on data will result in the callback being called two | ||
// times, with the index parameter matching the index of the key in the | ||
// keyArray. | ||
// arrayType == 'dense': | ||
// data is a dense array containing three entries and having a length of 3, | ||
// where data[1] is of type undefined: | ||
[Object, undefined, Object] | ||
0: Object | ||
1: undefined | ||
2: Object | ||
length: 3 | ||
__proto__: Array[0] | ||
// calling forEach on data will result in the callback being called three | ||
// times, with the index parameter matching the index of the key in the | ||
// keyArray, but the second call will have undefined as first argument. | ||
// arrayType == 'skip': | ||
// data is a dense array containing two entries and having a length of 2: | ||
[Object, Object] | ||
0: Object | ||
1: Object | ||
length: 2 | ||
__proto__: Array[0] | ||
// calling forEach on data will result in the callback being called two | ||
// times, with the index parameter not matching the index of the key in the | ||
// keyArray. | ||
``` | ||
___ | ||
6) The batch method. | ||
2) The putBatch method. | ||
```javascript | ||
putBatch: function (/*Array*/dataArray, /*Function?*/onSuccess, /*Function?*/onError) | ||
``` | ||
`putBatch` takes an array of objects and stores them in a single transaction. | ||
**Out-of-line Keys** | ||
The `putBatch` method does not support out-of-line keys. If you need to store multiple out-of-line objects, use the `batch` method. | ||
___ | ||
3) The removeBatch method. | ||
```javascript | ||
removeBatch: function (/*Array*/keyArray, /*Function?*/onSuccess, /*Function?*/onError) | ||
``` | ||
`removeBatch` takes an array of keys and removes matching objects in a single transaction. | ||
--- | ||
4) The batch method. | ||
```javascript | ||
batch: function (/*Array*/operations, /*Function?*/onSuccess, /*Function?*/onError) | ||
``` | ||
`batch` expects an array of operations that you want to apply in a single | ||
This method allows you to combine put and remove actions in a single call. `batch` expects an array of operations that you want to apply in a single | ||
IndexedDB transaction. `operations` is an Array of objects, each containing two | ||
@@ -293,27 +404,3 @@ properties, defining the type of operation. There are two operations | ||
___ | ||
7) The putBatch method. | ||
```javascript | ||
putBatch: function (/*Array*/dataArray, /*Function?*/onSuccess, /*Function?*/onError) | ||
``` | ||
`putBatch` takes an array of objects and stores them in a single transaction. | ||
___ | ||
8) The removeBatch method. | ||
```javascript | ||
removeBatch: function (/*Array*/keyArray, /*Function?*/onSuccess, /*Function?*/onError) | ||
``` | ||
`removeBatch` takes an array of keys and removes matching objects in a single transaction. | ||
Index Operations | ||
@@ -384,5 +471,3 @@ ---------------- | ||
To run queries, IDBWrapper provides a `query()` and an `iterate()` method. To | ||
create keyRanges, there is the `makeKeyRange()` method. In addition to these, | ||
IDBWrapper comes with a `count()` method. | ||
To run queries, IDBWrapper provides a `query()` and an `iterate()` method. To create keyRanges, there is the `makeKeyRange()` method. In addition to these, IDBWrapper comes with a `count()` method. The `query`, `iterate` and `count` methods return the `IDBTransaction` they open. | ||
@@ -471,2 +556,4 @@ ___ | ||
`only`: If you want a key range around only one value, pass just this property. | ||
___ | ||
@@ -473,0 +560,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
5559847
3839
571