hyperzone
hyperzone
Explore posts from servers
TTCTheo's Typesafe Cult
Created by hyperzone on 6/16/2023 in #questions
Caching ORM response in Next app dir
I've asked this question on the Next.js discord but wasn't able to get a solid answer, hoping someone here knows What is the recommended way to cache something like a prisma response? I have a page.tsx and in it I defined a revalidate variable and I have a getFirstPost function that fetches the first post:
export const revalidate = 3600; // 1 hour

const getFirstPost = async () => {
const latestPost = await prisma.post.findFirst({
orderBy: { createdAt: "desc" },
});

return latestPost;
};
export const revalidate = 3600; // 1 hour

const getFirstPost = async () => {
const latestPost = await prisma.post.findFirst({
orderBy: { createdAt: "desc" },
});

return latestPost;
};
in the page function I go
const latestPost = await getFirstPost();
const latestPost = await getFirstPost();
and then render the data. Even tho I defined the revalidate variable every time I refresh the page (on dev and on prod) I see prisma making a new query in console:
prisma:query SELECT `t3approuter`.`Post`.`id`, `t3approuter`.`Post`.`text`, `t3approuter`.`Post`.`createdAt`, `t3approuter`.`Post`.`updatedAt`, `t3approuter`.`Post`.`createdById` FROM `t3approuter`.`Post` WHERE 1=1 ORDER BY `t3approuter`.`Post`.`createdAt` DESC LIMIT ? OFFSET ?
prisma:query SELECT `t3approuter`.`Post`.`id`, `t3approuter`.`Post`.`text`, `t3approuter`.`Post`.`createdAt`, `t3approuter`.`Post`.`updatedAt`, `t3approuter`.`Post`.`createdById` FROM `t3approuter`.`Post` WHERE 1=1 ORDER BY `t3approuter`.`Post`.`createdAt` DESC LIMIT ? OFFSET ?
Does the cache get wiped on refresh or am I doing something wrong here?
4 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 2/12/2023 in #questions
Prisma middleware delete
I have 3 models - Category, Product and Color. Whenever a category gets deleted, its products also get deleted(Cascade) and each product's colors get deleted. Each color has an image saved in a bucket that I want to delete whenever a Color gets deleted, how can I write an event or middleware that get triggered whenever a Color gets deleted whenever a Category or Product get deleted? I tried doing $use but when deleting a Product I don't get params related to the colors
11 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 2/7/2023 in #questions
Parent model in child model in Prisma?
Sorry for the confusing title, I'm looking to do something like this:
model Parent {
id String @id @default(cuid())
name String

children Child[]
}

model Child {
id String @id @default(cuid())
name String

parent Parent? @relation(fields: [parentId], references: [id])
parentId String?

subChild Parent // is something like this possible? <<<<
}
model Parent {
id String @id @default(cuid())
name String

children Child[]
}

model Child {
id String @id @default(cuid())
name String

parent Parent? @relation(fields: [parentId], references: [id])
parentId String?

subChild Parent // is something like this possible? <<<<
}
5 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 12/28/2022 in #questions
Best way to implement a cart?
I've implemented a cart using jotai's atomWithStorage to save the items in lcoalStorage, but I was wondering if I should store the cart in the server instead? In this specific app only logged in users can access the store. Are there other better ways of implementing a cart?
44 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 12/23/2022 in #questions
A very basic thing that can not be done right using NextAuth?
I have an app that uses DB sessions. I've got an /admin route that I want to protect using a Next.js middleware by checking the user's role, but I can't do this because I don't have access to 'user' obj. How can I do this right without having to copy-paste a block of getServerSideProps?
56 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 12/17/2022 in #questions
Signin in turborepo t3
I'm running a t3 turborepo that has 2 apps, 1 admin (CMS) and the client next app. The login is done using credentials, how can I do so that when you login in the admin app you get redirected to localhost:3001/ (admin), and when you login in the client app you get redirected to localhost:3000/? Currently when I login in admin I get redirected to the client /
12 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 12/15/2022 in #questions
Next.js middleware with NextAuth server session?
What is the best way to write a Next.js middleware (middleware.ts) that redirects to /login if the user is not logged in? I'm using the t3 stack with db sessions
3 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 12/10/2022 in #questions
Custom admin CMS
Hey, say I have an app and I need a custom CMS I write to control its content, should I create a protected route in that app that leads to the CMS or should I go ct3app-turborepo and have 1 Next app and 1 Next admin app?
3 replies
TTCTheo's Typesafe Cult
Created by hyperzone on 12/3/2022 in #questions
Using a CMS with the t3 stack?
Say I'm building an e-commerce site and I need to give the client the ability to add/delete/edit products, should I use a CMS or just write this on my own? I looked into Sanity, but it looks like everything defined there is fetched via a REST api so I basically lose all the meaning of an end-to-end typesafety and type completions
16 replies