require('dotenv/config'); const seq = require('../../sequelize/models/index'); export class SequelizeBaseService { model: any; sequelize: any; constructor(modelName: string) { this.sequelize = seq; this.model = this.sequelize.models[modelName]; } async list(options?: any): Promise { return this.model.findAll(options); } async findOne(options?: any): Promise { return this.model.findOne(options); } async find(id: number): Promise { return this.model.findByPk(id); } async upsert(object: TParam): Promise { let where = { }; // eslint-disable-next-line @typescript-eslint/dot-notation const idAux = object['id']; if (idAux) { where = { id: idAux }; } else { where = { ...object }; } const row = await this.model.findCreateFind({ where, defaults: object, }); await this.model.update(object, { where: { id: row[0].id }, }); return { ...object, id: row[0].id, }; } async destroy(options: any): Promise { return (await this.model.destroy(options)) > 0; } }