bb-inspired
Version:
Core library for BB-inspired NestJS backend
78 lines • 3.43 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var PrismaService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrismaService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
const logger_1 = require("../../utils/logger");
let PrismaService = PrismaService_1 = class PrismaService extends client_1.PrismaClient {
constructor(options) {
var _a, _b;
super({
datasources: {
db: {
url: (_a = options.prisma) === null || _a === void 0 ? void 0 : _a.url,
},
},
log: ((_b = options.prisma) === null || _b === void 0 ? void 0 : _b.logQueries) ? ['query', 'error', 'warn'] : ['error', 'warn'],
});
this.options = options;
this.logger = new logger_1.AppLogger(PrismaService_1.name);
}
async onModuleInit() {
try {
await this.$connect();
this.logger.log('Prisma connected to PostgreSQL database');
this.applyMiddleware();
}
catch (error) {
this.logger.error(`Failed to connect to PostgreSQL: ${error.message}`, error.stack);
throw error;
}
}
async onModuleDestroy() {
try {
await this.$disconnect();
this.logger.log('Prisma disconnected from PostgreSQL database');
}
catch (error) {
this.logger.error(`Error disconnecting from PostgreSQL: ${error.message}`, error.stack);
}
}
applyMiddleware() {
this.$use(async (params, next) => {
var _a;
const startTime = Date.now();
const result = await next(params);
const duration = Date.now() - startTime;
if ((_a = this.options.prisma) === null || _a === void 0 ? void 0 : _a.logQueries) {
this.logger.verbose(`[${params.model}.${params.action}] took ${duration}ms`, { metadata: { model: params.model, action: params.action, duration } });
}
return result;
});
}
async executeInTransaction(operations) {
return this.$transaction(async (tx) => {
return operations(tx);
});
}
};
exports.PrismaService = PrismaService;
exports.PrismaService = PrismaService = PrismaService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)('DATABASE_OPTIONS')),
__metadata("design:paramtypes", [Object])
], PrismaService);
//# sourceMappingURL=prisma.service.js.map