Prisma 过滤

huangapple go评论61阅读模式
英文:

Prisma filtering

问题

我必须根据以下条件使用 Prisma 应用筛选器。
如果 ShowActiveOnly 为 true,则仅显示处于活动状态的项目。
如果 showActiveOnly 为 false,则显示所有项目。
以下是我尝试过的代码片段。

export const getAllProducts = async (req: Request, res: Response) => {
  try {
    const page: number = Number(req.query.page) - 1 || 0;
    const perPage: number = Number(req.query.perPage) || 10;
    const showActiveOnly: boolean =
      (req.query.showActiveOnly || "true") === "true";

    const products = await _db.serviceProduct.findMany({
      where: { IsActive: showActiveOnly },
      skip: page * 10,
      take: perPage,
    });
    res.json({ products, code: 200 }).status(200);
  } catch (error) {
    HandleError(error, res);
  }
};
英文:

I've to apply filter using prisma as per the below condition.
if ShowActiveOnly is true, then only show which are active
if showActiveOnly is false, then show everything.
Below is the snippet I've tried.

export const getAllProducts = async (req: Request, res: Response) => {
  try {
    const page: number = Number(req.query.page) - 1 || 0;
    const perPage: number = Number(req.query.perPage) || 10;
    const showActiveOnly: boolean =
      (req.query.showActiveOnly || "true") === "true";

    const products = await _db.serviceProduct.findMany({
      where: { IsActive: showActiveOnly },
      skip: page * 10,
      take: perPage,
    });
    res.json({ products, code: 200 }).status(200);
  } catch (error) {
    HandleError(error, res);
  }
};

答案1

得分: 0

export const getAllProducts = async (req: Request, res: Response) => {
  try {
    const page: number = Number(req.params.page) - 1 || 0;
    const perPage: number = Number(req.params.perPage) || 10;
    const showActiveOnly: boolean =
      (req.query.showActiveOnly || "true") === "true";

    const products = await _db.serviceProduct.findMany({
      where: showActiveOnly ? { IsActive: true } : undefined,
      skip: page * perPage,
      take: perPage,
    });
    res.json({ products, code: 200 }).status(200);
  } catch (error) {
    HandleError(error, res);
  }
};

我们可以这样做。在数据提取的where子句中应用了条件,如果showactiveonly为true,则只获取isActive为true的数据,否则where子句为undefined。

英文:
export const getAllProducts = async (req: Request, res: Response) => {
  try {
    const page: number = Number(req.params.page) - 1 || 0;
    const perPage: number = Number(req.params.perPage) || 10;
    const showActiveOnly: boolean =
      (req.query.showActiveOnly || "true") === "true";

    const products = await _db.serviceProduct.findMany({
      where: showActiveOnly ? { IsActive: true } : undefined,
      skip: page * perPage,
      take: perPage,
    });
    res.json({ products, code: 200 }).status(200);
  } catch (error) {
    HandleError(error, res);
  }
};

we can do in this way. I've applied a condition in where clause in data extraction, if showactiveonly is true then we're getting only isActive true else where clause is undefined.

huangapple
  • 本文由 发表于 2023年6月16日 15:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定