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 e08db9bae0
commit c56ebe8065
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 }
},
true
{ live: true }
).then(la => {
pushAppBarState();
subscribeToRender(

View File

@ -96,7 +96,7 @@ export function PouchORM(PouchDB) {
const _baseSelector = Object.freeze({
_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')) {
warn(`${cls.name} has no validation.`);
@ -104,7 +104,7 @@ export function PouchORM(PouchDB) {
const instantiate = doc => new cls(doc);
async function find(idOrSelector, live = false) {
async function find(idOrSelector, opts = {}) {
if (typeof idOrSelector === 'string') {
return instantiate(await _db.get(idOrSelector));
}
@ -115,10 +115,11 @@ export function PouchORM(PouchDB) {
isSelector && idOrSelector._deleted ? { _deleted: true } : { _deleted: { exists: false } },
isSelector ? idOrSelector : _baseSelector
);
if (live) {
return LiveArray(_db, idOrSelector, instantiate);
if (opts.live) {
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) {

View File

@ -1,17 +1,19 @@
import { prop, computed } from 'frptools';
import { prop, computed, id } from 'frptools';
import { Watcher } from './watcher.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.
export function LiveArray(db, selector, mapper) {
const _watcher = Watcher(db, selector);
export function LiveArray(db, selector, opts = {}) {
const mapper = opts.mapper || id;
opts.mapper && delete opts.mapper;
opts.include_docs = true;
const _watcher = Watcher(db, selector, opts);
let changeSub = null;
let _mapper = mapper || (doc => doc);
const ready = prop(false);
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 = () => {
docs.unsubscribeAll();

View File

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