@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.
69 lines • 2.11 kB
JavaScript
import { InternalServerError } from '../../../../error/index.js';
import { ORMType } from './types.js';
import { createMongooseAdapter, createSequelizeAdapter } from '../orm/index.js';
export * from './types.js';
const isMongooseConfig = (config) => {
return config.type === ORMType.Mongoose;
};
const isSequelizeConfig = (config) => {
return config.type === ORMType.Sequelize;
};
export class BaseRepository {
adapter;
ormType;
get model() {
return this.adapter.model;
}
constructor(config) {
this.ormType = config.type;
if (isMongooseConfig(config)) {
this.adapter = createMongooseAdapter(config);
}
else if (isSequelizeConfig(config)) {
this.adapter = createSequelizeAdapter(config);
}
else {
const exhaustive = config;
throw new 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 === ORMType.Mongoose;
}
isSequelize() {
return this.ormType === 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);
}
}
//# sourceMappingURL=abstract.js.map