tududi/backend/shared/database/BaseRepository.js
Chris 542be2c1e9
Fix bug 366 (#764)
* Optimize DB

* Clean up names

* fixup! Clean up names

* fixup! fixup! Clean up names
2026-01-07 18:18:07 +02:00

46 lines
1.1 KiB
JavaScript

'use strict';
/**
* Base repository class providing common data access methods.
* Extend this class to create entity-specific repositories.
*/
class BaseRepository {
constructor(model) {
this.model = model;
}
async findById(id, options = {}) {
return this.model.findByPk(id, options);
}
async findOne(where, options = {}) {
return this.model.findOne({ where, ...options });
}
async findAll(where = {}, options = {}) {
return this.model.findAll({ where, ...options });
}
async create(data, options = {}) {
return this.model.create(data, options);
}
async update(instance, data, options = {}) {
return instance.update(data, options);
}
async destroy(instance, options = {}) {
return instance.destroy(options);
}
async count(where = {}, options = {}) {
return this.model.count({ where, ...options });
}
async exists(where) {
const count = await this.count(where);
return count > 0;
}
}
module.exports = BaseRepository;