WammKD

WammKD

Extract Database Modules (for Phoenix App.) into Their Own Library?

Basically, I’m writing a Phoenix app. but a lot of the database (and the modules which handle INSERTing-into/UPDATEing and retrieving from the database) are stuff that could be reused for similar projects.

I gather that the migrations (the database should look the same, obviously, for the modules to work) would need to be generated and provided via a script that must be run (How to run migrations that are in a library? but Oban is an example given in that conversation). This is fine and makes sense.

Where I’m uncertain of how to do things is to structure the library which would hold the database-handling modules (would said library need to be a Phoenix project? Could it just need to worry about having Ecto as a dependency?) and how to include said library (in another project) such that I could invoke, say, reading data from the library and it’d know to use the database of the other project. Or call one of the functions to write some data to the database and it’d know to use the database of the other project.

This library isn’t complex or doing anything terribly out of the box or crazy. It’s your standard read-Object-A-and-Object-B-from-the-database-and-put-them-into-struct.s/schemas and write-data-to-database-as-Objects-A-and-B.

I just don’t want to have to write the same logic twice and, if two projects are both going to have Object-A and Object-B in their databases, I’d rather have a library I can just include as a dependency and, then, run LibraryObjectAs.list_all_object_a() without having to define that function out in both projects; the dependency takes care of writing the function once, for me, and I just import the library and can use the function seamlessly with my database of my project.

As such, I don’t think something like an umbrella project would work as I’d like for it to be able to be used by others, outside of myself.

I just…don’t have any idea of how I’d set this up.

First 9 of 9 Posts Switch mode

sodapopcan

sodapopcan

I’m not 100% sure I get everything you’re after—are you saying the schemas would also live in the library? If so, off the top of my head I would:

a) I would create a custom mix task in the library that will generate the appropriate migrations into your app.
b) Add a config option to your library to set the Repo it uses: config :my_library, :repo, MyApp.Repo.

In terms of keeping schemas in a library, I could see you running into problems where you’d want associations from your app on the schemas in your library, maybe. You could possibly run into some other issues too :thinking: But anyway, this is one way to do it!

WammKD

WammKD

I was thinking of keeping schemas in the library (at least, for those tables which are stemming from the library). While you can, obviously, do some creative stuff with the schemas, I’ve mostly been using them to map a module to the database table. Keeping the schemas (that are supposed to map to predefined DB structures) in the project, rather than the library, would mean needing to recreate the same schema in any new project that relies on the library (so goes my reasoning, at least).

Now, I may be misusing or misunderstanding something about how these things work but I’ve created associations, so far, by invoking the module that contains a schema (that I want to associate to); couldn’t I just reference the modules in the library that have the schema I wanted (e.g. something like belongs_to(:object_a, Library.ObjectA, foreign_key: :id, references: :object_a_id)) from within my project (which has the library as a dependency)?

Ah! There we go; I’d stumbled upon a few Elixir/Phoenix projects that really underscored the flexibility available in similar manners as this but help if I could remember where I’d seen them, again, now that I needed to implement something similar. Thanks so much for spelling this out so clearly.

sodapopcan

sodapopcan

Yes for sure, I was just saying if you found yourself needing the inverse association (has_many :object_bs, MyApp.ObjectB) then it’s going to start getting a little messy. Maybe you won’t, just something to be aware of!

WammKD

WammKD

Ahhh; I gotcha. I don’t think I will but a fair point!

WammKD

WammKD

Oh! One more question (if you have an idea): what would be the best method to generate the initial project structure of the library? I imagine I won’t need the Phoenix dependencies? I could, of course, setup all the files by hand (I expect they’ll – largely – be similar to the files as they exist in my current project) but I figured a mix command would be smarter in case I don’t think of anything or miss something, as is often the case with manual approaches.

sodapopcan

sodapopcan

Just $ mix new my_lib. You’ll need at least ecto as a dep, possibly ecto_sql.

WammKD

WammKD

Mmm; makes sense. Thanks – so much –, again!

mayel

mayel

You might find this library useful for those cases: Exto — exto v0.4.0

sodapopcan

sodapopcan

Well I’ll be… cool!

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement