Prisma and Types: Confused about Naming Conventions

76 Views Asked by At

I have started my first Prisma-project using Next.js and Prisma with Postgres. I have server functions set up, that query the database using Prisma client for specific purposes. Now, for different roles, different pages, etc. I need to query different values.

I am wondering, if I am doing it "right", as I get more and more specific and non-reusable types and functions. I learned years ago that more specific and descriptive names are good as everyone knows what to expect. I myself have benefitted from that a lot in my developer experience. But it does not feel "right" in this case.

E.g. My function names went from "getProduct" to "getProductWithCategoryAndTagsBySlug" and are on their way to become "getEnabledProductWithCategoryAndTagsBySlug". The same with type definitions now being "CategoryWithSubcategoriesAndProducts".

Am I on the right path and just have to get used to it? Is there something I missed?

Exerpt from my prisma schema:

model Product {
  id                  String            @default(cuid()) @id
  sku                 String
  name                String
  description         String?
  priceB2B            Int
  priceB2C            Int
  slug                String            @unique
  category            Category          @relation(fields: [categoryId], references: [id])
  categoryId          String
  color               String?
  weightInKg          Int
  dimensions          String?
  enabled             Boolean           @default(true)
  volumeScaleGroup    VolumeScaleGroup  @relation(fields: [volumeScaleGroupId], references: [id])
  volumeScaleGroupId  String
  unit                String
  images              String[]
  tags                Tag[]
  orderItems          OrderItem[]
  listItems           ListItem[]
  createdAt           DateTime          @default(now())
  updatedAt           DateTime          @updatedAt
}

model Category {
  id                String  @default(cuid()) @id
  name              String
  image             String?
  slug              String      @unique
  description       String?
  products          Product[]
  parentCategory    Category?   @relation("CategorySubcategories", fields: [parentCategoryId], references: [id])
  parentCategoryId  String?
  subCategories     Category[]  @relation("CategorySubcategories")
}

My definitions file, that extends Prisma types:

export type ProductWithCategoriesAndTags = Prisma.ProductGetPayload<{
  include: {
    tags: true;
    category: {
      include: {
        parentCategory: true;
      };
    };
  };
}>;

export type CategoryWithSubcategoriesAndProducts = Prisma.CategoryGetPayload<{
  include: {
    products: true;
    subCategories: true;
  };
}>;

One of my functions:

export const getProductWithCategoriesAndTagsBySlug = async (
  slug: string
): Promise<ProductWithCategoriesAndTags> => {
  const product = await prisma.product.findUnique({
    where: {
      slug,
      enabled: true,
    },
    include: {
      tags: true,
      category: {
        include: {
          parentCategory: true,
        },
      },
    },
  });

  if (!product) {
    return notFound();
  }

  return product;
};

I have tried to find results and examples but I could not find any that could push me in the right direction.

0

There are 0 best solutions below