Is there a way to define a filter (where clause) on a schema?

There is not, but let me propose not using the schema (name) in the first place. Most places where you use MySchema actually takes a Ecto.Queryable protocol implementation. The module name just happens to be one such implementation.

The docs show the implementations ecto comes with: Ecto.Queryable — Ecto v3.12.4

You could e.g. have MyQueryable.from(:post) and have it return a struct, which resolves to the correct Ecto.Query through the protocol or return the correct Ecto.Query as a result to the function. The former is a bit more flexible because you could have additional apis to interact with the queryable, which you can’t do with a Ecto.Query.

Unfortunately Atom, BitString and Tuple already have native implementations for the protocol, so you cannot use those types for your custom implementation, leaving either lists (charlists) or structs(/Map) as possible formats for the protocol.

1 Like