Database-first paradigm

hello,

Elixir is fantastic, but,

does it is possible to do a “database-first” entity mapping in MVC (as MS .NET CORE)?

  1. do database schema
  2. magically obtain the ex file with the mapped fields (names, kind, etc.) of the schema
    without have to write the entire class and fields hand by hand

thank you

1 Like

in elixir there are no classes so i’m not sure exactly what you mean. If you make a schema you have defined a struct already.

In elixir with ecto database tables aren’t a one to one mapping to the tables that back them. You can have schemas that aren’t backed by tables at all, so i’m not sure it would be desirable

Maybe https://github.com/jhartwell/Plsm is what you are looking for.
Never tried the package, just found on a forum search.
Tool for generating schema for existing mysql database

1 Like

Don’t let your old habits stop you from enjoying a new tech. :slight_smile:

As others said, not all Ecto.Schema-derived modules in your project are mandated to be backed by a database. There are embeds, there are structs just [ab-]using Ecto for validation, etc.

But you can probably tag your structs that are DB-backed. Something like @persistent true at the top?

6 Likes

@RDP1974 As a “former” dotnet developer that is now really enjoying the Elixir ecosystem, I can’t stress enough how @dimitarvp’s advice is important. The best way to cope with those differences is just letting your mental model adapt to the new environment.

I’ll go ahead and say that in 8 years doing almost exclusive dotnet stuff, I haven’t experienced a lot of cases that could benefit that much from using a “database first” approach. This mainly has to do with how you should be caring about your domain modeling.

Of course there are cases that this approach can be useful, but in my experience, I’ve only seen this on rare occasions, for example: legacy systems with extremely-simple modeled domains.

For those edge cases, I’ve also heard about: https://github.com/jhartwell/Plsm.

2 Likes

Thanks for your kind info

Depending on what you’re doing, it’s also possible to not have any entity mapping at all. It takes very few lines of code to transform an arbitrary Postgrex result into a list of maps.

1 Like

A schema is not always bind to a database table entirely.

Besides @dimitarvp said, You can enjoy another benifit: A schema can have multiple policies for storing data by defining different changesets.

I think this is a powerful feature.

You can also have more than one schema for a single database table, which is also a very powerful feature.

1 Like