@@ -24,9 +24,9 @@ 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 ) {
27+ export default function ( api , req , opts , connectionInfo , dbOpts , idbChanges , callback ) {
2828
29- let txn ;
29+ let metadata = { } ;
3030
3131 // TODO: I would prefer to get rid of these globals
3232 let error ;
@@ -49,7 +49,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
4949
5050 // Reads the original doc from the store if available
5151 // As in allDocs with keys option using multiple get calls is the fastest way
52- function fetchExistingDocs ( txn , docs ) {
52+ function fetchExistingDocs ( txn , metadata , docs ) {
5353 let fetched = 0 ;
5454 const oldDocs = { } ;
5555
@@ -58,7 +58,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
5858 oldDocs [ e . target . result . id ] = e . target . result ;
5959 }
6060 if ( ++ fetched === docs . length ) {
61- processDocs ( txn , docs , oldDocs ) ;
61+ processDocs ( txn , metadata , docs , oldDocs ) ;
6262 }
6363 }
6464
@@ -76,7 +76,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
7676 } ) ;
7777 }
7878
79- function processDocs ( txn , docs , oldDocs ) {
79+ function processDocs ( txn , metadata , docs , oldDocs ) {
8080
8181 docs . forEach ( function ( doc , i ) {
8282 let newDoc ;
@@ -116,7 +116,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
116116 } else {
117117 oldDocs [ newDoc . id ] = newDoc ;
118118 lastWriteIndex = i ;
119- write ( txn , newDoc , i ) ;
119+ write ( txn , metadata , newDoc , i ) ;
120120 }
121121 } ) ;
122122 }
@@ -182,7 +182,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
182182 return doc ;
183183 }
184184
185- function write ( txn , doc , i ) {
185+ function write ( txn , metadata , doc , i ) {
186186
187187 // We copy the data from the winning revision into the root
188188 // of the document so that it can be indexed
@@ -287,7 +287,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
287287 rev : '0-0'
288288 } ;
289289 } ;
290- updateSeq ( i ) ;
290+ updateSeq ( txn , metadata , i ) ;
291291 return ;
292292 }
293293
@@ -298,11 +298,11 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
298298 id : doc . id ,
299299 rev : writtenRev
300300 } ;
301- updateSeq ( i ) ;
301+ updateSeq ( txn , metadata , i ) ;
302302 } ;
303303 }
304304
305- function updateSeq ( i ) {
305+ function updateSeq ( txn , metadata , i ) {
306306 if ( i === lastWriteIndex ) {
307307 txn . objectStore ( META_LOCAL_STORE ) . put ( metadata ) ;
308308 }
@@ -320,12 +320,12 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
320320 } catch ( e ) {
321321 return Promise . reject ( createError ( BAD_ARG , 'Attachment is not a valid base64 string' ) ) ;
322322 }
323- if ( metadata . idb_attachment_format === 'binary' ) {
323+ if ( connectionInfo . idb_attachment_format === 'binary' ) {
324324 attachment . data = binStringToBlobOrBuffer ( binData , attachment . content_type ) ;
325325 }
326326 } else {
327327 binData = attachment . data ;
328- if ( metadata . idb_attachment_format === 'base64' ) {
328+ if ( connectionInfo . idb_attachment_format === 'base64' ) {
329329 // TODO could run these in parallel, if we cared
330330 return new Promise ( resolve => {
331331 blufferToBase64 ( attachment . data , function ( b64 ) {
@@ -395,13 +395,11 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
395395 // We _could_ check doc ids here, and skip opening DOC_STORE if all docs are local.
396396 // This may marginally slow things down for local docs. It seems pragmatic to keep
397397 // 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 ) {
398+ api . _openTransactionSafely ( [ DOC_STORE , META_LOCAL_STORE ] , 'readwrite' , function ( err , txn ) {
399399 if ( err ) {
400400 return callback ( err ) ;
401401 }
402402
403- txn = _txn ;
404-
405403 txn . onabort = function ( ) {
406404 callback ( error || createError ( UNKNOWN_ERROR , 'transaction was aborted' ) ) ;
407405 } ;
@@ -412,8 +410,11 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
412410 callback ( null , results ) ;
413411 } ;
414412
415- // We would like to use promises here, but idb sucks
416- fetchExistingDocs ( txn , docs ) ;
413+ const metaStore = txn . objectStore ( META_LOCAL_STORE ) ;
414+ metaStore . get ( META_LOCAL_STORE ) . onsuccess = function ( e ) {
415+ const metadata = e . target . result ;
416+ fetchExistingDocs ( txn , metadata , docs ) ;
417+ } ;
417418 } ) ;
418419 } ) . catch ( function ( err ) {
419420 callback ( err ) ;
0 commit comments