added document_id for sync identification

This commit is contained in:
azivner 2017-10-28 13:19:12 -04:00
parent c5f80051ae
commit 724f4b43b7
6 changed files with 32 additions and 4 deletions

5
app.js
View File

@ -95,6 +95,11 @@ app.use((req, res, next) => {
// error handler
app.use((err, req, res, next) => {
log.error(err.message);
res.status(err.status || 500);
res.send({
message: err.message
});
});
// triggers sync timer

View File

@ -0,0 +1 @@
INSERT INTO options (opt_name, opt_value) VALUES ('document_id', '');

View File

@ -3,7 +3,7 @@ const sql = require('./sql');
const fs = require('fs-extra');
const log = require('./log');
const APP_DB_VERSION = 16;
const APP_DB_VERSION = 17;
const MIGRATIONS_DIR = "./migrations";
async function migrate() {

View File

@ -34,9 +34,14 @@ async function rollback() {
}
async function getOption(optName) {
const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);
try {
const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);
return row['opt_value'];
return row['opt_value'];
}
catch (e) {
throw new Error("Option " + optName + " doesn't exist");
}
}
async function setOption(optName, optValue) {

View File

@ -112,6 +112,7 @@ async function sync() {
async function getChangedSince(since) {
return {
'documentId': await getDocumentId(),
'syncTimestamp': utils.nowTimestamp(),
'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]),
'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]),
@ -167,6 +168,22 @@ async function putNote(note) {
log.info("Update/sync note " + note.detail.note_id);
}
let documentIdCache = null;
async function getDocumentId() {
if (!documentIdCache) {
documentIdCache = await sql.getOption('document_id');
if (!documentIdCache) {
documentIdCache = utils.randomString(16);
await sql.setOption('document_id', documentIdCache);
}
}
return documentIdCache;
}
if (SYNC_SERVER) {
log.info("Setting up sync");

View File

@ -6,7 +6,7 @@ function newNoteId() {
const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function randomString(length, chars) {
function randomString(length) {
let result = '';
for (let i = length; i > 0; --i) {