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.

More elegant `select` API

See original GitHub issue

Problem

Currently in Blitz apps I’m manually selecting all the fields I need to prevent unwanted fields from being exposed to the API.

The problem is that this is super cumbersome to write as an object with : true for every field.

  const order = await prisma.order.findOne({
    where,
    select: {
      id: true,
      createdAt: true,
      updatedAt: true,
      chargeStatus: true,
      amount: true,
      customer: {
        select: {
          id: true,
          firstName: true,
          lastName: true,
          email: true,
          phone: true,
        },
      },
      orderGroup: {
        select: {
          name: true,
        },
      },
    },
  })

Suggested solution

Something easier, perhaps accepting an array of strings.

  const order = await prisma.order.findOne({
    where,
    select: {
      fields: ['id', 'createdAt', 'updatedAt', 'chargeStatus', 'amount'],
      customer: {
        select: {
          fields: ['id', 'firstName', 'lastName', 'email', 'phone']
        }
      },
      orderGroup: {
        select: {
          fields: ['name']
        }
      },
    },
  })

And it would be sweet to have a shortcut without fields:

  const order = await prisma.order.findOne({
    where,
    select: {
      fields: ['id', 'createdAt', 'updatedAt', 'chargeStatus', 'amount'],
      customer: {
        select: ['id', 'firstName', 'lastName', 'email', 'phone']
      },
      orderGroup: {
        select: ['name']
      },
    },
  })

Alternatives

I haven’t thought of anything else.

Additional context

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:63
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
nikolasburkcommented, Feb 9, 2021

For anyone finding this issue, I just quickly want to drop in and mention that a user on Slack today posted a nice workaround pattern for an exclude API:

image

We’ll also document this pattern soon.

3reactions
Syttencommented, Sep 30, 2020

My take on it is that is a vastly different syntax and it will cause beginners to be even more lost when they start (plus it confuses the autocomplete). If this is considered seriously, I would do the following:

const order = await prisma.order.findOne({
    where,
    select: {
      FIELDS: ['id', 'createdAt', 'updatedAt', 'chargeStatus', 'amount'],
      customer: {
        select: {
          FIELDS: ['id', 'firstName', 'lastName', 'email', 'phone']
        }
      },
      orderGroup: {
        select: {
          FIELDS: ['name']
        }
      },
    },
  })

Similar to AND, OR, etc in the where clause. It makes it clear that this is a Prisma “advanced” thing and it makes sure we don’t have a naming conflict on models that might have a column named fields.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Golden Rules for Great Web API Design - Toptal
Web APIs that are cleanly-designed, well-documented, and easy-to-use are rare. Here's how to design a great web API that is much more likely...
Read more >
Using Network API's In Monarch - Elegant Themes
In the sidebar on the left, select Credentials. Click the blue Create credentials button and select API key. For the key type, select...
Read more >
Elegant way to select nested objects with the associated key ...
Finally the actual object is selected through getpath . Is there a more elegant, or at the least shorter way to express this?...
Read more >
Login - Elegant CMS
Most fields are self-explanatory. Step 3: Decide on the title. The title selection decides which field to present when looking up the content....
Read more >
Elegant CMS | Heroku Dev Center
All content is available according to the JSON API standard. ... Then, select Elegant CMS from the Add-ons menu. You can also access...
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