Should I use "use server" directive for database queries

"use server"

import { db } from "~/server/db";

export async function getArticle(id: string) {
const article = await db.article.findUnique({
where: { id, published: true },
select: {
title: true,
description: true,
content: true,
tag: true,
createdAt: true,
author: {}
},
});
}
"use server"

import { db } from "~/server/db";

export async function getArticle(id: string) {
const article = await db.article.findUnique({
where: { id, published: true },
select: {
title: true,
description: true,
content: true,
tag: true,
createdAt: true,
author: {}
},
});
}
If I remove the directive, it would still be running on server and everything is safe, right?
1 Reply
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
yes, you should ever use "use server" only if you need to call that function (or functions from a module) from the client side directly, as "use server" means you're exposing all of this to the world as a public API to the whole world you don't add "use server" if you're calling those function from server components, since all of that remains in you server by design
Want results from more Discord servers?
Add your server