Skip to content

Fields of type reference should be able to embed their data #86

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

Open
seancdavis opened this issue Jan 5, 2022 · 3 comments
Open

Fields of type reference should be able to embed their data #86

seancdavis opened this issue Jan 5, 2022 · 3 comments
Labels
feature New feature or request

Comments

@seancdavis
Copy link
Collaborator

seancdavis commented Jan 5, 2022

Given the following config:

const Category = defineDocumentType(() => ({
  name: 'Category',
  filePathPattern: 'categories/*.md',
  bodyType: 'none',
  fields: {
    title: { type: 'string', required: true },
  },
}))

const Doc = defineDocumentType(() => ({
  name: 'Doc',
  filePathPattern: '**/*.mdx',
  bodyType: 'mdx',
  fields: {
    title: { type: 'string', required: true },
    category: { type: 'reference', of: Category }
  },
}))

export default makeSource({
  contentDirPath: 'content',
  documentTypes: [Doc, Category],
})

And the following content files:

content/categories/a.md

---
title: My First Category
---

content/docs/index.mdx

---
title: Home Page
category: categories/a.md
---

The output document would have the following:

{
  "title": "Home Page",
  "category": "categories/a.md",
  // ...
}

It would be nice to (optionally) embed the category data directly in the output document. Otherwise, I have to defer that logic to my application. Being that it's data manipulation, it seems right that Contentlayer would handle this.

I'd rather see the output like this:

{
  "title": "Home Page",
  "category": {
    "title": "My First Category"
  },
  // ...
}
@seancdavis seancdavis added the feature New feature or request label Jan 5, 2022
@schickling
Copy link
Collaborator

Version 0.0.34 introduces experimental support for this by letting you set embedDocument: true e.g. like this:

export const Post = defineDocumentType(() => ({
    name: 'Post',
    fields: {
        // ...
        author: { type: 'reference', of: Person, embedDocument: true },
        // ...
}));

Note: This currently just works properly on the "top level" i.e. for documents referencing other documents but not yet for references in nested document definitions.

@aaronyih1
Copy link

Version 0.0.34 introduces experimental support for this by letting you set embedDocument: true e.g. like this:

export const Post = defineDocumentType(() => ({
    name: 'Post',
    fields: {
        // ...
        author: { type: 'reference', of: Person, embedDocument: true },
        // ...
}));

Note: This currently just works properly on the "top level" i.e. for documents referencing other documents but not yet for references in nested document definitions.

This is awesome! Just to clarify, does this mean that there isn't currently a way to create a list of references with embedded documents enabled?

@schickling
Copy link
Collaborator

@aaronyih1 I don't know from the top of my head anymore but worth giving it a try and see whether it already works.

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

No branches or pull requests

3 participants