UNPKG

@nestdevx/tenant

Version:

Tenant module for multi-tenant NestJS applications.

98 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Repository = void 0; const common_1 = require("@nestjs/common"); const response_paginated_dto_1 = require("../dtos/response-paginated.dto"); const request_context_1 = require("../hooks/request-context"); class Repository { constructor(model, logger) { this.model = model; this.model = model; this.logger = logger || new common_1.Logger(Repository.name); } async findOne(filter) { const tenantId = (0, request_context_1.currentTenantId)(); this.logger.debug(`Finding one document with filter: ${JSON.stringify(filter)}`); const found = await this.model.findOne({ ...filter, tenantId }).exec(); return found ? found : null; } async create(data) { this.logger.debug(`Creating document...`); const tenantId = (0, request_context_1.currentTenantId)(); if (tenantId) { this.logger.debug(`Setting tenantId: ${tenantId} for the new document`); data.tenantId = tenantId; } const createdDocument = new this.model(data); const created = await createdDocument.save(); this.logger.debug(`Document created with ID: ${created._id}`); return { id: created._id.toString() }; } async findById(id) { const tenantId = (0, request_context_1.currentTenantId)(); this.logger.debug(`Finding document with ID: ${id}`); const found = await this.model.findById(id).where({ tenantId }).exec(); if (!found) { this.logger.error(`Document with ID ${id} not found`); throw new common_1.NotFoundException(`Document with ID ${id} not found`); } this.logger.debug(`Document found for the id: ${id}`); return found; } async count(id) { const tenantId = (0, request_context_1.currentTenantId)(); if (id) { this.logger.debug(`Counting documents with ID: ${id}`); const count = await this.model.countDocuments({ _id: id, tenantId }).exec(); this.logger.debug(`Total documents with ID ${id}: ${count}`); return count; } this.logger.debug(`Counting all documents`); const count = await this.model.countDocuments({ tenantId }).exec(); this.logger.debug(`Total documents in collection: ${count}`); return count; } async findAll() { this.logger.debug(`Finding all documents`); const tenantId = (0, request_context_1.currentTenantId)(); return this.model.find({ tenantId }).exec(); } async update(id, data) { this.logger.debug(`Updating document with ID: ${id}`); const tenantId = (0, request_context_1.currentTenantId)(); const updatedDocument = await this.model.findByIdAndUpdate(id, data, { new: true }).where({ tenantId }).exec(); if (!updatedDocument) { this.logger.error(`Document with ID ${id} not found`); throw new common_1.NotFoundException(`Document with ID ${id} not found`); } this.logger.debug(`Document updated with ID: ${id}`); return updatedDocument; } async delete(id) { const tenantId = (0, request_context_1.currentTenantId)(); this.logger.debug(`Deleting document with ID: ${id}`); const deletedDocument = await this.model.findByIdAndDelete(id).where({ tenantId }).exec(); if (!deletedDocument) { this.logger.error(`Document with ID ${id} not found`); throw new common_1.NotFoundException(`Document with ID ${id} not found`); } this.logger.debug(`Document deleted with ID: ${id}`); } async query(filter) { const tenantId = (0, request_context_1.currentTenantId)(); this.logger.debug(`Querying documents with filter: ${JSON.stringify(filter)}`); return this.model.find().where({ ...filter, tenantId }); } async getPaginatedResult(page = 1, limit = 10, filter) { const tenantId = (0, request_context_1.currentTenantId)(); this.logger.debug(`Getting paginated result for page: ${page}, limit: ${limit}, filter: ${JSON.stringify(filter)}`); const [data, total] = await Promise.all([ this.model.find().where({ ...filter, tenantId }).skip((page - 1) * limit).limit(limit).exec(), this.model.countDocuments().where({ ...filter, tenantId }).exec() ]); const totalPages = Math.ceil(total / limit); return new response_paginated_dto_1.ResponsePaginatedDto(data, total, Number(page), totalPages); } } exports.Repository = Repository; //# sourceMappingURL=repository.js.map