UNPKG

@russ-b/nestjs-common-tools

Version:
64 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseEntityService = void 0; const common_1 = require("@nestjs/common"); const util_1 = require("../common/util"); class BaseEntityService { constructor(repository) { this.repository = repository; this.logger = new common_1.Logger(this.constructor.name); } create(entity) { return this.repository.create(entity); } async save(entityOrEntities, options) { if (Array.isArray(entityOrEntities)) { return await this.repository.save(entityOrEntities, options); } return await this.repository.save(entityOrEntities, options); } async update(id, entity, options) { if (!options || !options.where) { const where = typeof id === 'object' && !Array.isArray(id) ? id : { id }; options = { ...options, ...{ where } }; } await this.repository.update(id, entity); const data = await this.findOne(options); if (data === null) { throw new Error(`Unable to find ${id}`); } return data; } async findOne(options) { return this.repository.findOne(options); } async findOneBy(where) { return this.repository.findOneBy(where); } async find(options) { return this.repository.find(options); } async findBy(where) { return this.repository.findBy(where); } async findAll() { return this.find(); } async findAndCount(options) { return this.repository.findAndCount(options); } async delete(id) { await this.repository.delete(id); } async softDelete(id) { await this.repository.softDelete(id); } toKeyValue(data, key, value) { return (0, util_1.collectionToMap)(data, key, value); } async upsert(entityOrEntities, conflictPathsOrOptions) { return await this.repository.upsert(entityOrEntities, conflictPathsOrOptions); } } exports.BaseEntityService = BaseEntityService; //# sourceMappingURL=base-entity.service.js.map