2018-03-31 21:07:58 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-30 12:17:44 +08:00
|
|
|
const sql = require('./sql');
|
2018-04-02 09:27:46 +08:00
|
|
|
const syncTableService = require('../services/sync_table');
|
2018-08-16 04:06:49 +08:00
|
|
|
const eventService = require('./events');
|
2018-11-27 05:27:57 +08:00
|
|
|
const cls = require('./cls');
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-03-31 22:51:37 +08:00
|
|
|
let entityConstructor;
|
|
|
|
|
|
|
|
async function setEntityConstructor(constructor) {
|
|
|
|
entityConstructor = constructor;
|
|
|
|
}
|
|
|
|
|
2018-08-10 02:08:00 +08:00
|
|
|
async function getEntityFromName(entityName, entityId) {
|
2018-08-13 16:59:31 +08:00
|
|
|
if (!entityName || !entityId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-17 05:00:04 +08:00
|
|
|
const constructor = entityConstructor.getEntityFromEntityName(entityName);
|
2018-08-10 02:08:00 +08:00
|
|
|
|
2018-08-17 05:00:04 +08:00
|
|
|
return await getEntity(`SELECT * FROM ${constructor.entityName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]);
|
2018-08-10 02:08:00 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
async function getEntities(query, params = []) {
|
|
|
|
const rows = await sql.getRows(query, params);
|
|
|
|
|
2018-08-10 02:08:00 +08:00
|
|
|
return rows.map(entityConstructor.createEntityFromRow);
|
2018-03-31 21:07:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-10 02:08:00 +08:00
|
|
|
return entityConstructor.createEntityFromRow(row);
|
2018-03-31 21:07:58 +08:00
|
|
|
}
|
2018-01-30 12:17:44 +08:00
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
/** @returns {Note|null} */
|
2018-03-31 21:07:58 +08:00
|
|
|
async function getNote(noteId) {
|
2018-03-31 22:51:37 +08:00
|
|
|
return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
2018-03-31 21:07:58 +08:00
|
|
|
}
|
2018-01-30 12:35:36 +08:00
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
/** @returns {Branch|null} */
|
2018-04-01 11:08:22 +08:00
|
|
|
async function getBranch(branchId) {
|
|
|
|
return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
/** @returns {Image|null} */
|
2018-04-01 23:05:09 +08:00
|
|
|
async function getImage(imageId) {
|
|
|
|
return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
/** @returns {Attribute|null} */
|
2018-08-06 02:08:56 +08:00
|
|
|
async function getAttribute(attributeId) {
|
|
|
|
return await getEntity("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]);
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
/** @returns {Option|null} */
|
2018-05-22 12:22:43 +08:00
|
|
|
async function getOption(name) {
|
|
|
|
return await getEntity("SELECT * FROM options WHERE name = ?", [name]);
|
|
|
|
}
|
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
async function updateEntity(entity) {
|
2018-08-17 05:00:04 +08:00
|
|
|
const entityName = entity.constructor.entityName;
|
2018-08-16 04:06:49 +08:00
|
|
|
const primaryKeyName = entity.constructor.primaryKeyName;
|
|
|
|
|
|
|
|
const isNewEntity = !entity[primaryKeyName];
|
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
if (entity.beforeSaving) {
|
2018-04-02 00:45:35 +08:00
|
|
|
await entity.beforeSaving();
|
2018-03-31 21:07:58 +08:00
|
|
|
}
|
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-11-30 17:20:03 +08:00
|
|
|
// this check requires that updatePojo is not static
|
|
|
|
if (entity.updatePojo) {
|
|
|
|
await entity.updatePojo(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
// indicates whether entity actually changed
|
2018-08-13 02:04:48 +08:00
|
|
|
delete clone.isChanged;
|
2018-01-31 09:12:19 +08:00
|
|
|
|
2018-08-06 14:59:26 +08:00
|
|
|
for (const key in clone) {
|
2018-08-12 01:45:55 +08:00
|
|
|
// !isBuffer is for images and attachments
|
|
|
|
if (clone[key] !== null && typeof clone[key] === 'object' && !Buffer.isBuffer(clone[key])) {
|
2018-08-06 14:59:26 +08:00
|
|
|
clone[key] = JSON.stringify(clone[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2018-08-16 04:06:49 +08:00
|
|
|
await sql.replace(entityName, clone);
|
|
|
|
|
|
|
|
const primaryKey = entity[primaryKeyName];
|
|
|
|
|
2018-12-14 04:18:35 +08:00
|
|
|
if (entity.isChanged) {
|
|
|
|
if (entityName !== 'options' || entity.isSynced) {
|
|
|
|
await syncTableService.addEntitySync(entityName, primaryKey);
|
|
|
|
}
|
2018-01-31 09:12:19 +08:00
|
|
|
|
2018-11-27 05:37:59 +08:00
|
|
|
if (!cls.isEntityEventsDisabled()) {
|
|
|
|
const eventPayload = {
|
|
|
|
entityName,
|
|
|
|
entity
|
|
|
|
};
|
2018-03-31 21:07:58 +08:00
|
|
|
|
2018-11-27 05:37:59 +08:00
|
|
|
if (isNewEntity && !entity.isDeleted) {
|
|
|
|
await eventService.emit(eventService.ENTITY_CREATED, eventPayload);
|
|
|
|
}
|
2018-11-27 05:22:16 +08:00
|
|
|
|
2018-11-27 05:37:59 +08: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-23 07:29:18 +08:00
|
|
|
}
|
2018-04-02 00:45:35 +08:00
|
|
|
});
|
2018-01-30 12:17:44 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:07:58 +08:00
|
|
|
module.exports = {
|
2018-08-10 02:08:00 +08:00
|
|
|
getEntityFromName,
|
2018-03-31 21:07:58 +08:00
|
|
|
getEntities,
|
|
|
|
getEntity,
|
|
|
|
getNote,
|
2018-04-01 11:08:22 +08:00
|
|
|
getBranch,
|
2018-04-01 23:05:09 +08:00
|
|
|
getImage,
|
2018-08-06 02:08:56 +08:00
|
|
|
getAttribute,
|
2018-05-22 12:22:43 +08:00
|
|
|
getOption,
|
2018-03-31 22:51:37 +08:00
|
|
|
updateEntity,
|
|
|
|
setEntityConstructor
|
2018-03-31 21:07:58 +08:00
|
|
|
};
|