Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a prisma relation to a response? #20

Open
simpel opened this issue May 23, 2021 · 3 comments
Open

How to add a prisma relation to a response? #20

simpel opened this issue May 23, 2021 · 3 comments

Comments

@simpel
Copy link

simpel commented May 23, 2021

Hi! How can I add a relation to a response? I have the following code but the users doesn't get returned:

import NextCrud, { PrismaAdapter } from "@premieroctet/next-crud";

const handler = NextCrud({
  resourceName: "bookmarks", // Same as your folder name
  adapter: new PrismaAdapter({
    modelName: "bookmark", // Prisma model name, must match the one generated in your prisma client
    options: {
      select: {
        users: true,
      },
    },
  }),
});
export default handler;
@foyarash
Copy link
Contributor

Hello

It seems that kind of option is not part of the PrismaClient possible options as described here

But for what you want to do, a simple solution would be to create your own adapter that extends the Prisma one, and override the parseQuery method so that you can add that option in the result of the base parseQuery method.

class MyPrismaAdapter extends PrismaAdapter {
  parseQuery(query) {
    const parsed = super.parseQuery(query)

    parsed.select = {
      ...parsed.select,
      users: true
    }
  }
}

Let me know if that fixes your issue

@darmane
Copy link

darmane commented May 31, 2022

Hi! How can I add a relation when creating a new record?

This is an example.

model Article {
id String @id @default(uuid())
title String
description String
content String
slug String @unique
image String?
publishedAt DateTime?
categoryId String
category Category @relation(fields: [categoryId], references: [id], onDelete: SetNull)
authorId String
author User @relation(fields: [authorId], references: [id], onDelete: SetNull)
}

When I create a new Article I want to add a relation with a Category. What do I have to send in the request body?

Thanks!

@foyarash
Copy link
Contributor

Hi! How can I add a relation when creating a new record?

This is an example.

model Article { id String @id @default(uuid()) title String description String content String slug String @unique image String? publishedAt DateTime? categoryId String category Category @relation(fields: [categoryId], references: [id], onDelete: SetNull) authorId String author User @relation(fields: [authorId], references: [id], onDelete: SetNull) }

When I create a new Article I want to add a relation with a Category. What do I have to send in the request body?

Thanks!

Hello,

The adapter accepts whatever data you are passing in the request body. So basically, you can send whatever prisma accepts in the data property to make your relation.

https://github.com/premieroctet/next-crud/blob/master/src/adapters/prisma/index.ts#L183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants