payload icon indicating copy to clipboard operation
payload copied to clipboard

feat: select fields

Open r1tsuu opened this issue 1 year ago • 2 comments

  • [x] generate types for the select property
  • [x] infer the return type by select with 2 modes - include (field: true) and exclude (field: false)
  • [ ] lots of integration tests, including deep fields / localization etc
  • [ ] implement the property in db adapters
  • [ ] docs

r1tsuu avatar Oct 04 '24 06:10 r1tsuu

Do you want to fix the failing build before or after we review this?

DanRibbens avatar Oct 04 '24 16:10 DanRibbens

I have some issues when types aren't generated (when the code inside of packages/, not test/), trying to resolve it and then the build should be fixed

r1tsuu avatar Oct 04 '24 18:10 r1tsuu

Does this work on fields from related documents (relationship fields). For example, usually I only want a few fields from an image relationship.

revnelson avatar Oct 29 '24 21:10 revnelson

We will implement soon exactly what you described in a separate PR. This is just the basement

r1tsuu avatar Oct 29 '24 21:10 r1tsuu

🚀 This is included in version v3.0.0-beta.121

github-actions[bot] avatar Oct 30 '24 18:10 github-actions[bot]

How can we target a field within an array? For example, if we have:

{ name: 'items', type: 'array', fields: [ { name: 'title', type: 'text' }, { name: 'text', type: 'text' } ] }

How can we exclude the text field from each item in the array?

revnelson avatar Nov 01 '24 21:11 revnelson

How can we target a field within an array? For example, if we have:

{ name: 'items', type: 'array', fields: [ { name: 'title', type: 'text' }, { name: 'text', type: 'text' } ] }

How can we exclude the text field from each item in the array?

This will work, I'd recommend you to generate types as it provides autocompletion for that if you haven't already

select: {
   items:{ 
     title: false
}
}

But it doesn't affect the return type for typescript if the field is nested.

r1tsuu avatar Nov 01 '24 22:11 r1tsuu

Ahh, I used an array thinking it was a simple example. That does indeed work, but I have a blocks field and want to select out some fields in a blockOptions group that all blocks will have. Would it be possible to have the defaultPopulate property on groups, arrays, and blocks?

revnelson avatar Nov 01 '24 22:11 revnelson

Also, as an aside, I'm running beta.123 with generated types and am not getting any autocompletion. Screenshot 2024-11-01 at 3 23 56 PM Screenshot 2024-11-01 at 3 24 29 PM

revnelson avatar Nov 01 '24 22:11 revnelson

Possible

select: {
  blocks: {
    cta: { // blockType here
      text: false // exclude text from blocks within the "cta" blockType
           }
     }
}

r1tsuu avatar Nov 01 '24 22:11 r1tsuu

Also, as an aside, I'm running beta.123 with generated types and am not getting any autocompletion.

This is interesting because it works in our tests. I'll look into this

r1tsuu avatar Nov 01 '24 22:11 r1tsuu

Okay, I think I understand the problem

Can you try to temporary disable strict: true or strictNullChecks: true in your tsconfig? It should work then

Then, after 124 beta you can re-enable it again.

Fixed here https://github.com/payloadcms/payload/pull/8991 thanks for the report I didn't notice this

r1tsuu avatar Nov 01 '24 22:11 r1tsuu

Also, I forgot to mention that you can just remove ? from this (and globalsSelect if you wish) image in your payload-types.ts, so it'll be required

r1tsuu avatar Nov 01 '24 22:11 r1tsuu