Update passport/prisma libs

This commit is contained in:
jeffvli
2022-10-24 21:28:22 -07:00
parent c09e9b6583
commit db8a7d6a63
2 changed files with 46 additions and 12 deletions
+22 -9
View File
@@ -9,16 +9,30 @@ import {
import { Strategy as LocalStrategy } from 'passport-local';
import { prisma } from './prisma';
export const generateToken = (userId: string) => {
return jwt.sign({ id: userId }, String(process.env.TOKEN_SECRET), {
expiresIn: String(process.env.TOKEN_EXPIRATION || '15m'),
});
export const generateToken = (
id: string,
otherProperties?: { [key: string]: any }
) => {
return jwt.sign(
{ id, ...otherProperties },
String(process.env.TOKEN_SECRET),
{
expiresIn: String(process.env.TOKEN_EXPIRATION || '15m'),
}
);
};
export const generateRefreshToken = (userId: string) => {
return jwt.sign({ id: userId }, String(process.env.TOKEN_SECRET), {
expiresIn: String(process.env.TOKEN_REFRESH_EXPIRATION || '90d'),
});
export const generateRefreshToken = (
id: string,
otherProperties?: { [key: string]: any }
) => {
return jwt.sign(
{ id, ...otherProperties },
String(process.env.TOKEN_SECRET),
{
expiresIn: String(process.env.TOKEN_REFRESH_EXPIRATION || '90d'),
}
);
};
const authenticateUser = async (
@@ -55,7 +69,6 @@ passport.use(
await prisma.user
.findUnique({
include: {
serverCredentials: true,
serverFolderPermissions: true,
serverPermissions: true,
},
+24 -3
View File
@@ -1,4 +1,4 @@
import { PrismaClient } from '@prisma/client';
import { Prisma, PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient({ errorFormat: 'minimal' });
export const exclude = <T, Key extends keyof T>(
@@ -17,7 +17,7 @@ function sleep(ms: number) {
}
prisma.$use(async (params, next) => {
const maxRetries = 5;
const maxRetries = 3;
let retries = 0;
do {
@@ -25,8 +25,29 @@ prisma.$use(async (params, next) => {
const result = await next(params);
return result;
} catch (err) {
console.log('err', err);
if (err instanceof Prisma.PrismaClientKnownRequestError) {
if (err.code === 'P2002') {
retries = 3; // Don't retry on unique constraint violation
return null;
}
}
retries += 1;
return sleep(500);
return sleep(100);
}
} while (retries < maxRetries);
});
// prisma.$use(async (params, next) => {
// const before = Date.now();
// const result = await next(params);
// const after = Date.now();
// console.log(
// `Query ${params.model}.${params.action} took ${after - before}ms`
// );
// return result;
// });