-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from RoutinelyOrganization/develop
Sync branch
- Loading branch information
Showing
1 changed file
with
2 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |