Drops.Relation.Plugins.Schema (drops_relation v0.1.0)
View SourcePlugin that provides schema definition and automatic inference capabilities.
This plugin adds the schema/1
and schema/2
macros for defining relation schemas.
It supports both automatic schema inference from database tables and manual schema
definition with Ecto.Schema syntax.
SQlite & schema inference
Due to missing type information in SQlite, schema inference is very limited.
Summary
Functions
Defines a schema for the relation.
Defines a schema with manual field definitions or combines inference with custom fields.
Functions
Defines a schema for the relation.
By default, this creates an empty schema that you must populate with manual field
definitions. Use infer: true
option to automatically introspect the database table.
Parameters
table_name
- String name of the database tableopts
- Keyword list of options (optional)
Options
infer: false
- Use only manual field definitions (default: false)infer: true
- Automatically infer schema from database tablestruct: "CustomName"
- Use custom struct module name instead of default
Returns
Sets up the relation to generate:
- A
schema/0
function that returns the complete schema metadata - A
schema/1
function that returns a specific field by name
Examples
Manual schema definition:
iex> defmodule Relations.Users do
...> use Drops.Relation, repo: MyApp.Repo
...>
...> schema("users") do
...> field(:name, :string)
...> field(:email, :string)
...> end
...> end
...>
iex> user = Relations.Users.struct(%{name: "Alice Johnson", email: "alice@company.com"})
iex> user.__struct__
Relations.Users.User
iex> user.name
"Alice Johnson"
iex> user.email
"alice@company.com"
With automatic inference:
iex> defmodule Relations.Users do
...> use Drops.Relation, repo: MyApp.Repo
...>
...> schema("users", infer: true)
...> end
iex> schema = Relations.Users.schema()
iex> schema.source
:users
iex> length(schema.fields) > 0
true
With custom struct name:
iex> defmodule Relations.People do
...> use Drops.Relation, repo: MyApp.Repo
...>
...> schema("users", struct: "Person", infer: true)
...> end
...>
iex> user = Relations.People.struct(%{name: "Alice Johnson", email: "alice@company.com"})
iex> user.__struct__
Relations.People.Person
iex> user.name
"Alice Johnson"
iex> user.email
"alice@company.com"
Defines a schema with manual field definitions or combines inference with custom fields.
This form allows you to either define a completely manual schema using Ecto.Schema syntax, or combine automatic inference with additional custom fields and associations.
Parameters
table_name
- String name of the database tableopts
- Keyword list of optionsblock
- Schema definition block using Ecto.Schema syntax
Options
infer: false
- Use only the manual field definitions in the block (default: false)infer: true
- Automatically infer schema from database and merge with blockstruct: "CustomName"
- Use custom struct module name
Returns
Sets up the relation with either a purely manual schema or a merged schema combining inference with custom definitions.
Examples
iex> defmodule Relations.Users do
...> use Drops.Relation, repo: MyApp.Repo
...>
...> schema("users", infer: true) do
...> field(:role, :string, default: "member")
...> field(:full_name, :string, virtual: true)
...> end
...> end
...>
iex> schema = Relations.Users.schema()
iex> %{name: name, meta: %{default: default}} = schema[:role]
iex> name
:role
iex> default
"member"