From 94249321c6e09f3f0a0127b4f0852c2a4f32848a Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Thu, 6 Apr 2017 16:32:09 -0500 Subject: [PATCH] Centralize database source. --- packages/gallery/package.json | 2 +- packages/gallery/src/app.js | 17 +++++++++-------- packages/gallery/src/data/image.js | 15 +++++++-------- packages/gallery/src/services/db.js | 10 +++++++++- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/gallery/package.json b/packages/gallery/package.json index f27e245..5891ed0 100644 --- a/packages/gallery/package.json +++ b/packages/gallery/package.json @@ -20,7 +20,7 @@ "pouchdb-binary-utils": "~6.1.2", "pouchdb-core": "~6.1.2", "pouchdb-replication": "~6.1.2", - "webpack": "~2.3.0" + "webpack": "^2.3.3" }, "devDependencies": { "webpack-dev-server": "~2.4.2" diff --git a/packages/gallery/src/app.js b/packages/gallery/src/app.js index 2a9458b..61ca3f1 100644 --- a/packages/gallery/src/app.js +++ b/packages/gallery/src/app.js @@ -1,22 +1,23 @@ -import { add, imported, db, remove } from './data/image.js'; -import * as thumbnailContext from './context/generateThumbnails.js'; +import * as image from './data/image.js'; +import { getDatabase } from './services/db.js'; + +import './context/generateThumbnails.js'; document.querySelector('#fInput').onchange = async evt => { - add(evt.currentTarget.files); + image.add(evt.currentTarget.files); }; window.__DEV__ = true; -window.db = db; -window.remove = remove; +window.imagedb = getDatabase('gallery-images'); -imported.subscribe(refresh); +image.imported.subscribe(refresh); // To test the output: function refresh() { setTimeout(() => history.go(0), 100); } -db.allDocs({ include_docs: true, attachments: true }).then(results => { +imagedb.allDocs({ include_docs: true, attachments: true }).then(results => { results.rows.forEach(r => { for (let aName in r.doc._attachments) { const a = r.doc._attachments[aName]; @@ -25,7 +26,7 @@ db.allDocs({ include_docs: true, attachments: true }).then(results => { e.title = `${r.doc._id} ${aName}`; e.src = `data:${a.content_type};base64,${a.data}`; e.dataset.id = r.doc._id; - e.onclick = evt => remove(evt.currentTarget.dataset.id).then(refresh); + e.onclick = evt => image.remove(evt.currentTarget.dataset.id).then(refresh); } }); }); diff --git a/packages/gallery/src/data/image.js b/packages/gallery/src/data/image.js index 2e28c8c..c512f34 100644 --- a/packages/gallery/src/data/image.js +++ b/packages/gallery/src/data/image.js @@ -1,15 +1,14 @@ import ExifParser from 'exif-parser'; -import { PouchDB, generateAttachmentUrl } from '../services/db.js'; +import { getDatabase, generateAttachmentUrl } from '../services/db.js'; import { log, error } from '../services/console.js'; import { sha256 } from '../utils/crypto.js'; import { blobToArrayBuffer, deepAssign } from '../utils/conversion.js'; import { Event, backgroundTask } from '../utils/event.js'; export const DB_NAME = 'gallery-images'; -export const db = new PouchDB(DB_NAME); // FIXME - don't export -const subscribers = []; -const IMPORT_PREFIX = 'importing'; +const db = getDatabase(DB_NAME); +const PROCESS_PREFIX = 'importing'; // Events export const imported = new Event('Image.imported'); @@ -21,7 +20,7 @@ export async function find(keys, options = {}) { export async function add(imageFileList) { const docs = Array.prototype.map.call(imageFileList, f => ({ - _id: `${IMPORT_PREFIX}_${f.name}`, + _id: `${PROCESS_PREFIX}_${f.name}`, name: f.name, mimetype: f.type, size: f.size, @@ -49,7 +48,7 @@ export async function remove(ids, rev) { return true; } catch (e) { if (e.status !== 404) { - error(`Error removing Image import placeholder ${_id}`, e); + error(`Error removing Image ${_id}`, e); } return false; } @@ -77,8 +76,8 @@ export async function addAttachment(doc, key, blob) { // Internal Functions const processImportables = backgroundTask(async function _processImportables() { const result = await db.allDocs({ - startkey: `${IMPORT_PREFIX}_0`, - endkey: `${IMPORT_PREFIX}_z`, + startkey: `${PROCESS_PREFIX}_0`, + endkey: `${PROCESS_PREFIX}_z`, include_docs: true, attachments: true, binary: true, diff --git a/packages/gallery/src/services/db.js b/packages/gallery/src/services/db.js index f716d7c..4d96c98 100644 --- a/packages/gallery/src/services/db.js +++ b/packages/gallery/src/services/db.js @@ -1,4 +1,4 @@ -export const PouchDB = require('pouchdb-core') +const PouchDB = require('pouchdb-core') .plugin(require('pouchdb-adapter-websql')) .plugin(require('pouchdb-adapter-idb')) .plugin(require('pouchdb-adapter-http')) @@ -7,3 +7,11 @@ export const PouchDB = require('pouchdb-core') export function generateAttachmentUrl(dbName, docId, attachmentKey) { return `/_doc_attachments/${dbName}/${docId}/${attachmentKey}`; } + +const dbs = new Map(); +export function getDatabase(name) { + if (!dbs.has(name)) { + dbs.set(name, new PouchDB(name)); + } + return dbs.get(name); +}