DB layer needs the ability to specify other query options:

- limit
- skip
- sort
- fields
This commit is contained in:
Timothy Farrell 2017-12-29 12:49:47 -06:00
parent e4baa07d61
commit fa88b36c76
4 changed files with 25 additions and 18 deletions

View File

@ -145,7 +145,7 @@ export function AllImagesView(vm, params, key, { appbar }) {
{ {
['sizes.thumbnail']: { $exists: true } ['sizes.thumbnail']: { $exists: true }
}, },
true { live: true }
).then(la => { ).then(la => {
pushAppBarState(); pushAppBarState();
subscribeToRender( subscribeToRender(

View File

@ -96,7 +96,7 @@ export function PouchORM(PouchDB) {
const _baseSelector = Object.freeze({ const _baseSelector = Object.freeze({
_id: { $gt: `${prefix}_0`, $lt: `${prefix}_\ufff0` } _id: { $gt: `${prefix}_0`, $lt: `${prefix}_\ufff0` }
}); });
const watch = Watcher(_db, _baseSelector, true); const watch = Watcher(_db, _baseSelector, { include_docs: true });
if (!cls.hasOwnProperty('validate')) { if (!cls.hasOwnProperty('validate')) {
warn(`${cls.name} has no validation.`); warn(`${cls.name} has no validation.`);
@ -104,7 +104,7 @@ export function PouchORM(PouchDB) {
const instantiate = doc => new cls(doc); const instantiate = doc => new cls(doc);
async function find(idOrSelector, live = false) { async function find(idOrSelector, opts = {}) {
if (typeof idOrSelector === 'string') { if (typeof idOrSelector === 'string') {
return instantiate(await _db.get(idOrSelector)); return instantiate(await _db.get(idOrSelector));
} }
@ -115,10 +115,11 @@ export function PouchORM(PouchDB) {
isSelector && idOrSelector._deleted ? { _deleted: true } : { _deleted: { exists: false } }, isSelector && idOrSelector._deleted ? { _deleted: true } : { _deleted: { exists: false } },
isSelector ? idOrSelector : _baseSelector isSelector ? idOrSelector : _baseSelector
); );
if (live) { if (opts.live) {
return LiveArray(_db, idOrSelector, instantiate); opts.mapper = instantiate;
return LiveArray(_db, idOrSelector, opts);
} }
return (await _db.find({ selector: idOrSelector })).docs.map(instantiate); return (await _db.find(Object.assign({ selector: idOrSelector }, opts))).docs.map(instantiate);
} }
async function getOrCreate(props) { async function getOrCreate(props) {

View File

@ -1,17 +1,19 @@
import { prop, computed } from 'frptools'; import { prop, computed, id } from 'frptools';
import { Watcher } from './watcher.js'; import { Watcher } from './watcher.js';
import { pouchDocArrayHash } from './conversion.js'; import { pouchDocArrayHash } from './conversion.js';
// LiveArray is a subscribable property function that always returns the db results that match the provided selector and calls subscribers when the results change. // LiveArray is a subscribable property function that always returns the db results that match the provided selector and calls subscribers when the results change.
export function LiveArray(db, selector, mapper) { export function LiveArray(db, selector, opts = {}) {
const _watcher = Watcher(db, selector); const mapper = opts.mapper || id;
opts.mapper && delete opts.mapper;
opts.include_docs = true;
const _watcher = Watcher(db, selector, opts);
let changeSub = null; let changeSub = null;
let _mapper = mapper || (doc => doc);
const ready = prop(false); const ready = prop(false);
const data = prop({ docs: [] }); const data = prop({ docs: [] });
const docs = computed(r => r.docs.map(_mapper), [data], pouchDocArrayHash); const docs = computed(r => r.docs.map(mapper), [data], pouchDocArrayHash);
const cleanup = () => { const cleanup = () => {
docs.unsubscribeAll(); docs.unsubscribeAll();

View File

@ -1,6 +1,6 @@
import { log, error } from '../services/console.js'; import { log, error } from '../services/console.js';
export function Watcher(db, selector, include_docs) { export function Watcher(db, selector, opts) {
const subscribers = new Set(); const subscribers = new Set();
let changes = null; let changes = null;
@ -10,12 +10,16 @@ export function Watcher(db, selector, include_docs) {
if (subscribers.size === 1 && !changes) { if (subscribers.size === 1 && !changes) {
log(`Watching "${db.name}" for ${JSON.stringify(selector)}`); log(`Watching "${db.name}" for ${JSON.stringify(selector)}`);
changes = db changes = db
.changes({ .changes(
since: 'now', Object.assign(
live: true, {
include_docs, since: 'now',
selector live: true,
}) selector
},
opts
)
)
.on('change', change => { .on('change', change => {
const { id, deleted, doc } = change; const { id, deleted, doc } = change;
log( log(