renaming attributes to labels, fixes #79

This commit is contained in:
azivner
2018-03-24 22:02:26 -04:00
parent 4c472ce78b
commit 95bb2cf0bb
40 changed files with 334 additions and 312 deletions

View File

@@ -3,7 +3,7 @@
const build = require('./build');
const packageJson = require('../../package');
const APP_DB_VERSION = 79;
const APP_DB_VERSION = 80;
module.exports = {
app_version: packageJson.version,

View File

@@ -1,89 +0,0 @@
"use strict";
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
const BUILTIN_ATTRIBUTES = [
'frontend_startup',
'backend_startup',
'disable_versioning',
'calendar_root',
'hide_in_autocomplete',
'exclude_from_export',
'run',
'manual_transaction_handling',
'disable_inclusion',
'app_css'
];
async function getNoteAttributeMap(noteId) {
return await sql.getMap(`SELECT name, value FROM attributes WHERE noteId = ? AND isDeleted = 0`, [noteId]);
}
async function getNoteIdWithAttribute(name, value) {
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0
AND attributes.isDeleted = 0
AND attributes.name = ?
AND attributes.value = ?`, [name, value]);
}
async function getNotesWithAttribute(repository, 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(repository, name, value) {
const notes = getNotesWithAttribute(repository, name, value);
return notes.length > 0 ? notes[0] : null;
}
async function getNoteIdsWithAttribute(name) {
return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? AND attributes.isDeleted = 0`, [name]);
}
async function createAttribute(noteId, name, value = "", sourceId = null) {
if (value === null || value === undefined) {
value = "";
}
const now = utils.nowDate();
const attributeId = utils.newAttributeId();
const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [noteId]);
await sql.insert("attributes", {
attributeId: attributeId,
noteId: noteId,
name: name,
value: value,
position: position,
dateModified: now,
dateCreated: now,
isDeleted: false
});
await sync_table.addAttributeSync(attributeId, sourceId);
}
module.exports = {
getNoteAttributeMap,
getNoteIdWithAttribute,
getNotesWithAttribute,
getNoteWithAttribute,
getNoteIdsWithAttribute,
createAttribute,
BUILTIN_ATTRIBUTES
};

View File

@@ -7,16 +7,16 @@ module.exports = function(attrFilters, searchText) {
let i = 1;
for (const filter of attrFilters) {
joins.push(`LEFT JOIN attributes AS attr${i} ON attr${i}.noteId = notes.noteId AND attr${i}.name = ?`);
joins.push(`LEFT JOIN labels AS attr${i} ON attr${i}.noteId = notes.noteId AND attr${i}.name = ?`);
joinParams.push(filter.name);
where += " " + filter.relation + " ";
if (filter.operator === 'exists') {
where += `attr${i}.attributeId IS NOT NULL`;
where += `attr${i}.labelId IS NOT NULL`;
}
else if (filter.operator === 'not-exists') {
where += `attr${i}.attributeId IS NULL`;
where += `attr${i}.labelId IS NULL`;
}
else if (filter.operator === '=' || filter.operator === '!=') {
where += `attr${i}.value ${filter.operator} ?`;

View File

@@ -233,7 +233,7 @@ async function runAllChecks() {
await runSyncRowChecks("recent_notes", "branchId", errorList);
await runSyncRowChecks("images", "imageId", errorList);
await runSyncRowChecks("note_images", "noteImageId", errorList);
await runSyncRowChecks("attributes", "attributeId", errorList);
await runSyncRowChecks("labels", "labelId", errorList);
await runSyncRowChecks("api_tokens", "apiTokenId", errorList);
if (errorList.length === 0) {

View File

@@ -83,16 +83,16 @@ async function getHashes() {
FROM images
ORDER BY imageId`)),
attributes: getHash(await sql.getRows(`
labels: getHash(await sql.getRows(`
SELECT
attributeId,
labelId,
noteId
name,
value
dateModified,
dateCreated
FROM attributes
ORDER BY attributeId`))
FROM labels
ORDER BY labelId`))
};
const elapseTimeMs = new Date().getTime() - startTime.getTime();

View File

@@ -2,13 +2,13 @@
const sql = require('./sql');
const notes = require('./notes');
const attributes = require('./attributes');
const labels = require('./labels');
const utils = require('./utils');
const CALENDAR_ROOT_ATTRIBUTE = 'calendar_root';
const YEAR_ATTRIBUTE = 'year_note';
const MONTH_ATTRIBUTE = 'month_note';
const DATE_ATTRIBUTE = 'date_note';
const CALENDAR_ROOT_LABEL = 'calendar_root';
const YEAR_LABEL = 'year_note';
const MONTH_LABEL = 'month_note';
const DATE_LABEL = 'date_note';
const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];
@@ -30,8 +30,8 @@ async function getNoteStartingWith(parentNoteId, startsWith) {
}
async function getRootCalendarNoteId() {
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`);
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
if (!rootNoteId) {
rootNoteId = (await notes.createNewNote('root', {
@@ -40,7 +40,7 @@ async function getRootCalendarNoteId() {
isProtected: false
})).noteId;
await attributes.createAttribute(rootNoteId, CALENDAR_ROOT_ATTRIBUTE);
await labels.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);
}
return rootNoteId;
@@ -49,7 +49,7 @@ async function getRootCalendarNoteId() {
async function getYearNoteId(dateTimeStr, rootNoteId) {
const yearStr = dateTimeStr.substr(0, 4);
let yearNoteId = await attributes.getNoteIdWithAttribute(YEAR_ATTRIBUTE, yearStr);
let yearNoteId = await labels.getNoteIdWithLabel(YEAR_LABEL, yearStr);
if (!yearNoteId) {
yearNoteId = await getNoteStartingWith(rootNoteId, yearStr);
@@ -58,7 +58,7 @@ async function getYearNoteId(dateTimeStr, rootNoteId) {
yearNoteId = await createNote(rootNoteId, yearStr);
}
await attributes.createAttribute(yearNoteId, YEAR_ATTRIBUTE, yearStr);
await labels.createLabel(yearNoteId, YEAR_LABEL, yearStr);
}
return yearNoteId;
@@ -68,7 +68,7 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {
const monthStr = dateTimeStr.substr(0, 7);
const monthNumber = dateTimeStr.substr(5, 2);
let monthNoteId = await attributes.getNoteIdWithAttribute(MONTH_ATTRIBUTE, monthStr);
let monthNoteId = await labels.getNoteIdWithLabel(MONTH_LABEL, monthStr);
if (!monthNoteId) {
const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId);
@@ -83,7 +83,7 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {
monthNoteId = await createNote(yearNoteId, noteTitle);
}
await attributes.createAttribute(monthNoteId, MONTH_ATTRIBUTE, monthStr);
await labels.createLabel(monthNoteId, MONTH_LABEL, monthStr);
}
return monthNoteId;
@@ -97,7 +97,7 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) {
const dateStr = dateTimeStr.substr(0, 10);
const dayNumber = dateTimeStr.substr(8, 2);
let dateNoteId = await attributes.getNoteIdWithAttribute(DATE_ATTRIBUTE, dateStr);
let dateNoteId = await labels.getNoteIdWithLabel(DATE_LABEL, dateStr);
if (!dateNoteId) {
const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId);
@@ -112,7 +112,7 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) {
dateNoteId = await createNote(monthNoteId, noteTitle);
}
await attributes.createAttribute(dateNoteId, DATE_ATTRIBUTE, dateStr);
await labels.createLabel(dateNoteId, DATE_LABEL, dateStr);
}
return dateNoteId;

89
src/services/labels.js Normal file
View File

@@ -0,0 +1,89 @@
"use strict";
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
const BUILTIN_LABELS = [
'frontend_startup',
'backend_startup',
'disable_versioning',
'calendar_root',
'hide_in_autocomplete',
'exclude_from_export',
'run',
'manual_transaction_handling',
'disable_inclusion',
'app_css'
];
async function getNoteLabelMap(noteId) {
return await sql.getMap(`SELECT name, value FROM labels WHERE noteId = ? AND isDeleted = 0`, [noteId]);
}
async function getNoteIdWithLabel(name, value) {
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0
AND labels.isDeleted = 0
AND labels.name = ?
AND labels.value = ?`, [name, value]);
}
async function getNotesWithLabel(repository, name, value) {
let notes;
if (value !== undefined) {
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.value = ?`, [name, value]);
}
else {
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ?`, [name]);
}
return notes;
}
async function getNoteWithLabel(repository, name, value) {
const notes = getNotesWithLabel(repository, name, value);
return notes.length > 0 ? notes[0] : null;
}
async function getNoteIdsWithLabel(name) {
return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.isDeleted = 0`, [name]);
}
async function createLabel(noteId, name, value = "", sourceId = null) {
if (value === null || value === undefined) {
value = "";
}
const now = utils.nowDate();
const labelId = utils.newLabelId();
const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]);
await sql.insert("labels", {
labelId: labelId,
noteId: noteId,
name: name,
value: value,
position: position,
dateModified: now,
dateCreated: now,
isDeleted: false
});
await sync_table.addLabelSync(labelId, sourceId);
}
module.exports = {
getNoteLabelMap,
getNoteIdWithLabel,
getNotesWithLabel,
getNoteWithLabel,
getNoteIdsWithLabel,
createLabel,
BUILTIN_LABELS
};

View File

@@ -2,7 +2,7 @@ const sql = require('./sql');
const options = require('./options');
const utils = require('./utils');
const sync_table = require('./sync_table');
const attributes = require('./attributes');
const labels = require('./labels');
const protected_session = require('./protected_session');
async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
@@ -108,9 +108,9 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
const {noteId} = await createNewNote(parentNoteId, note, extraOptions.dataKey, extraOptions.sourceId);
if (extraOptions.attributes) {
for (const attrName in extraOptions.attributes) {
await attributes.createAttribute(noteId, attrName, extraOptions.attributes[attrName]);
if (extraOptions.labels) {
for (const attrName in extraOptions.labels) {
await labels.createLabel(noteId, attrName, extraOptions.labels[attrName]);
}
}
@@ -274,7 +274,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) {
await protected_session.encryptNote(dataKey, newNote.detail);
}
const attributesMap = await attributes.getNoteAttributeMap(noteId);
const labelsMap = await labels.getNoteLabelMap(noteId);
const now = new Date();
const nowStr = utils.nowDate();
@@ -289,7 +289,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) {
await sql.doInTransaction(async () => {
const msSinceDateCreated = now.getTime() - utils.parseDateTime(newNote.detail.dateCreated).getTime();
if (attributesMap.disable_versioning !== 'true'
if (labelsMap.disable_versioning !== 'true'
&& !existingnoteRevisionId
&& msSinceDateCreated >= historySnapshotTimeInterval * 1000) {

View File

@@ -18,7 +18,7 @@ module.exports = function(searchText) {
value: match[7] !== undefined ? trimQuotes(match[7]) : null
});
// remove attributes from further fulltext search
// remove labels from further fulltext search
searchText = searchText.split(match[0]).join('');
match = attrRegex.exec(searchText);

View File

@@ -3,7 +3,7 @@ const protected_session = require('./protected_session');
const Note = require('../entities/note');
const NoteRevision = require('../entities/note_revision');
const Branch = require('../entities/branch');
const Attribute = require('../entities/attribute');
const Label = require('../entities/label');
const sync_table = require('../services/sync_table');
class Repository {
@@ -40,8 +40,8 @@ class Repository {
createEntityFromRow(row) {
let entity;
if (row.attributeId) {
entity = new Attribute(this, row);
if (row.labelId) {
entity = new Label(this, row);
}
else if (row.noteRevisionId) {
entity = new NoteRevision(this, row);

View File

@@ -3,14 +3,14 @@ const Repository = require('./repository');
const repo = new Repository();
async function runNotesWithAttribute(runAttrValue) {
async function runNotesWithLabel(runAttrValue) {
const notes = await repo.getEntities(`
SELECT notes.*
FROM notes
JOIN attributes ON attributes.noteId = notes.noteId
AND attributes.isDeleted = 0
AND attributes.name = 'run'
AND attributes.value = ?
JOIN labels ON labels.noteId = notes.noteId
AND labels.isDeleted = 0
AND labels.name = 'run'
AND labels.value = ?
WHERE
notes.type = 'code'
AND notes.isDeleted = 0`, [runAttrValue]);
@@ -20,8 +20,8 @@ async function runNotesWithAttribute(runAttrValue) {
}
}
setTimeout(() => runNotesWithAttribute('backend_startup'), 10 * 1000);
setTimeout(() => runNotesWithLabel('backend_startup'), 10 * 1000);
setInterval(() => runNotesWithAttribute('hourly'), 3600 * 1000);
setInterval(() => runNotesWithLabel('hourly'), 3600 * 1000);
setInterval(() => runNotesWithAttribute('daily'), 24 * 3600 * 1000);
setInterval(() => runNotesWithLabel('daily'), 24 * 3600 * 1000);

View File

@@ -23,7 +23,7 @@ async function executeBundle(dataKey, bundle, startNote) {
const ctx = new ScriptContext(dataKey, startNote, bundle.allNotes);
if (await bundle.note.hasAttribute('manual_transaction_handling')) {
if (await bundle.note.hasLabel('manual_transaction_handling')) {
return await execute(ctx, script, '');
}
else {
@@ -73,7 +73,7 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote
return;
}
if (!root && await note.hasAttribute('disable_inclusion')) {
if (!root && await note.hasLabel('disable_inclusion')) {
return;
}

View File

@@ -3,7 +3,7 @@ const protected_session = require('./protected_session');
const notes = require('./notes');
const sql = require('./sql');
const utils = require('./utils');
const attributes = require('./attributes');
const labels = require('./labels');
const date_notes = require('./date_notes');
const config = require('./config');
const Repository = require('./repository');
@@ -48,12 +48,12 @@ function ScriptApi(dataKey, startNote, currentNote) {
return repository.getNote(noteId);
};
this.getNotesWithAttribute = async function (attrName, attrValue) {
return await attributes.getNotesWithAttribute(repository, attrName, attrValue);
this.getNotesWithLabel = async function (attrName, attrValue) {
return await labels.getNotesWithLabel(repository, attrName, attrValue);
};
this.getNoteWithAttribute = async function (attrName, attrValue) {
const notes = await this.getNotesWithAttribute(attrName, attrValue);
this.getNoteWithLabel = async function (attrName, attrValue) {
const notes = await this.getNotesWithLabel(attrName, attrValue);
return notes.length > 0 ? notes[0] : null;
};
@@ -64,7 +64,7 @@ function ScriptApi(dataKey, startNote, currentNote) {
return await notes.createNote(parentNoteId, title, content, extraOptions);
};
this.createAttribute = attributes.createAttribute;
this.createLabel = labels.createLabel;
this.updateEntity = repository.updateEntity;

View File

@@ -146,8 +146,8 @@ async function pullSync(syncContext) {
else if (sync.entityName === 'note_images') {
await syncUpdate.updateNoteImage(resp, syncContext.sourceId);
}
else if (sync.entityName === 'attributes') {
await syncUpdate.updateAttribute(resp, syncContext.sourceId);
else if (sync.entityName === 'labels') {
await syncUpdate.updateLabel(resp, syncContext.sourceId);
}
else if (sync.entityName === 'api_tokens') {
await syncUpdate.updateApiToken(resp, syncContext.sourceId);
@@ -235,8 +235,8 @@ async function pushEntity(sync, syncContext) {
else if (sync.entityName === 'note_images') {
entity = await sql.getRow('SELECT * FROM note_images WHERE noteImageId = ?', [sync.entityId]);
}
else if (sync.entityName === 'attributes') {
entity = await sql.getRow('SELECT * FROM attributes WHERE attributeId = ?', [sync.entityId]);
else if (sync.entityName === 'labels') {
entity = await sql.getRow('SELECT * FROM labels WHERE labelId = ?', [sync.entityId]);
}
else if (sync.entityName === 'api_tokens') {
entity = await sql.getRow('SELECT * FROM api_tokens WHERE apiTokenId = ?', [sync.entityId]);

View File

@@ -36,8 +36,8 @@ 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);
}
async function addApiTokenSync(apiTokenId, sourceId) {
@@ -96,7 +96,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("api_tokens", "apiTokenId");
}
@@ -109,7 +109,7 @@ module.exports = {
addRecentNoteSync,
addImageSync,
addNoteImageSync,
addAttributeSync,
addLabelSync,
addApiTokenSync,
addEntitySync,
cleanupSyncRowsForMissingEntities,

View File

@@ -130,17 +130,17 @@ async function updateNoteImage(entity, sourceId) {
}
}
async function updateAttribute(entity, sourceId) {
const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
async function updateLabel(entity, sourceId) {
const origLabel = await sql.getRow("SELECT * FROM labels WHERE labelId = ?", [entity.labelId]);
if (!origAttribute || origAttribute.dateModified <= entity.dateModified) {
if (!origLabel || origLabel.dateModified <= entity.dateModified) {
await sql.doInTransaction(async () => {
await sql.replace("attributes", entity);
await sql.replace("labels", entity);
await sync_table.addAttributeSync(entity.attributeId, sourceId);
await sync_table.addLabelSync(entity.labelId, sourceId);
});
log.info("Update/sync attribute " + entity.attributeId);
log.info("Update/sync label " + entity.labelId);
}
}
@@ -167,6 +167,6 @@ module.exports = {
updateRecentNotes,
updateImage,
updateNoteImage,
updateAttribute,
updateLabel,
updateApiToken
};

View File

@@ -24,7 +24,7 @@ function newNoteImageId() {
return randomString(12);
}
function newAttributeId() {
function newLabelId() {
return randomString(12);
}
@@ -159,7 +159,7 @@ module.exports = {
newNoteRevisionId,
newImageId,
newNoteImageId,
newAttributeId,
newLabelId,
newApiTokenId,
toBase64,
fromBase64,