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-01-29 23:17:44 -05:00
|
|
|
|
2018-03-31 10:51:37 -04:00
|
|
|
let entityConstructor;
|
|
|
|
|
|
|
|
async function setEntityConstructor(constructor) {
|
|
|
|
entityConstructor = constructor;
|
|
|
|
}
|
|
|
|
|
2018-08-09 20:08:00 +02:00
|
|
|
async function getEntityFromName(entityName, entityId) {
|
|
|
|
const entityConstructor = entityConstructor.getEntityFromTableName(entityName);
|
|
|
|
|
|
|
|
return await getEntity(`SELECT * FROM ${entityConstructor.tableName()} WHERE ${entityConstructor.primaryKeyName()} = ?`, [entityId]);
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
async function getNote(noteId) {
|
2018-03-31 10:51:37 -04:00
|
|
|
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
|
|
|
|
2018-03-31 23:08:22 -04:00
|
|
|
async function getBranch(branchId) {
|
|
|
|
return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);
|
|
|
|
}
|
|
|
|
|
2018-04-01 11:05:09 -04:00
|
|
|
async function getImage(imageId) {
|
|
|
|
return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
|
|
|
|
}
|
|
|
|
|
2018-08-05 20:08:56 +02:00
|
|
|
async function getAttribute(attributeId) {
|
|
|
|
return await getEntity("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]);
|
|
|
|
}
|
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
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) {
|
|
|
|
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
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
delete clone.jsonContent;
|
2018-08-07 13:33:10 +02:00
|
|
|
delete clone.isOwned;
|
2018-01-30 20:12:19 -05:00
|
|
|
|
2018-08-06 08:59:26 +02:00
|
|
|
for (const key in clone) {
|
|
|
|
if (clone[key] !== null && typeof clone[key] === 'object') {
|
|
|
|
clone[key] = JSON.stringify(clone[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:03:16 -04:00
|
|
|
await sql.transactional(async () => {
|
2018-04-01 12:45:35 -04:00
|
|
|
await sql.replace(entity.constructor.tableName, clone);
|
2018-01-30 20:12:19 -05:00
|
|
|
|
2018-04-01 12:45:35 -04:00
|
|
|
const primaryKey = entity[entity.constructor.primaryKeyName];
|
2018-03-31 09:07:58 -04:00
|
|
|
|
2018-05-22 19:29:18 -04:00
|
|
|
if (entity.constructor.tableName !== 'options' || entity.isSynced) {
|
|
|
|
await syncTableService.addEntitySync(entity.constructor.tableName, primaryKey);
|
|
|
|
}
|
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,
|
2018-03-31 23:08:22 -04:00
|
|
|
getBranch,
|
2018-04-01 11:05:09 -04:00
|
|
|
getImage,
|
2018-08-05 20:08:56 +02:00
|
|
|
getAttribute,
|
2018-05-22 00:22:43 -04:00
|
|
|
getOption,
|
2018-03-31 10:51:37 -04:00
|
|
|
updateEntity,
|
|
|
|
setEntityConstructor
|
2018-03-31 09:07:58 -04:00
|
|
|
};
|