This tool is now deprecated. We decided to create a new tool called db-gen, which can generate code for any language, is more configurable and is very long-lasting, not dependent on any other framework or tool.
This tool is for Postgres
database. It is designed for database-first approach. When executed, it generates DbContext
module with configured module prefix with elixir functions that call db stored procedures.
The generated code even contains parsing mechanism that generates struct
s for each "complex" type returned from db functions. It even supports procedures (with no return type), functions returning simple types (int, text etc.).
Generated code has no runtime overhead - the code purely uses Ecto
repo.
The tool uses EEx so it is also quite customizable. You are able to select just the functions you need from given schemas or you can include all and exclude just some.
The package can be installed
by adding ecto_gen
to your list of dependencies in mix.exs
:
def deps do
[
{:ecto_gen, "~> 0.10.0", runtime: false, only: :dev}
]
end
Prepare tool's configuration:
config :my_app, MyApp.Repo,
username: "db_username",
password: "db_password",
database: "database",
hostname: "hostname"
config :ecto_gen,
otp_app: :my_app,
db_config: MyApp.Repo,
output_location: "path/to/generated/output", # relative path should be relative to the project root
output_module: "MyApp.EctoGenOutput", # Module prefix that will be used for generated content
# This way, you can provide custom template for individual parts of generation
# default files are in /priv/templates directory of this package
# NOTE:
# If you add these overrides in the middle of project, you will need to recompile this package using the following to generate new EEx template functions:
# mix deps.compile ecto_gen
template_overrides: [
db_module: "/path/to/db_module.ex.eex",
routine: "/path/to/db_routine.ex.eex",
routine_result: "/path/to/db_routine_result.ex.eex",
routine_parser: "/path/to/db_routine_parser.ex.eex"
],
# This config holds information about what routines (funcs) from database will have generated elixir functions etc.
# db project has keys, each representing database's schema which has config for what routines it includes/ingores
db_project: [
public: [
funcs: "*", # or ["func_name_1", "func_name_2"]
# makes sense to specify ignored functions (routines) only when funcs equal "*"
ignored_funcs: ["ignored_func_name_1"]
]
]
With this added to your configuration, you can generate the db context issuing following command:
$ mix eg.gen
Using generated functions is straightforward. Simply call the function to get results: The Ecto repo must be started.
MyApp.EctoGenOutput.DbContext.func_name_1(arg1, arg2)
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_gen.