DEV Community

Osazuwa J. Agbonze
Osazuwa J. Agbonze

Posted on

SQLAlchemy with Docker - The Structure

The setup of mushrooms

Image credit to Phoenix Han

I really do not have to say this to get started and do forgive me if you find it appalling, you could also meet me in the second paragraph. But, Ohhh my beer 🍺 !!! I LOVE STRUCTURE. I'm starting with my love for structure because I've ignored this journey for sometime now as I couldn't find a structured way to get started. This really was a pain for me due to my background with ORM ( django ORM to be precise ). The struggle for me was, after I had consumed a getting started resource on sqlalchemy, I couldn't really find a structure to fit it into existing or new project as none of the resource I used could teach me that ( maybe I just couldn't find it, it's a big ocean). I do understand why most resources are like that and every single one deserves their credit honestly but, I do also hope that, with this post, I can show a different approach to getting started by first dealing with my own pain - The Structure ( If you've a different structure, leave a github link in the comment section. I/someone else could learn something - thanks )

If you've not read The Genesis of my Journey to SQLAlchemy, please do.

I could easily just proceed to sqlalchemy installation and how it can be imported into our terminal showing different examples which would portray how simple and flexible it is to use, but no! To begin with, lets define a structure for our codebase in anticipation for the future.

Worthy Of Note:

This journey is solely focused on SQLAlchemy - not flask or any other web framework, we'll keep the focus of our structure to that.

Disclaimer

This structure is by no mean an Opinionated approach on how you should structure your sqlalchemy project or codebase and as such not imposed on anyone as the golden structure. This is my boilerplate for my journey.

There will be three folders in the root directory, so open up VSCode or your favorite IDE/editor and create accordingly.

conf/: This folder will house all application configurations/settings for different environment. There are at least 3 environments for real projects i.e development, staging and production environments. These three environment files/folder can be housed within conf/. We'll leave it as is for now.

db/: This folder will be used to manage database engine initializer and metadata. It will also contain models/ folder when the time comes. We'll see what all of these are as we proceed in this journey.

src/: Application source code will reside here.

main.py: Will be the entry-point into the application.

If you've created the above file and folders, your codebase should look like mine.

Codebase Structure

The importance of structure cannot be overemphasized especially in programming. Just by defining this structure, we already have a head start which you'll see very shortly in this Journey. Let me know your thought on this structure in the comment section.

Thanks for sticking with me on this one. I'm still holding my beer 🍻

Top comments (0)