Skip to content

Data collections and collection references#3233

Merged
sarah11918 merged 51 commits intomainfrom
feat/data-collections-references
May 18, 2023
Merged

Data collections and collection references#3233
sarah11918 merged 51 commits intomainfrom
feat/data-collections-references

Conversation

@bholmesdev
Copy link
Copy Markdown
Contributor

@bholmesdev bholmesdev commented May 11, 2023

What kind of changes does this PR include?

  • New or updated content

Description

Document the new "data collection" and "collection reference" features. See the RFC for the original feature proposal: withastro/roadmap#569

  • Update preambles to mention both content and data collections.
  • Add new sections on defining and resolving references. Each are added as subsections to maintain the overall flow of the guide.
  • Add the new type field to all defineCollection() examples. Yes, type will default to 'content' to make this change non-breaking for existing content collections. Though to keep these docs future-proof (in case type becomes a required field), type has been added for both content and data collections. Open to adding the line "type defaults to 'content' but you can specify manually" if we prefer!
  • Update language to be consistent when explaining content or data collections (ex. frontmatter -> "frontmatter or data file")
  • Update getEntryBySlug() recommendations to use getEntry(). This allows you to query content and data collections, without needing to explain how getEntryBySlug() is for content collections only. getEntryBySlug() is still around in the API reference, with a note to use the new getEntry() function for ease-of-use.
  • Add new sections to the API reference:
    • getEntry()
    • getEntries()
    • defineCollection() -> type
    • reference()
    • Update "Collection Entry Type"

@netlify
Copy link
Copy Markdown

netlify bot commented May 11, 2023

Deploy Preview for astro-docs-2 ready!

Name Link
🔨 Latest commit fc64768
🔍 Latest deploy log https://app.netlify.com/sites/astro-docs-2/deploys/64665448cada590008158868
😎 Deploy Preview https://deploy-preview-3233--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Copy Markdown
Member

@yanthomasdev yanthomasdev left a comment

Choose a reason for hiding this comment

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

Thanks @bholmesdev, amazing work on data collections! Added a few initial suggestions for you 🙌

bholmesdev and others added 5 commits May 11, 2023 13:28
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
bholmesdev and others added 2 commits May 15, 2023 10:47
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
@sarah11918 sarah11918 added the merge-on-release Don't merge this before the feature is released! (MQ=approved but WAIT for feature release!) label May 15, 2023
Copy link
Copy Markdown
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Yay Ben! Left some more thoughts on the earlier part of this. Really liking it!

I'm tagging out for the night, and I've commented on the entire thing here, so some stuff for you to consider, and think about whether it's helpful!

A **content collection** is any directory inside the reserved `src/content` project directory, such as `src/content/newsletter` and `src/content/authors`. Only content collections are allowed inside the `src/content` directory. This directory cannot be used for anything else.

A **content entry** is any piece of content stored inside of your content collection directory. Content entries are stored as either Markdown (`.md`) or MDX (`.mdx`) files. You can use any filename you want, but we recommend using a consistent naming scheme (lower-case, dashes instead of spaces) to make it easier to find and organize your content.
A **collection entry** is any piece of content stored inside of your content collection directory. Entries can use content authoring formats including Markdown (`.md`) and MDX (`.mdx`) or as data formats including YAML (`.yaml`) and JSON (`.json`). You can use any filename you want, but we recommend using a consistent naming scheme (lower-case, dashes instead of spaces) to make it easier to find and organize your content.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
A **collection entry** is any piece of content stored inside of your content collection directory. Entries can use content authoring formats including Markdown (`.md`) and MDX (`.mdx`) or as data formats including YAML (`.yaml`) and JSON (`.json`). You can use any filename you want, but we recommend using a consistent naming scheme (lower-case, dashes instead of spaces) to make it easier to find and organize your content.
A **collection entry** is any piece of content stored inside of your content collection directory. Entries can use content authoring formats including Markdown (`.md`) and MDX (`.mdx`) or as data formats including YAML (`.yaml`) and JSON (`.json`). You can use any filename you want, or begin your filename with an underscore to [exclude pages from the build](/en/core-concepts/routing/#excluding-pages). We recommend using a consistent naming scheme (lower-case, dashes instead of spaces) to make it easier to find and organize your content, but this is not required.

Just had an issue with someone over the underscore thing this morning, so I think this is a good place to qualify "you can use any filename you want"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point! I agree documenting the underscore pattern is useful, but this isn't quite right. Underscore files will still be included in the build if you're using them for, say, relative image paths or MDX component imports. They're just excluded from collection queries. Wondering if this is the best place to explain that given it's the intro?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, so rather than get into more detail here, what about dropping that you can name them whatever you want entirely:

A collection entry is any piece of content stored inside of your content collection directory. Entries can use content authoring formats including Markdown (.md) and MDX (.mdx) or as data formats including YAML (.yaml) and JSON (.json). We recommend using a consistent naming scheme (lower-case, dashes instead of spaces) for your files to make it easier to find and organize your content, but this is not required.

2. Define each collection that you'd like to validate with a schema.
3. Export a single `collections` object to register your collections.
1. **Import the proper utilities** from `astro:content`.
2. **Define each collection that you'd like to validate.** This includes a `type` specifying whether the collection contains content authoring formats like Markdown (`type: 'content'`) or data formats like JSON or YAML (`type: 'data'`). It also includes a `schema` that defines the shape of your frontmatter or entry data.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nicely done here, Ben!

@bholmesdev
Copy link
Copy Markdown
Contributor Author

bholmesdev commented May 16, 2023

@sarah11918 Re "Resolving References" section: I just divided with a heading to split up the examples, not for SEO. Though I do expect people to wonder "ah crap, I just queried. Now how do I get the data for these references?" Resolving references was my stab at a short title to capture that need. Happy to either remove the title or find a better name if you prefer!

We can also merge the examples into one large code block, but this would lead to code comment overload imo 🤷

Copy link
Copy Markdown
Member

@sarah11918 sarah11918 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 it, Ben! 🎉

Very tiny thoughts left for you to look at, and one or two nits. Thank you, this page is even more amazing now!

A **content collection** is any directory inside the reserved `src/content` project directory, such as `src/content/newsletter` and `src/content/authors`. Only content collections are allowed inside the `src/content` directory. This directory cannot be used for anything else.

A **content entry** is any piece of content stored inside of your content collection directory. Content entries are stored as either Markdown (`.md`) or MDX (`.mdx`) files. You can use any filename you want, but we recommend using a consistent naming scheme (lower-case, dashes instead of spaces) to make it easier to find and organize your content.
A **collection entry** is any piece of content stored inside of your content collection directory. Entries can use content authoring formats including Markdown (`.md`) and MDX (`.mdx`) or as data formats including YAML (`.yaml`) and JSON (`.json`). You can use any filename you want, but we recommend using a consistent naming scheme (lower-case, dashes instead of spaces) to make it easier to find and organize your content.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, so rather than get into more detail here, what about dropping that you can name them whatever you want entirely:

A collection entry is any piece of content stored inside of your content collection directory. Entries can use content authoring formats including Markdown (.md) and MDX (.mdx) or as data formats including YAML (.yaml) and JSON (.json). We recommend using a consistent naming scheme (lower-case, dashes instead of spaces) for your files to make it easier to find and organize your content, but this is not required.

bholmesdev and others added 5 commits May 17, 2023 11:20
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
…ro/docs into feat/data-collections-references
Copy link
Copy Markdown
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

It is hereby my pleasure to approve this PR, @bholmesdev! 🥳

Copy link
Copy Markdown
Member

@yanthomasdev yanthomasdev left a comment

Choose a reason for hiding this comment

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

It is hereby my pleasure to find one more nit! But that's the last one, get my approval as well 🙌

bholmesdev and others added 2 commits May 17, 2023 20:01
@sarah11918 sarah11918 merged commit b18535b into main May 18, 2023
@sarah11918 sarah11918 deleted the feat/data-collections-references branch May 18, 2023 16:45
@sarah11918 sarah11918 added the minor-release For the next minor release; in the milestone, "merge queue" = approved for merging on release day. label Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

add new content Document something that is not in docs. May require testing, confirmation, or affect other pages. merge-on-release Don't merge this before the feature is released! (MQ=approved but WAIT for feature release!) minor-release For the next minor release; in the milestone, "merge queue" = approved for merging on release day.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants