Giter Club home page Giter Club logo

Comments (11)

michalmuskala avatar michalmuskala commented on August 18, 2024 1

token_source would be a function to load the table name, for example:

defp token_source() do
  Application.fetch_env!(:guardian_db, GuardianDb)
  |> Keyword.get(:schema_name, "guardian_tokens")
end

This isn't documented anywhere in one place, unfortunately. Some bits are in the Ecto.Schema docs, some in Ecto.Query and some in Ecto. This would use the same technique as the one described in the polymorphic belongs_to docs.

from guardian_db.

michalmuskala avatar michalmuskala commented on August 18, 2024 1

I opened a PR to ecto with improvements to the docs around the __meta__ field: elixir-ecto/ecto#2228

from guardian_db.

davejlong avatar davejlong commented on August 18, 2024

It seems that the error was because I had changed the schema_name value without recompiling the dependencies. I'll submit a PR to add a note about this to the documentation.

from guardian_db.

yordis avatar yordis commented on August 18, 2024

@davejlong the correct solution for this is to change

@schema_name Keyword.get(config, :schema_name, "guardian_tokens")

Talking to Michal about it, use module attributes for reading this configurations is a bad practice because of the situation you ran into, which is pretty common to run into

from guardian_db.

yordis avatar yordis commented on August 18, 2024

hhhhmmm I think I can't switch it to some function call 😢 so it will be better to actually add some documentation I think

from guardian_db.

michalmuskala avatar michalmuskala commented on August 18, 2024

Ecto supports using {"table_name", Schema} as the source of a query. This can be leveraged by changing, for example:

GuardianDb.repo.get_by(Token, jti: jti, aud: aud)

to

GuardianDb.repo.get_by({token_source(), Token}, jti: jti, aud: aud)

It's also supported when creating structs like here:

%Token{}
|> cast(prepared_claims, [:jti, :typ, :aud, :iss, :sub, :exp, :jwt, :claims])
|> GuardianDb.repo.insert()

which could be replaced by:

%Token{}
|> Ecto.put_meta(source: token_source())
#...

Ecto makes sure that structs extracted from tagged queries when used with update or delete issue correct queries.

This should cover all the use cases.

from guardian_db.

yordis avatar yordis commented on August 18, 2024

@michalmuskala what is that token_source the schema_name in this case? What about the schema "name over here" situation?

Could you point me to the doc spot so I can read about it please

from guardian_db.

yordis avatar yordis commented on August 18, 2024

@michalmuskala I notice this https://github.com/elixir-ecto/ecto/blob/v2.2.3/lib/ecto/schema.ex#L378 if I use embedded_schema I dont need any random name on the schema name because I will be using what you recommend.

What are your thoughts about it?

from guardian_db.

michalmuskala avatar michalmuskala commented on August 18, 2024

embedded_schema generates a slightly different struct from schema - in particular, it doesn't track the lifetime of the struct (since it doesn't matter for embedded associations). The difference is the __meta__ key.

from guardian_db.

yordis avatar yordis commented on August 18, 2024

@michalmuskala wouldn't be worth to add the differences in Ecto documentation?

Situation like this one people dont have to ask you what the differences are and it's important to know for apply the correct use of it.

For example right now, I don't know what you meant by lifetime of the struct and other situations.

What do you think?

from guardian_db.

yordis avatar yordis commented on August 18, 2024

@michalmuskala you are the boss!

from guardian_db.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.