(api, opts, callback)
| 5875 | } |
| 5876 | |
| 5877 | function init(api, opts, callback) { |
| 5878 | |
| 5879 | var dbName = opts.name; |
| 5880 | |
| 5881 | var idb = null; |
| 5882 | var idbGlobalFailureError = null; |
| 5883 | api._meta = null; |
| 5884 | |
| 5885 | function enrichCallbackError(callback) { |
| 5886 | return function (error, result) { |
| 5887 | if (error && error instanceof Error && !error.reason) { |
| 5888 | if (idbGlobalFailureError) { |
| 5889 | error.reason = idbGlobalFailureError; |
| 5890 | } |
| 5891 | } |
| 5892 | |
| 5893 | callback(error, result); |
| 5894 | }; |
| 5895 | } |
| 5896 | |
| 5897 | // called when creating a fresh new database |
| 5898 | function createSchema(db) { |
| 5899 | var docStore = db.createObjectStore(DOC_STORE, {keyPath : 'id'}); |
| 5900 | db.createObjectStore(BY_SEQ_STORE, {autoIncrement: true}) |
| 5901 | .createIndex('_doc_id_rev', '_doc_id_rev', {unique: true}); |
| 5902 | db.createObjectStore(ATTACH_STORE, {keyPath: 'digest'}); |
| 5903 | db.createObjectStore(META_STORE, {keyPath: 'id', autoIncrement: false}); |
| 5904 | db.createObjectStore(DETECT_BLOB_SUPPORT_STORE); |
| 5905 | |
| 5906 | // added in v2 |
| 5907 | docStore.createIndex('deletedOrLocal', 'deletedOrLocal', {unique : false}); |
| 5908 | |
| 5909 | // added in v3 |
| 5910 | db.createObjectStore(LOCAL_STORE, {keyPath: '_id'}); |
| 5911 | |
| 5912 | // added in v4 |
| 5913 | var attAndSeqStore = db.createObjectStore(ATTACH_AND_SEQ_STORE, |
| 5914 | {autoIncrement: true}); |
| 5915 | attAndSeqStore.createIndex('seq', 'seq'); |
| 5916 | attAndSeqStore.createIndex('digestSeq', 'digestSeq', {unique: true}); |
| 5917 | } |
| 5918 | |
| 5919 | // migration to version 2 |
| 5920 | // unfortunately "deletedOrLocal" is a misnomer now that we no longer |
| 5921 | // store local docs in the main doc-store, but whaddyagonnado |
| 5922 | function addDeletedOrLocalIndex(txn, callback) { |
| 5923 | var docStore = txn.objectStore(DOC_STORE); |
| 5924 | docStore.createIndex('deletedOrLocal', 'deletedOrLocal', {unique : false}); |
| 5925 | |
| 5926 | docStore.openCursor().onsuccess = function (event) { |
| 5927 | var cursor = event.target.result; |
| 5928 | if (cursor) { |
| 5929 | var metadata = cursor.value; |
| 5930 | var deleted = isDeleted(metadata); |
| 5931 | metadata.deletedOrLocal = deleted ? "1" : "0"; |
| 5932 | docStore.put(metadata); |
| 5933 | cursor.continue(); |
| 5934 | } else { |
no test coverage detected
searching dependent graphs…