UNPKG

@sentzunhat/zacatl

Version:

A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.

87 lines 2.95 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseRepository = void 0; const error_1 = require("../../../../error/index.js"); const types_1 = require("./types"); const orm_1 = require("../orm"); __exportStar(require("./types"), exports); const isMongooseConfig = (config) => { return config.type === types_1.ORMType.Mongoose; }; const isSequelizeConfig = (config) => { return config.type === types_1.ORMType.Sequelize; }; class BaseRepository { adapter; ormType; get model() { return this.adapter.model; } constructor(config) { this.ormType = config.type; if (isMongooseConfig(config)) { this.adapter = (0, orm_1.createMongooseAdapter)(config); } else if (isSequelizeConfig(config)) { this.adapter = (0, orm_1.createSequelizeAdapter)(config); } else { const exhaustive = config; throw new error_1.InternalServerError({ message: `Invalid repository config: ${JSON.stringify(exhaustive)}`, reason: 'Config is neither Mongoose nor Sequelize format', component: 'BaseRepository', operation: 'constructor', metadata: { config: exhaustive }, }); } } isMongoose() { return this.ormType === types_1.ORMType.Mongoose; } isSequelize() { return this.ormType === types_1.ORMType.Sequelize; } async initializeModel() { if ('initialize' in this.adapter && typeof this.adapter.initialize === 'function') { await this.adapter.initialize(); } } toLean(input) { return this.adapter.toLean(input); } async findById(id) { return this.adapter.findById(id); } async findMany(filter) { return this.adapter.findMany(filter); } async create(entity) { return this.adapter.create(entity); } async update(id, update) { return this.adapter.update(id, update); } async delete(id) { return this.adapter.delete(id); } async exists(id) { return this.adapter.exists(id); } } exports.BaseRepository = BaseRepository; //# sourceMappingURL=abstract.js.map