1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
/*global window:false, self:false, define:false, module:false */
/**
* @license IDBWrapper - A cross-browser wrapper for IndexedDB
* Version 1.7.1
* Copyright (c) 2011 - 2016 Jens Arps
* http://jensarps.de/
*
* Licensed under the MIT (X11) license
*/
(function (name, definition, global) {
'use strict';
if (typeof define === 'function') {
define(definition);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = definition();
} else {
global[name] = definition();
}
})('IDBStore', function () {
'use strict';
var defaultErrorHandler = function (error) {
throw error;
};
var defaultSuccessHandler = function () {
};
var defaults = {
storeName: 'Store',
storePrefix: 'IDBWrapper-',
dbVersion: 1,
keyPath: 'id',
autoIncrement: true,
onStoreReady: function () {
},
onError: defaultErrorHandler,
indexes: [],
implementationPreference: [
'indexedDB',
'webkitIndexedDB',
'mozIndexedDB',
'shimIndexedDB'
]
};
/**
*
* The IDBStore constructor
*
* @constructor
* @name IDBStore
* @version 1.7.1
*
* @param {Object} [kwArgs] An options object used to configure the store and
* set callbacks
* @param {String} [kwArgs.storeName='Store'] The name of the store
* @param {String} [kwArgs.storePrefix='IDBWrapper-'] A prefix that is
* internally used to construct the name of the database, which will be
* kwArgs.storePrefix + kwArgs.storeName
* @param {Number} [kwArgs.dbVersion=1] The version of the store
* @param {String} [kwArgs.keyPath='id'] The key path to use. If you want to
* setup IDBWrapper to work with out-of-line keys, you need to set this to
* `null`
* @param {Boolean} [kwArgs.autoIncrement=true] If set to true, IDBStore will
* automatically make sure a unique keyPath value is present on each object
* that is stored.
* @param {Function} [kwArgs.onStoreReady] A callback to be called when the
* store is ready to be used.
* @param {Function} [kwArgs.onError=throw] A callback to be called when an
* error occurred during instantiation of the store.
* @param {Array} [kwArgs.indexes=[]] An array of indexData objects
* defining the indexes to use with the store. For every index to be used
* one indexData object needs to be passed in the array.
* An indexData object is defined as follows:
* @param {Object} [kwArgs.indexes.indexData] An object defining the index to
* use
* @param {String} kwArgs.indexes.indexData.name The name of the index
* @param {String} [kwArgs.indexes.indexData.keyPath] The key path of the index
* @param {Boolean} [kwArgs.indexes.indexData.unique] Whether the index is unique
* @param {Boolean} [kwArgs.indexes.indexData.multiEntry] Whether the index is multi entry
* @param {Array} [kwArgs.implementationPreference=['indexedDB','webkitIndexedDB','mozIndexedDB','shimIndexedDB']] An array of strings naming implementations to be used, in order or preference
* @param {Function} [onStoreReady] A callback to be called when the store
* is ready to be used.
* @example
// create a store for customers with an additional index over the
// `lastname` property.
var myCustomerStore = new IDBStore({
dbVersion: 1,
storeName: 'customer-index',
keyPath: 'customerid',
autoIncrement: true,
onStoreReady: populateTable,
indexes: [
{ name: 'lastname', keyPath: 'lastname', unique: false, multiEntry: false }
]
});
* @example
// create a generic store
var myCustomerStore = new IDBStore({
storeName: 'my-data-store',
onStoreReady: function(){
// start working with the store.
}
});
*/
var IDBStore = function (kwArgs, onStoreReady) {
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];
}
this.dbName = this.storePrefix + this.storeName;
this.dbVersion = parseInt(this.dbVersion, 10) || 1;
onStoreReady && (this.onStoreReady = onStoreReady);
var env = typeof window == 'object' ? window : self;
var availableImplementations = this.implementationPreference.filter(function (implName) {
return implName in env;
});
this.implementation = availableImplementations[0];
this.idb = env[this.implementation];
this.keyRange = env.IDBKeyRange || env.webkitIDBKeyRange || env.mozIDBKeyRange;
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();
};
/** @lends IDBStore.prototype */
var proto = {
/**
* A pointer to the IDBStore ctor
*
* @private
* @type {Function}
* @constructs
*/
constructor: IDBStore,
/**
* The version of IDBStore
*
* @type {String}
*/
version: '1.7.1',
/**
* A reference to the IndexedDB object
*
* @type {IDBDatabase}
*/
db: null,
/**
* The full name of the IndexedDB used by IDBStore, composed of
* this.storePrefix + this.storeName
*
* @type {String}
*/
dbName: null,
/**
* The version of the IndexedDB used by IDBStore
*
* @type {Number}
*/
dbVersion: null,
/**
* A reference to the objectStore used by IDBStore
*
* @type {IDBObjectStore}
*/
store: null,
/**
* The store name
*
* @type {String}
*/
storeName: null,
/**
* The prefix to prepend to the store name
*
* @type {String}
*/
storePrefix: null,
/**
* The key path
*
* @type {String}
*/
keyPath: null,
/**
* Whether IDBStore uses autoIncrement
*
* @type {Boolean}
*/
autoIncrement: null,
/**
* The indexes used by IDBStore
*
* @type {Array}
*/
indexes: null,
/**
* The implemantations to try to use, in order of preference
*
* @type {Array}
*/
implementationPreference: null,
/**
* The actual implementation being used
*
* @type {String}
*/
implementation: '',
/**
* The callback to be called when the store is ready to be used
*
* @type {Function}
*/
onStoreReady: null,
/**
* The callback to be called if an error occurred during instantiation
* of the store
*
* @type {Function}
*/
onError: null,
/**
* The internal insertID counter
*
* @type {Number}
* @private
*/
_insertIdCount: 0,
/**
* Opens an IndexedDB; called by the constructor.
*
* Will check if versions match and compare provided index configuration
* with existing ones, and update indexes if necessary.
*
* Will call this.onStoreReady() if everything went well and the store
* is ready to use, and this.onError() is something went wrong.
*
* @private
*
*/
openDB: function () {
var openRequest = this.idb.open(this.dbName, this.dbVersion);
var preventSuccessCallback = false;
openRequest.onerror = function (errorEvent) {
if (hasVersionError(errorEvent)) {
this.onError(new Error('The version number provided is lower than the existing one.'));
} else {
var error;
if (errorEvent.target.error) {
error = errorEvent.target.error;
} else {
var errorMessage = 'IndexedDB unknown error occurred when opening DB ' + this.dbName + ' version ' + this.dbVersion;
if ('errorCode' in errorEvent.target) {
errorMessage += ' with error code ' + errorEvent.target.errorCode;
}
error = new Error(errorMessage);
}
this.onError(error);
}
}.bind(this);
openRequest.onsuccess = function (event) {
if (preventSuccessCallback) {
return;
}
if (this.db) {
this.onStoreReady();
return;
}
this.db = event.target.result;
if (typeof this.db.version == 'string') {
this.onError(new Error('The IndexedDB implementation in this browser is outdated. Please upgrade your browser.'));
return;
}
if (!this.db.objectStoreNames.contains(this.storeName)) {
// We should never ever get here.
// Lets notify the user anyway.
this.onError(new Error('Object store couldn\'t be created.'));
return;
}
var emptyTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
this.store = emptyTransaction.objectStore(this.storeName);
// check indexes
var existingIndexes = Array.prototype.slice.call(this.getIndexList());
this.indexes.forEach(function (indexData) {
var indexName = indexData.name;
if (!indexName) {
preventSuccessCallback = true;
this.onError(new Error('Cannot create index: No index name given.'));
return;
}
this.normalizeIndexData(indexData);
if (this.hasIndex(indexName)) {
// check if it complies
var actualIndex = this.store.index(indexName);
var complies = this.indexComplies(actualIndex, indexData);
if (!complies) {
preventSuccessCallback = true;
this.onError(new Error('Cannot modify index "' + indexName + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
}
existingIndexes.splice(existingIndexes.indexOf(indexName), 1);
} else {
preventSuccessCallback = true;
this.onError(new Error('Cannot create new index "' + indexName + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
}
}, this);
if (existingIndexes.length) {
preventSuccessCallback = true;
this.onError(new Error('Cannot delete index(es) "' + existingIndexes.toString() + '" for current version. Please bump version number to ' + ( this.dbVersion + 1 ) + '.'));
}
preventSuccessCallback || this.onStoreReady();
}.bind(this);
openRequest.onupgradeneeded = function (/* IDBVersionChangeEvent */ event) {
this.db = event.target.result;
if (this.db.objectStoreNames.contains(this.storeName)) {
this.store = event.target.transaction.objectStore(this.storeName);
} else {
var optionalParameters = {autoIncrement: this.autoIncrement};
if (this.keyPath !== null) {
optionalParameters.keyPath = this.keyPath;
}
this.store = this.db.createObjectStore(this.storeName, optionalParameters);
}
var existingIndexes = Array.prototype.slice.call(this.getIndexList());
this.indexes.forEach(function (indexData) {
var indexName = indexData.name;
if (!indexName) {
preventSuccessCallback = true;
this.onError(new Error('Cannot create index: No index name given.'));
}
this.normalizeIndexData(indexData);
if (this.hasIndex(indexName)) {
// check if it complies
var actualIndex = this.store.index(indexName);
var complies = this.indexComplies(actualIndex, indexData);
if (!complies) {
// index differs, need to delete and re-create
this.store.deleteIndex(indexName);
this.store.createIndex(indexName, indexData.keyPath, {
unique: indexData.unique,
multiEntry: indexData.multiEntry
});
}
existingIndexes.splice(existingIndexes.indexOf(indexName), 1);
} else {
this.store.createIndex(indexName, indexData.keyPath, {
unique: indexData.unique,
multiEntry: indexData.multiEntry
});
}
}, this);
if (existingIndexes.length) {
existingIndexes.forEach(function (_indexName) {
this.store.deleteIndex(_indexName);
}, this);
}
}.bind(this);
},
/**
* Deletes the database used for this store if the IDB implementations
* provides that functionality.
*
* @param {Function} [onSuccess] A callback that is called if deletion
* was successful.
* @param {Function} [onError] A callback that is called if deletion
* failed.
*/
deleteDatabase: function (onSuccess, onError) {
if (this.idb.deleteDatabase) {
this.db.close();
var deleteRequest = this.idb.deleteDatabase(this.dbName);
deleteRequest.onsuccess = onSuccess;
deleteRequest.onerror = onError;
} else {
onError(new Error('Browser does not support IndexedDB deleteDatabase!'));
}
},
/*********************
* data manipulation *
*********************/
/**
* Puts an object into the store. If an entry with the given id exists,
* it will be overwritten. This method has a different signature for inline
* keys and out-of-line keys; please see the examples below.
*
* @param {*} [key] The key to store. This is only needed if IDBWrapper
* is set to use out-of-line keys. For inline keys - the default scenario -
* this can be omitted.
* @param {Object} value The data object to store.
* @param {Function} [onSuccess] A callback that is called if insertion
* was successful.
* @param {Function} [onError] A callback that is called if insertion
* failed.
* @returns {IDBTransaction} The transaction used for this operation.
* @example
// Storing an object, using inline keys (the default scenario):
var myCustomer = {
customerid: 2346223,
lastname: 'Doe',
firstname: 'John'
};
myCustomerStore.put(myCustomer, mySuccessHandler, myErrorHandler);
// Note that passing success- and error-handlers is optional.
* @example
// Storing an object, using out-of-line keys:
var myCustomer = {
lastname: 'Doe',
firstname: 'John'
};
myCustomerStore.put(2346223, myCustomer, mySuccessHandler, myErrorHandler);
// Note that passing success- and error-handlers is optional.
*/
put: function (key, value, onSuccess, onError) {
if (this.keyPath !== null) {
onError = onSuccess;
onSuccess = value;
value = key;
}
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
var hasSuccess = false,
result = null,
putRequest;
var putTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
putTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
putTransaction.onabort = onError;
putTransaction.onerror = onError;
if (this.keyPath !== null) { // in-line keys
this._addIdPropertyIfNeeded(value);
putRequest = putTransaction.objectStore(this.storeName).put(value);
} else { // out-of-line keys
putRequest = putTransaction.objectStore(this.storeName).put(value, key);
}
putRequest.onsuccess = function (event) {
hasSuccess = true;
result = event.target.result;
};
putRequest.onerror = onError;
return putTransaction;
},
/**
* Retrieves an object from the store. If no entry exists with the given id,
* the success handler will be called with null as first and only argument.
*
* @param {*} key The id of the object to fetch.
* @param {Function} [onSuccess] A callback that is called if fetching
* was successful. Will receive the object as only argument.
* @param {Function} [onError] A callback that will be called if an error
* occurred during the operation.
* @returns {IDBTransaction} The transaction used for this operation.
*/
get: function (key, onSuccess, onError) {
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
var hasSuccess = false,
result = null;
var getTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
getTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
getTransaction.onabort = onError;
getTransaction.onerror = onError;
var getRequest = getTransaction.objectStore(this.storeName).get(key);
getRequest.onsuccess = function (event) {
hasSuccess = true;
result = event.target.result;
};
getRequest.onerror = onError;
return getTransaction;
},
/**
* Removes an object from the store.
*
* @param {*} key The id of the object to remove.
* @param {Function} [onSuccess] A callback that is called if the removal
* was successful.
* @param {Function} [onError] A callback that will be called if an error
* occurred during the operation.
* @returns {IDBTransaction} The transaction used for this operation.
*/
remove: function (key, onSuccess, onError) {
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
var hasSuccess = false,
result = null;
var removeTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
removeTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
removeTransaction.onabort = onError;
removeTransaction.onerror = onError;
var deleteRequest = removeTransaction.objectStore(this.storeName)['delete'](key);
deleteRequest.onsuccess = function (event) {
hasSuccess = true;
result = event.target.result;
};
deleteRequest.onerror = onError;
return removeTransaction;
},
/**
* Runs a batch of put and/or remove operations on the store.
*
* @param {Array} dataArray An array of objects containing the operation to run
* and the data object (for put operations).
* @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.
* @returns {IDBTransaction} The transaction used for this operation.
*/
batch: function (dataArray, onSuccess, onError) {
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
if (Object.prototype.toString.call(dataArray) != '[object Array]') {
onError(new Error('dataArray argument must be of type Array.'));
} else if (dataArray.length === 0) {
return onSuccess(true);
}
var count = dataArray.length;
var called = false;
var hasSuccess = false;
var batchTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
batchTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(hasSuccess);
};
batchTransaction.onabort = onError;
batchTransaction.onerror = onError;
var onItemSuccess = function () {
count--;
if (count === 0 && !called) {
called = true;
hasSuccess = true;
}
};
dataArray.forEach(function (operation) {
var type = operation.type;
var key = operation.key;
var value = operation.value;
var onItemError = function (err) {
batchTransaction.abort();
if (!called) {
called = true;
onError(err, type, key);
}
};
if (type == 'remove') {
var deleteRequest = batchTransaction.objectStore(this.storeName)['delete'](key);
deleteRequest.onsuccess = onItemSuccess;
deleteRequest.onerror = onItemError;
} else if (type == 'put') {
var putRequest;
if (this.keyPath !== null) { // in-line keys
this._addIdPropertyIfNeeded(value);
putRequest = batchTransaction.objectStore(this.storeName).put(value);
} else { // out-of-line keys
putRequest = batchTransaction.objectStore(this.storeName).put(value, key);
}
putRequest.onsuccess = onItemSuccess;
putRequest.onerror = onItemError;
}
}, this);
return batchTransaction;
},
/**
* Takes an array of objects and stores them in a single transaction.
*
* @param {Array} dataArray An array of objects to store
* @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.
* @returns {IDBTransaction} The transaction used for this operation.
*/
putBatch: function (dataArray, onSuccess, onError) {
var batchData = dataArray.map(function (item) {
return {type: 'put', value: item};
});
return this.batch(batchData, onSuccess, onError);
},
/**
* Like putBatch, takes an array of objects and stores them in a single
* transaction, but allows processing of the result values. Returns the
* processed records containing the key for newly created records to the
* onSuccess calllback instead of only returning true or false for success.
* In addition, added the option for the caller to specify a key field that
* should be set to the newly created key.
*
* @param {Array} dataArray An array of objects to store
* @param {Object} [options] An object containing optional options
* @param {String} [options.keyField=this.keyPath] Specifies a field in the record to update
* with the auto-incrementing key. Defaults to the store's keyPath.
* @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.
* @returns {IDBTransaction} The transaction used for this operation.
*
*/
upsertBatch: function (dataArray, options, onSuccess, onError) {
// handle `dataArray, onSuccess, onError` signature
if (typeof options == 'function') {
onSuccess = options;
onError = onSuccess;
options = {};
}
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
options || (options = {});
if (Object.prototype.toString.call(dataArray) != '[object Array]') {
onError(new Error('dataArray argument must be of type Array.'));
}
var keyField = options.keyField || this.keyPath;
var count = dataArray.length;
var called = false;
var hasSuccess = false;
var index = 0; // assume success callbacks are executed in order
var batchTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
batchTransaction.oncomplete = function () {
if (hasSuccess) {
onSuccess(dataArray);
} else {
onError(false);
}
};
batchTransaction.onabort = onError;
batchTransaction.onerror = onError;
var onItemSuccess = function (event) {
var record = dataArray[index++];
record[keyField] = event.target.result;
count--;
if (count === 0 && !called) {
called = true;
hasSuccess = true;
}
};
dataArray.forEach(function (record) {
var key = record.key;
var onItemError = function (err) {
batchTransaction.abort();
if (!called) {
called = true;
onError(err);
}
};
var putRequest;
if (this.keyPath !== null) { // in-line keys
this._addIdPropertyIfNeeded(record);
putRequest = batchTransaction.objectStore(this.storeName).put(record);
} else { // out-of-line keys
putRequest = batchTransaction.objectStore(this.storeName).put(record, key);
}
putRequest.onsuccess = onItemSuccess;
putRequest.onerror = onItemError;
}, this);
return batchTransaction;
},
/**
* Takes an array of keys and removes matching objects in a single
* transaction.
*
* @param {Array} keyArray An array of keys to remove
* @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.
* @returns {IDBTransaction} The transaction used for this operation.
*/
removeBatch: function (keyArray, onSuccess, onError) {
var batchData = keyArray.map(function (key) {
return {type: 'remove', key: key};
});
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
// 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
// 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
// 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 = defaultSuccessHandler);
arrayType || (arrayType = 'sparse');
if (Object.prototype.toString.call(keyArray) != '[object Array]') {
onError(new Error('keyArray argument must be of type Array.'));
} else if (keyArray.length === 0) {
return onSuccess([]);
}
var data = [];
var count = keyArray.length;
var called = false;
var hasSuccess = false;
var result = null;
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 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.
*
* @param {Function} [onSuccess] A callback that is called if the operation
* was successful. Will receive an array of objects.
* @param {Function} [onError] A callback that will be called if an error
* occurred during the operation.
* @returns {IDBTransaction} The transaction used for this operation.
*/
getAll: function (onSuccess, onError) {
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
var getAllTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
var store = getAllTransaction.objectStore(this.storeName);
if (store.getAll) {
this._getAllNative(getAllTransaction, store, onSuccess, onError);
} else {
this._getAllCursor(getAllTransaction, store, onSuccess, onError);
}
return getAllTransaction;
},
/**
* Implements getAll for IDB implementations that have a non-standard
* getAll() method.
*
* @param {IDBTransaction} getAllTransaction An open READ transaction.
* @param {IDBObjectStore} store A reference to the store.
* @param {Function} onSuccess A callback that will be called if the
* operation was successful.
* @param {Function} onError A callback that will be called if an
* error occurred during the operation.
* @private
*/
_getAllNative: function (getAllTransaction, store, onSuccess, onError) {
var hasSuccess = false,
result = null;
getAllTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
getAllTransaction.onabort = onError;
getAllTransaction.onerror = onError;
var getAllRequest = store.getAll();
getAllRequest.onsuccess = function (event) {
hasSuccess = true;
result = event.target.result;
};
getAllRequest.onerror = onError;
},
/**
* Implements getAll for IDB implementations that do not have a getAll()
* method.
*
* @param {IDBTransaction} getAllTransaction An open READ transaction.
* @param {IDBObjectStore} store A reference to the store.
* @param {Function} onSuccess A callback that will be called if the
* operation was successful.
* @param {Function} onError A callback that will be called if an
* error occurred during the operation.
* @private
*/
_getAllCursor: function (getAllTransaction, store, onSuccess, onError) {
var all = [],
hasSuccess = false,
result = null;
getAllTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
getAllTransaction.onabort = onError;
getAllTransaction.onerror = onError;
var cursorRequest = store.openCursor();
cursorRequest.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
all.push(cursor.value);
cursor['continue']();
}
else {
hasSuccess = true;
result = all;
}
};
cursorRequest.onError = onError;
},
/**
* Clears the store, i.e. deletes all entries in the store.
*
* @param {Function} [onSuccess] A callback that will be called if the
* operation was successful.
* @param {Function} [onError] A callback that will be called if an
* error occurred during the operation.
* @returns {IDBTransaction} The transaction used for this operation.
*/
clear: function (onSuccess, onError) {
onError || (onError = defaultErrorHandler);
onSuccess || (onSuccess = defaultSuccessHandler);
var hasSuccess = false,
result = null;
var clearTransaction = this.db.transaction([this.storeName], this.consts.READ_WRITE);
clearTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
clearTransaction.onabort = onError;
clearTransaction.onerror = onError;
var clearRequest = clearTransaction.objectStore(this.storeName).clear();
clearRequest.onsuccess = function (event) {
hasSuccess = true;
result = event.target.result;
};
clearRequest.onerror = onError;
return clearTransaction;
},
/**
* Checks if an id property needs to present on a object and adds one if
* necessary.
*
* @param {Object} dataObj The data object that is about to be stored
* @private
*/
_addIdPropertyIfNeeded: function (dataObj) {
if (typeof dataObj[this.keyPath] == 'undefined') {
dataObj[this.keyPath] = this._insertIdCount++ + Date.now();
}
},
/************
* indexing *
************/
/**
* Returns a DOMStringList of index names of the store.
*
* @return {DOMStringList} The list of index names
*/
getIndexList: function () {
return this.store.indexNames;
},
/**
* Checks if an index with the given name exists in the store.
*
* @param {String} indexName The name of the index to look for
* @return {Boolean} Whether the store contains an index with the given name
*/
hasIndex: function (indexName) {
return this.store.indexNames.contains(indexName);
},
/**
* Normalizes an object containing index data and assures that all
* properties are set.
*
* @param {Object} indexData The index data object to normalize
* @param {String} indexData.name The name of the index
* @param {String} [indexData.keyPath] The key path of the index
* @param {Boolean} [indexData.unique] Whether the index is unique
* @param {Boolean} [indexData.multiEntry] Whether the index is multi entry
*/
normalizeIndexData: function (indexData) {
indexData.keyPath = indexData.keyPath || indexData.name;
indexData.unique = !!indexData.unique;
indexData.multiEntry = !!indexData.multiEntry;
},
/**
* Checks if an actual index complies with an expected index.
*
* @param {IDBIndex} actual The actual index found in the store
* @param {Object} expected An Object describing an expected index
* @return {Boolean} Whether both index definitions are identical
*/
indexComplies: function (actual, expected) {
var complies = ['keyPath', 'unique', 'multiEntry'].every(function (key) {
// IE10 returns undefined for no multiEntry
if (key == 'multiEntry' && actual[key] === undefined && expected[key] === false) {
return true;
}
// Compound keys
if (key == 'keyPath' && Object.prototype.toString.call(expected[key]) == '[object Array]') {
var exp = expected.keyPath;
var act = actual.keyPath;
// IE10 can't handle keyPath sequences and stores them as a string.
// The index will be unusable there, but let's still return true if
// the keyPath sequence matches.
if (typeof act == 'string') {
return exp.toString() == act;
}
// Chrome/Opera stores keyPath squences as DOMStringList, Firefox
// as Array
if (!(typeof act.contains == 'function' || typeof act.indexOf == 'function')) {
return false;
}
if (act.length !== exp.length) {
return false;
}
for (var i = 0, m = exp.length; i < m; i++) {
if (!( (act.contains && act.contains(exp[i])) || act.indexOf(exp[i] !== -1) )) {
return false;
}
}
return true;
}
return expected[key] == actual[key];
});
return complies;
},
/**********
* cursor *
**********/
/**
* Iterates over the store using the given options and calling onItem
* for each entry matching the options.
*
* @param {Function} onItem A callback to be called for each match
* @param {Object} [options] An object defining specific options
* @param {String} [options.index=null] A name of an IDBIndex to operate on
* @param {String} [options.order=ASC] The order in which to provide the
* results, can be 'DESC' or 'ASC'
* @param {Boolean} [options.autoContinue=true] Whether to automatically
* iterate the cursor to the next result
* @param {Boolean} [options.filterDuplicates=false] Whether to exclude
* duplicate matches
* @param {IDBKeyRange} [options.keyRange=null] An IDBKeyRange to use
* @param {Boolean} [options.writeAccess=false] Whether grant write access
* to the store in the onItem callback
* @param {Function} [options.onEnd=null] A callback to be called after
* iteration has ended
* @param {Function} [options.onError=throw] A callback to be called
* if an error occurred during the operation.
* @param {Number} [options.limit=Infinity] Limit the number of returned
* results to this number
* @param {Number} [options.offset=0] Skip the provided number of results
* in the resultset
* @param {Boolean} [options.allowItemRejection=false] Allows the onItem
* function to return a Boolean to accept or reject the current item
* @returns {IDBTransaction} The transaction used for this operation.
*/
iterate: function (onItem, options) {
options = mixin({
index: null,
order: 'ASC',
autoContinue: true,
filterDuplicates: false,
keyRange: null,
writeAccess: false,
onEnd: null,
onError: defaultErrorHandler,
limit: Infinity,
offset: 0,
allowItemRejection: false
}, options || {});
var directionType = options.order.toLowerCase() == 'desc' ? 'PREV' : 'NEXT';
if (options.filterDuplicates) {
directionType += '_NO_DUPLICATE';
}
var hasSuccess = false;
var cursorTransaction = this.db.transaction([this.storeName], this.consts[options.writeAccess ? 'READ_WRITE' : 'READ_ONLY']);
var cursorTarget = cursorTransaction.objectStore(this.storeName);
if (options.index) {
cursorTarget = cursorTarget.index(options.index);
}
var recordCount = 0;
cursorTransaction.oncomplete = function () {
if (!hasSuccess) {
options.onError(null);
return;
}
if (options.onEnd) {
options.onEnd();
} else {
onItem(null);
}
};
cursorTransaction.onabort = options.onError;
cursorTransaction.onerror = options.onError;
var cursorRequest = cursorTarget.openCursor(options.keyRange, this.consts[directionType]);
cursorRequest.onerror = options.onError;
cursorRequest.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
if (options.offset) {
cursor.advance(options.offset);
options.offset = 0;
} else {
var onItemReturn = onItem(cursor.value, cursor, cursorTransaction);
if (!options.allowItemRejection || onItemReturn !== false) {
recordCount++;
}
if (options.autoContinue) {
if (recordCount + options.offset < options.limit) {
cursor['continue']();
} else {
hasSuccess = true;
}
}
}
} else {
hasSuccess = true;
}
};
return cursorTransaction;
},
/**
* Runs a query against the store and passes an array containing matched
* objects to the success handler.
*
* @param {Function} onSuccess A callback to be called when the operation
* was successful.
* @param {Object} [options] An object defining specific options
* @param {String} [options.index=null] A name of an IDBIndex to operate on
* @param {String} [options.order=ASC] The order in which to provide the
* results, can be 'DESC' or 'ASC'
* @param {Boolean} [options.filterDuplicates=false] Whether to exclude
* duplicate matches
* @param {IDBKeyRange} [options.keyRange=null] An IDBKeyRange to use
* @param {Function} [options.onError=throw] A callback to be called
* if an error occurred during the operation.
* @param {Number} [options.limit=Infinity] Limit the number of returned
* results to this number
* @param {Number} [options.offset=0] Skip the provided number of results
* in the resultset
* @param {Function} [options.filter=null] A custom filter function to
* apply to query resuts before returning. Must return `false` to reject
* an item. Can be combined with keyRanges.
* @returns {IDBTransaction} The transaction used for this operation.
*/
query: function (onSuccess, options) {
var result = [],
processedItems = 0;
options = options || {};
options.autoContinue = true;
options.writeAccess = false;
options.allowItemRejection = !!options.filter;
options.onEnd = function () {
onSuccess(result, processedItems);
};
return this.iterate(function (item) {
processedItems++;
var accept = options.filter ? options.filter(item) : true;
if (accept !== false) {
result.push(item);
}
return accept;
}, options);
},
/**
*
* Runs a query against the store, but only returns the number of matches
* instead of the matches itself.
*
* @param {Function} onSuccess A callback to be called if the opration
* was successful.
* @param {Object} [options] An object defining specific options
* @param {String} [options.index=null] A name of an IDBIndex to operate on
* @param {IDBKeyRange} [options.keyRange=null] An IDBKeyRange to use
* @param {Function} [options.onError=throw] A callback to be called if an error
* occurred during the operation.
* @returns {IDBTransaction} The transaction used for this operation.
*/
count: function (onSuccess, options) {
options = mixin({
index: null,
keyRange: null
}, options || {});
var onError = options.onError || defaultErrorHandler;
var hasSuccess = false,
result = null;
var cursorTransaction = this.db.transaction([this.storeName], this.consts.READ_ONLY);
cursorTransaction.oncomplete = function () {
var callback = hasSuccess ? onSuccess : onError;
callback(result);
};
cursorTransaction.onabort = onError;
cursorTransaction.onerror = onError;
var cursorTarget = cursorTransaction.objectStore(this.storeName);
if (options.index) {
cursorTarget = cursorTarget.index(options.index);
}
var countRequest = cursorTarget.count(options.keyRange);
countRequest.onsuccess = function (evt) {
hasSuccess = true;
result = evt.target.result;
};
countRequest.onError = onError;
return cursorTransaction;
},
/**************/
/* key ranges */
/**************/
/**
* Creates a key range using specified options. This key range can be
* handed over to the count() and iterate() methods.
*
* Note: You must provide at least one or both of "lower" or "upper" value.
*
* @param {Object} options The options for the key range to create
* @param {*} [options.lower] The lower bound
* @param {Boolean} [options.excludeLower] Whether to exclude the lower
* bound passed in options.lower from the key range
* @param {*} [options.upper] The upper bound
* @param {Boolean} [options.excludeUpper] Whether to exclude the upper
* 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 {IDBKeyRange} The IDBKeyRange representing the specified options
*/
makeKeyRange: function (options) {
/*jshint onecase:true */
var keyRange,
hasLower = typeof options.lower != '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:
keyRange = this.keyRange.bound(options.lower, options.upper, options.excludeLower, options.excludeUpper);
break;
case hasLower:
keyRange = this.keyRange.lowerBound(options.lower, options.excludeLower);
break;
case hasUpper:
keyRange = this.keyRange.upperBound(options.upper, options.excludeUpper);
break;
default:
throw new Error('Cannot create KeyRange. Provide one or both of "lower" or "upper" value, or an "only" value.');
}
return keyRange;
}
};
/** helpers **/
var empty = {};
function mixin (target, source) {
var name, s;
for (name in source) {
s = source[name];
if (s !== empty[name] && s !== target[name]) {
target[name] = s;
}
}
return target;
}
function hasVersionError(errorEvent) {
if ('error' in errorEvent.target) {
return errorEvent.target.error.name == 'VersionError';
} else if ('errorCode' in errorEvent.target) {
return errorEvent.target.errorCode == 12;
}
return false;
}
IDBStore.prototype = proto;
IDBStore.version = proto.version;
return IDBStore;
}, this);