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

#2 Relations and References #3

Closed
wants to merge 7 commits into from
Closed

Conversation

Fermain
Copy link
Contributor

@Fermain Fermain commented Dec 12, 2024

This PR updates the relation transformer to include astro's reference helper function.

Closes #2

Copy link
Owner

Choose a reason for hiding this comment

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

There's a relation example as well, wouldn't it make more sense to extend that instead?

Copy link
Owner

@davidenke davidenke left a comment

Choose a reason for hiding this comment

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

Thanks for you input. There are some points that might be addressed:

  • Relative fields are recognized on top level only (for the import).
  • The imports required for the transformers should be defined by the transformers themselves. Thus, they should be called first, returning their dependencies in the result payload.
  • Runtime types are unsupported, which might be okay. But consequently, this feature has to be removed entirely someday, as I'm not even using them for myself.

I don't want to bother you aligning this, I might add the changes myself, if it's okay for you.

@@ -56,9 +56,16 @@ export async function loadAndTransformCollections(
const keys = { name: collection.name };
const name = naming.replace(/%%(\w+)%%/, (_, k: keyof typeof keys) => keys[k] ?? _);
const path = resolve(to, name);
const relational = collection.fields?.some(field => field.widget === 'relation');
Copy link
Owner

@davidenke davidenke Dec 13, 2024

Choose a reason for hiding this comment

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

This would fail on nested fields. This should be a recursive lookup. Or what might be better, is some additional property in the result payload of the transformers, so the imports would be build after transformations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't think about or test with nested relations, my bad

Copy link
Owner

Choose a reason for hiding this comment

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

No problem. I changed the transform result to include a dependencies property, this should do the trick.

}

// Normalises quotes in the cptime output for consistent comparison
function normaliseCptime(cptime: string): string {
Copy link
Owner

Choose a reason for hiding this comment

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

As the test data is explicitly defined and the transformers are deterministic, this should not be necessary. Instead, we must trust the resulting output to have a specific kind of quote and expect them to have always the same shape.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was only added because of a battle between eslint and prettier, so it's there to satisfy the IDE only.

I couldn't get eslint and prettier rules to agree on quotes.

Copy link
Owner

Choose a reason for hiding this comment

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

Ah, I see. But badly configured tooling shouldn't force us to do such things. I'll align the configuration.

cptime: 'z.string()',
});
) => {
const runtime = multiple ? z.array(z.string()) : z.string();
Copy link
Owner

Choose a reason for hiding this comment

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

That's the reason I haven't thrown it together yet... 😅
Even though I like the idea using the generated reference function, this is no option for the runtime version.

Tbh, I'm not sure if supporting the runtime types anyway, as I personally just generate the types in watch mode as static files. So it maybe makes sense to drop them entirely...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm also not really using / interested in the runtime side of things. I include adc as part of my build process and watch the content dir to trigger a new build.

Copy link
Owner

Choose a reason for hiding this comment

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

I'll drop the runtime support, that reduces complexity for several aspects.
Maybe that can be implemented one day using the approach now available for tests using eval (or something less evil)...

@davidenke davidenke closed this in 1d8ff62 Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Suggestion] Implement astro:content/reference for relational fields
2 participants