Skip to content

Commit

Permalink
Merge pull request #53 from RoutinelyOrganization/develop
Browse files Browse the repository at this point in the history
Sync branch
  • Loading branch information
viniciuscosmome authored Nov 9, 2023
2 parents 6d9175d + efd790f commit ef7736b
Showing 1 changed file with 2 additions and 48 deletions.
50 changes: 2 additions & 48 deletions src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
import {
Injectable,
INestApplication,
OnModuleInit,
InternalServerErrorException,
} from '@nestjs/common';
import { Injectable, INestApplication } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
private async insertsNewFunctionToDeleteExpiredSessions() {
try {
await this.$queryRaw`
CREATE OR REPLACE FUNCTION delete_expired_sessions() RETURNS TRIGGER AS
$del_exp_ses$
BEGIN
DELETE FROM sessions
WHERE refresh_expires_in < now();
RETURN NULL;
END;
$del_exp_ses$ LANGUAGE plpgsql;
`;
} catch (error) {
throw new InternalServerErrorException(`
Error when trying to create function to delete expired sessions
${error}
`);
}
}

private async activateTheTriggerAfterUpdatingAnySessionData() {
try {
await this.$queryRaw`
CREATE OR REPLACE TRIGGER del_exp_ses
AFTER UPDATE ON sessions
FOR EACH STATEMENT
EXECUTE PROCEDURE delete_expired_sessions();
`;
} catch (error) {
throw new InternalServerErrorException(`
Error when trying create trigger to delete expired sessions
${error}
`);
}
}

export class PrismaService extends PrismaClient {
async enableShutdownHooks(app: INestApplication) {
this.$on('beforeExit', async () => {
await app.close();
});
}

async onModuleInit() {
await this.insertsNewFunctionToDeleteExpiredSessions();
await this.activateTheTriggerAfterUpdatingAnySessionData();
}
}

0 comments on commit ef7736b

Please sign in to comment.