UNPKG

umbot

Version:

Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber

144 lines (143 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Model = void 0; const mmApp_1 = require("../../mmApp"); const QueryData_1 = require("./QueryData"); const DbController_1 = require("./DbController"); const ProxyUtils_1 = require("./ProxyUtils"); class Model { dbController; queryData; startIndex = 0; state = {}; constructor() { if (mmApp_1.mmApp.userDbController) { this.dbController = mmApp_1.mmApp.userDbController; } else { this.dbController = new DbController_1.DbController(); } this.dbController.tableName = this.tableName(); this.dbController.setRules(this.rules()); this.dbController.primaryKeyName = this.getId(); this.queryData = new QueryData_1.QueryData(); Object.keys(this.attributeLabels()).forEach((key) => { this.state[key] = undefined; }); return (0, ProxyUtils_1.ProxyUtils)(this); } async isConnected() { return await this.dbController.isConnected(); } escapeString(text) { return this.dbController.escapeString(text); } validate() { } getId() { const labels = this.attributeLabels(); for (const index in labels) { if (labels[index] === 'id' || labels[index] === 'ID') { return index; } } return null; } init(data) { if (data === null) { return; } let i = this.startIndex; const labels = this.attributeLabels(); for (const index in labels) { if (data) { if (typeof data[index] !== 'undefined') { this.state[index] = data[index]; } else if (typeof data[i] !== 'undefined') { this.state[index] = data[i]; } else { this.state[index] = ''; } } else { this.state[index] = ''; } i++; } } async selectOne() { const idName = this.dbController.primaryKeyName; if (idName) { this.queryData.setQuery({ [idName]: this.state[idName], }); } this.queryData.setData(null); return await this.dbController.select(this.queryData.getQuery(), true); } _initData() { this.validate(); const idName = this.dbController.primaryKeyName; if (idName) { this.queryData.setQuery({ [idName]: this.state[idName], }); } const data = {}; for (const index in this.attributeLabels()) { if (index !== idName) { data[index] = this.state[index]; } } this.queryData.setData(data); } async save(isNew = false) { this._initData(); return await this.dbController.save(this.queryData, isNew); } async update() { this._initData(); return await this.dbController.update(this.queryData); } async add() { this.validate(); this.queryData.setQuery(null); const data = {}; for (const index in this.attributeLabels()) { data[index] = this.state[index]; } this.queryData.setData(data); return await this.dbController.insert(this.queryData); } async remove() { this.validate(); const idName = this.dbController.primaryKeyName; if (idName) { this.queryData.setQuery({ [idName]: this.state[idName], }); } this.queryData.setData(null); return await this.dbController.remove(this.queryData); } async where(where = '1', isOne = false) { const select = typeof where === 'string' ? QueryData_1.QueryData.getQueryData(where) : where; return await this.dbController.select(select, isOne); } async whereOne(where = '1') { const res = await this.where(where, true); if (res && res.status) { this.init(this.dbController.getValue(res)); return true; } return false; } query(callback) { return this.dbController.query(callback); } destroy() { this.dbController.destroy(); } } exports.Model = Model;