Data collections and collection references#569
Conversation
| - **We'd need to parse the whole array of entries** to determine entry IDs. This could be a performance bottleneck vs. pulling IDs from file names. | ||
| - **It would be different from content collections,** which means a learning curve. | ||
|
|
||
| Due to these, we decided against single-file for now. Though we do recognize the convenience of colocation that can be explored in the future. |
There was a problem hiding this comment.
+1 to exploring in the future. A solution that I'd love to see explored more in a future RFC that I think would kill a few birds with one stone:
+ import authorsData from '../authors.json';
const authors = defineCollection({
type: 'data',
schema: z.object({
name: z.string(),
twitter: z.string().url(),
}),
+ data: authorsData,
});Just a thought / not at all blocking this RFC which I agree is smart to treat this as out-of-scope!
There was a problem hiding this comment.
What's the use-case for importing like this? It won't work with remote data, I'm surprised at the suggestion.
There was a problem hiding this comment.
@matthewp I assume this could allow users to parse data in any format they choose, or even write data in-line with their config. The downside: it would be possible for data to exist outside of content/, which may not feel consistent. Not sure I mind that inconsistency though.
| </ul> | ||
| ``` | ||
|
|
||
| To retrieve individual entries, `getEntry()` can be used. This receives both the collection name and the entry `id` as described in the [Return type](#return-type). These can be passed as separate arguments or as object keys. |
There was a problem hiding this comment.
Huge fan of this new getEntry API! It's a clever solution to the ID vs. slug naming complexity you outline below.
| export const collections = { authors }; | ||
| ``` | ||
|
|
||
| These collections can also be queried using the `getCollection()` and `getEntry()` utilities: |
There was a problem hiding this comment.
You mention getEntries() in the video, but I did a quick search on the page and didn't see that here. Is that included or not? If included, it should be defined here!
There was a problem hiding this comment.
Good point! We should add that 👍
There was a problem hiding this comment.
Update: added to the references doc. getEntries() is meant for resolving references primarily and shouldn't be recommended for manual use. This is how a manual call would look:
const relatedPosts = await getEntries({ collection: 'blog', slug: 'post-1' }, { collection: 'blog', slug: 'post-2' }, ...);In fact, we can document "if you want to retrieve multiple entries manually, we suggest getCollection() with a filter."
ematipico
left a comment
There was a problem hiding this comment.
First round of question about data collections
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
| }); | ||
| ``` | ||
|
|
||
| Then, we will update our type generator to recognize these data-specific file extensions. This should also raise errors when collections are misconfigured (i.e. `type: 'data'` is missing from the config file) and when a mix of content and data in the same collection is detected. |
There was a problem hiding this comment.
This should also raise errors ... a mix of content and data in the same collection is detected.
I could see this being a bit of a blocker for the easy upgrade path of a unified src/content directory
If I have a large data collection and only need to upgrade one to use body content for some specific use case, I'd end up having to update every item in the collection to .md files
I can't think of a great example of this at the moment so it may very well be a corner case, feel free to ignore if it isn't a use case worth covering!
There was a problem hiding this comment.
Fair enough! There may be a case to allow content inside data collections, since data collections have a subset of content collections' properties. Though it sounds like you're proposing the other way. In that case, we could either mix return types based on the entry's file extension, or allow data collections in content. For the former, properties like render() and body would be added to every collection item and stubbed out with undefined's, which I worry about for usability. For the latter, mixed return types feel risky for usable types when calling getCollection(). We'd need to ship some type guards for appropriate filtering.
My thinking is this:
- Move forward with collection-level types for launch
- Consider allowing content collections in data collections as a fast follow
- Look into the "singleton" discussion here (Singletons in the Content Collections API #449) for one-off
.mdfiles as you're describing here. I think there's a use case to allowing one-off entries with a shared schema, though it's something we could tackle separately.
Let me know if that makes sense, or if I'm missing anything!
There was a problem hiding this comment.
Given that the RFC is focused on adding support for data collections and a migration path from data entries to content entries isn't a specific goal, I think we can save this question/idea for later!
I honestly don't have a great argument for a solution there. It'll be interesting to see how data collections get used and whether mixing content and data entries in the same collection is even a common enough use case to consider 👍
There was a problem hiding this comment.
Yep, another case of "defensively make restrictive, open up if people want it" 👍
|
Thanks for your contributions everyone! We're looking to reach consensus on this RFC by end of the week, so please share any final feedback (blocking or otherwise) that you want us to address. If we don't hear anything, that's our go-ahead to ship 🚢 |
|
Well, I'll take your stunned silence as a "ship it!" Merging |
Summary
This introduces two related features to content collections:
Links
Visual.Studio.Code.-.config.ts.with-data.-.2.May.2023.mp4