ForcedToCode
ForcedToCode
Explore posts from servers
PPrisma
Created by ForcedToCode on 4/30/2024 in #help-and-questions
Sorting by distance
Hi, I am stuck with this, please help me out. I want to fetch data based on users location. I am using prisma. I have one prisma query where i have applied all sorts or filter and search, which would be a nightmare if i were to do it in raw query. but now that i want data by location i want to run this query and get data on the ordered by distance table. How should i use it with first query. because this query cannot be run with prisma query.

prisma.$queryRaw`SELECT id,
(6371 * acos(cos(radians(${lat}))
* cos(radians(lat))
* cos(radians(lng) - radians(${lng}))
+ sin(radians(${lng}))
* sin(radians(lat)))) AS distance
FROM venues
ORDER BY distance;`

prisma.$queryRaw`SELECT id,
(6371 * acos(cos(radians(${lat}))
* cos(radians(lat))
* cos(radians(lng) - radians(${lng}))
+ sin(radians(${lng}))
* sin(radians(lat)))) AS distance
FROM venues
ORDER BY distance;`
2 replies
PPrisma
Created by ForcedToCode on 4/23/2024 in #help-and-questions
Filtering time
I am using mysql db with prisma this are the fields in my schema
time_from DateTime @db.Time(0)
time_to DateTime @db.Time(0)
time_from DateTime @db.Time(0)
time_to DateTime @db.Time(0)
I want to filter using these feilds.
time_from: {
lte: dateFrom ? new Date(dateFrom) : undefined,
gte: dateTo ? new Date(dateTo) : undefined,
},
time_from: {
lte: dateFrom ? new Date(dateFrom) : undefined,
gte: dateTo ? new Date(dateTo) : undefined,
},
I know that i cannot filter time directly in primsa but i dont want to use raw query because i have lots of other optional filters in this query which gets complicated using raw query. When i fetch data from this table i get 1970-01-01T23:37:32.000Z for the time fields even though there is only time in the db. I already tried using time with this date 1970-01-01 but still i dont get any results. Is there any way to handle this? Thanks
1 replies
TTCTheo's Typesafe Cult
Created by ForcedToCode on 1/31/2024 in #questions
Code runner using AWS lambda function
Hi guys, i want to build code runner using AWS lambda function with custom runtime images for each language. I am don't exactly know how would i do it any info/resources/approach is much appreciated.
1 replies
TTCTheo's Typesafe Cult
Created by ForcedToCode on 1/20/2024 in #questions
Typescript ExpressJS middleware for jwt auth
hey guys! I am new to ts. I am trying to create a expressJS middleware for verifying jwt token. And after successful verification i want to attach the user object i get from payload which is typed string | jwt.JwtPayload in request object. but typescript is complaining. I dont want to just make it work i want to do it the correct way. help me out or Guide me to relavant resources.
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { env } from '../types/env';

export const verifyToken = (req: Request, res: Response, next: NextFunction) => {
try {
const token = req.header('authorization')?.replace('Bearer ', '');

if (!token) {
return res.status(401).json({ message: 'Missing access token' });
}

const user = jwt.verify(token, env.ACCESS_TOKEN_SECRET);

// req.user = user;
next();
} catch (error) {
console.log(error);
res.status(403).json({ message: 'Invalid access token' });
}
};
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { env } from '../types/env';

export const verifyToken = (req: Request, res: Response, next: NextFunction) => {
try {
const token = req.header('authorization')?.replace('Bearer ', '');

if (!token) {
return res.status(401).json({ message: 'Missing access token' });
}

const user = jwt.verify(token, env.ACCESS_TOKEN_SECRET);

// req.user = user;
next();
} catch (error) {
console.log(error);
res.status(403).json({ message: 'Invalid access token' });
}
};
3 replies
TTCTheo's Typesafe Cult
Created by ForcedToCode on 1/19/2024 in #questions
hey guys help me handle this prisma error in typescript
hey guys help me handle this prisma error in typescript
if (error instanceof Prisma.PrismaClientUnknownRequestError) {
if (error.code === 'P2002') { // 'error' is of type 'unknown'
res.status(400).json({ message: 'Username already exists' });
}
} else {
res.status(500).json({ message: 'Internal server error' });
}
if (error instanceof Prisma.PrismaClientUnknownRequestError) {
if (error.code === 'P2002') { // 'error' is of type 'unknown'
res.status(400).json({ message: 'Username already exists' });
}
} else {
res.status(500).json({ message: 'Internal server error' });
}
5 replies