Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
added basic infrastructure for attributes
azivnerb250ad593c7a
diff --git a/migrations/0066__create_attributes_table.sql b/migrations/0066__create_attributes_table.sqlnew file mode 100644index 0000000000..f5bbfd84e7--- /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 filediff --git a/routes/api/cleanup.js b/routes/api/cleanup.jsindex 1d9722a43e..f8a9b3af3b 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, rawait 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.jsindex 90b3e8d3cc..1d869c74d5 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 filediff --git a/services/app_info.js b/services/app_info.jsindex ed4ba0c00f..b0c80ef599 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.jsindex 874740ad7b..931bde8c40 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.jsindex 1b01119e25..b53a13d858 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 filediff --git a/services/sync_update.js b/services/sync_update.jsindex d207bd79f8..7816d473f3 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