mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-18 11:22:21 -06:00
feat: prisma
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { opendirSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { Auth, ConfigService } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { AUTH_DIR } from '../../config/path.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { AuthRaw, IAuthModel, IntegrationModel } from '../models';
|
||||
import { Auth, ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { AUTH_DIR } from '../../../config/path.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { AuthRaw, IAuthModel, IntegrationModel } from '../../models';
|
||||
|
||||
export class AuthRepository extends Repository {
|
||||
constructor(
|
||||
@@ -35,11 +35,11 @@ export class AuthRepository extends Repository {
|
||||
this.logger.verbose('saving auth to store');
|
||||
|
||||
this.writeStore<AuthRaw>({
|
||||
path: join(AUTH_DIR, this.auth.TYPE),
|
||||
path: join(AUTH_DIR, 'apikey'),
|
||||
fileName: instance,
|
||||
data,
|
||||
});
|
||||
this.logger.verbose('auth saved to store in path: ' + join(AUTH_DIR, this.auth.TYPE) + '/' + instance);
|
||||
this.logger.verbose('auth saved to store in path: ' + join(AUTH_DIR, 'apikey') + '/' + instance);
|
||||
|
||||
this.logger.verbose('auth created');
|
||||
return { insertCount: 1 };
|
||||
@@ -59,7 +59,7 @@ export class AuthRepository extends Repository {
|
||||
this.logger.verbose('finding auth in store');
|
||||
|
||||
return JSON.parse(
|
||||
readFileSync(join(AUTH_DIR, this.auth.TYPE, instance + '.json'), {
|
||||
readFileSync(join(AUTH_DIR, 'apikey', instance + '.json'), {
|
||||
encoding: 'utf-8',
|
||||
}),
|
||||
) as AuthRaw;
|
||||
@@ -92,14 +92,14 @@ export class AuthRepository extends Repository {
|
||||
this.logger.verbose('listing auth in store');
|
||||
|
||||
const auths: AuthRaw[] = [];
|
||||
const openDir = opendirSync(join(AUTH_DIR, this.auth.TYPE), {
|
||||
const openDir = opendirSync(join(AUTH_DIR, 'apikey'), {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
for await (const dirent of openDir) {
|
||||
if (dirent.isFile()) {
|
||||
auths.push(
|
||||
JSON.parse(
|
||||
readFileSync(join(AUTH_DIR, this.auth.TYPE, dirent.name), {
|
||||
readFileSync(join(AUTH_DIR, 'apikey', dirent.name), {
|
||||
encoding: 'utf-8',
|
||||
}),
|
||||
),
|
||||
@@ -1,10 +1,10 @@
|
||||
import { opendirSync, readFileSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { ChatRaw, ChatRawSelect, IChatModel } from '../models';
|
||||
import { ConfigService, StoreConf } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { ChatRaw, ChatRawSelect, IChatModel } from '../../models';
|
||||
|
||||
export class ChatQuery {
|
||||
select?: ChatRawSelect;
|
||||
@@ -1,10 +1,10 @@
|
||||
import { opendirSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { ContactRaw, ContactRawSelect, IContactModel } from '../models';
|
||||
import { ConfigService, StoreConf } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { ContactRaw, ContactRawSelect, IContactModel } from '../../models';
|
||||
|
||||
export class ContactQuery {
|
||||
select?: ContactRawSelect;
|
||||
149
src/api/repository/mongodb/instance.repository.ts
Normal file
149
src/api/repository/mongodb/instance.repository.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import { opendirSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { Auth, ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { AUTH_DIR } from '../../../config/path.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { AuthRaw, IAuthModel, IntegrationModel } from '../../models';
|
||||
|
||||
export class AuthRepository extends Repository {
|
||||
constructor(
|
||||
private readonly authModel: IAuthModel,
|
||||
private readonly integrationModel: IntegrationModel,
|
||||
readonly configService: ConfigService,
|
||||
) {
|
||||
super(configService);
|
||||
this.auth = configService.get<Auth>('AUTHENTICATION');
|
||||
}
|
||||
|
||||
private readonly auth: Auth;
|
||||
private readonly logger = new Logger('AuthRepository');
|
||||
|
||||
public async create(data: AuthRaw, instance: string): Promise<IInsert> {
|
||||
try {
|
||||
this.logger.verbose('creating auth');
|
||||
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('saving auth to db');
|
||||
const insert = await this.authModel.replaceOne({ _id: instance }, { ...data }, { upsert: true });
|
||||
|
||||
this.logger.verbose('auth saved to db: ' + insert.modifiedCount + ' auth');
|
||||
return { insertCount: insert.modifiedCount };
|
||||
}
|
||||
|
||||
this.logger.verbose('saving auth to store');
|
||||
|
||||
this.writeStore<AuthRaw>({
|
||||
path: join(AUTH_DIR, 'apikey'),
|
||||
fileName: instance,
|
||||
data,
|
||||
});
|
||||
this.logger.verbose('auth saved to store in path: ' + join(AUTH_DIR, 'apikey') + '/' + instance);
|
||||
|
||||
this.logger.verbose('auth created');
|
||||
return { insertCount: 1 };
|
||||
} catch (error) {
|
||||
return { error } as any;
|
||||
}
|
||||
}
|
||||
|
||||
public async find(instance: string): Promise<AuthRaw> {
|
||||
try {
|
||||
this.logger.verbose('finding auth');
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('finding auth in db');
|
||||
return await this.authModel.findOne({ _id: instance });
|
||||
}
|
||||
|
||||
this.logger.verbose('finding auth in store');
|
||||
|
||||
return JSON.parse(
|
||||
readFileSync(join(AUTH_DIR, 'apikey', instance + '.json'), {
|
||||
encoding: 'utf-8',
|
||||
}),
|
||||
) as AuthRaw;
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
public async findByKey(key: string): Promise<AuthRaw> {
|
||||
try {
|
||||
this.logger.verbose('finding auth');
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('finding auth in db');
|
||||
return await this.authModel.findOne({ apikey: key });
|
||||
}
|
||||
|
||||
return {};
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
public async list(): Promise<AuthRaw[]> {
|
||||
try {
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('listing auth in db');
|
||||
return await this.authModel.find();
|
||||
}
|
||||
|
||||
this.logger.verbose('listing auth in store');
|
||||
|
||||
const auths: AuthRaw[] = [];
|
||||
const openDir = opendirSync(join(AUTH_DIR, 'apikey'), {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
for await (const dirent of openDir) {
|
||||
if (dirent.isFile()) {
|
||||
auths.push(
|
||||
JSON.parse(
|
||||
readFileSync(join(AUTH_DIR, 'apikey', dirent.name), {
|
||||
encoding: 'utf-8',
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return auths;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public async findInstanceNameById(instanceId: string): Promise<string | null> {
|
||||
try {
|
||||
this.logger.verbose('finding auth by instanceId');
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('finding auth in db');
|
||||
const response = await this.authModel.findOne({ instanceId });
|
||||
|
||||
return response._id;
|
||||
}
|
||||
|
||||
this.logger.verbose('finding auth in store is not supported');
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async findInstanceNameByNumber(number: string): Promise<string | null> {
|
||||
try {
|
||||
this.logger.verbose('finding auth by number');
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('finding auth in db');
|
||||
const instance = await this.integrationModel.findOne({ number });
|
||||
|
||||
const response = await this.authModel.findOne({ _id: instance._id });
|
||||
|
||||
return response._id;
|
||||
}
|
||||
|
||||
this.logger.verbose('finding auth in store is not supported');
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { IntegrationModel, IntegrationRaw } from '../models';
|
||||
import { ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { IntegrationModel, IntegrationRaw } from '../../models';
|
||||
|
||||
export class IntegrationRepository extends Repository {
|
||||
constructor(private readonly integrationModel: IntegrationModel, private readonly configService: ConfigService) {
|
||||
@@ -1,10 +1,10 @@
|
||||
import { opendirSync, readFileSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { ILabelModel, LabelRaw, LabelRawSelect } from '../models';
|
||||
import { ConfigService, StoreConf } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { ILabelModel, LabelRaw, LabelRawSelect } from '../../models';
|
||||
|
||||
export class LabelQuery {
|
||||
select?: LabelRawSelect;
|
||||
@@ -1,10 +1,10 @@
|
||||
import { opendirSync, readFileSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { IMessageModel, MessageRaw, MessageRawSelect } from '../models';
|
||||
import { ConfigService, StoreConf } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { IMessageModel, MessageRaw, MessageRawSelect } from '../../models';
|
||||
|
||||
export class MessageQuery {
|
||||
select?: MessageRawSelect;
|
||||
@@ -1,10 +1,10 @@
|
||||
import { opendirSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { IMessageUpModel, MessageUpdateRaw } from '../models';
|
||||
import { ConfigService, StoreConf } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { IMessageUpModel, MessageUpdateRaw } from '../../models';
|
||||
|
||||
export class MessageUpQuery {
|
||||
where: MessageUpdateRaw;
|
||||
@@ -1,10 +1,10 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { IProxyModel, ProxyRaw } from '../models';
|
||||
import { ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { IProxyModel, ProxyRaw } from '../../models';
|
||||
|
||||
export class ProxyRepository extends Repository {
|
||||
constructor(private readonly proxyModel: IProxyModel, private readonly configService: ConfigService) {
|
||||
@@ -1,15 +1,15 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import fs from 'fs';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { join } from 'path';
|
||||
|
||||
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { ChamaaiRepository } from '../integrations/chamaai/repository/chamaai.repository';
|
||||
import { ChatwootRepository } from '../integrations/chatwoot/repository/chatwoot.repository';
|
||||
import { RabbitmqRepository } from '../integrations/rabbitmq/repository/rabbitmq.repository';
|
||||
import { SqsRepository } from '../integrations/sqs/repository/sqs.repository';
|
||||
import { TypebotRepository } from '../integrations/typebot/repository/typebot.repository';
|
||||
import { WebsocketRepository } from '../integrations/websocket/repository/websocket.repository';
|
||||
import { ConfigService, Database } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { ChatwootRepository } from '../../integrations/chatwoot/repository/chatwoot.repository';
|
||||
import { RabbitmqRepository } from '../../integrations/rabbitmq/repository/rabbitmq.repository';
|
||||
import { SqsRepository } from '../../integrations/sqs/repository/sqs.repository';
|
||||
import { TypebotRepository } from '../../integrations/typebot/repository/typebot.repository';
|
||||
import { WebsocketRepository } from '../../integrations/websocket/repository/websocket.repository';
|
||||
import { AuthRepository } from './auth.repository';
|
||||
import { ChatRepository } from './chat.repository';
|
||||
import { ContactRepository } from './contact.repository';
|
||||
@@ -20,7 +20,7 @@ import { MessageUpRepository } from './messageUp.repository';
|
||||
import { ProxyRepository } from './proxy.repository';
|
||||
import { SettingsRepository } from './settings.repository';
|
||||
import { WebhookRepository } from './webhook.repository';
|
||||
export class RepositoryBroker {
|
||||
export class MongodbRepository {
|
||||
constructor(
|
||||
public readonly message: MessageRepository,
|
||||
public readonly chat: ChatRepository,
|
||||
@@ -34,22 +34,28 @@ export class RepositoryBroker {
|
||||
public readonly sqs: SqsRepository,
|
||||
public readonly typebot: TypebotRepository,
|
||||
public readonly proxy: ProxyRepository,
|
||||
public readonly chamaai: ChamaaiRepository,
|
||||
public readonly integration: IntegrationRepository,
|
||||
public readonly auth: AuthRepository,
|
||||
public readonly labels: LabelRepository,
|
||||
private configService: ConfigService,
|
||||
dbServer?: MongoClient,
|
||||
mongodbServer?: MongoClient,
|
||||
prismaServer?: PrismaClient,
|
||||
) {
|
||||
this.dbClient = dbServer;
|
||||
this.mongodbClient = mongodbServer;
|
||||
this.prismaClient = prismaServer;
|
||||
this.__init_repo_without_db__();
|
||||
}
|
||||
|
||||
private dbClient?: MongoClient;
|
||||
private mongodbClient?: MongoClient;
|
||||
private prismaClient?: PrismaClient;
|
||||
private readonly logger = new Logger('RepositoryBroker');
|
||||
|
||||
public get dbServer() {
|
||||
return this.dbClient;
|
||||
public get mongodbServer() {
|
||||
return this.mongodbClient;
|
||||
}
|
||||
|
||||
public get prismaServer() {
|
||||
return this.prismaClient;
|
||||
}
|
||||
|
||||
private __init_repo_without_db__() {
|
||||
@@ -59,7 +65,7 @@ export class RepositoryBroker {
|
||||
|
||||
this.logger.verbose('creating store path: ' + storePath);
|
||||
try {
|
||||
const authDir = join(storePath, 'auth', this.configService.get<Auth>('AUTHENTICATION').TYPE);
|
||||
const authDir = join(storePath, 'auth', 'apikey');
|
||||
const chatsDir = join(storePath, 'chats');
|
||||
const contactsDir = join(storePath, 'contacts');
|
||||
const messagesDir = join(storePath, 'messages');
|
||||
@@ -72,7 +78,6 @@ export class RepositoryBroker {
|
||||
const sqsDir = join(storePath, 'sqs');
|
||||
const typebotDir = join(storePath, 'typebot');
|
||||
const proxyDir = join(storePath, 'proxy');
|
||||
const chamaaiDir = join(storePath, 'chamaai');
|
||||
const integrationDir = join(storePath, 'integration');
|
||||
const tempDir = join(storePath, 'temp');
|
||||
|
||||
@@ -128,10 +133,6 @@ export class RepositoryBroker {
|
||||
this.logger.verbose('creating proxy dir: ' + proxyDir);
|
||||
fs.mkdirSync(proxyDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(chamaaiDir)) {
|
||||
this.logger.verbose('creating chamaai dir: ' + chamaaiDir);
|
||||
fs.mkdirSync(chamaaiDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(integrationDir)) {
|
||||
this.logger.verbose('creating integration dir: ' + integrationDir);
|
||||
fs.mkdirSync(integrationDir, { recursive: true });
|
||||
@@ -1,10 +1,10 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { ISettingsModel, SettingsRaw } from '../models';
|
||||
import { ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { ISettingsModel, SettingsRaw } from '../../models';
|
||||
|
||||
export class SettingsRepository extends Repository {
|
||||
constructor(private readonly settingsModel: ISettingsModel, private readonly configService: ConfigService) {
|
||||
@@ -1,10 +1,10 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { IWebhookModel, WebhookRaw } from '../models';
|
||||
import { ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
import { IInsert, Repository } from '../../abstract/abstract.repository';
|
||||
import { IWebhookModel, WebhookRaw } from '../../models';
|
||||
|
||||
export class WebhookRepository extends Repository {
|
||||
constructor(private readonly webhookModel: IWebhookModel, private readonly configService: ConfigService) {
|
||||
29
src/api/repository/prisma/repository.service.ts
Normal file
29
src/api/repository/prisma/repository.service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
import { ConfigService } from '../../../config/env.config';
|
||||
import { Logger } from '../../../config/logger.config';
|
||||
|
||||
export class Query<T> {
|
||||
where?: T;
|
||||
sort?: 'asc' | 'desc';
|
||||
page?: number;
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
export class PrismaRepository extends PrismaClient {
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
super();
|
||||
}
|
||||
|
||||
private readonly logger = new Logger(PrismaRepository.name);
|
||||
|
||||
public async onModuleInit() {
|
||||
await this.$connect();
|
||||
this.logger.info('Repository:Prisma - ON');
|
||||
}
|
||||
|
||||
public async onModuleDestroy() {
|
||||
await this.$disconnect();
|
||||
this.logger.warn('Repository:Prisma - OFF');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user