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

fix!: improve collection / global slugs type-safety in various places #8311

Open
wants to merge 8 commits into
base: beta
Choose a base branch
from

Conversation

r1tsuu
Copy link
Member

@r1tsuu r1tsuu commented Sep 19, 2024

BREAKING:
Improves type-safety of collection / global slugs by using CollectionSlug and GlobalSlug types instead of string in these places:

  • Sanitized collection / global config, keys of payload.collections
  • Direct Payload DB operations through payload.db
  • collections argument of plugins: storage-adapters, nested-docs, cloud
  • collectionSlugsproperty ofListDrawerProps`
  • collectionSlugs and selectCollectionSlugs arguments of useListDrawer
  • Permissions of AuthResult

This also changes how we suggest to add a upload collection to cloud-storage adapter:

Before:

azureStorage({
  collections: {
    [Media.slug]: true,
  },
}) 

After:

azureStorage({
  collections: {
    media: true,
  },
}) 

It's not needed anymore, as keys of the collection argument are type-safe now and Media.slug from this code actually has string type so this will lead to a TS error.

export const Media: CollectionConfig = {
  slug: 'media',
  fields: [],
  upload: true
}

If you still want to use Media.slug, you can use this notation:

export const Media = {
  slug: 'media' as const,
  fields: [],
  upload: true
} satisfies CollectionConfig

This way, Media.slug is typed as 'media' instead of string and can be used as CollectionSlug.

@github-actions github-actions bot added the v3 label Sep 19, 2024
@DanRibbens
Copy link
Contributor

I'd like @denolfe to review this also before we merge it in and we need to have a BREAKING CHANGES section written in the commit for release notes.

Copy link
Member

@denolfe denolfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good change that will help DX. Left a few questions.

@@ -79,7 +80,7 @@ export interface CollectionOptions {
}

export interface PluginOptions {
collections: Record<string, CollectionOptions>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning for this to be a partial?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if it's not partial, you have to define all the collections there.
image

@@ -26,7 +26,7 @@ export default buildConfig({
plugins: [
vercelBlobStorage({
collections: {
[Media.slug]: true,
media: true,
Copy link
Member

@denolfe denolfe Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does TS still allow using Media.slug without error? Having people change all their slugs to as const seems less than ideal.

Copy link
Member Author

@r1tsuu r1tsuu Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because Media.slug itself has a type of string, not 'media'. Other way around it is to write collection like this:
image

It would work if the slug property itself of CollectionConfig would be of the CollectionSlug type, though.
But the drawback of this is that you wouldn't be able to define a new collection slug without typescript error, which can be fixed only after types generation. That's why I only did it for SanitizedCollectionConfig['slug']

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

Successfully merging this pull request may close these issues.

3 participants