bockster6669
bockster6669
Explore posts from servers
PPrisma
Created by bockster6669 on 9/11/2024 in #help-and-questions
Search text with Prisma
Hello, guys! I am trying to create a search functionality with prisma and want to ask which is the better way: making it with the search prop or making it with contains prop. Now I am using search and it looks like this
function formatSearchQuery(query: string | null): string | undefined {
if (!query) return undefined;
return query.split(' ').join(' &');
}

export async function GET(req: NextRequest) {
const searchParams = req.nextUrl.searchParams;
let searchInput: string | null = searchParams.get('search');
const formattedQuery = formatSearchQuery(searchInput);

try {
const posts = await PostRepo.findMany({
where: {
title: {
search: formattedQuery,
},
},
include: {
author: true,
tags: true,
comments: {
include: {
author: true,
},
},
},
});
function formatSearchQuery(query: string | null): string | undefined {
if (!query) return undefined;
return query.split(' ').join(' &');
}

export async function GET(req: NextRequest) {
const searchParams = req.nextUrl.searchParams;
let searchInput: string | null = searchParams.get('search');
const formattedQuery = formatSearchQuery(searchInput);

try {
const posts = await PostRepo.findMany({
where: {
title: {
search: formattedQuery,
},
},
include: {
author: true,
tags: true,
comments: {
include: {
author: true,
},
},
},
});
4 replies
PPrisma
Created by bockster6669 on 9/4/2024 in #help-and-questions
Repo pattern
Hello, guys! I want toa ask about repo pattern with prisma. What to do if I have select: author, comments, tags, replys and etc into a single query. This will mean that my function name should be something like this getPostWithAuthorCommentsTagsReplys is there some best practice when dealing with such a case
2 replies
PPrisma
Created by bockster6669 on 9/3/2024 in #help-and-questions
Prisma Types
Pls somebody help with these types
type PostFindUniqueResult = Prisma.Result<
typeof db.post,
{
where: {
id: string;
};
include: {
author: true;
tags: true;
comments: {
include: {
author: true;
};
};
};
},
'findUnique'
>;

let post: PostFindUniqueResult;

post = await PostModel.findUnique({
where: {
id: params.id,
},
include: {
author: true,
tags: true,
comments: {
include: {
author: true,
},
},
},
});
type PostFindUniqueResult = Prisma.Result<
typeof db.post,
{
where: {
id: string;
};
include: {
author: true;
tags: true;
comments: {
include: {
author: true;
};
};
};
},
'findUnique'
>;

let post: PostFindUniqueResult;

post = await PostModel.findUnique({
where: {
id: params.id,
},
include: {
author: true,
tags: true,
comments: {
include: {
author: true,
},
},
},
});
I am getting error that the type PostFindUniqueResult is not the same as the return type from PostModel.findUnique
6 replies
PPrisma
Created by bockster6669 on 8/31/2024 in #help-and-questions
is there a `substring` method in prisma?
is there a substring method in prisma?
7 replies
PPrisma
Created by bockster6669 on 8/11/2024 in #help-and-questions
Get part of a text from DB with Prisma
Is there a way to get the first 50 symbols from a String column content instead getting the all content of 300 symbols for example. I know there are
SELECT LEFT(content, 50) AS preview
FROM your_table;

SELECT SUBSTRING(content FROM 1 FOR 50) AS preview
FROM your_table;
SELECT LEFT(content, 50) AS preview
FROM your_table;

SELECT SUBSTRING(content FROM 1 FOR 50) AS preview
FROM your_table;
What is the way in prisma?
3 replies