question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change response field names

See original GitHub issue

Problem

I’m trying to rename some fields when using select.

Suggested solution

This is my current query:

const data = await prisma.data_riskscores.findMany({
  select: {
    id: true,
    sections: true,
    configuration_country: {
      select: {
        id: true,
        name: true
      }
    }
  }
})

Now I’d like to rename sections and configuration_country, because the data should not necessarily reflect the naming scheme of the database (IMO these are very different usecases):

const data = await prisma.data_riskscores.findMany({
  select: {
    id: true,
    results: sections,
    configuration_country: {
      as: country,
      select: {
        id: true,
        name: true
      }
    }
  }
})

Alternatives

Using @map resp. rename the relation field. It’s a reasonable alternative, but I think the schema is not the right place to make this change. IMO, the schema should (more or less) reflect the database. Whereas the select statement should allow to change the response data (because that might differ depending on the context of my request). For now, I’d probably go with a raw query instead.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:74
  • Comments:20

github_iconTop GitHub Comments

32reactions
devalnorcommented, Dec 22, 2020

Another alternative inspired from JS destructuring renaming

const data = await prisma.profile.findMany({
  select: {
    id: true,
    name: true,
    configuration_country: 'country' // work like an 'as'
 }
})

But it won’t resolve the typed approach of Prisma

22reactions
jbdemontecommented, Jan 11, 2022

totally agreed,

Too sad to have to map:

    const invoices = await this.prisma.invoice.findMany({
      where: { AND: where },
      include: { client: true, _count: { select: { notes: true } } },
      orderBy,
    });
    return invoices.map(({ _count, ...invoice }) => ({
      ...invoice,
      count: _count,
    }));

Instead of simply have:

    return this.prisma.invoice.findMany({
      where: { AND: where },
      include: { client: true, _count: { select: { notes: true }, as: 'count' } },
      orderBy,
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change a field name in JSON using Jackson
If you are using Jackson, then you can use the @JsonProperty annotation to customize the name of a given JSON property. Therefore, you...
Read more >
Change Field Name in JSON using Jackson - Java Guides
In this quick tutorial, I show you how to change the name of a field to map to another JSON property on serialization....
Read more >
Jackson - Change Name of Field | Baeldung
This quick tutorial illustrates how to change the name of a field to map to another JSON property on serialization.
Read more >
How can we change a field name in JSON using Jackson in ...
In the below example, we can change a field name in JSON using @JsonProperty annotation. Example. import java.io.IOException; import ...
Read more >
Change Field Names - Oracle Help Center
Change Field Names. Typing in new field names by hand: Rename the fields as you like, using only the characters listed below the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found