added basic infrastructure for attributes

This commit is contained in:
azivner 2018-01-09 22:09:45 -05:00
parent 58362405c6
commit b250ad593c
7 changed files with 54 additions and 2 deletions

View 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);

View file

@ -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");

View file

@ -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;

View file

@ -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,

View file

@ -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}`);
}

View file

@ -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
};

View file

@ -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
};