2018-03-31 09:07:58 -04:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-29 23:17:44 -05:00
|
|
|
const sql = require('./sql');
|
2018-04-01 21:27:46 -04:00
|
|
|
const syncTableService = require('../services/sync_table');
|
2018-08-15 22:06:49 +02:00
|
|
|
const eventService = require('./events');
|
2018-11-26 22:27:57 +01:00
|
|
|
const cls = require('./cls');
|
2020-06-20 12:31:38 +02:00
|
|
|
const entityConstructor = require('../entities/entity_constructor');
|
2018-01-29 23:17:44 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getEntityFromName(entityName, entityId) {
|
2018-08-13 10:59:31 +02:00
|
|
|
if (!entityName || !entityId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:00:04 +02:00
|
|
|
const constructor = entityConstructor.getEntityFromEntityName(entityName);
|
2018-08-09 20:08:00 +02:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
return getEntity(`SELECT * FROM ${constructor.entityName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]);
|
2018-08-09 20:08:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getEntities(query, params = []) {
|
|
|
|
const rows = sql.getRows(query, params);
|
2018-03-31 09:07:58 -04:00
|
|
|
|
2018-08-09 20:08:00 +02:00
|
|
|
return rows.map(entityConstructor.createEntityFromRow);
|
2018-03-31 09:07:58 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getEntity(query, params = []) {
|
|
|
|
const row = sql.getRowOrNull(query, params);
|
2018-01-29 23:17:44 -05:00
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
if (!row) {
|
|
|
|
return null;
|
2018-01-29 23:17:44 -05:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:08:00 +02:00
|
|
|
return entityConstructor.createEntityFromRow(row);
|
2018-03-31 09:07:58 -04:00
|
|
|
}
|
2018-01-29 23:17:44 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getCachedEntity(entityName, entityId, query) {
|
2020-04-06 20:59:04 +02:00
|
|
|
let entity = cls.getEntityFromCache(entityName, entityId);
|
|
|
|
|
|
|
|
if (!entity) {
|
2020-06-20 12:31:38 +02:00
|
|
|
entity = getEntity(query, [entityId]);
|
2020-04-06 20:59:04 +02:00
|
|
|
|
|
|
|
cls.setEntityToCache(entityName, entityId, entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:50:33 +02:00
|
|
|
/** @returns {Note|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNote(noteId) {
|
|
|
|
return getCachedEntity('notes', noteId, "SELECT * FROM notes WHERE noteId = ?");
|
2018-03-31 09:07:58 -04:00
|
|
|
}
|
2018-01-29 23:35:36 -05:00
|
|
|
|
2020-07-01 23:50:33 +02:00
|
|
|
/** @returns {Note[]} */
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNotes(noteIds) {
|
2019-04-22 18:08:33 +02:00
|
|
|
// this note might be optimised, but remember that it must keep the existing order of noteIds
|
|
|
|
// (important e.g. for @orderBy in search)
|
|
|
|
const notes = [];
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = getNote(noteId);
|
2019-04-22 18:08:33 +02:00
|
|
|
|
|
|
|
notes.push(note);
|
|
|
|
}
|
|
|
|
|
|
|
|
return notes;
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:50:33 +02:00
|
|
|
/** @returns {NoteRevision|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNoteRevision(noteRevisionId) {
|
|
|
|
return getCachedEntity('note_revisions', noteRevisionId, "SELECT * FROM note_revisions WHERE noteRevisionId = ?");
|
2019-11-01 19:21:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:50:33 +02:00
|
|
|
/** @returns {Branch|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
function getBranch(branchId) {
|
|
|
|
return getCachedEntity('branches', branchId, "SELECT * FROM branches WHERE branchId = ?", [branchId]);
|
2018-03-31 23:08:22 -04:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:50:33 +02:00
|
|
|
/** @returns {Attribute|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
function getAttribute(attributeId) {
|
|
|
|
return getCachedEntity('attributes', attributeId, "SELECT * FROM attributes WHERE attributeId = ?");
|
2018-08-05 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:50:33 +02:00
|
|
|
/** @returns {Option|null} */
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOption(name) {
|
|
|
|
return getEntity("SELECT * FROM options WHERE name = ?", [name]);
|
2018-05-22 00:22:43 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function updateEntity(entity) {
|
2018-08-16 23:00:04 +02:00
|
|
|
const entityName = entity.constructor.entityName;
|
2018-08-15 22:06:49 +02:00
|
|
|
const primaryKeyName = entity.constructor.primaryKeyName;
|
|
|
|
|
|
|
|
const isNewEntity = !entity[primaryKeyName];
|
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
if (entity.beforeSaving) {
|
2020-06-20 12:31:38 +02:00
|
|
|
entity.beforeSaving();
|
2018-03-31 09:07:58 -04:00
|
|
|
}
|
2018-01-29 23:35:36 -05:00
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
const clone = Object.assign({}, entity);
|
2018-01-29 23:35:36 -05:00
|
|
|
|
2018-11-30 10:20:03 +01:00
|
|
|
// this check requires that updatePojo is not static
|
|
|
|
if (entity.updatePojo) {
|
2020-06-20 12:31:38 +02:00
|
|
|
entity.updatePojo(clone);
|
2018-11-30 10:20:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// indicates whether entity actually changed
|
2018-08-12 20:04:48 +02:00
|
|
|
delete clone.isChanged;
|
2018-01-30 20:12:19 -05:00
|
|
|
|
2018-08-06 08:59:26 +02:00
|
|
|
for (const key in clone) {
|
2018-08-11 19:45:55 +02:00
|
|
|
// !isBuffer is for images and attachments
|
|
|
|
if (clone[key] !== null && typeof clone[key] === 'object' && !Buffer.isBuffer(clone[key])) {
|
2018-08-06 08:59:26 +02:00
|
|
|
clone[key] = JSON.stringify(clone[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.transactional(() => {
|
|
|
|
sql.upsert(entityName, primaryKeyName, clone);
|
2018-08-15 22:06:49 +02:00
|
|
|
|
|
|
|
const primaryKey = entity[primaryKeyName];
|
|
|
|
|
2018-12-13 21:18:35 +01:00
|
|
|
if (entity.isChanged) {
|
2020-03-09 21:28:41 +01:00
|
|
|
const isSynced = entityName !== 'options' || entity.isSynced;
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
syncTableService.addEntitySync(entityName, primaryKey, null, isSynced);
|
2018-01-30 20:12:19 -05:00
|
|
|
|
2018-11-26 22:37:59 +01:00
|
|
|
if (!cls.isEntityEventsDisabled()) {
|
|
|
|
const eventPayload = {
|
|
|
|
entityName,
|
|
|
|
entity
|
|
|
|
};
|
2018-03-31 09:07:58 -04:00
|
|
|
|
2018-11-26 22:37:59 +01:00
|
|
|
if (isNewEntity && !entity.isDeleted) {
|
2020-06-20 12:31:38 +02:00
|
|
|
eventService.emit(eventService.ENTITY_CREATED, eventPayload);
|
2018-11-26 22:37:59 +01:00
|
|
|
}
|
2018-11-26 22:22:16 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
eventService.emit(entity.isDeleted ? eventService.ENTITY_DELETED : eventService.ENTITY_CHANGED, eventPayload);
|
2018-11-26 22:37:59 +01:00
|
|
|
}
|
2018-05-22 19:29:18 -04:00
|
|
|
}
|
2018-04-01 12:45:35 -04:00
|
|
|
});
|
2018-01-29 23:17:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
module.exports = {
|
2018-08-09 20:08:00 +02:00
|
|
|
getEntityFromName,
|
2018-03-31 09:07:58 -04:00
|
|
|
getEntities,
|
|
|
|
getEntity,
|
|
|
|
getNote,
|
2019-04-22 18:08:33 +02:00
|
|
|
getNotes,
|
2019-11-01 20:00:56 +01:00
|
|
|
getNoteRevision,
|
2018-03-31 23:08:22 -04:00
|
|
|
getBranch,
|
2018-08-05 20:08:56 +02:00
|
|
|
getAttribute,
|
2018-05-22 00:22:43 -04:00
|
|
|
getOption,
|
2020-06-20 12:31:38 +02:00
|
|
|
updateEntity
|
2020-06-14 00:35:53 +02:00
|
|
|
};
|