bb-inspired
Version:
Core library for BB-inspired NestJS backend
108 lines • 5.32 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 DatabaseService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseService = void 0;
const common_1 = require("@nestjs/common");
const prisma_service_1 = require("./prisma.service");
const mongodb_service_1 = require("./mongodb.service");
const logger_1 = require("../../utils/logger");
let DatabaseService = DatabaseService_1 = class DatabaseService {
constructor(options, prismaService, mongodbService) {
this.options = options;
this.prismaService = prismaService;
this.mongodbService = mongodbService;
this.logger = new logger_1.AppLogger(DatabaseService_1.name);
if (!this.prismaService && !this.mongodbService) {
this.logger.warn('Neither Prisma nor MongoDB service is available. Database operations will fail.');
}
}
getDatabaseServices() {
const isPrismaAvailable = !!this.prismaService;
const isMongoAvailable = !!this.mongodbService;
return {
primary: isPrismaAvailable ? 'prisma' : 'mongodb',
secondary: isPrismaAvailable && isMongoAvailable ? 'mongodb' : null,
prisma: this.prismaService,
mongodb: this.mongodbService,
};
}
async executeQuery(model, operation, args = {}, options = {}) {
const { primary, secondary, prisma, mongodb } = this.getDatabaseServices();
const source = options.source || 'primary';
try {
if ((source === 'primary' || source === 'auto') && primary === 'prisma' && prisma) {
return await prisma[model][operation](args);
}
if ((source === 'primary' && primary === 'mongodb') ||
(source === 'secondary' && secondary === 'mongodb') ||
(source === 'auto' && !prisma)) {
const collection = mongodb.getCollection(model);
switch (operation) {
case 'findMany':
return collection.find(args.where || {}).toArray();
case 'findUnique':
case 'findFirst':
return collection.findOne(args.where || {});
case 'create':
return collection.insertOne(args.data).then(result => ({
...args.data,
id: result.insertedId
}));
case 'update':
return collection.updateOne(args.where || {}, { $set: args.data }).then(() => args.data);
case 'delete':
return collection.deleteOne(args.where || {})
.then(result => !!result.deletedCount);
default:
throw new Error(`Unsupported MongoDB operation: ${operation}`);
}
}
throw new Error(`No available database service for ${source}`);
}
catch (error) {
this.logger.error(`Database query failed: ${model}.${operation}`, undefined, { metadata: { model, operation, args, error: error.message } });
throw error;
}
}
async transaction(operations) {
const { primary, prisma, mongodb } = this.getDatabaseServices();
if (primary === 'prisma' && prisma) {
return prisma.executeInTransaction(operations);
}
else if (primary === 'mongodb' && mongodb) {
return mongodb.executeInTransaction(operations);
}
throw new Error('No database service available for transactions');
}
getRawClient(type) {
if (type === 'prisma' && this.prismaService) {
return this.prismaService;
}
else if (type === 'mongodb' && this.mongodbService) {
return this.mongodbService;
}
throw new Error(`Database client ${type} is not available`);
}
};
exports.DatabaseService = DatabaseService;
exports.DatabaseService = DatabaseService = DatabaseService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)('DATABASE_OPTIONS')),
__param(1, (0, common_1.Optional)()),
__param(2, (0, common_1.Optional)()),
__metadata("design:paramtypes", [Object, prisma_service_1.PrismaService,
mongodb_service_1.MongodbService])
], DatabaseService);
//# sourceMappingURL=database.service.js.map