Skip to content

Commit

Permalink
better read handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stockulus committed Sep 5, 2016
1 parent afcfbb5 commit d50c311
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
8 changes: 6 additions & 2 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, startkey, endkey, skip, limit, inclusiveEnd, includeDeleted},
{filterKey, filterKeySet, startkey, endkey, skip, limit, inclusiveEnd, includeDeleted},
callback) => {
db.storage.getKeys((error, keys) => {
if (error) return callback(error)
Expand All @@ -14,6 +14,7 @@ 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 Down Expand Up @@ -52,6 +53,7 @@ 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 @@ -85,7 +87,7 @@ export default function (db, opts, callback) {
}
}

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

Expand All @@ -95,6 +97,8 @@ export default function (db, opts, callback) {
callback(null, {
total_rows: db.meta.doc_count,
offset: skip,
opts,
filterKeySet,
rows
})
}
Expand Down
22 changes: 17 additions & 5 deletions packages/pouchdb-adapter-asyncstorage/src/asyncstorage_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,42 @@ AsyncStorageCore.prototype.getKeys = function (callback) {
})
}

const stringifyValue = value => {
if (value === null) return ''
if (value === undefined) return ''

return JSON.stringify(value)
}

AsyncStorageCore.prototype.put = function (key, value, callback) {
key = prepareKey(key, this)
AsyncStorage.setItem(key, JSON.stringify(value), callback)
AsyncStorage.setItem(key, stringifyValue(value), callback)
}

AsyncStorageCore.prototype.multiPut = function (pairs, callback) {
pairs = pairs.map(pair => [prepareKey(pair[0], this), JSON.stringify(pair[1])])
pairs = pairs.map(pair => [prepareKey(pair[0], this), stringifyValue(pair[1])])
AsyncStorage.multiSet(pairs)
.then(result => callback(null, result))
.catch(callback)
}

const parseValue = value => {
if (typeof value === 'string') return JSON.parse(value)
return null
}

AsyncStorageCore.prototype.get = function (key, callback) {
key = prepareKey(key, this)
AsyncStorage.getItem(key)
.then(item => callback(null, JSON.parse(item)))
.then(item => callback(null, parseValue(item)))
.catch(callback)
}

AsyncStorageCore.prototype.multiGet = function (keys, callback) {
keys = keys.map(key => prepareKey(key, this))

AsyncStorage.multiGet(keys)
.then(pairs => callback(null, pairs.map(pair => JSON.parse(pair[1]))))
.then(pairs => callback(null, pairs.map(pair => parseValue(pair[1]))))
.catch(callback)
}

Expand All @@ -71,7 +83,7 @@ AsyncStorageCore.prototype.multiGetAsObj = function (keys, callback) {
.then(pairs => callback(
null,
pairs.reduce((result, pair) => {
result[pair[0].slice(this._prefix.length)] = JSON.parse(pair[1])
result[pair[0].slice(this._prefix.length)] = parseValue(pair[1])
return result
}, {}))
)
Expand Down
6 changes: 4 additions & 2 deletions packages/pouchdb-adapter-asyncstorage/src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import {
createError,
generateErrorFromResponse,
MISSING_DOC } from 'pouchdb-errors'
import { forDocument, forSequence } from './keys'

Expand All @@ -19,7 +18,10 @@ export default function (db, id, opts, callback) {
}

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

callback(null, {
doc: result,
Expand Down

0 comments on commit d50c311

Please sign in to comment.