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 Entity = require('./entity');
const repository = require('../services/repository'); const repository = require('../services/repository');
const utils = require('../services/utils'); const utils = require('../services/utils');
const sql = require('../services/sql');
class Label extends Entity { class Label extends Entity {
static get tableName() { return "labels"; } static get tableName() { return "labels"; }
@ -12,7 +13,24 @@ class Label extends Entity {
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); 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) { if (!this.dateCreated) {
this.dateCreated = utils.nowDate(); this.dateCreated = utils.nowDate();
} }

View File

@ -2,7 +2,6 @@
const sql = require('./sql'); const sql = require('./sql');
const utils = require('./utils'); const utils = require('./utils');
const sync_table = require('./sync_table');
const repository = require('./repository'); const repository = require('./repository');
const Label = require('../entities/label'); const Label = require('../entities/label');
@ -58,21 +57,15 @@ async function getNoteIdsWithLabel(name) {
} }
async function createLabel(noteId, name, value = "") { async function createLabel(noteId, name, value = "") {
if (value === null || value === undefined) { const label = new Label({
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,
noteId: noteId, noteId: noteId,
name: name, name: name,
value: value, value: value
position: position, });
isDeleted: false
})).save(); await label.save();
return label;
} }
module.exports = { module.exports = {

View File

@ -43,18 +43,20 @@ async function getLabel(labelId) {
async function updateEntity(entity) { async function updateEntity(entity) {
if (entity.beforeSaving) { if (entity.beforeSaving) {
entity.beforeSaving(); await entity.beforeSaving();
} }
const clone = Object.assign({}, entity); const clone = Object.assign({}, entity);
delete clone.jsonContent; delete clone.jsonContent;
await sql.doInTransaction(async () => {
await sql.replace(entity.constructor.tableName, clone); 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 = { module.exports = {

View File

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

View File

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

View File

@ -95,9 +95,7 @@ async function getLastSyncedPull() {
} }
async function setLastSyncedPull(syncId) { 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) { async function pullSync(syncContext) {
@ -168,9 +166,7 @@ async function getLastSyncedPush() {
} }
async function setLastSyncedPush(lastSyncedPush) { 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) { async function pushSync(syncContext) {