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

feat: support polymorphic associations / abstract schemas #51

Merged
merged 8 commits into from
Sep 20, 2024

Conversation

cylkdev
Copy link
Contributor

@cylkdev cylkdev commented Sep 15, 2024

This closes #48

Changelog

  • Adds support for the abstract table syntax {source :: binary(), queryable :: Ecto.Queryable.t()} with the addition of EctoShorts.CommonSchemas. The syntax can be used interchangeable in the places where an ecto schema is accepted as an argument. When the abstract table tuple is used it sets the source used for the query which takes precedence over the source defined in the schema.
  • Adds EctoShorts.QueryHelpers which contains helper functions for ecto queries.
  • Adds documentation to EctoShorts.Actions along with more examples.

) :: Ecto.Query.t
def convert_params_to_filter(query, params) when params === %{} do
query
query_or_queryable_or_source_queryable :: query() | queryable() | source_queryable(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I would just simplify this to query. It makes it more readable, and you're using it as a query within the context of the function. In it's current form the name is to hyper specific and a little redundant considering you have the definition to the right of the name that tells you exactly what types you expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, I'm still working on a review, there is a lot to digest here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries, thanks for taking the time to look at it @BobbieBarker! For the areas that are unclear feel free to raise them and I can provide more information. Ecto is pretty huge in terms of functionality so I think there is opportunities to improve on the documentation as well for how it's supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some of this could also be split out into smaller prs as well taking a second look at this. For eg the refactor of the less public api such as the QueryBuilder.Common / QueryBuilder.Schema could be it's own pr.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you use git mv when you moved these files around? I suspect you didn't, and if you had it would help it not look like you rewrote the library with this pr. 😆.

Also It seems to me that the chief value proposition in this PR is support for polymorphic associations through EctoShorts as described in the docs.

Is that true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. No i did not use git mv, good catch. I could go back to the last commit on main and then re-play those changes with git mv and push it back up. Is that okay?

  2. Yes the main goal is to support polymorphic associations as described in the documentation. When developing the feature I didn't want to repeat the code and the handling for the syntax so EctoShorts.CommonSchemas was created as interface to handle the translation based on whether {source, schema} or schema is given.

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I think that would go a long ways to making the PR easier to review for the changes you're introducing. Thank you.
  2. I think we should start considering what sorts of documentation we could introduce to help make users aware of this functionality, and make sure they understand how to take advantage of it etc. -- If this already exists and I didn't notice it I apologize.


alias EctoShorts.QueryHelpers

defmodule MockSchema do
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of defining MockSchema and MockSchemaWithPrefix in multiple test files maybe it's time to move them out into their own files so they can cleanly be reused across tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good, will make that change

@BobbieBarker
Copy link
Contributor

BobbieBarker commented Sep 17, 2024

Something I'm having a hard time detecting or looking out for in this sea of changes are any breaking changes to the API that would harm any existing users. To the best of your knowledge does this PR contain anything like that?

@cylkdev
Copy link
Contributor Author

cylkdev commented Sep 17, 2024

Something I'm having a hard time detecting or looking out for in this sea of changes are any breaking changes to the API that would harm any existing users. To the best of your knowledge does this PR contain anything like that?

Not to my knowledge. However I'm going to take your feedback and split this out into smaller changes so this is easier to digest today :)

@cylkdev cylkdev changed the title feat: adds common schemas and actions improvements feat: support polymorphic associations / abstract schemas Sep 18, 2024
@cylkdev
Copy link
Contributor Author

cylkdev commented Sep 18, 2024

@BobbieBarker I've applied the feedback. This is ready for another pass.

  • The pr now only focus on the changes that integrate EctoShorts.CommonSchemas.
  • I've added tests to cover as much of the functionality as I could and this is going to be an iterative process
  • I took a pass at adding documentation on how to use the feature to the EctoShorts module doc
  • I've updated the documentation in EctoShorts.Actions as well to describe more of the functionality

|> queryable.changeset(params)
|> func.()
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When using an abstract schema the "assoc_id" is a polymorphic reference and the name of the foreign key is different per table. This function was added so that constraints can be added to those schemas with the appropriate constraint names before the changeset is handed off to the Ecto.Repo api.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actions.delete(user_avatar, changeset: fn changeset ->

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure 100% when we should bump the version in this file, but these changes def warrant a v 2.5.0 imho.

@MikaAK MikaAK merged commit daaefe5 into MikaAK:main Sep 20, 2024
1 check passed
@cylkdev cylkdev deleted the feat-common-schemas branch September 22, 2024 00:09
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.

missing support for Tuple implementation of Ecto.Queryable protocol when using filter params
3 participants