joeljuca / swiss_schema

A Swiss Army knife for your Ecto schemas
https://hex.pm/packages/swiss_schema
Apache License 2.0
27 stars 3 forks source link

Accept custom schema in `use SwissSchema` [Blocked] #4

Closed joeljuca closed 3 months ago

joeljuca commented 1 year ago

I've been thinking about making SwissSchema able to generate schema functions in other modules than the target schema itself. The use case is to allow it to be used in ctx modules.

Something like that:

# accounts.ex

defmodule MyApp.Accounts do
  use SwissSchema,
    schema: MyApp.Accounts.User
end

This way, SwissSchema could be used to replace generated ctx functions, resulting in cleaner ctx modules.

mohammedzeglam-pg commented 1 year ago

Maybe it be like

defmodule MyApp.Accounts do
  use SwissSchema,
    schema: [user: MyApp.Accounts.User, token: MyApp.Accounts.Token]
end
joeljuca commented 1 year ago

Hi @mohammedzeglam-pg,

What role do you think the :token keyword should play?

mohammedzeglam-pg commented 1 year ago

Hi @mohammedzeglam-pg,

What role do you think the :token keyword should play?

generate function name not all context relay on one schema could context use 2-3 different schema

MyApp.Accuonts.get_user/1
MyApp.Accounts.get_token/1
joeljuca commented 1 year ago

@mohammedzeglam-pg I see!

Interesting idea. So far, I've considered SwissSchema functions for a single schema only, but if it's going to be used in context modules it might be a good idea to inject functions for multiple schemas.

I'll think more about how the API should look if it will support multiple schemas per context. Thanks for the suggestion!

mohammedzeglam-pg commented 1 year ago

@mohammedzeglam-pg I see!

Interesting idea. So far, I've considered SwissSchema functions for a single schema only, but if it's going to be used in context modules it might be a good idea to inject functions for multiple schemas.

I'll think more about how the API should look if it will support multiple schemas per context. Thanks for the suggestion!

Maybe run different function as phoenix do with ~:controller~ ~:live_view~ ...etc

and could you consider review #6

joeljuca commented 5 months ago

I thought about this issue and the scope of this project, and I realized it's not a good idea to extend SwissSchema to such scenarios. SwissSchema should be about having queryable Ecto schemas, not generating functions in context modules.

This should not be implemented, but it's worth a PR to include this decision in FAQ.md. I'm leaving it open until I get some free time to do it right.