smaller refactorings continued

This commit is contained in:
azivner 2018-04-01 12:45:35 -04:00
parent 8ba830c04b
commit 15d951b04e
6 changed files with 43 additions and 35 deletions

View File

@ -3,6 +3,7 @@
const Entity = require('./entity');
const repository = require('../services/repository');
const utils = require('../services/utils');
const sql = require('../services/sql');
class Label extends Entity {
static get tableName() { return "labels"; }
@ -12,7 +13,24 @@ class Label extends Entity {
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
}
beforeSaving() {
async beforeSaving() {
if (!this.labelId) {
this.labelId = utils.newLabelId();
}
if (this.value) {
// null value isn't allowed
this.value = "";
}
if (this.position === undefined) {
this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]);
}
if (!this.isDeleted) {
this.isDeleted = false;
}
if (!this.dateCreated) {
this.dateCreated = utils.nowDate();
}

View File

@ -2,7 +2,6 @@
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
const repository = require('./repository');
const Label = require('../entities/label');
@ -58,21 +57,15 @@ async function getNoteIdsWithLabel(name) {
}
async function createLabel(noteId, name, value = "") {
if (value === null || value === undefined) {
value = "";
}
const labelId = utils.newLabelId();
const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]);
await (new Label({
labelId: labelId,
const label = new Label({
noteId: noteId,
name: name,
value: value,
position: position,
isDeleted: false
})).save();
value: value
});
await label.save();
return label;
}
module.exports = {

View File

@ -43,18 +43,20 @@ async function getLabel(labelId) {
async function updateEntity(entity) {
if (entity.beforeSaving) {
entity.beforeSaving();
await entity.beforeSaving();
}
const clone = Object.assign({}, entity);
delete clone.jsonContent;
await sql.replace(entity.constructor.tableName, clone);
await sql.doInTransaction(async () => {
await sql.replace(entity.constructor.tableName, clone);
const primaryKey = entity[entity.constructor.primaryKeyName];
const primaryKey = entity[entity.constructor.primaryKeyName];
await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);
await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);
});
}
module.exports = {

View File

@ -40,9 +40,12 @@ function ScriptApi(startNote, currentNote) {
this.getInstanceName = () => config.General ? config.General.instanceName : null;
this.getNoteById = async function(noteId) {
return await repository.getNote(noteId);
};
this.getNote = repository.getNote;
this.getBranch = repository.getBranch;
this.getLabel = repository.getLabel;
this.getImage = repository.getImage;
this.getEntity = repository.getEntity;
this.getEntities = repository.getEntities;
this.getNotesWithLabel = async function (labelName, labelValue) {
return await labels.getNotesWithLabel(labelName, labelValue);
@ -60,8 +63,6 @@ function ScriptApi(startNote, currentNote) {
this.createLabel = labels.createLabel;
this.updateEntity = repository.updateEntity;
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;

View File

@ -4,11 +4,9 @@ const sql = require('./sql');
const cls = require('./cls');
async function saveSourceId(sourceId) {
await sql.doInTransaction(async () => {
await sql.insert("source_ids", {
sourceId: sourceId,
dateCreated: utils.nowDate()
});
await sql.insert("source_ids", {
sourceId: sourceId,
dateCreated: utils.nowDate()
});
await refreshSourceIds();

View File

@ -95,9 +95,7 @@ async function getLastSyncedPull() {
}
async function setLastSyncedPull(syncId) {
await sql.doInTransaction(async () => {
await options.setOption('last_synced_pull', syncId);
});
await options.setOption('last_synced_pull', syncId);
}
async function pullSync(syncContext) {
@ -168,9 +166,7 @@ async function getLastSyncedPush() {
}
async function setLastSyncedPush(lastSyncedPush) {
await sql.doInTransaction(async () => {
await options.setOption('last_synced_push', lastSyncedPush);
});
await options.setOption('last_synced_push', lastSyncedPush);
}
async function pushSync(syncContext) {