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

Data Transformation in content.config.ts (v3) #2894

Open
franklin-tina opened this issue Dec 7, 2024 · 1 comment
Open

Data Transformation in content.config.ts (v3) #2894

franklin-tina opened this issue Dec 7, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@franklin-tina
Copy link

franklin-tina commented Dec 7, 2024

Is your feature request related to a problem? Please describe

Note

This feature request for v3

I currently repeat data transformation code in multiple places.
For example, on the home page, I fetch post data and transform it like so:

// Used in: index.vue, all-post.vue
const { data: posts } = await useAsyncData('blog', () => queryCollection('blog').select('id', 'title', 'path', 'description', 'tags', 'createdAt', 'updatedAt').order('createdAt', 'DESC').limit(2).all(), {
  transform(data) {
    return data.map((post) => ({
      ...post,
      // some other transformations for tags, and other values etc
      createdAt: new Date(post.createdAt).toLocaleDateString(),
    }))
  },
})

I do the same transformation on the "All posts" page and possibly other pages. This repetition can make it harder to maintain, and have consistent data across the pages where one might use it.

Describe the solution you'd like

I would like to configure data transformations directly in the content.config.ts file, if possible. This would centralize the transformation logic, making future updates in one file only

Describe alternatives you've considered

An alternative is to keep the transformation logic in each place where data is fetched, what I currently do.

Additional context

I saw a reference to a defineContentPlugin, with a transform property in an example file in the docs. However, I couldn't find any implementation of it in the source code.
Does this exist? If yes, I suppose we can perform transformations in there too.

Thanks 💚,
cc @larbish

@franklin-tina franklin-tina added the enhancement New feature or request label Dec 7, 2024
@franklin-tina
Copy link
Author

Looking at this again, I think one could define a server plugin and do a transform in the content:file:afterParse hook. 🤔

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

No branches or pull requests

1 participant