Files
notesnook/packages/core/api/sync/prepare.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-04-09 16:36:57 +05:00
import Database from "../index";
2020-04-09 16:46:03 +05:00
var tfun = require("transfun/transfun.js").tfun;
if (!tfun) {
tfun = global.tfun;
}
2020-04-09 16:36:57 +05:00
class Prepare {
/**
*
* @param {Database} db
*/
constructor(db) {
this._db = db;
}
async get(lastSyncedTimestamp) {
this._lastSyncedTimestamp = lastSyncedTimestamp;
this.key = await this._db.user.key();
2020-04-09 16:36:57 +05:00
return {
notes: await this._prepareForServer(this._db.notes.raw),
notebooks: await this._prepareForServer(this._db.notebooks.raw),
delta: await this._prepareForServer(await this._db.delta.all()),
text: await this._prepareForServer(await this._db.text.all()),
tags: await this._prepareForServer(this._db.tags.raw),
colors: await this._prepareForServer(this._db.colors.raw),
trash: await this._prepareForServer(this._db.trash.raw),
2020-04-09 16:36:57 +05:00
};
}
_serialize(item) {
return this._db.context.encrypt(this.key, JSON.stringify(item));
}
2020-04-09 16:36:57 +05:00
_prepareForServer(array) {
return Promise.all(
tfun
.filter((item) => item.dateEdited > this._lastSyncedTimestamp)
.map(async (item) => {
// in case of resolved delta, we do not want to send this key to the server
if (item.resolved) delete item.resolved;
return {
id: item.id,
...(await this._serialize(item)),
};
})(array)
);
2020-04-09 16:36:57 +05:00
}
}
export default Prepare;