basic entities for attributes (unification of labels and relations)

This commit is contained in:
azivner
2018-08-02 22:48:21 +02:00
parent 040f9185f8
commit 097114c0f2
10 changed files with 253 additions and 25 deletions

View File

@@ -3,7 +3,7 @@
const build = require('./build');
const packageJson = require('../../package');
const APP_DB_VERSION = 108;
const APP_DB_VERSION = 109;
const SYNC_VERSION = 1;
module.exports = {

View File

@@ -0,0 +1,52 @@
"use strict";
const repository = require('./repository');
const Attribute = require('../entities/attribute');
const BUILTIN_ATTRIBUTES = [
'disableVersioning',
'calendarRoot',
'archived',
'excludeFromExport',
'run',
'manualTransactionHandling',
'disableInclusion',
'appCss',
'hideChildrenOverview'
];
async function getNotesWithAttribute(name, value) {
let notes;
if (value !== undefined) {
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
}
else {
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ?`, [name]);
}
return notes;
}
async function getNoteWithAttribute(name, value) {
const notes = await getNotesWithAttribute(name, value);
return notes.length > 0 ? notes[0] : null;
}
async function createAttribute(noteId, name, value = "") {
return await new Attribute({
noteId: noteId,
name: name,
value: value
}).save();
}
module.exports = {
getNotesWithAttribute,
getNoteWithAttribute,
createAttribute,
BUILTIN_ATTRIBUTES
};

View File

@@ -38,6 +38,10 @@ async function addNoteImageSync(noteImageId, sourceId) {
await addEntitySync("note_images", noteImageId, sourceId);
}
async function addAttributeSync(attributeId, sourceId) {
await addEntitySync("attributes", attributeId, sourceId);
}
async function addLabelSync(labelId, sourceId) {
await addEntitySync("labels", labelId, sourceId);
}
@@ -104,6 +108,7 @@ async function fillAllSyncRows() {
await fillSyncRows("recent_notes", "branchId");
await fillSyncRows("images", "imageId");
await fillSyncRows("note_images", "noteImageId");
await fillSyncRows("attributes", "attributeId");
await fillSyncRows("labels", "labelId");
await fillSyncRows("relations", "relationId");
await fillSyncRows("api_tokens", "apiTokenId");
@@ -119,6 +124,7 @@ module.exports = {
addRecentNoteSync,
addImageSync,
addNoteImageSync,
addAttributeSync,
addLabelSync,
addRelationSync,
addApiTokenSync,

View File

@@ -30,6 +30,9 @@ async function updateEntity(sync, entity, sourceId) {
else if (entityName === 'note_images') {
await updateNoteImage(entity, sourceId);
}
else if (entityName === 'attributes') {
await updateAttribute(entity, sourceId);
}
else if (entityName === 'labels') {
await updateLabel(entity, sourceId);
}
@@ -174,6 +177,20 @@ async function updateNoteImage(entity, sourceId) {
}
}
async function updateAttribute(entity, sourceId) {
const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
if (!origAttribute || origAttribute.dateModified <= entity.dateModified) {
await sql.transactional(async () => {
await sql.replace("attributes", entity);
await syncTableService.addAttributeSync(entity.attributeId, sourceId);
});
log.info("Update/sync attribute " + entity.attributeId);
}
}
async function updateLabel(entity, sourceId) {
const origLabel = await sql.getRow("SELECT * FROM labels WHERE labelId = ?", [entity.labelId]);