Skip to content

Commit

Permalink
correct handling of "0" limit
Browse files Browse the repository at this point in the history
  • Loading branch information
stockulus committed Sep 7, 2016
1 parent 4f61f71 commit 352717b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
10 changes: 3 additions & 7 deletions packages/pouchdb-adapter-asyncstorage/src/all_docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { collectConflicts } from 'pouchdb-merge'
import { getDocumentKeys, toDocumentKeys, forSequence } from './keys'

const getDocs = (db,
{filterKey, filterKeySet, startkey, endkey, skip, limit, inclusiveEnd, includeDeleted},
{filterKey, startkey, endkey, skip, limit, inclusiveEnd, includeDeleted},
callback) => {
db.storage.getKeys((error, keys) => {
if (error) return callback(error)
Expand All @@ -14,7 +14,6 @@ const getDocs = (db,
if (startkey && startkey > key) return false
if (endkey) return inclusiveEnd ? endkey >= key : endkey > key
if (filterKey) return filterKey === key
if (filterKeySet) return filterKeySet.has(key)

return true
})
Expand All @@ -27,7 +26,7 @@ const getDocs = (db,
: docs.filter(doc => !doc.deleted)

if (skip > 0) result = result.slice(skip)
if (limit > 0 && result.length > limit) result = result.slice(0, limit)
if (limit >= 0 && result.length > limit) result = result.slice(0, limit)

let seqKeys = result.map(item => forSequence(item.seq))
db.storage.multiGet(seqKeys, (error, dataDocs) => {
Expand All @@ -53,7 +52,6 @@ export default function (db, opts, callback) {
const startkey = 'startkey' in opts ? opts.startkey : false
const endkey = 'endkey' in opts ? opts.endkey : false
const filterKey = 'key' in opts ? opts.key : false
const filterKeySet = 'keys' in opts ? new Set(opts.keys) : false
const skip = opts.skip || 0
const limit = typeof opts.limit === 'number' ? opts.limit : -1
const inclusiveEnd = opts.inclusive_end !== false
Expand Down Expand Up @@ -87,7 +85,7 @@ export default function (db, opts, callback) {
}
}

getDocs(db, {filterKey, filterKeySet, startkey, endkey, skip, limit, inclusiveEnd, includeDeleted},
getDocs(db, {filterKey, startkey, endkey, skip, limit, inclusiveEnd, includeDeleted},
(error, docs) => {
if (error) return callback(generateErrorFromResponse(error))

Expand All @@ -97,8 +95,6 @@ export default function (db, opts, callback) {
callback(null, {
total_rows: db.meta.doc_count,
offset: skip,
opts,
filterKeySet,
rows
})
}
Expand Down
15 changes: 6 additions & 9 deletions packages/pouchdb-adapter-asyncstorage/src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ import {
import { forDocument, forSequence } from './keys'

export default function (db, id, opts, callback) {
db.storage.get(forDocument(id), (error, doc) => {
if (error) {
db.storage.get(forDocument(id), (error, meta) => {
if (error || meta === null) {
return callback(createError(
MISSING_DOC, error.message || 'missing-read-error'))
}

const rev = opts.rev || (doc && doc.rev)
if (!doc || (doc.deleted && !opts.rev) || !(rev in doc.rev_map)) {
const rev = opts.rev || (meta && meta.rev)
if (!meta || (meta.deleted && !opts.rev) || !(rev in meta.rev_map)) {
return callback(createError(MISSING_DOC, 'missing'))
}

db.storage.get(forSequence(doc.rev_map[rev]), (error, result) => {
db.storage.get(forSequence(meta.rev_map[rev]), (error, doc) => {
if (error) {
return callback(createError(
MISSING_DOC, error.message || 'missing-read-error'))
}

callback(null, {
doc: result,
metadata: doc
})
callback(null, {doc, metadata: meta})
})
})
}

0 comments on commit 352717b

Please sign in to comment.