WammKD

WammKD

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.

Showing Posts 1 to 9

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 OP

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 OP

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

WammKD

WammKD OP

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 OP

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? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews