blob: f3e5ac6de1ccb2629b6e1b2e190d68b150a03b2f [file] [log] [blame]
'use strict';
import { createError, MISSING_DOC } from 'pouchdb-errors';
import { DOC_STORE } from './util';
import { latest as getLatest } from 'pouchdb-merge';
export default function (txn, id, opts, callback) {
if (txn.error) {
return callback(txn.error);
}
txn.txn.objectStore(DOC_STORE).get(id).onsuccess = function (e) {
var doc = e.target.result;
var rev;
if (!opts.rev) {
rev = (doc && doc.rev);
} else {
rev = opts.latest ? getLatest(opts.rev, doc) : opts.rev;
}
if (!doc || (doc.deleted && !opts.rev) || !(rev in doc.revs)) {
callback(createError(MISSING_DOC, 'missing'));
return;
}
var result = doc.revs[rev].data;
result._id = doc.id;
result._rev = rev;
// WARNING: expecting possible old format
// TODO: why are we passing the transaction in the context?
// It's not clear we ever thread these txns usefully
callback(null, {
doc: result,
metadata: doc,
ctx: txn
});
};
}