Notes/src/services/repository.js

149 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-03-31 09:07:58 -04:00
"use strict";
const sql = require('./sql');
const syncTableService = require('../services/sync_table');
const eventService = require('./events');
const cls = require('./cls');
let entityConstructor;
async function setEntityConstructor(constructor) {
entityConstructor = constructor;
}
2018-08-09 20:08:00 +02:00
async function getEntityFromName(entityName, entityId) {
2018-08-13 10:59:31 +02:00
if (!entityName || !entityId) {
return null;
}
const constructor = entityConstructor.getEntityFromEntityName(entityName);
2018-08-09 20:08:00 +02:00
return await getEntity(`SELECT * FROM ${constructor.entityName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]);
2018-08-09 20:08:00 +02:00
}
2018-03-31 09:07:58 -04:00
async function getEntities(query, params = []) {
const rows = await sql.getRows(query, params);
2018-08-09 20:08:00 +02:00
return rows.map(entityConstructor.createEntityFromRow);
2018-03-31 09:07:58 -04:00
}
async function getEntity(query, params = []) {
const row = await sql.getRowOrNull(query, params);
2018-03-31 09:07:58 -04:00
if (!row) {
return null;
}
2018-08-09 20:08:00 +02:00
return entityConstructor.createEntityFromRow(row);
2018-03-31 09:07:58 -04:00
}
/** @returns {Promise<Note|null>} */
2018-03-31 09:07:58 -04:00
async function getNote(noteId) {
return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
2018-03-31 09:07:58 -04:00
}
2018-01-29 23:35:36 -05:00
2019-04-22 18:08:33 +02:00
/** @returns {Promise<Note[]>} */
async function getNotes(noteIds) {
// 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) {
const note = await getNote(noteId);
notes.push(note);
}
return notes;
}
2019-11-01 19:21:48 +01:00
/** @returns {Promise<NoteRevision|null>} */
async function getNoteRevision(noteRevisionId) {
return await getEntity("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);
}
/** @returns {Promise<Branch|null>} */
2018-03-31 23:08:22 -04:00
async function getBranch(branchId) {
return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);
}
/** @returns {Promise<Attribute|null>} */
async function getAttribute(attributeId) {
return await getEntity("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]);
}
/** @returns {Promise<Option|null>} */
async function getOption(name) {
return await getEntity("SELECT * FROM options WHERE name = ?", [name]);
}
2018-03-31 09:07:58 -04:00
async function updateEntity(entity) {
const entityName = entity.constructor.entityName;
const primaryKeyName = entity.constructor.primaryKeyName;
const isNewEntity = !entity[primaryKeyName];
2018-03-31 09:07:58 -04:00
if (entity.beforeSaving) {
2018-04-01 12:45:35 -04:00
await 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
// this check requires that updatePojo is not static
if (entity.updatePojo) {
await entity.updatePojo(clone);
}
// indicates whether entity actually changed
delete clone.isChanged;
2018-01-30 20:12:19 -05: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])) {
clone[key] = JSON.stringify(clone[key]);
}
}
await sql.transactional(async () => {
await sql.upsert(entityName, primaryKeyName, clone);
const primaryKey = entity[primaryKeyName];
if (entity.isChanged) {
if (entityName !== 'options' || entity.isSynced) {
await syncTableService.addEntitySync(entityName, primaryKey);
}
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) {
await eventService.emit(eventService.ENTITY_CREATED, eventPayload);
}
2018-11-26 22:22:16 +01:00
2018-11-26 22:37:59 +01:00
// it seems to be better to handle deletion and update separately
await eventService.emit(entity.isDeleted ? eventService.ENTITY_DELETED : eventService.ENTITY_CHANGED, eventPayload);
}
2018-05-22 19:29:18 -04:00
}
if (entity.afterSaving) {
await entity.afterSaving();
}
2018-04-01 12:45:35 -04: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,
2018-03-31 23:08:22 -04:00
getBranch,
getAttribute,
getOption,
updateEntity,
setEntityConstructor
2018-03-31 09:07:58 -04:00
};