add db to doc function

This commit is contained in:
casperlamboo 2017-11-02 17:09:19 +01:00
parent 81b144b86d
commit 04cb04b615
4 changed files with 1616 additions and 34 deletions

1583
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,7 @@
"bowser": "^1.8.1",
"imports-loader": "^0.7.1",
"memoizee": "^0.3.9",
"pouchdb": "^6.3.4",
"proptypes": "^1.1.0",
"raw-loader": "^0.5.1",
"react": "^16.0.0",

63
src/shape/dbToDoc.js Normal file
View File

@ -0,0 +1,63 @@
import { blobToJSON } from '../utils/binaryUtils.js';
import JSONToSketchData from './JSONToSketchData.js';
import semver from 'semver';
export async function docToFile(db, doc, { image = false, sketch = false } = {}) {
// NOTE doc.appVersion can be used to check in which version of the app the file was saved in
const response = {
name: doc.name,
id: doc._id,
createdOn: doc.createdOn,
updatedOn: doc.updatedOn
};
const { appVersion } = doc;
const keys = [];
if (sketch && semver.gt(appVersion, '0.0.4')) keys.push('sketch');
if (keys.length > 0) await addAttachments(db, [doc], keys);
if (sketch) {
if (semver.gt(appVersion, '0.0.4')) {
if (!(doc._attachments && doc._attachments.sketch && doc._attachments.sketch.data)) {
throw new Error(`'${doc.name}' doesn't include sketch attachment`);
}
const data = await blobToJSON(doc._attachments.sketch.data);
response.data = await JSONToSketchData(data);
} else {
const data = { data: doc.data, appVersion };
response.data = await JSONToSketchData(data);
}
}
if (image) {
response.img = `${db.name}/${doc._id}/img`;
}
return response;
}
export async function addAttachments(db, docs, keys) {
if (keys) {
keys = keys.reduce((obj, value) => {
obj[value] = null;
return obj;
}, {});
}
const promises = [];
for (const { _attachments: attachments, _id: id } of docs) {
for (const attachmentId in keys || attachments) {
const attachment = attachments[attachmentId];
const promise = db.getAttachment(id, attachmentId).then(blob => {
attachment.data = blob;
});
promises.push(promise);
}
}
await Promise.all(promises);
}

View File

@ -1,5 +1,6 @@
import dbToDoc from './dbToDoc.js';
import shapeToPoints from './shapeToPoints.js';
import JSONToSketchData from './JSONToSketchData.js';
import ShapeDataUtils from './shapeDataUtils.js';
export { shapeToPoints, JSONToSketchData, ShapeDataUtils };
export { dbToDoc, shapeToPoints, JSONToSketchData, ShapeDataUtils };