146 lines
3.4 KiB
JavaScript
146 lines
3.4 KiB
JavaScript
const { Sequelize } = require("sequelize");
|
|
const dbModel = require("./dbModel");
|
|
const logger = require("../logger");
|
|
|
|
class DatabaseCache {
|
|
async users (rows, table) {
|
|
// Add root user only where root user is activated
|
|
if (global.config.rootuser.activated) {
|
|
const isRootHasOnDB = rows.find(x => x.username === global.config.rootuser.login);
|
|
const rootLevel = 5; // TODO: replace it to AccessLevel.root in future
|
|
|
|
if (!isRootHasOnDB) {
|
|
await table.create({
|
|
username: global.config.rootuser.login,
|
|
accessLevel: rootLevel,
|
|
password: "",
|
|
});
|
|
return await this.users((
|
|
await table.findAll()
|
|
), table);
|
|
} else if (!rows.find(x => x.username === global.config.rootuser.login && x.accessLevel.code === rootLevel)) {
|
|
throw new Error(`Username ${
|
|
global.config.rootuser.login
|
|
} is busy`);
|
|
}
|
|
}
|
|
this.users.rows = rows;
|
|
this.users.byUsername = new Object();
|
|
this.users.byId = new Object();
|
|
|
|
for (let row of rows) {
|
|
this.users.byUsername[row.username] = row;
|
|
this.users.byId[row.id] = row;
|
|
}
|
|
|
|
// Add user info
|
|
this.users.add = async (row, isCreate = false) => {
|
|
if (isCreate)
|
|
await table.create(row);
|
|
|
|
let addedData = null;
|
|
if (row.username)
|
|
addedData = await table.findAll({
|
|
where: {
|
|
username: row.username
|
|
}
|
|
})[0];
|
|
else if (row.id)
|
|
addedData = await table.findAll({
|
|
where: {
|
|
id: row.id
|
|
}
|
|
})[0];
|
|
|
|
if (!addedData) throw new Error("Invalid row information");
|
|
this.users.rows.push(addedData);
|
|
// By keys
|
|
this.users.byUsername[addedData.username] = addedData;
|
|
this.users.byId[addedData.id] = addedData;
|
|
}
|
|
}
|
|
|
|
async userCharacters (rows, table) {
|
|
this.userCharacters.rows = rows;
|
|
|
|
// Add new character at cache
|
|
this.userCharacters.add = async (row, isCreate = false) => {
|
|
if (isCreate)
|
|
await table.create(row);
|
|
|
|
let addedData = null;
|
|
if (row.name)
|
|
addedData = await table.findAll({
|
|
where: {
|
|
name: row.name
|
|
}
|
|
})[0];
|
|
else if (row.id)
|
|
addedData = await table.findAll({
|
|
where: {
|
|
id: row.id
|
|
}
|
|
})[0];
|
|
|
|
if (!addedData) throw new Error("Invalid row information");
|
|
this.userCharacters.rows.push(addedData);
|
|
// By keys
|
|
}
|
|
}
|
|
|
|
async npcCharacters (rows, table) {
|
|
this.npcCharacters.rows = rows;
|
|
|
|
// Add new character at cache
|
|
this.npcCharacters.add = async (row, isCreate = false) => {
|
|
if (isCreate)
|
|
await table.create(row);
|
|
|
|
let addedData = null;
|
|
if (row.name)
|
|
addedData = await table.findAll({
|
|
where: {
|
|
name: row.name
|
|
}
|
|
})[0];
|
|
else if (row.id)
|
|
addedData = await table.findAll({
|
|
where: {
|
|
id: row.id
|
|
}
|
|
})[0];
|
|
|
|
if (!addedData) throw new Error("Invalid row information");
|
|
this.npcCharacters.rows.push(addedData);
|
|
// By keys
|
|
}
|
|
}
|
|
}
|
|
|
|
class Database {
|
|
constructor(sequlizeData) {
|
|
this.sequlize = new Sequelize(sequlizeData);
|
|
}
|
|
|
|
async init(fromHandler = false) {
|
|
global.server.toggleLock(true);
|
|
this.cache = new DatabaseCache();
|
|
await dbModel(this.sequlize, this);
|
|
global.server.toggleLock(false);
|
|
if (!fromHandler)
|
|
global.server.on("config.update", async ({
|
|
oldConfig, newConfig
|
|
}) => {
|
|
if (JSON.stringify(oldConfig.database) !== JSON.stringify(newConfig.database))
|
|
await this.init(true);
|
|
});
|
|
}
|
|
|
|
// Return ROW of 'users' table by username
|
|
async getUserRowByName (name) {
|
|
return this.cache.users.byUsername[name];
|
|
}
|
|
}
|
|
|
|
module.exports = Database;
|