Centralize database source.
This commit is contained in:
parent
386f8e0012
commit
94249321c6
@ -20,7 +20,7 @@
|
|||||||
"pouchdb-binary-utils": "~6.1.2",
|
"pouchdb-binary-utils": "~6.1.2",
|
||||||
"pouchdb-core": "~6.1.2",
|
"pouchdb-core": "~6.1.2",
|
||||||
"pouchdb-replication": "~6.1.2",
|
"pouchdb-replication": "~6.1.2",
|
||||||
"webpack": "~2.3.0"
|
"webpack": "^2.3.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"webpack-dev-server": "~2.4.2"
|
"webpack-dev-server": "~2.4.2"
|
||||||
|
|||||||
@ -1,22 +1,23 @@
|
|||||||
import { add, imported, db, remove } from './data/image.js';
|
import * as image from './data/image.js';
|
||||||
import * as thumbnailContext from './context/generateThumbnails.js';
|
import { getDatabase } from './services/db.js';
|
||||||
|
|
||||||
|
import './context/generateThumbnails.js';
|
||||||
|
|
||||||
document.querySelector('#fInput').onchange = async evt => {
|
document.querySelector('#fInput').onchange = async evt => {
|
||||||
add(evt.currentTarget.files);
|
image.add(evt.currentTarget.files);
|
||||||
};
|
};
|
||||||
|
|
||||||
window.__DEV__ = true;
|
window.__DEV__ = true;
|
||||||
window.db = db;
|
window.imagedb = getDatabase('gallery-images');
|
||||||
window.remove = remove;
|
|
||||||
|
|
||||||
imported.subscribe(refresh);
|
image.imported.subscribe(refresh);
|
||||||
|
|
||||||
// To test the output:
|
// To test the output:
|
||||||
function refresh() {
|
function refresh() {
|
||||||
setTimeout(() => history.go(0), 100);
|
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 => {
|
results.rows.forEach(r => {
|
||||||
for (let aName in r.doc._attachments) {
|
for (let aName in r.doc._attachments) {
|
||||||
const a = r.doc._attachments[aName];
|
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.title = `${r.doc._id} ${aName}`;
|
||||||
e.src = `data:${a.content_type};base64,${a.data}`;
|
e.src = `data:${a.content_type};base64,${a.data}`;
|
||||||
e.dataset.id = r.doc._id;
|
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,15 +1,14 @@
|
|||||||
import ExifParser from 'exif-parser';
|
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 { log, error } from '../services/console.js';
|
||||||
import { sha256 } from '../utils/crypto.js';
|
import { sha256 } from '../utils/crypto.js';
|
||||||
import { blobToArrayBuffer, deepAssign } from '../utils/conversion.js';
|
import { blobToArrayBuffer, deepAssign } from '../utils/conversion.js';
|
||||||
import { Event, backgroundTask } from '../utils/event.js';
|
import { Event, backgroundTask } from '../utils/event.js';
|
||||||
|
|
||||||
export const DB_NAME = 'gallery-images';
|
export const DB_NAME = 'gallery-images';
|
||||||
export const db = new PouchDB(DB_NAME); // FIXME - don't export
|
const db = getDatabase(DB_NAME);
|
||||||
const subscribers = [];
|
const PROCESS_PREFIX = 'importing';
|
||||||
const IMPORT_PREFIX = 'importing';
|
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
export const imported = new Event('Image.imported');
|
export const imported = new Event('Image.imported');
|
||||||
@ -21,7 +20,7 @@ export async function find(keys, options = {}) {
|
|||||||
|
|
||||||
export async function add(imageFileList) {
|
export async function add(imageFileList) {
|
||||||
const docs = Array.prototype.map.call(imageFileList, f => ({
|
const docs = Array.prototype.map.call(imageFileList, f => ({
|
||||||
_id: `${IMPORT_PREFIX}_${f.name}`,
|
_id: `${PROCESS_PREFIX}_${f.name}`,
|
||||||
name: f.name,
|
name: f.name,
|
||||||
mimetype: f.type,
|
mimetype: f.type,
|
||||||
size: f.size,
|
size: f.size,
|
||||||
@ -49,7 +48,7 @@ export async function remove(ids, rev) {
|
|||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.status !== 404) {
|
if (e.status !== 404) {
|
||||||
error(`Error removing Image import placeholder ${_id}`, e);
|
error(`Error removing Image ${_id}`, e);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -77,8 +76,8 @@ export async function addAttachment(doc, key, blob) {
|
|||||||
// Internal Functions
|
// Internal Functions
|
||||||
const processImportables = backgroundTask(async function _processImportables() {
|
const processImportables = backgroundTask(async function _processImportables() {
|
||||||
const result = await db.allDocs({
|
const result = await db.allDocs({
|
||||||
startkey: `${IMPORT_PREFIX}_0`,
|
startkey: `${PROCESS_PREFIX}_0`,
|
||||||
endkey: `${IMPORT_PREFIX}_z`,
|
endkey: `${PROCESS_PREFIX}_z`,
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
attachments: true,
|
attachments: true,
|
||||||
binary: true,
|
binary: true,
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
export const PouchDB = require('pouchdb-core')
|
const PouchDB = require('pouchdb-core')
|
||||||
.plugin(require('pouchdb-adapter-websql'))
|
.plugin(require('pouchdb-adapter-websql'))
|
||||||
.plugin(require('pouchdb-adapter-idb'))
|
.plugin(require('pouchdb-adapter-idb'))
|
||||||
.plugin(require('pouchdb-adapter-http'))
|
.plugin(require('pouchdb-adapter-http'))
|
||||||
@ -7,3 +7,11 @@ export const PouchDB = require('pouchdb-core')
|
|||||||
export function generateAttachmentUrl(dbName, docId, attachmentKey) {
|
export function generateAttachmentUrl(dbName, docId, attachmentKey) {
|
||||||
return `/_doc_attachments/${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);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user