More elegant `select` API
See original GitHub issueProblem
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:
- Created 3 years ago
- Reactions:63
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:We’ll also document this pattern soon.
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:
Similar to
AND
,OR
, etc in thewhere
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 namedfields
.