fix: adjusts on prisma connections

This commit is contained in:
Davidson Gomes 2024-10-03 17:59:40 -03:00
parent 14d10c00ec
commit d1bc0e6150
4 changed files with 9 additions and 29 deletions

View File

@ -19,6 +19,7 @@
* Update in Baileys version that fixes timeout when updating profile picture * Update in Baileys version that fixes timeout when updating profile picture
* Adjusts for fix timeout error on send status message * Adjusts for fix timeout error on send status message
* Chatwoot verbose logs * Chatwoot verbose logs
* Adjusts on prisma connections
# 2.1.1 (2024-09-22 10:31) # 2.1.1 (2024-09-22 10:31)

View File

@ -1,8 +1,7 @@
import { InstanceDto } from '@api/dto/instance.dto'; import { InstanceDto } from '@api/dto/instance.dto';
import { cache, waMonitor } from '@api/server.module'; import { cache, prismaRepository, waMonitor } from '@api/server.module';
import { CacheConf, configService } from '@config/env.config'; import { CacheConf, configService } from '@config/env.config';
import { BadRequestException, ForbiddenException, InternalServerErrorException, NotFoundException } from '@exceptions'; import { BadRequestException, ForbiddenException, InternalServerErrorException, NotFoundException } from '@exceptions';
import { prismaServer } from '@libs/prisma.connect';
import { NextFunction, Request, Response } from 'express'; import { NextFunction, Request, Response } from 'express';
async function getInstance(instanceName: string) { async function getInstance(instanceName: string) {
@ -17,9 +16,7 @@ async function getInstance(instanceName: string) {
return exists || keyExists; return exists || keyExists;
} }
const prisma = prismaServer; return exists || (await prismaRepository.instance.findMany({ where: { name: instanceName } })).length > 0;
return exists || (await prisma.instance.findMany({ where: { name: instanceName } })).length > 0;
} catch (error) { } catch (error) {
throw new InternalServerErrorException(error?.toString()); throw new InternalServerErrorException(error?.toString());
} }

View File

@ -1,16 +0,0 @@
import { Logger } from '@config/logger.config';
import { PrismaClient } from '@prisma/client';
const logger = new Logger('Prisma');
export const prismaServer = (() => {
logger.verbose('connecting');
const db = new PrismaClient();
process.on('beforeExit', () => {
logger.verbose('instance destroyed');
db.$disconnect();
});
return db;
})();

View File

@ -1,12 +1,10 @@
import { prismaRepository } from '@api/server.module';
import { CacheService } from '@api/services/cache.service'; import { CacheService } from '@api/services/cache.service';
import { INSTANCE_DIR } from '@config/path.config'; import { INSTANCE_DIR } from '@config/path.config';
import { prismaServer } from '@libs/prisma.connect';
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys'; import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
const prisma = prismaServer;
// const fixFileName = (file: string): string | undefined => { // const fixFileName = (file: string): string | undefined => {
// if (!file) { // if (!file) {
// return undefined; // return undefined;
@ -18,7 +16,7 @@ const prisma = prismaServer;
export async function keyExists(sessionId: string): Promise<any> { export async function keyExists(sessionId: string): Promise<any> {
try { try {
const key = await prisma.session.findUnique({ where: { sessionId: sessionId } }); const key = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
return !!key; return !!key;
} catch (error) { } catch (error) {
return false; return false;
@ -29,13 +27,13 @@ export async function saveKey(sessionId: string, keyJson: any): Promise<any> {
const exists = await keyExists(sessionId); const exists = await keyExists(sessionId);
try { try {
if (!exists) if (!exists)
return await prisma.session.create({ return await prismaRepository.session.create({
data: { data: {
sessionId: sessionId, sessionId: sessionId,
creds: JSON.stringify(keyJson), creds: JSON.stringify(keyJson),
}, },
}); });
await prisma.session.update({ await prismaRepository.session.update({
where: { sessionId: sessionId }, where: { sessionId: sessionId },
data: { creds: JSON.stringify(keyJson) }, data: { creds: JSON.stringify(keyJson) },
}); });
@ -48,7 +46,7 @@ export async function getAuthKey(sessionId: string): Promise<any> {
try { try {
const register = await keyExists(sessionId); const register = await keyExists(sessionId);
if (!register) return null; if (!register) return null;
const auth = await prisma.session.findUnique({ where: { sessionId: sessionId } }); const auth = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
return JSON.parse(auth?.creds); return JSON.parse(auth?.creds);
} catch (error) { } catch (error) {
return null; return null;
@ -59,7 +57,7 @@ async function deleteAuthKey(sessionId: string): Promise<any> {
try { try {
const register = await keyExists(sessionId); const register = await keyExists(sessionId);
if (!register) return; if (!register) return;
await prisma.session.delete({ where: { sessionId: sessionId } }); await prismaRepository.session.delete({ where: { sessionId: sessionId } });
} catch (error) { } catch (error) {
return; return;
} }