2018-03-31 21:07:58 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-30 12:17:44 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const Note = require('../entities/note');
|
|
|
|
const NoteRevision = require('../entities/note_revision');
|
2018-03-25 09:39:15 +08:00
|
|
|
const Branch = require('../entities/branch');
|
2018-03-25 10:02:26 +08:00
|
|
|
const Label = require('../entities/label');
|
2018-01-31 09:12:19 +08:00
|
|
|
const sync_table = require('../services/sync_table');
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
async function getEntities(query, params = []) {
|
|
|
|
const rows = await sql.getRows(query, params);
|
|
|
|
|
|
|
|
return rows.map(row => this.createEntityFromRow(row));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getEntity(query, params = []) {
|
|
|
|
const row = await sql.getRowOrNull(query, params);
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
if (!row) {
|
|
|
|
return null;
|
2018-01-30 12:17:44 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
return this.createEntityFromRow(row);
|
|
|
|
}
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
async function getNote(noteId) {
|
|
|
|
return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
|
|
|
}
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
function createEntityFromRow(row) {
|
|
|
|
let entity;
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
if (row.labelId) {
|
|
|
|
entity = new Label(row);
|
2018-01-30 12:17:44 +08:00
|
|
|
}
|
2018-03-31 21:07:58 +08:00
|
|
|
else if (row.noteRevisionId) {
|
|
|
|
entity = new NoteRevision(row);
|
|
|
|
}
|
|
|
|
else if (row.branchId) {
|
|
|
|
entity = new Branch(row);
|
|
|
|
}
|
|
|
|
else if (row.noteId) {
|
|
|
|
entity = new Note(row);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
|
2018-01-30 12:17:44 +08:00
|
|
|
}
|
2018-01-30 12:35:36 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
return entity;
|
|
|
|
}
|
2018-01-30 12:35:36 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
async function updateEntity(entity) {
|
|
|
|
if (entity.beforeSaving) {
|
|
|
|
entity.beforeSaving();
|
|
|
|
}
|
2018-01-30 12:35:36 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
const clone = Object.assign({}, entity);
|
2018-01-30 12:35:36 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
delete clone.jsonContent;
|
2018-01-31 09:12:19 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
await sql.replace(entity.constructor.tableName, clone);
|
2018-01-31 09:12:19 +08:00
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
const primaryKey = entity[entity.constructor.primaryKeyName];
|
|
|
|
|
|
|
|
await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);
|
2018-01-30 12:17:44 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
module.exports = {
|
|
|
|
getEntities,
|
|
|
|
getEntity,
|
|
|
|
getNote,
|
|
|
|
updateEntity
|
|
|
|
};
|