Once nice thing about Prisma is you can connect to an existing database, and use the CLI to run “prisma db pull” - and then it uses introspection on the database to build a nice model file.
The thing that is not as nice, is then the models are prisma models, not Ecto schemas
Is there an up-to-date / still working Elixir project to do the same thing?
Ecto schemas are not guaranteed to be 1:1 with database table structure (this is done by design to avoid coupling), hence why when we run migrations, we use the explicit approach for creating a table:
defmodule MyRepo.Migrations.AddWeatherTable do
use Ecto.Migration
def change do
create table("weather") do
add :city, :string, size: 40
add :temp_lo, :integer
add :temp_hi, :integer
add :prcp, :float
timestamps()
end
end
end
As you can see there are zero references to the underlying schema that will be using this table.
Taking this in consideration, I’m not entirely sure what value such a tool would have, even if you had to integrate with a new database, the time spent on initial creation of schemas would not be that long.
Thanks D4no0. Makes sense. Though I think tools like this can be quite nice to shorten the “yak shaving” at the beginning of a project, in cases where you using an existing database and want to build an Elixir project on it, even if they don’t get it quite right when the schema model doesn’t align perfectly with the data structure.
For example - looking at the generated Prisma schema for the project I’m starting, I’ve got 21 models and three enums, and it has captured the models, most of the relationships and data types in a model file that is used both for the schemas and migrations, in just one CLI command. For me it’s pretty handy, if only at the beginning of the project. It’d take me a day or two to get all these set up correctly as schemas and migrations in Ecto, and test to make sure I didn’t mess up any of the datatypes or relationships.
While I understand the benefits, building such a tool might not be that trivial, especially if you expect the relations to be determined too.
I think it could be an interesting project for someone trying to learn how to play with generators, but the value it would bring would not be that great sadly, as creation of schemas in mass is not something you do often.
This implies that you need all those schemas out of the gate, otherwise you can create and improve the schemas iteratively, this is what I usually do and it works like a charm.
This would indeed be very nice for brownfield projects, or spinning up an API for an existing app. If you are interested in creating a generator, the Endo project would give you a good boost by providing DB introspection.
Even though I agree it would be difficult to build such a tool – and that it will not be used as super often – I still think it could be a great marketing if the Elixir ecosystem had such a tool.
Endo looks cool - ty. It looks like there are a few projects that could perhaps be revived. One mechanism would be a little hacky, which is to literally use/wrap the Prisma tool and then use Prisma’s code generation facilities to generate the schemas. This has the advantage of relying on Prisma existing/continuing work, and @raarts already built an alpha version (GitHub - raarts/prisma-generator-elixir: Prisma generator to generate files for Elixir programs (Ecto and Absinthe)) of a prisma to Elixir generator, but it would need to be updated. But you know… a lot hackier than using something native (perhaps Endo) to assemble the introspection deets.