Florian
Florian
PPrisma
Created by Florian on 6/29/2024 in #help-and-questions
How do you organize your Prisma.validator schemas?
Right now I have them in lib/types.ts together with other types and interfaces. But I don't know if that's the right place.
3 replies
PPrisma
Created by Florian on 6/4/2024 in #help-and-questions
Type-safety for dates when returning DB entries as JSON
Because when I fetch a prisma object in a server component, I also get a Date back
11 replies
PPrisma
Created by Florian on 6/4/2024 in #help-and-questions
Type-safety for dates when returning DB entries as JSON
I think so. But I decided to create a wrapper around fetch that parses timestamp strings back to Date objects:
import ky from "ky";

const kyInstance = ky.create({
parseJson: (text) =>
JSON.parse(text, (key, value) => {
if (key.endsWith("At")) return new Date(value);
return value;
}),
});

export default kyInstance;
import ky from "ky";

const kyInstance = ky.create({
parseJson: (text) =>
JSON.parse(text, (key, value) => {
if (key.endsWith("At")) return new Date(value);
return value;
}),
});

export default kyInstance;
11 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
Thank you for the hint
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
A related question: Is it correct to create functions like getPostInclude around Prisma.validator? I do that because I need the authenticated user's ID in the query.
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
I'll try your suggestion and remove the separation between quotes and reposts. It will only get rid of one FK but maybe that's enough.
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
I'm not a database expert
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
It already feels impossible to work with in its current form
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
This will become even more complex if I ever need to add more FKs and query them. I was wondering if my whole DB structure is wrong.
18 replies
PPrisma
Created by Florian on 6/4/2024 in #help-and-questions
Type-safety for dates when returning DB entries as JSON
I don't want to litter my components with Jsonify<T>
11 replies
PPrisma
Created by Florian on 6/4/2024 in #help-and-questions
Type-safety for dates when returning DB entries as JSON
Right. I just want to know how this is commonly handled? The best approach seems to be to parse it back to Date when I retrieve the json response.
11 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
In my JS code, the additional distinction helps
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
Yes but this is almost what I have, isn't it? I have a repostOfId FK. I could get rid of the quoteOfPostId with your suggestions, but would that make the structure a lot easier?
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
This all works but the types are really messy
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
If the post is a repost of another post, the Post.tsx component detects this and decides which post to show:
const postToDisplay = post.repostOfPost ?? post;
const postToDisplay = post.repostOfPost ?? post;
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
Some feeds render parent posts if one exists:
{posts.map((post) => (
<Fragment key={post.id}>
{post.replyToPost && (
<Post post={post.replyToPost} showReplyIndicatorBottom />
)}
<Post post={post} showReplyIndicatorTop={!!post.replyToPost} />
<hr />
</Fragment>
))}
{posts.map((post) => (
<Fragment key={post.id}>
{post.replyToPost && (
<Post post={post.replyToPost} showReplyIndicatorBottom />
)}
<Post post={post} showReplyIndicatorTop={!!post.replyToPost} />
<hr />
</Fragment>
))}
18 replies
PPrisma
Created by Florian on 6/12/2024 in #help-and-questions
Tips on designing this recursive database model
function getPostInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
user: {
select: getUserProfileSelect(loggedInUserId),
},
reposts: {
where: {
userId: loggedInUserId,
},
select: {
userId: true,
},
},
likes: {
where: {
userId: loggedInUserId,
},
select: {
userId: true,
},
},
bookmarks: {
where: {
userId: loggedInUserId,
},
select: {
userId: true,
},
},
attachments: true,
_count: {
select: {
likes: true,
replies: true,
reposts: true,
views: true,
},
},
});
}

function getPostWithQuoteInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
...getPostInclude(loggedInUserId),
quoteOfPost: { include: getPostInclude(loggedInUserId) },
});
}

export function getPostWithRepostInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
...getPostWithQuoteInclude(loggedInUserId),
repostOfPost: { include: getPostWithQuoteInclude(loggedInUserId) },
});
}

export function getPostWithReplyToInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
...getPostWithRepostInclude(loggedInUserId),
replyToPost: { include: getPostWithRepostInclude(loggedInUserId) },
});
}
function getPostInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
user: {
select: getUserProfileSelect(loggedInUserId),
},
reposts: {
where: {
userId: loggedInUserId,
},
select: {
userId: true,
},
},
likes: {
where: {
userId: loggedInUserId,
},
select: {
userId: true,
},
},
bookmarks: {
where: {
userId: loggedInUserId,
},
select: {
userId: true,
},
},
attachments: true,
_count: {
select: {
likes: true,
replies: true,
reposts: true,
views: true,
},
},
});
}

function getPostWithQuoteInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
...getPostInclude(loggedInUserId),
quoteOfPost: { include: getPostInclude(loggedInUserId) },
});
}

export function getPostWithRepostInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
...getPostWithQuoteInclude(loggedInUserId),
repostOfPost: { include: getPostWithQuoteInclude(loggedInUserId) },
});
}

export function getPostWithReplyToInclude(loggedInUserId: string | undefined) {
return Prisma.validator<Prisma.PostInclude>()({
...getPostWithRepostInclude(loggedInUserId),
replyToPost: { include: getPostWithRepostInclude(loggedInUserId) },
});
}
18 replies
PPrisma
Created by Florian on 6/4/2024 in #help-and-questions
Type-safety for dates when returning DB entries as JSON
Thank you! I don't necessarily need the data, but I want a type that doesn't lie to me.
11 replies
PPrisma
Created by Florian on 5/24/2024 in #help-and-questions
No index found for fulltext search over relation (Planetscale)
Ok turns out I had to write the query like this
const results = await prismadb.favorite.findMany({
where: {
userId: user.id,
...(query && {
companion: {
OR: [
{
name: {
search: `${query}*`,
},
},
{
description: {
search: `${query}*`,
},
},
],
},
}),
},
[...]
});
const results = await prismadb.favorite.findMany({
where: {
userId: user.id,
...(query && {
companion: {
OR: [
{
name: {
search: `${query}*`,
},
},
{
description: {
search: `${query}*`,
},
},
],
},
}),
},
[...]
});
4 replies
PPrisma
Created by Florian on 5/11/2024 in #help-and-questions
Extra query into object (GetPayload)
thank you
5 replies