Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stores.docStore.get(doc._id, function (err, metadata) {
if (err) {
return console.warn(err)
}
/* istanbul ignore if */
if (opts.cancelled || opts.done || db.isClosed() ||
isLocalId(metadata.id)) {
return next()
}
docIdsToMetadata.set(doc._id, metadata)
onGetMetadata(metadata)
})
}, function (next) {
function update(txn, doc, oldDoc) {
// Ignore updates to existing revisions
if ((doc.rev in oldDoc.revs) && !opts.new_edits) {
return false;
}
var isRoot = /^1-/.test(doc.rev);
// Reattach first writes after a deletion to last deleted tree
if (oldDoc.deleted && !doc.deleted && opts.new_edits && isRoot) {
var tmp = doc.revs[doc.rev].data;
tmp._rev = oldDoc.rev;
tmp._id = oldDoc.id;
doc = convertDocFormat(parseDoc(tmp, opts.new_edits, dbOpts));
}
var merged = merge(oldDoc.rev_tree, doc.rev_tree[0], docsRevsLimit(doc));
doc.stemmedRevs = merged.stemmedRevs;
doc.rev_tree = merged.tree;
// Merge the old and new rev data
var revs = oldDoc.revs;
revs[doc.rev] = doc.revs[doc.rev];
doc.revs = revs;
doc.attachments = oldDoc.attachments;
var inConflict = opts.new_edits && (((oldDoc.deleted && doc.deleted) ||
(!oldDoc.deleted && merged.conflicts !== 'new_leaf') ||
(oldDoc.deleted && !doc.deleted && merged.conflicts === 'new_branch') ||
docInfos.forEach(function (docInfo) {
if (docInfo._id && isLocalId(docInfo._id)) {
return checkDone(); // skip local docs
}
var id = docInfo.metadata.id;
tx.executeSql('SELECT json FROM ' + DOC_STORE +
' WHERE id = ?', [id], function (tx, result) {
if (result.rows.length) {
var metadata = safeJsonParse(result.rows.item(0).json);
fetchedDocs.set(id, metadata);
}
checkDone();
});
});
}
stores.docStore.get(doc._id, function (err, metadata) {
/* istanbul ignore if */
if (opts.cancelled || opts.done || db.isClosed() ||
pouchdbAdapterUtils.isLocalId(metadata.id)) {
return next();
}
docIdsToMetadata.set(doc._id, metadata);
onGetMetadata(metadata);
});
}, function (next) {
userDocs.forEach(function (doc) {
if (doc._id && isLocalId(doc._id)) {
// skip local docs
return checkDone();
}
txn.get(stores.docStore, doc._id, function (err, info) {
if (err) {
/* istanbul ignore if */
if (err.name !== 'NotFoundError') {
overallErr = err;
}
} else {
fetchedDocs.set(doc._id, info);
}
checkDone();
});
});
}
userDocs.forEach(function (doc) {
if (doc._id && isLocalId(doc._id)) {
// skip local docs
return checkDone()
}
txn.get(stores.docStore, doc._id, function (err, info) {
if (err) {
/* istanbul ignore if */
if (err.name !== 'NotFoundError') {
overallErr = err
}
} else {
fetchedDocs.set(doc._id, info)
}
checkDone()
})
})
}
stores.docStore.get(doc._id, function (err, metadata) {
/* istanbul ignore if */
if (opts.cancelled || opts.done || db.isClosed() ||
pouchdbAdapterUtils.isLocalId(metadata.id)) {
return next();
}
docIdsToMetadata.set(doc._id, metadata);
onGetMetadata(metadata);
});
}, function (next) {
idbProcessDocs();
}
}
function readMetadata(event) {
var metadata = decodeMetadata(event.target.result);
if (metadata) {
fetchedDocs.set(metadata.id, metadata);
}
checkDone();
}
for (var i = 0, len = docInfos.length; i < len; i++) {
var docInfo = docInfos[i];
if (docInfo._id && isLocalId(docInfo._id)) {
checkDone(); // skip local docs
continue;
}
var req = docStore.get(docInfo.metadata.id);
req.onsuccess = readMetadata;
}
}
processed[attachment.name] = attachment;
delete attachment.name;
});
data._attachments = processed;
return data;
});
});
return Promise.all(promises);
}
for (var i = 0, len = req.docs.length; i < len; i++) {
var result;
// TODO: We should get rid of throwing for invalid docs, also not sure
// why this is needed in idb-next and not idb
try {
result = parseDoc(req.docs[i], opts.new_edits, dbOpts);
} catch (err) {
result = err;
}
if (result.error) {
return callback(result);
}
// Ideally parseDoc would return data in this format, but it is currently
// shared so we need to convert
docs.push(convertDocFormat(result));
}
preProcessAttachments().then(function () {
api._openTransactionSafely([DOC_STORE, META_STORE], 'readwrite', function (err, _txn) {
if (err) {
return callback(err);
fetchExistingDocs(function (err) {
/* istanbul ignore if */
if (err) {
return callback(err);
}
processDocs(revLimit, docInfos, api, fetchedDocs, txn, results,
writeDoc, opts, finish);
});
});