From b250ad593c7a4c3a02a9232f79ecd81e230ffb15 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 9 Jan 2018 22:09:45 -0500 Subject: [PATCH] added basic infrastructure for attributes --- migrations/0066__create_attributes_table.sql | 12 ++++++++++++ routes/api/cleanup.js | 4 ++++ routes/api/sync.js | 12 ++++++++++++ services/app_info.js | 2 +- services/sync.js | 3 +++ services/sync_table.js | 6 ++++++ services/sync_update.js | 17 ++++++++++++++++- 7 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 migrations/0066__create_attributes_table.sql diff --git a/migrations/0066__create_attributes_table.sql b/migrations/0066__create_attributes_table.sql new file mode 100644 index 000000000..f5bbfd84e --- /dev/null +++ b/migrations/0066__create_attributes_table.sql @@ -0,0 +1,12 @@ +CREATE TABLE attributes +( + attribute_id TEXT PRIMARY KEY NOT NULL, + note_id TEXT NOT NULL, + name TEXT NOT NULL, + value TEXT, + date_created TEXT NOT NULL, + date_modified TEXT NOT NULL +); + +CREATE INDEX attributes_note_id_index ON attributes (note_id); +CREATE INDEX attributes_note_id_name_index ON attributes (note_id, name); \ No newline at end of file diff --git a/routes/api/cleanup.js b/routes/api/cleanup.js index 1d9722a43..f8a9b3af3 100644 --- a/routes/api/cleanup.js +++ b/routes/api/cleanup.js @@ -20,6 +20,10 @@ router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, r await sql.execute(`DELETE FROM notes_history WHERE note_id IN (${noteIdsSql})`); + await sql.execute(`DELETE FROM notes_image WHERE note_id IN (${noteIdsSql})`); + + await sql.execute(`DELETE FROM attributes WHERE note_id IN (${noteIdsSql})`); + await sql.execute("DELETE FROM notes_tree WHERE is_deleted = 1"); await sql.execute("DELETE FROM notes_image WHERE is_deleted = 1"); diff --git a/routes/api/sync.js b/routes/api/sync.js index 90b3e8d3c..1d869c74d 100644 --- a/routes/api/sync.js +++ b/routes/api/sync.js @@ -140,6 +140,12 @@ router.get('/notes_image/:noteImageId', auth.checkApiAuth, wrap(async (req, res, res.send(await sql.getFirst("SELECT * FROM notes_image WHERE note_image_id = ?", [noteImageId])); })); +router.get('/attributes/:attributeId', auth.checkApiAuth, wrap(async (req, res, next) => { + const attributeId = req.params.attributeId; + + res.send(await sql.getFirst("SELECT * FROM attributes WHERE attribute_id = ?", [attributeId])); +})); + router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => { await syncUpdate.updateNote(req.body.entity, req.body.sourceId); @@ -188,4 +194,10 @@ router.put('/notes_image', auth.checkApiAuth, wrap(async (req, res, next) => { res.send({}); })); +router.put('/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { + await syncUpdate.updateNoteImage(req.body.entity, req.body.sourceId); + + res.send({}); +})); + module.exports = router; \ No newline at end of file diff --git a/services/app_info.js b/services/app_info.js index ed4ba0c00..b0c80ef59 100644 --- a/services/app_info.js +++ b/services/app_info.js @@ -3,7 +3,7 @@ const build = require('./build'); const packageJson = require('../package'); -const APP_DB_VERSION = 65; +const APP_DB_VERSION = 66; module.exports = { app_version: packageJson.version, diff --git a/services/sync.js b/services/sync.js index 874740ad7..931bde8c4 100644 --- a/services/sync.js +++ b/services/sync.js @@ -230,6 +230,9 @@ async function pushEntity(sync, syncContext) { else if (sync.entity_name === 'notes_image') { entity = await sql.getFirst('SELECT * FROM notes_image WHERE note_image_id = ?', [sync.entity_id]); } + else if (sync.entity_name === 'attributes') { + entity = await sql.getFirst('SELECT * FROM attributes WHERE attribute_id = ?', [sync.entity_id]); + } else { throw new Error(`Unrecognized entity type ${sync.entity_name} in sync #${sync.id}`); } diff --git a/services/sync_table.js b/services/sync_table.js index 1b01119e2..b53a13d85 100644 --- a/services/sync_table.js +++ b/services/sync_table.js @@ -36,6 +36,10 @@ async function addNoteImageSync(noteImageId, sourceId) { await addEntitySync("notes_image", noteImageId, sourceId); } +async function addAttributeSync(noteImageId, sourceId) { + await addEntitySync("attributes", noteImageId, sourceId); +} + async function addEntitySync(entityName, entityId, sourceId) { await sql.replace("sync", { entity_name: entityName, @@ -88,6 +92,7 @@ async function fillAllSyncRows() { await fillSyncRows("recent_notes", "note_tree_id"); await fillSyncRows("images", "image_id"); await fillSyncRows("notes_image", "note_image_id"); + await fillSyncRows("attributes", "attribute_id"); } module.exports = { @@ -99,6 +104,7 @@ module.exports = { addRecentNoteSync, addImageSync, addNoteImageSync, + addAttributeSync, cleanupSyncRowsForMissingEntities, fillAllSyncRows }; \ No newline at end of file diff --git a/services/sync_update.js b/services/sync_update.js index d207bd79f..7816d473f 100644 --- a/services/sync_update.js +++ b/services/sync_update.js @@ -124,6 +124,20 @@ async function updateNoteImage(entity, sourceId) { } } +async function updateAttribute(entity, sourceId) { + const origAttribute = await sql.getFirst("SELECT * FROM attribute WHERE attribute_id = ?", [entity.attribute_id]); + + if (!origAttribute || origAttribute.date_modified <= entity.date_modified) { + await sql.doInTransaction(async () => { + await sql.replace("attribute", entity); + + await sync_table.addAttributeSync(entity.attribute_id, sourceId); + }); + + log.info("Update/sync attribute " + entity.attribute_id); + } +} + module.exports = { updateNote, updateNoteTree, @@ -132,5 +146,6 @@ module.exports = { updateOptions, updateRecentNotes, updateImage, - updateNoteImage + updateNoteImage, + updateAttribute }; \ No newline at end of file