Centralize database source.

This commit is contained in:
Timothy Farrell 2017-04-06 16:32:09 -05:00
parent 386f8e0012
commit 94249321c6
4 changed files with 26 additions and 18 deletions

View File

@ -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"

View File

@ -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);
}
});
});

View File

@ -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,

View File

@ -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);
}