2017-11-21 22:11:27 -05:00
|
|
|
const sql = require('./sql');
|
|
|
|
const utils = require('./utils');
|
2018-01-01 19:47:50 -05:00
|
|
|
const log = require('./log');
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2017-12-15 21:14:10 -05:00
|
|
|
function getHash(rows) {
|
|
|
|
let hash = '';
|
|
|
|
|
2017-11-21 22:11:27 -05:00
|
|
|
for (const row of rows) {
|
|
|
|
hash = utils.hash(hash + JSON.stringify(row));
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2017-12-15 21:14:10 -05:00
|
|
|
async function getHashes() {
|
2018-01-01 19:47:50 -05:00
|
|
|
const startTime = new Date();
|
|
|
|
|
|
|
|
const hashes = {
|
2018-01-29 17:41:59 -05:00
|
|
|
notes: getHash(await sql.getRows(`
|
2018-01-06 15:56:00 -05:00
|
|
|
SELECT
|
2018-01-28 19:30:14 -05:00
|
|
|
noteId,
|
|
|
|
title,
|
|
|
|
content,
|
2018-01-20 21:56:03 -05:00
|
|
|
type,
|
2018-01-28 19:30:14 -05:00
|
|
|
dateModified,
|
|
|
|
isProtected,
|
|
|
|
isDeleted
|
2018-01-06 15:56:00 -05:00
|
|
|
FROM notes
|
2018-01-28 19:30:14 -05:00
|
|
|
ORDER BY noteId`)),
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
branches: getHash(await sql.getRows(`
|
2018-01-06 15:56:00 -05:00
|
|
|
SELECT
|
2018-03-24 21:39:15 -04:00
|
|
|
branchId,
|
2018-01-28 19:30:14 -05:00
|
|
|
noteId,
|
|
|
|
parentNoteId,
|
|
|
|
notePosition,
|
|
|
|
dateModified,
|
|
|
|
isDeleted,
|
2018-01-06 15:56:00 -05:00
|
|
|
prefix
|
2018-03-24 21:39:15 -04:00
|
|
|
FROM branches
|
|
|
|
ORDER BY branchId`)),
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
note_revisions: getHash(await sql.getRows(`
|
2018-01-06 15:56:00 -05:00
|
|
|
SELECT
|
2018-01-28 19:38:05 -05:00
|
|
|
noteRevisionId,
|
2018-01-28 19:30:14 -05:00
|
|
|
noteId,
|
|
|
|
title,
|
|
|
|
content,
|
|
|
|
dateModifiedFrom,
|
|
|
|
dateModifiedTo
|
2018-01-28 19:38:05 -05:00
|
|
|
FROM note_revisions
|
|
|
|
ORDER BY noteRevisionId`)),
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
recent_notes: getHash(await sql.getRows(`
|
2018-01-06 15:56:00 -05:00
|
|
|
SELECT
|
2018-03-24 21:39:15 -04:00
|
|
|
branchId,
|
2018-01-28 19:30:14 -05:00
|
|
|
notePath,
|
|
|
|
dateAccessed,
|
|
|
|
isDeleted
|
2018-01-06 15:56:00 -05:00
|
|
|
FROM recent_notes
|
2018-01-28 19:30:14 -05:00
|
|
|
ORDER BY notePath`)),
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
options: getHash(await sql.getRows(`
|
2018-01-06 15:56:00 -05:00
|
|
|
SELECT
|
2018-01-28 19:30:14 -05:00
|
|
|
name,
|
|
|
|
value
|
2018-01-06 15:56:00 -05:00
|
|
|
FROM options
|
2018-01-28 19:30:14 -05:00
|
|
|
WHERE isSynced = 1
|
|
|
|
ORDER BY name`)),
|
2018-01-06 15:56:00 -05:00
|
|
|
|
|
|
|
// we don't include image data on purpose because they are quite large, checksum is good enough
|
|
|
|
// to represent the data anyway
|
2018-01-29 17:41:59 -05:00
|
|
|
images: getHash(await sql.getRows(`
|
2018-01-06 15:56:00 -05:00
|
|
|
SELECT
|
2018-01-28 19:30:14 -05:00
|
|
|
imageId,
|
2018-01-06 15:56:00 -05:00
|
|
|
format,
|
|
|
|
checksum,
|
|
|
|
name,
|
2018-01-28 19:30:14 -05:00
|
|
|
isDeleted,
|
|
|
|
dateModified,
|
|
|
|
dateCreated
|
2018-01-06 15:56:00 -05:00
|
|
|
FROM images
|
2018-01-28 19:30:14 -05:00
|
|
|
ORDER BY imageId`)),
|
2018-01-13 23:33:09 -05:00
|
|
|
|
2018-03-31 23:19:54 -04:00
|
|
|
note_images: getHash(await sql.getRows(`
|
|
|
|
SELECT
|
|
|
|
noteImageId,
|
|
|
|
noteId,
|
|
|
|
imageId,
|
|
|
|
isDeleted,
|
|
|
|
dateModified,
|
|
|
|
dateCreated
|
|
|
|
FROM note_images
|
|
|
|
ORDER BY noteImageId`)),
|
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
labels: getHash(await sql.getRows(`
|
2018-01-13 23:33:09 -05:00
|
|
|
SELECT
|
2018-03-24 22:02:26 -04:00
|
|
|
labelId,
|
2018-03-31 23:19:54 -04:00
|
|
|
noteId,
|
2018-01-13 23:33:09 -05:00
|
|
|
name,
|
2018-03-31 23:19:54 -04:00
|
|
|
value,
|
2018-01-28 19:30:14 -05:00
|
|
|
dateModified,
|
|
|
|
dateCreated
|
2018-03-24 22:02:26 -04:00
|
|
|
FROM labels
|
2018-03-31 23:19:54 -04:00
|
|
|
ORDER BY labelId`)),
|
|
|
|
|
|
|
|
api_tokens: getHash(await sql.getRows(`
|
|
|
|
SELECT
|
|
|
|
apiTokenId,
|
|
|
|
token,
|
|
|
|
dateCreated,
|
|
|
|
isDeleted
|
|
|
|
FROM api_tokens
|
|
|
|
ORDER BY apiTokenId`))
|
2017-12-15 21:14:10 -05:00
|
|
|
};
|
2018-01-01 19:47:50 -05:00
|
|
|
|
|
|
|
const elapseTimeMs = new Date().getTime() - startTime.getTime();
|
|
|
|
|
|
|
|
log.info(`Content hash computation took ${elapseTimeMs}ms`);
|
|
|
|
|
|
|
|
return hashes;
|
2017-11-21 22:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-12-15 21:14:10 -05:00
|
|
|
getHashes
|
2017-11-21 22:11:27 -05:00
|
|
|
};
|