UNPKG

@jsgurucompany/jsg-nestjs-common

Version:

Initial README.md

52 lines 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseRepository = void 0; class BaseRepository { constructor(model) { this.model = model; this.maxLimit = 100; this.defaultLimit = 10; this.defaultOffset = 0; } resolveLimit(limit = this.defaultLimit) { return limit > this.maxLimit ? this.maxLimit : limit; } async findAll(options) { return this.model.findAll(options); } async findAndCountAll(options) { return this.model.findAndCountAll(options); } async findPaginated(paginationParams, options) { var _a; return this.findAndCountAll(Object.assign(Object.assign({}, options), { offset: (_a = paginationParams.offset) !== null && _a !== void 0 ? _a : this.defaultOffset, limit: this.resolveLimit(paginationParams.limit) })); } async findOneById(id) { return this.model.findByPk(id); } async findOne(options) { return this.model.findOne(options); } async create(payload) { return this.model.create(payload); } async createMany(payload) { return this.model.bulkCreate(payload); } async update(id, payload) { await this.model.update(payload, { where: { id }, }); return this.findOneById(id); } async updateMany(options, payload) { await this.model.update(payload, options); return; } async remove(id) { await this.model.destroy({ where: { id } }); return; } } exports.BaseRepository = BaseRepository; //# sourceMappingURL=base.repository.js.map