SelectoMix.Introspector behaviour (selecto_mix v0.4.7)
Facade for introspecting different schema sources.
This is a plain dispatch facade (not an Elixir defprotocol/@behaviour
pair) that provides a single introspect/2 entry point over several
explicit backends, dispatching on the shape of source:
- Ecto schema modules (atoms) - delegates to
SelectoMix.Introspector.Ecto, which reads Ecto's__schema__/1callbacks {:db, adapter, connection, table_name, opts}tuples - delegates to the givenadaptermodule'sintrospect_table/3(anyselecto_db_*adapter satisfying that contract; seeAdapterResolver), then normalizes the result (table/schema/source/primary-key defaults){:postgrex, connection, table_name}/{:postgrex, connection, table_name, schema}tuples - delegates toSelectoMix.Introspector.Postgres, which queries Postgrex/PostgreSQL system catalogs directly- Future: Other database types (MySQL, SQLite, etc.)
The @callback below documents the standardized introspect/2 contract
that this facade's own dispatch honors; no module is required to declare
@behaviour SelectoMix.Introspector since dispatch here is a plain case
on the shape of source, not dynamic behaviour invocation.
Usage
# Ecto schema
{:ok, metadata} = SelectoMix.Introspector.introspect(MyApp.User, [])
# Postgrex connection
{:ok, conn} = Postgrex.start_link(...)
{:ok, metadata} = SelectoMix.Introspector.introspect(
{:postgrex, conn, "users"},
[]
)Metadata Format
All introspectors return a standardized metadata map:
%{
table_name: "users",
schema: "public",
fields: [:id, :name, :email, :inserted_at, :updated_at],
field_types: %{
id: :integer,
name: :string,
email: :string,
inserted_at: :naive_datetime,
updated_at: :naive_datetime
},
primary_key: :id, # or [:id, :tenant_id] for composite
associations: %{
posts: %{
type: :has_many,
...
},
profile: %{
type: :has_one,
...
}
},
columns: %{
id: %{type: :integer, nullable: false, ...},
name: %{type: :string, nullable: false, ...},
...
},
source: :ecto # or :postgres, :mysql, etc.
}
Summary
Callbacks
Introspect a schema source and return standardized metadata.
Functions
Introspects source, dispatching to the appropriate backend based on its
shape (Ecto module, {:db, ...} adapter tuple, or {:postgrex, ...} tuple).