class Base { constructor() { } async save() { if (!this.isValid()) { return; } try { const [element, created] = await (await this.getModel()).findOrCreate({ where: await this.#createWhereClause() }); if (created) { logger.debug('created \'' + this.tableName + '\' entry: ' + JSON.stringify(element)); } if (element?.id === undefined) { return undefined; } this.#fillThis(element); } catch (error) { logger.error('error finding or creating \'' + this.tableName + '\' entry: \'' + JSON.stringify(this) + '\' > ' + error); } } async find() { if (!this.isValid()) { return; } try { const element = await (await this.getModel()).findOne({ where: await this.#createWhereClause() }); if (element?.id === undefined) { return undefined; } this.#fillThis(element); } catch (error) { logger.error('error finding \'' + this.tableName + '\' entry: \'' + JSON.stringify(this) + '\' > ' + error); } } async createTable() { return await createTable(); } async createTable() { const model = await this.getModel(); if (model === undefined) { return; } await model.sync({ alter: true }); } async getModel() { return undefined; } isValid() { return false; } isRelationTable() { return this.relationTable === true; } #fillThis(data) { if (data === undefined || data.dataValues === undefined || data.dataValues.id === undefined || data.dataValues.id < 0) { return; } for (const key in data.dataValues) { this[key] = data.dataValues[key]; } } async #createWhereClause(fields) { const model = await this.getModel(); if (model === undefined || model.tableAttributes === undefined) { return undefined; } const where = {}; for (const key in model.tableAttributes) { if (key === 'id' || key === 'createdAt' || key === 'updatedAt' || fields !== undefined && (key !== fields || !fields.includes(key))) { continue; } const value = this[key]; if (value === undefined) { continue; } if (this.isRelationTable()) { const id = value.id; if (id === undefined) { continue; } where[key] = id; } else { where[key] = value; } } return where; } } module.exports = Base;