From d50c3116423dfa57af239153c883c9c2ec7d66e7 Mon Sep 17 00:00:00 2001 From: stockulus Date: Mon, 5 Sep 2016 18:25:08 +0200 Subject: [PATCH] better read handling --- .../src/all_docs.js | 8 +++++-- .../src/asyncstorage_core.js | 22 ++++++++++++++----- .../pouchdb-adapter-asyncstorage/src/get.js | 6 +++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/pouchdb-adapter-asyncstorage/src/all_docs.js b/packages/pouchdb-adapter-asyncstorage/src/all_docs.js index 52db967..6c17e8b 100644 --- a/packages/pouchdb-adapter-asyncstorage/src/all_docs.js +++ b/packages/pouchdb-adapter-asyncstorage/src/all_docs.js @@ -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) @@ -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 }) @@ -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 @@ -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)) @@ -95,6 +97,8 @@ export default function (db, opts, callback) { callback(null, { total_rows: db.meta.doc_count, offset: skip, + opts, + filterKeySet, rows }) } diff --git a/packages/pouchdb-adapter-asyncstorage/src/asyncstorage_core.js b/packages/pouchdb-adapter-asyncstorage/src/asyncstorage_core.js index a89ae41..d322b12 100644 --- a/packages/pouchdb-adapter-asyncstorage/src/asyncstorage_core.js +++ b/packages/pouchdb-adapter-asyncstorage/src/asyncstorage_core.js @@ -37,22 +37,34 @@ 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) } @@ -60,7 +72,7 @@ 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) } @@ -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 }, {})) ) diff --git a/packages/pouchdb-adapter-asyncstorage/src/get.js b/packages/pouchdb-adapter-asyncstorage/src/get.js index cb88cc6..fee0425 100644 --- a/packages/pouchdb-adapter-asyncstorage/src/get.js +++ b/packages/pouchdb-adapter-asyncstorage/src/get.js @@ -2,7 +2,6 @@ import { createError, - generateErrorFromResponse, MISSING_DOC } from 'pouchdb-errors' import { forDocument, forSequence } from './keys' @@ -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,