mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	added basic infrastructure for attributes
This commit is contained in:
		
							
								
								
									
										12
									
								
								migrations/0066__create_attributes_table.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								migrations/0066__create_attributes_table.sql
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
				
			||||||
@@ -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_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_tree WHERE is_deleted = 1");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await sql.execute("DELETE FROM notes_image WHERE is_deleted = 1");
 | 
					        await sql.execute("DELETE FROM notes_image WHERE is_deleted = 1");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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]));
 | 
					    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) => {
 | 
					router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => {
 | 
				
			||||||
    await syncUpdate.updateNote(req.body.entity, req.body.sourceId);
 | 
					    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({});
 | 
					    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;
 | 
					module.exports = router;
 | 
				
			||||||
@@ -3,7 +3,7 @@
 | 
				
			|||||||
const build = require('./build');
 | 
					const build = require('./build');
 | 
				
			||||||
const packageJson = require('../package');
 | 
					const packageJson = require('../package');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const APP_DB_VERSION = 65;
 | 
					const APP_DB_VERSION = 66;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
    app_version: packageJson.version,
 | 
					    app_version: packageJson.version,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -230,6 +230,9 @@ async function pushEntity(sync, syncContext) {
 | 
				
			|||||||
    else if (sync.entity_name === 'notes_image') {
 | 
					    else if (sync.entity_name === 'notes_image') {
 | 
				
			||||||
        entity = await sql.getFirst('SELECT * FROM notes_image WHERE note_image_id = ?', [sync.entity_id]);
 | 
					        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 {
 | 
					    else {
 | 
				
			||||||
        throw new Error(`Unrecognized entity type ${sync.entity_name} in sync #${sync.id}`);
 | 
					        throw new Error(`Unrecognized entity type ${sync.entity_name} in sync #${sync.id}`);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,6 +36,10 @@ async function addNoteImageSync(noteImageId, sourceId) {
 | 
				
			|||||||
    await addEntitySync("notes_image", noteImageId, sourceId);
 | 
					    await addEntitySync("notes_image", noteImageId, sourceId);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function addAttributeSync(noteImageId, sourceId) {
 | 
				
			||||||
 | 
					    await addEntitySync("attributes", noteImageId, sourceId);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function addEntitySync(entityName, entityId, sourceId) {
 | 
					async function addEntitySync(entityName, entityId, sourceId) {
 | 
				
			||||||
    await sql.replace("sync", {
 | 
					    await sql.replace("sync", {
 | 
				
			||||||
        entity_name: entityName,
 | 
					        entity_name: entityName,
 | 
				
			||||||
@@ -88,6 +92,7 @@ async function fillAllSyncRows() {
 | 
				
			|||||||
    await fillSyncRows("recent_notes", "note_tree_id");
 | 
					    await fillSyncRows("recent_notes", "note_tree_id");
 | 
				
			||||||
    await fillSyncRows("images", "image_id");
 | 
					    await fillSyncRows("images", "image_id");
 | 
				
			||||||
    await fillSyncRows("notes_image", "note_image_id");
 | 
					    await fillSyncRows("notes_image", "note_image_id");
 | 
				
			||||||
 | 
					    await fillSyncRows("attributes", "attribute_id");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
@@ -99,6 +104,7 @@ module.exports = {
 | 
				
			|||||||
    addRecentNoteSync,
 | 
					    addRecentNoteSync,
 | 
				
			||||||
    addImageSync,
 | 
					    addImageSync,
 | 
				
			||||||
    addNoteImageSync,
 | 
					    addNoteImageSync,
 | 
				
			||||||
 | 
					    addAttributeSync,
 | 
				
			||||||
    cleanupSyncRowsForMissingEntities,
 | 
					    cleanupSyncRowsForMissingEntities,
 | 
				
			||||||
    fillAllSyncRows
 | 
					    fillAllSyncRows
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -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 = {
 | 
					module.exports = {
 | 
				
			||||||
    updateNote,
 | 
					    updateNote,
 | 
				
			||||||
    updateNoteTree,
 | 
					    updateNoteTree,
 | 
				
			||||||
@@ -132,5 +146,6 @@ module.exports = {
 | 
				
			|||||||
    updateOptions,
 | 
					    updateOptions,
 | 
				
			||||||
    updateRecentNotes,
 | 
					    updateRecentNotes,
 | 
				
			||||||
    updateImage,
 | 
					    updateImage,
 | 
				
			||||||
    updateNoteImage
 | 
					    updateNoteImage,
 | 
				
			||||||
 | 
					    updateAttribute
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
		Reference in New Issue
	
	Block a user