Remove image from album upon deletion

This commit is contained in:
Timothy Farrell 2017-11-25 16:02:06 -06:00
parent 6f233cb764
commit c7e76fc169
2 changed files with 17 additions and 7 deletions

View File

@ -68,10 +68,16 @@ class AlbumSpec extends TypeSpec {
export const AlbumType = PouchDB.registerType('Album', AlbumSpec); export const AlbumType = PouchDB.registerType('Album', AlbumSpec);
// ImageType.watch({_deleted: true}, true) ImageType.subscribe((id, deleted, doc) => {
// .then(la => { if (!deleted) {
// la.subscribe() ); return;
// }
// image.removed.subscribe(image => {
// Object.keys(image.tags).forEach(t => index.removeMember(t, image._id)); Object.keys(doc.$links)
// }) .filter(k => k.startsWith(AlbumType.prefix))
.forEach(async albumId => {
const album = await AlbumType.find(albumId);
album.count -= 1;
album.save();
});
});

View File

@ -7,6 +7,7 @@ import find from 'pouchdb-find';
import { log, warn } from './console.js'; import { log, warn } from './console.js';
import { isObject } from '../utils/comparators.js'; import { isObject } from '../utils/comparators.js';
import { LiveArray } from '../utils/livearray.js'; import { LiveArray } from '../utils/livearray.js';
import { Watcher } from '../utils/watcher.js';
import { deepAssign, pouchDocHash } from '../utils/conversion.js'; import { deepAssign, pouchDocHash } from '../utils/conversion.js';
export const PouchDB = core export const PouchDB = core
@ -95,6 +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);
if (!cls.hasOwnProperty('validate')) { if (!cls.hasOwnProperty('validate')) {
warn(`${cls.name} has no validation.`); warn(`${cls.name} has no validation.`);
@ -156,8 +158,10 @@ export function PouchORM(PouchDB) {
getOrCreate: { value: getOrCreate }, getOrCreate: { value: getOrCreate },
find: { value: find }, find: { value: find },
delete: { value: _delete }, delete: { value: _delete },
subscribe: { value: watch },
db: { value: _db }, db: { value: _db },
name: { value: name }, name: { value: name },
prefix: { value: prefix },
selector: { value: _baseSelector } selector: { value: _baseSelector }
}); });