Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Loading context into schema struct metadata

21 views
Skip to first unread message

Jesse Stimpson

unread,
Aug 17, 2024, 3:24:34 PM8/17/24
to elixir-ecto
Hello,

In my adapter for FoundationDB I've been misusing the :prefix option to pass an opaque data structure from the caller to my adapter. (Specifically this is a tuple of the type `{:erlfdb_tenant, reference()}`, which is the handle to an open FDB Tenant).

It seems that the :context may be a more appropriate way of doing this, which is necessary as of 3.12, since the :prefix is now subjected to is_binary checks.

However, there is a nice feature of the :prefix that isn't present with :context.

```
      tenant = open_tenant()
      user = TestRepo.get(User, id, context: tenant)
      assert ^tenant = Ecto.get_meta(user, :context) # fail
```

The assertion here fails, with the :context entry being nil. If you do something similar with :prefix, it works as expected.

I'd like to load the context into the struct metadata, so that future calls on the Repo can be made without the explicit option, and the user doesn't have to put it there explicitly.

I tried using the Ecto.Adapter.__before_compile__ to implement this behavior as custom to my adapter, but that requires that callers cannot use the standard Repo.get/3, which is not desirable.

Is it possible to automatically load the context into the struct metadata like what is done in Ecto.Schema.Loader.load_struct/3 ?

Thanks,
Jesse

José Valim

unread,
Aug 17, 2024, 3:35:24 PM8/17/24
to elixi...@googlegroups.com
We would be glad to consider a PR that explores this direction. :)

I'd prefer if this was somehow an adapter callback, but I don't think it will be that easy in practice. Let's see!

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/d5169a44-6321-411d-84b1-4fa65c604241n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages