@@ -24,9 +24,7 @@ import { DOC_STORE, META_LOCAL_STORE, idbError } from './util';
2424import { rewrite , sanitise } from './rewrite' ;
2525const sanitisedAttachmentKey = sanitise ( '_attachments' ) ;
2626
27- export default function ( api , req , opts , metadata , dbOpts , idbChanges , callback ) {
28-
29- let txn ;
27+ export default function ( api , req , opts , connectionInfo , dbOpts , idbChanges , callback ) {
3028
3129 // TODO: I would prefer to get rid of these globals
3230 let error ;
@@ -49,7 +47,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
4947
5048 // Reads the original doc from the store if available
5149 // As in allDocs with keys option using multiple get calls is the fastest way
52- function fetchExistingDocs ( txn , docs ) {
50+ function fetchExistingDocs ( txn , metadata , docs ) {
5351 let fetched = 0 ;
5452 const oldDocs = { } ;
5553
@@ -58,7 +56,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
5856 oldDocs [ e . target . result . id ] = e . target . result ;
5957 }
6058 if ( ++ fetched === docs . length ) {
61- processDocs ( txn , docs , oldDocs ) ;
59+ processDocs ( txn , metadata , docs , oldDocs ) ;
6260 }
6361 }
6462
@@ -76,7 +74,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
7674 } ) ;
7775 }
7876
79- function processDocs ( txn , docs , oldDocs ) {
77+ function processDocs ( txn , metadata , docs , oldDocs ) {
8078
8179 docs . forEach ( function ( doc , i ) {
8280 let newDoc ;
@@ -116,7 +114,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
116114 } else {
117115 oldDocs [ newDoc . id ] = newDoc ;
118116 lastWriteIndex = i ;
119- write ( txn , newDoc , i ) ;
117+ write ( txn , metadata , newDoc , i ) ;
120118 }
121119 } ) ;
122120 }
@@ -182,7 +180,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
182180 return doc ;
183181 }
184182
185- function write ( txn , doc , i ) {
183+ function write ( txn , metadata , doc , i ) {
186184
187185 // We copy the data from the winning revision into the root
188186 // of the document so that it can be indexed
@@ -287,7 +285,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
287285 rev : '0-0'
288286 } ;
289287 } ;
290- updateSeq ( i ) ;
288+ updateSeq ( txn , metadata , i ) ;
291289 return ;
292290 }
293291
@@ -298,11 +296,11 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
298296 id : doc . id ,
299297 rev : writtenRev
300298 } ;
301- updateSeq ( i ) ;
299+ updateSeq ( txn , metadata , i ) ;
302300 } ;
303301 }
304302
305- function updateSeq ( i ) {
303+ function updateSeq ( txn , metadata , i ) {
306304 if ( i === lastWriteIndex ) {
307305 txn . objectStore ( META_LOCAL_STORE ) . put ( metadata ) ;
308306 }
@@ -320,12 +318,12 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
320318 } catch ( e ) {
321319 return Promise . reject ( createError ( BAD_ARG , 'Attachment is not a valid base64 string' ) ) ;
322320 }
323- if ( metadata . idb_attachment_format === 'binary' ) {
321+ if ( connectionInfo . idb_attachment_format === 'binary' ) {
324322 attachment . data = binStringToBlobOrBuffer ( binData , attachment . content_type ) ;
325323 }
326324 } else {
327325 binData = attachment . data ;
328- if ( metadata . idb_attachment_format === 'base64' ) {
326+ if ( connectionInfo . idb_attachment_format === 'base64' ) {
329327 // TODO could run these in parallel, if we cared
330328 return new Promise ( resolve => {
331329 blufferToBase64 ( attachment . data , function ( b64 ) {
@@ -395,13 +393,11 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
395393 // We _could_ check doc ids here, and skip opening DOC_STORE if all docs are local.
396394 // This may marginally slow things down for local docs. It seems pragmatic to keep
397395 // the code simple and optimise for calls to bulkDocs() which include non-local docs.
398- api . _openTransactionSafely ( [ DOC_STORE , META_LOCAL_STORE ] , 'readwrite' , function ( err , _txn ) {
396+ api . _openTransactionSafely ( [ DOC_STORE , META_LOCAL_STORE ] , 'readwrite' , function ( err , txn ) {
399397 if ( err ) {
400398 return callback ( err ) ;
401399 }
402400
403- txn = _txn ;
404-
405401 txn . onabort = function ( ) {
406402 callback ( error || createError ( UNKNOWN_ERROR , 'transaction was aborted' ) ) ;
407403 } ;
@@ -412,8 +408,11 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
412408 callback ( null , results ) ;
413409 } ;
414410
415- // We would like to use promises here, but idb sucks
416- fetchExistingDocs ( txn , docs ) ;
411+ const metaStore = txn . objectStore ( META_LOCAL_STORE ) ;
412+ metaStore . get ( META_LOCAL_STORE ) . onsuccess = function ( e ) {
413+ const metadata = e . target . result ;
414+ fetchExistingDocs ( txn , metadata , docs ) ;
415+ } ;
417416 } ) ;
418417 } ) . catch ( function ( err ) {
419418 callback ( err ) ;
0 commit comments