-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathixDbEz.js
630 lines (544 loc) · 34.5 KB
/
ixDbEz.js
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
//*********************************************************************************************************
//ixDbEz - IndexedDB EZ is a js wrapper for IndexedDB providing rapid client-side development
// of IndexedDB databases.
//
// Copyright (C) 2012 - Jake Drew
// Dual licensed under the MIT and GPL licenses.
// - http://www.opensource.org/licenses/mit-license.php
// - http://www.gnu.org/copyleft/gpl.html
//
//Created By - Jake Drew
//Version - 1.0, 07/16/2012
//Version - 2.0, 08/11/2012
// 1. Added Support for ixDbSync.
// 2. Made all error messages consistent, fixed error event capture.
// 3. Updated all data change functions to detect the IndexedDB API versionchange
// transaction and re-schedule themselves for the oncomplete event in the event that
// they are called during the version change transaction.
// 4. Added clear() function for the IndexedDB API clear method.
// 5. Added keyRange variable to getCursor() function to support IDBKeyRanges.
// 6. Added objectContainsProperty method to identify, if an object contains a property
// prior to adding the property to the object (used in ixDbEz and ixDbEzSync)
// 7. indexName was added to getCursor() function to provide support for opening index
// based cursors.
//*********************************************************************************************************
var ixDbEz = (function () {
//Populate the window.indexedDB variables with the appropriate browser specific instance.
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.mozIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange || window.msIDBKeyRange;
var ixDb; //The current ixdb database instance being accessed in all functions below.
var ixDbRequest; //The current ixdb request instance being accessed in all functions below.
var ixDbVersionTansaction; //Holds a reference to a versionchange transaction object anytime a version change is in process.
var ixDbVersionRequest;
var ixDbSyncFlag;
//*********************************************************************************************************
//Function StartDB - Open or create the requested database and populate the variable ixDb with the new IndexedDB instance.
// dbName - Name of the IndexedDB database to open or create
// dbVersion - MUST be a valid integer. If not, the database is given a version number = 1.
// ixdbDDL - javascript var that contains a function with all the IndexedDB's valid ixDbEz DDL calls (see usage example)
// onSuccess - (optional) callback function to execute if function successful.
// onError - (optional) callback function to execute if function fails.
// ixDbSync - (optional) if true, fires ixDbSync events on all datachanges.
// !!!only use the ixDbSync option for ixDbSync.js data server-side synchronization!!!
//*********************************************************************************************************
function StartDB_(dbName, dbVersion, ixdbDDL, onSuccess, onError, useIxDbSync) {
//Check to see if we have a browser that supports IndexedDB
if (window.indexedDB) {
//Trigger data synchronization hooks, if ixDbSync is being used.
ixDbSyncFlag = useIxDbSync;
//Open or create the requested IndexedDB Database
ixDbRequest = window.indexedDB.open(dbName, dbVersion);
var newVersion = parseInt(dbVersion || 1);
newVersion = isNaN(newVersion) || newVersion == null ? 1 : newVersion;
ixDbRequest.onsuccess = function (e) {
ixDb = ixDbRequest.result || e.result; // FF4 requires e.result.
//Check to see if a database upgrade is required.
//This logic should work with Chrome until they catch up with Firefox and support onupgradeneeded event.
//Also works on older browsers builds that still support setVersion
if (typeof ixDb.setVersion === "function") {
var oldVersion = parseInt(ixDb.version || -1001);
oldVersion = isNaN(oldVersion) || oldVersion == null ? -1001 : oldVersion;
if (oldVersion < newVersion) {
ixDbVersionRequest = ixDb.setVersion(newVersion);
//Get a reference to the version request from the old setVersion method.
//ixDbVersionRequest = verRequest; //.result || e.currentTarget.result;
ixDbVersionRequest.onerror = ixDbEz.onerror;
ixDbVersionRequest.onsuccess = function (e) {
ixDbVersionTansaction = e.result || e.currentTarget.result;
//log successful database creation
console.log('ixDbEz: Created Database: ' + dbName + ', Version: ' + newVersion + '.');
//Create database structure using function provided by the user.
ixdbDDL();
//Create any required ixDbSync database structures
if(ixDbSyncFlag) {
ixDbSync.syncDDL();
}
//must clear version request so getCursor callback works right after db creation
ixDbVersionRequest = undefined;
//destroy the version trasaction variable (since version change transactions lock the database)
ixDbVersionTansaction = undefined;
}
}
else {
//log successful database open
console.log('ixDbEz: Opened Database: ' + dbName + ', Version: ' + newVersion + '.')
}
}
//execute onsuccess function, if one was provided
if(typeof onSuccess === 'function') {
onSuccess();
}
};
ixDbRequest.onerror = function (e) {
logError(e, onError, ixDbVersionTansaction);
console.log('ixDbEz Error: Opened Database: ' + dbName + ', Version: ' + newVersion + ' failed.')
};
//The onupgradeneeded event is not yet supported by Chrome and requires a hook in the onsuccess event above.
ixDbRequest.onupgradeneeded = function (e) {
//FF uses this event to fire DDL function for upgrades. All browsers will eventually use this method. Per - W3C Working Draft 24 May 2012
ixDb = ixDbRequest.result || e.currentTarget.result;
//Get a reference to the version transaction via the onupgradeneeded event (e)
ixDbVersionTansaction = e.transaction || e.currentTarget.transaction;
//Clear out upgrade flags as soon as the upgrade is completed.
ixDbVersionTansaction.oncomplete = function (e) {
//must clear version request so getCursor callback works right after db creation
ixDbVersionRequest = undefined;
//destroy the version trasaction variable (since version change transactions lock the database)
ixDbVersionTansaction = undefined;
};
//log successful database creation
console.log('ixDbEz: Created Database: ' + dbName + ', Version: ' + newVersion + '.');
//Create database using function provided by the user.
ixdbDDL();
//Create any required ixDbSync database structures
if(ixDbSyncFlag) {
ixDbSync.syncDDL();
}
};
}
}
//*********************************************************************************************************
//Function CreateObjStore - Create IndexedDB object store (similar to a table)
// objectStoreName - Name of the Object Store / Table "MyOsName"
// pkName - Keypath name (Similar to Primary Key)
// autoIncrement - true or false (assigns an autonumber to the primary key / Keypath value)
// Default value = false.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//*********************************************************************************************************
function CreateObjStore_(objectStoreName, pkName, autoIncrement, skipSync) {
//Create a default value for the autoIncrement variable
autoIncrement = typeof autoIncrement === 'undefined' ? false : autoIncrement;
var objectStore;
try {
objectStore = ixDb.createObjectStore(objectStoreName, { keyPath: pkName, autoIncrement: autoIncrement });
//ixDbSync - data sync hook
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
ixDbSync.createObjStoreSync(objectStoreName, pkName, autoIncrement);
}
//Log os creation. onsuccess does not fire for objectStore!
console.log('ixDbEz: Created ObjectStore ' + objectStoreName + '.');
} catch (e) {
logError(e);
console.log('ixDbEz Error: Create ObjectStore ' + objectStoreName + ' failed.');
}
return objectStore;
}
//*********************************************************************************************************
//Function CreateIndex - Create IndexedDB object store index (similar to a table index on a field)
// objectStoreName - Name of the Object Store / Table "MyOsName"
// ixName - Name of the Index to create
// fieldName - Keypath name to add the index too. (Can the name of any property / field in the object store)
// unique - true or false, if True - all values in the index must be unique.
// Default value = false.
// multiEntry - true or false, see w3 documentation: http://www.w3.org/TR/IndexedDB/#dfn-multientry
// Default value = false.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//*********************************************************************************************************
function CreateIndex_(objectStoreName, ixName, fieldName, unique, multiEntry, skipSync) {
//Create a default value for the autoIncrement variable
unique = typeof unique === 'undefined' ? false : unique;
multiEntry = typeof multiEntry === 'undefined' ? false : multiEntry;
try {
var ObjectStore = ixDbVersionTansaction.objectStore(objectStoreName);
var index = ObjectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry });
//ixDbSync - data sync hook
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
ixDbSync.createIndexSync(objectStoreName, ixName, fieldName, unique, multiEntry);
}
//Log index creation. onsuccess does not fire for index!
console.log('ixDbEz: Created index: ' + ixName + ' against keypath: ' + fieldName + '.');
} catch (e) {
logError(e);
console.log('ixDbEz Error: Created index - ' + ixName + ' failed.');
}
}
//*********************************************************************************************************
//Function Add - Insert a record into an object store.
// objectStoreName - Name of the Object Store / Table "MyOsName"
// value - Record object or value to insert.
// key - (optional) Key to access record.
// onSuccess - (optional) callback function to execute if function successful.
// onError - (optional) callback function to execute if function fails.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//*********************************************************************************************************
function Add_(objectStoreName, value, key, onSuccess, onError, skipSync) {
if (ixDb) {
//The database is being created or upgraded, re-run when completed.
if(ixDbVersionTansaction) {
ixDbVersionTansaction.addEventListener ("complete", function() { Add_(objectStoreName, value, key, onSuccess, onError, skipSync); }, false);
return;
}
var transaction = ixDb.transaction(objectStoreName, "readwrite" ); //IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore(objectStoreName);
request = typeof key === 'undefined' ? objectStore.add(value) : objectStore.add(value, key);
request.onsuccess = function (e) {
if(typeof onSuccess === 'function') {
onSuccess();
}
//ixDbSync - data sync hook
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
ixDbSync.addPutSync(objectStoreName, value, key, "Add");
}
console.log('ixDbEz: Created record in ObjectStore: ' + objectStoreName + ".");
};
request.onerror = function (e) {
logError(e, onError);
console.log('ixDbEz Error: Create record in ObjectStore: ' + objectStoreName + " failed.");
}
}
else {
//The database is in the middle of opening
if (ixDbRequest) {
ixDbRequest.addEventListener ("success", function() { Add_(objectStoreName, value, key, onSuccess, onError, skipSync); }, false);
}
}
}
//*********************************************************************************************************
//Function Clear - Delete all records from an object store.
// onSuccess - (optional) callback function to execute if function successful.
// onError - (optional) callback function to execute if function fails.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//*********************************************************************************************************
function Clear_(objectStoreName, onSuccess, onError, skipSync) {
if (ixDb) {
//The database is being created or upgraded, re-run when completed.
if(ixDbVersionTansaction) {
ixDbVersionTansaction.addEventListener ("complete", function() { Clear_(objectStoreName, onSuccess, onError, skipSync) }, false);
return;
}
var transaction = ixDb.transaction(objectStoreName, "readwrite"); // IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore(objectStoreName);
var request = objectStore.clear();
request.onsuccess = function (e) {
if(typeof onSuccess === 'function') {
onSuccess();
}
//ixDbSync - data sync hook
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
ixDbSync.clearSync(objectStoreName);
}
console.log('ixDbEz: Deleted all records from ObjectStore ' + objectStoreName + ".");
};
request.onerror = function (e) {
logError(e, onError);
console.log('ixDbEz Error: Clear ObjectStore: ' + objectStoreName + " failed.");
}
}
else {
//The database is in the middle of opening
if (ixDbRequest) {
ixDbRequest.addEventListener ("success", function() { Clear_(objectStoreName, onSuccess, onError, skipSync) }, false);
}
}
}
//*********************************************************************************************************
//Function Put - Replace or insert a record in an object store.
// objectStoreName - Name of the Object Store / Table "MyOsName"
// value - Record object or value to insert.
// key - (optional) Key to access record.
// onSuccess - (optional) callback function to execute if function successful.
// onError - (optional) callback function to execute if function fails.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//*********************************************************************************************************
function Put_(objectStoreName, value, key, onSuccess, onError, skipSync) {
if (ixDb) {
//The database is being created or upgraded, re-run when completed.
if(ixDbVersionTansaction) {
ixDbVersionTansaction.addEventListener ("complete", function() { Put_(objectStoreName, value, key, onSuccess, onError, skipSync); }, false);
return;
}
var transaction = ixDb.transaction(objectStoreName, "readwrite"); //IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore(objectStoreName);
var request = typeof key === 'undefined' ? objectStore.put(value) : objectStore.put(value, key);
request.onsuccess = function (e) {
if(typeof onSuccess === 'function') {
onSuccess();
}
//ixDbSync - data sync hook
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
//Pass the primary key value to ixDbSync for the server lastUpdateLog
var logKey = typeof key === 'undefined' ? value[objectStore.keyPath] : key;
ixDbSync.addPutSync(objectStoreName, value, key, "Put", logKey);
}
console.log('ixDbEz: Put record into ObjectStore ' + objectStoreName + ".");
};
request.onerror = function (e) {
logError(e, onError);
console.log('ixDbEz Error: Put record into ObjectStore ' + objectStoreName + " failed.");
}
}
else {
if (ixDbRequest) {
ixDbRequest.addEventListener ("success", function() { Put_(objectStoreName, value, key, onSuccess, onError, skipSync); }, false);
}
}
}
//*********************************************************************************************************
//Function updateKey - Replace or insert a record in an object store.
// objectStoreName - Name of the Object Store / Table "MyOsName"
// oldKey - The Key value that needs to be updated.
// newKey - New value for the oldKey.
// onSuccess - (optional) callback function to execute if function successful.
// onError - (optional) callback function to execute if function fails.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//
// newKey Warning! - If newKey exists in the ObjectStore, it's value will be replaced by oldKey.value
//*********************************************************************************************************
function UpdateKey_(objectStoreName, oldKey, newKey, onSuccess, onError, skipSync) {
if (ixDb) {
//The database is being created or upgraded, re-run when completed.
if(ixDbVersionTansaction) {
ixDbVersionTansaction.addEventListener ("complete", function() { UpdateKey_(objectStoreName, oldKey, newKey, onSuccess, onError, skipSync); }, false);
return;
}
var keyInObject = false;
var transaction = ixDb.transaction(objectStoreName, "readwrite"); // IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore(objectStoreName);
//Check oldKey exists request
var request = objectStore.get(oldKey);
request.onsuccess = function (e) {
//Get the value from the oldKey record
var oldKeyResult = e.result||this.result;
//oldKey provided does not exist in database.
if(typeof oldKeyResult === 'undefined'){
console.log('ixDbEz Error: updateKey failed. Key: ' + oldKey + ' does not exist in ObjectStore ' + objectStoreName + ".");
}
//oldKey provided does exist in the database
else {
//if the value in the oldKey record is an object, and that object contains a
//property that matches the current ObjectStore's KeyPath name, update that property
//with the newKey value.
if(typeof oldKeyResult === 'object' && objectContainsProperty_(oldKeyResult, objectStore.keyPath)){
oldKeyResult[objectStore.keyPath] = newKey;
//since the newKey was updated in the object, newKey variable must = undefined
//or add_ and put_ will fail. keyInObject is checked later to set newKey = undefined
keyInObject = true;
}
//delete oldKey request
var request = objectStore.delete(oldKey);
//ixDbSync - data sync hook for above delete operation
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
ixDbSync.deleteSync(objectStoreName, oldKey);
}
request.onsuccess = function (e) {
//check newKey exists request
var request = objectStore.get(newKey);
request.onsuccess = function (e) {
var newKeyResult = e.result || this.result;
//newKey provided does not exist in database, so a new record is added
if(typeof newKeyResult === 'undefined'){
if(keyInObject){
Add_(objectStoreName, oldKeyResult, undefined , onSuccess, onError, skipSync);
}
else{
Add_(objectStoreName, oldKeyResult, newKey, onSuccess, onError, skipSync);
}
}
//newKey does exist in database, and it's value is replaced.
else {
if(keyInObject){
Put_(objectStoreName, oldKeyResult, undefined , onSuccess, onError, skipSync);
}
else{
Put_(objectStoreName, oldKeyResult, newKey, onSuccess, onError, skipSync);
}
} //else - newKey exists
} //check newKey.onsuccess
//check newKey failed
request.onerror = function (e) {
var errEvent = e.result||this.result;
logError(errEvent, onError);
console.log('ixDbEz Error: updateKey failed. Key: ' + newKey + ' is not valid in ObjectStore: ' + objectStoreName + ".");
}
} //delete oldKey.onsuccess
//delete oldKey failed
request.onerror = function (e) {
var errEvent = e.result||this.result;
logError(errEvent, onError);
console.log('ixDbEz Error: updateKey failed. Could not delete Key: ' + oldKey + ' from ObjectStore: ' + objectStoreName + ".");
}
} //else - oldKey exists
} //check oldKey.onsuccess
//check oldKey failed
request.onerror = function (e) {
var errEvent = e.result||this.result;
logError(errEvent, onError);
console.log('ixDbEz Error: updateKey failed. Key: ' + oldKey + ' is not valid in ObjectStore: ' + objectStoreName + ".");
}
} // ixDb exists
else{
if (ixDbRequest) {
ixDbRequest.addEventListener ("success", function() { UpdateKey_(objectStoreName, oldKey, newKey, onSuccess, onError, skipSync); }, false);
}
}
} // function
//*********************************************************************************************************
//Function Delete - Delete a record in an object store.
// objectStoreName - Name of the Object Store / Table "MyOsName"
// key - Key of the record to be deleted.
// onSuccess - (optional) callback function to execute if function successful.
// onError - (optional) callback function to execute if function fails.
// skipSync - (optional) If true, skips all data synchronization in ixDbSync for a single transaction.
//*********************************************************************************************************
function Delete_(objectStoreName, key, onSuccess, onError, skipSync) {
if (ixDb) {
//The database is being created or upgraded, re-run when completed.
if(ixDbVersionTansaction) {
ixDbVersionTansaction.addEventListener ("complete", function() { Delete_(objectStoreName, key, onSuccess, onError, skipSync); }, false);
return;
}
var transaction = ixDb.transaction(objectStoreName, "readwrite"); // IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore(objectStoreName);
request = objectStore.delete(key);
request.onsuccess = function (e) {
if(typeof onSuccess === 'function') {
onSuccess();
}
//ixDbSync - data sync hook
if(ixDbSyncFlag && objectStoreName != "ixDbSync" && skipSync != true) {
ixDbSync.deleteSync(objectStoreName, key);
}
console.log('ixDbEz: Deleted record key: ' + key + ' from ObjectStore ' + objectStoreName + ".");
};
request.onerror = function (e) {
var errEvent = e.result||this.result;
logError(errEvent, onError);
console.log('ixDbEz Error: Deleted record key: ' + key + ' from ObjectStore ' + objectStoreName + " failed.");
}
}
else{
if (ixDbRequest) {
ixDbRequest.addEventListener ("success", function() { Delete_(objectStoreName, key, onSuccess, onError, skipSync); }, false);
}
}
}
//*********************************************************************************************************
//Function getCursor - Returns a cursor for the requested ObjectStore
// objectStoreName - Name of the Object Store / Table "MyOsName"
// onSuccess - Name of the function to call and pass the cursor request back to upon
// successful completion.
// onError - (optional) callback function to execute if function fails.
// keyRange - (optional) IDBKeyRange to use when getting the cursor.
// readWrite - (optional) If set to true, returns a readwrite cursor.
// indexName - (optional) Name of a valid objectStore index. If provided, the cursor is opened
// using the requested indexName.
// onSuccess Ex. - getCursor_("ObjectStore_Name", MyCallBackFunction)
// !! onSuccess function definition must have input variable for the request object !!
//
// Function MyCallBackFunction(CursorRequestObj) {
// CursorRequestObj.onsuccess = function() {//do stuff here};
// }
//
//*********************************************************************************************************
function getCursor_(objectStoreName, onSuccess, onError, keyRange, readWrite, indexName) {
//The the openCursor call is asynchronous, so we must check to ensure a database
//connection has been established and then provide the cursor via callback.
if (ixDb) {
//If the database is in the middle of an upgrade, return an undefined cursor.
if(ixDbVersionTansaction || ixDbVersionRequest) {
onSuccess();
}
else
{
try{
var transaction;
if(readWrite == true) {
transaction = ixDb.transaction(objectStoreName, "readwrite"); // IDBTransaction.READ_ONLY);
}
else{
transaction = ixDb.transaction(objectStoreName, "readonly");
}
var objectStore = transaction.objectStore(objectStoreName);
//If an indexName is provided, the cursor is opened using the requested index
//Otherwise it is opened via the object store. In either case, cursor is opened using
//a keyRange, if one is provided.
var cursor;
if(typeof indexName === "undefined") {
cursor = typeof keyRange === "undefined" ? objectStore.openCursor() : objectStore.openCursor(keyRange);
}
else {
var index = objectStore.index(indexName);
cursor = typeof keyRange === "undefined" ? index.openCursor() : index.openCursor(keyRange);
}
//Return the requested cursor via the callback function provided by the user.
onSuccess(cursor);
console.log('ixDbEz: Getting cursor request for ' + objectStoreName + ".");
}
catch(e){
logError(e, onError);
console.log('ixDbEz Error: getCursor failed');
}
}
}
else {
if (ixDbRequest) {
ixDbRequest.addEventListener ("success", function() { getCursor_(objectStoreName, onSuccess, onError); }, false);
}
}
}
//*********************************************************************************************************
//Function objectContainsProperty - Returns true if the object contains the requested property, otherwise false.
// object - A valid javascript object.
// property - A string with the requested property name.
//*********************************************************************************************************
function objectContainsProperty_(object, property){
var prototype = object.__prototype__ || object.constructor.prototype;
return (property in object) && (!(property in prototype)
|| prototype[property] !== object[property]);
}
return {
startDB: StartDB_,
createObjStore: CreateObjStore_,
createIndex: CreateIndex_,
add : Add_,
clear: Clear_,
put : Put_,
delete : Delete_,
getCursor: getCursor_,
updateKey : UpdateKey_,
objectContainsProperty: objectContainsProperty_
}
})();
//default console error handler
ixDbEz.onerror = function () { logError(e) };
//*********************************************************************************************************
//Function logError - Writes all errors to console.log
// errEvent - event objects or and other javascript object which contains a errEvent.code
// and errEvent.message property.
// onError - (optional) callback function to execute if function fails.
//
// Tip - Re-route any Console.log messages to whereever you want. (div file etc...)
// window.console.log = function (msg) { //your code here }
//*********************************************************************************************************
function logError(errEvent, onError, transaction) {
//if a valid onError function was passed, execute it.
if(typeof onError === 'function') {
onError();
}
//if a transaction object was passed, attempt to abort it
if(typeof transaction !== 'undefined' && transaction.constructor.name == "IDBTransaction") {
transaction.abort();
}
console.log('ixDbEz Error' + '(' + errEvent.code + '): ' + errEvent.message + '.');
}