<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mubarak Muhammed on Medium]]></title>
        <description><![CDATA[Stories by Mubarak Muhammed on Medium]]></description>
        <link>https://medium.com/@mubarakoladele98?source=rss-91b7de76669d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XVBW4zovs-ZeIS9eZljRVg.jpeg</url>
            <title>Stories by Mubarak Muhammed on Medium</title>
            <link>https://medium.com/@mubarakoladele98?source=rss-91b7de76669d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:15:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mubarakoladele98/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rebuilding My SaaS Backend with NestJS 12, Day 2: One Database, Many Tailor Shops]]></title>
            <link>https://medium.com/@mubarakoladele98/rebuilding-my-saas-backend-with-nestjs-12-day-2-one-database-many-tailor-shops-f0dd2dd1c6d5?source=rss-91b7de76669d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0dd2dd1c6d5</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[nestjs]]></category>
            <dc:creator><![CDATA[Mubarak Muhammed]]></dc:creator>
            <pubDate>Wed, 25 Nov 2026 16:08:11 GMT</pubDate>
            <atom:updated>2026-11-25T16:08:11.905Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Connecting Drizzle, a health check that actually checks, and my first real system design decision: multi-tenancy.</em></p><p><em>Part 2 of a build-in-public series: rebuilding Smart Tailor, a tailor management SaaS, from a Next.js app into a dedicated NestJS backend.</em></p><p><strong>Where we left off</strong></p><p>On Day 0, I set up the project, ran Postgres in Docker, and made the app refuse to start if its configuration was wrong.</p><p>Today has two halves. The first is practical: connect the app to the database and prove the connection works. The second is the first big design question of the project: how does one backend serve many tailor shops without ever mixing up their data?</p><p>But first, a reader made my Day 0 code better.</p><p><strong>A reader caught two things</strong></p><p>In Day 0, I admitted a mistake: I built validated config, then still started the server with the raw `process.env.PORT`. My fix was a rule: outside the config folder, nothing touches `process.env` directly.</p><p>A reader left a comment that improved this in two ways.</p><p><strong>First, a rule you have to remember is just a habit. </strong>He<strong> </strong>suggested making it mechanical with a lint rule. So now the linter fails the check if any file reads `process.env`, except inside the config folder, where reading it is the whole point. The next time I slip, I don’t have to catch it by rereading my code. The tooling catches it for me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mMPHn8H0sip01YQUqlLQVQ.png" /></figure><p><strong>Second, there was a bug in my schema that I hadn’t noticed.</strong> My port field converted the string to a number and fell back to 3001 if it was missing. But “missing” and “empty” are not the same thing. A `.env` file with a blank `PORT=` line gives an empty string, not a missing value, so the default never kicks in. And an empty string converts to the number `0`.</p><p>Port 0 doesn’t fail. It tells Node to pick any random free port. So the app would start without an error, on a port nobody knows, which is exactly the kind of quiet failure my config validation was meant to prevent.</p><p>The fix is one line:</p><blockquote>PORT: z.coerce.number().int().min(1).max(65535).default(3001),</blockquote><p>Now the port has to be a whole number in the valid range. A blank value fails at startup, loudly, the way it should.</p><p>Thank you to that reader. This is exactly why I’m building in public.</p><p><strong>Connecting Drizzle the NestJS way</strong></p><p>I use Drizzle as my ORM. NestJS 12 has a first-party Drizzle module, so connecting it took a few lines in the root module and no custom provider code.</p><p>The detail I care about is <em>where the database URL comes from.</em> I didn’t read it from `process.env`. The Drizzle module waits for the config service and asks it for the URL. That’s the same validated config from Day 0, so the database connection can never start with a missing or malformed URL.</p><p>That’s the Day 0 rule in action, and thanks to the lint check, it’s no longer something I have to remember.</p><p><strong>A health check that tells the truth</strong></p><p>Most health endpoints I’ve seen look like this: a route that returns `{ status: “ok” }`. It proves the server is running. It doesn’t prove the app can <em>*do*</em> anything.</p><p>If the database goes down, that endpoint still happily says “ok”, while every real request fails. A load balancer or monitoring tool trusting it would keep sending traffic to a broken server.</p><p>So my health check actually talks to the database:</p><blockquote>async check() {</blockquote><blockquote>await this.db.execute(sql`select 1`);</blockquote><blockquote>return { status: ‘ok’, db: ‘up’, timestamp: new Date() };</blockquote><blockquote>}</blockquote><p>`select 1` is the cheapest query Postgres can answer. It reads no tables and touches no data. But if it succeeds, I know the connection is alive and the database is responding.</p><p>I also deleted the “Hello World” controller and service that came with the scaffold. They tested nothing real, and leaving starter code around makes a project harder to read. The health module replaces them as the first real feature, with its own test.</p><p><strong>What’s not perfect yet</strong></p><p>When the database is down, the query throws, and NestJS turns any unhandled error into a `500 Internal Server Error`. That works, but it’s not quite the right signal. The proper answer is `503 Service Unavailable`: “I’m alive, but I can’t serve you right now.” Monitoring tools treat those differently.</p><p>There’s also no timeout. If the database hangs instead of failing, the health check hangs with it. Both are on my list for when I deploy for real.</p><p><strong>The big question: one backend, many tailor shops</strong></p><p>Smart Tailor is meant to be a SaaS. Many tailor shops sign up, and each one manages its own customers, measurements and orders on the same platform. In SaaS language, each shop is a <strong>tenant</strong>, and this setup is called <strong>multi-tenancy.</strong></p><p>The hard requirement is simple to say: <strong>**one shop must never see another shop’s data.**</strong> A customer’s measurements and phone number leaking to a competitor would end the product.</p><p>There are three common ways to build this.</p><p><strong>1. A separate database per shop.</strong> The strongest isolation you can get. But every new shop means a new database to create, migrate, back up and monitor. With a hundred shops, one schema change becomes a hundred migrations.</p><p><strong>2. A separate schema per shop, in one database.</strong> Postgres lets you have many schemas (think folders of tables) in a single database. Isolation is still good, but you still have to run every migration once per shop, and tooling gets awkward fast.</p><p><strong>3. Shared tables with a tenant_id column.</strong> Every shop’s data lives in the same tables, and every row is labeled with the shop it belongs to. One database, one migration, and adding a shop is just inserting a row.</p><p><strong>My decision</strong></p><p>I went with <strong>option 3: shared tables with a tenant_id</strong>.</p><figure><img alt="One database, many tailor shops" src="https://cdn-images-1.medium.com/proxy/1*CWpFMU4tgUk_Qx-l8AfRLg.png" /><figcaption>One database, many tailor shops</figcaption></figure><p>It’s the most common choice for SaaS products, and for a product that hasn’t launched yet it’s the simplest one that’s still safe. I don’t have the operations team that databases per tenant would need, and I don’t have the scale that would justify it.</p><p>But option 3 has one real danger, and it’s worth saying out loud: <strong>isolation depends on every query remembering to filter by tenant.</strong> Forget one `where tenant_id = …` and you’ve leaked data across shops. The database won’t stop you.</p><p>That’s the requirement → options → tradeoff → decision shape I’m trying to practice. The tradeoff here is clear: I’m trading the database’s built-in isolation for simplicity, so I have to guard that weakness some other way.</p><p><strong>Guarding the weak spot: the repository pattern</strong></p><p>I’m guarding it with a layer whose whole job is data access.</p><p>Every business feature will be split three ways:</p><p>- The <strong>controller</strong> handles HTTP: routes, request and response.</p><p>- The <strong>service</strong> holds the business logic: what happens when an order is created, what rules apply.</p><p>- The <strong>repository</strong> is the <em>only</em> place that talks to the database.</p><p>Because repositories are the only code that runs queries, they’re also the one place that adds the tenant filter. A service asks for “this shop’s customers”. It can’t forget the filter, because it never writes the query.</p><p>A few rules come with it. The tenant id comes from the logged-in user, never from the request body, because anything in a request body can be faked. Every business table gets a required <em>tenant_id</em> column. Indexes start with <em>tenant_id</em>, since almost every query will filter by it first.</p><p>This layering also matches what I’ll meet in Java and Spring Boot later: `@RestController`, `@Service`, `@Repository`. Same idea, different language.</p><p><strong>And later: a safety net in the database itself</strong></p><p>Postgres has a feature called <strong>Row-Level Security</strong>. It lets the database itself enforce “this connection may only see rows for tenant X”, even if application code forgets the filter.</p><p>I plan to add it later as a second layer of defense, and I’m being honest that I haven’t learned it properly yet. When I do, it gets its own post. For now, the repository layer is the guard, and I know exactly where it lives.</p><p><strong>Key takeaways</strong></p><p>1. <strong>Turn rules into checks.</strong> A habit fails the day you’re tired; a lint rule doesn’t. And “missing” and “empty” are different values, so validate both.</p><p>2. <strong>Wire your database through validated config. </strong>The connection should never see an unchecked value.</p><p>3. <strong>A health check should check something real. </strong>“The server is running” and “the app works” are different claims.</p><p>4. <strong>Multi-tenancy is a tradeoff, not a default.</strong> Shared tables are simple, but you take responsibility for isolation.</p><p>5. <strong>Put the risky rule in one place. </strong>If every query must filter by tenant, make one layer own every query.</p><p>6. <strong>Add patterns when a feature needs them, not before.</strong></p><p><strong>## Next up</strong></p><p>The first real feature: tenants and customers. That means the first Drizzle schema, the first migration, and the first repository that filters by shop.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0dd2dd1c6d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rebuilding My SaaS Backend with NestJS 12, Day 0: Config That Refuses to Lie]]></title>
            <link>https://medium.com/@mubarakoladele98/rebuilding-my-saas-backend-with-nestjs-12-day-0-config-that-refuses-to-lie-538dac16575b?source=rss-91b7de76669d------2</link>
            <guid isPermaLink="false">https://medium.com/p/538dac16575b</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nestjs]]></category>
            <dc:creator><![CDATA[Mubarak Muhammed]]></dc:creator>
            <pubDate>Wed, 18 Nov 2026 15:03:11 GMT</pubDate>
            <atom:updated>2026-11-18T15:03:11.261Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Why I’m doing this</strong></p><p>Smart Tailor started as a Next.js app, and API routes inside a frontend framework got me a working first version fast. But what I’m planning next (orders, measurements, customers, and eventually many tailor shops sharing one platform) needs a real backend. That means clear module boundaries, dependency injection, proper validation, and tests I can trust.</p><p>I’d rather build that foundation now, while there are no users depending on it, than bolt it on later.</p><p><strong>The stack</strong>: NestJS 12, PostgreSQL 17 running in Docker, Drizzle ORM, Zod, and strict TypeScript.</p><p>Day 0 has no features. It’s about getting three things right before writing any business logic: the project setup, the database, and the configuration.</p><p><strong>NestJS 12 is not the NestJS in most tutorials</strong></p><p>The first surprise came from the generated project itself. NestJS 12 has changed a lot from the version most tutorials teach.</p><p>It’s ESM only, so relative imports end in `.js` even though the files are TypeScript. Tests run on Vitest instead of Jest. Linting uses oxlint instead of ESLint. Strict TypeScript is on from the start.</p><p>None of this is hard, but it trips you up if you copy from an older tutorial line by line. My first lesson was simple: before following any guide, look at what your version actually generated.</p><p><strong>Postgres in Docker, and why “started” isn’t “ready”</strong></p><p>I run the app directly on my machine, and only the database runs in a container. That keeps my development loop fast while giving me a real Postgres that I can wipe and recreate in seconds.</p><p>Two things I learned while setting it up.</p><p>First, a container being <em>*started*</em> doesn’t mean the database is <em>*ready*</em>. Postgres takes a moment to accept connections. Adding a healthcheck means Docker only reports the container as healthy once the database can actually take a query, and anything that depends on it can wait for that signal instead of guessing.</p><p>Second, the username, password and database name you set in the Compose file only apply once: the first time the container starts with an empty data volume. Change them later and nothing happens. That cost me some confused minutes before I understood it. To apply new values, you have to remove the volume and start fresh.</p><p><strong>What about Redis?</strong></p><p>Almost every backend tutorial adds Redis on day one. I didn’t.</p><p>I have no background jobs, nothing slow enough to be worth caching, and a single server. Redis would be one more thing to run, monitor and reason about, and it would solve none of my actual problems today.</p><p>This is how I want to make every infrastructure decision in this project: start from a real requirement, look at the options, weigh the tradeoff, then decide. When something slow shows up, such as sending emails or generating reports, a queue will earn its place. Until then: you aren’t gonna need it.</p><p><strong>Config that refuses to start when it’s wrong</strong></p><p>This is the part I’m happiest with.</p><p>Every app depends on environment variables: the port, the database URL, the environment name. The worst failure is a missing or malformed one that doesn’t crash at startup, but quietly breaks something an hour later, in production, on the first real request.</p><p>So the app checks its whole configuration the moment it boots. If anything is missing or invalid, it refuses to start and tells me exactly what’s wrong.</p><p>In my previous NestJS project I did this with Joi. This time I used Zod:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*XMKGNuh881oAW7GV41llrw.png" /></figure><p>The reason I switched is that last line. With Joi, I wrote the schema <em>*and*</em> a separate TypeScript type describing the same thing, and nothing kept the two in sync. With Zod, the type is inferred from the schema. One source of truth: if I add a variable to the schema, the type updates itself.</p><p>A few details that matter:</p><p>- Every field is required unless I say otherwise. No database URL, no startup.</p><p>- Environment variables are always strings, so the port gets converted from ”3001&quot; to the number “3001” during validation.</p><ul><li>NestJS 12’s config module accepts a Zod schema directly, so there’s no glue code to write.</li></ul><p><strong># A mistake I made</strong></p><p>After building all of this, I made the app read its config through the validated config service in the entry file, and then still started the server with the raw `process.env.PORT`, falling back to 3000.</p><p>The validation existed. My code just went around it.</p><p>Nothing warned me. The type checker was happy, because `process.env.PORT` is perfectly valid TypeScript. I only caught it by reading my own code again. Validation only protects you if every part of the app reads config through it.</p><p>My rule now: outside the config folder, nothing touches `process.env` directly. Everything goes through the typed config service.</p><p><strong>Key takeaways</strong></p><p>1. <strong>Check the version you’re actually running.</strong> NestJS 12 is ESM-first and changes many defaults that older tutorials assume.</p><p>2. <strong>”Started” isn’t “ready.”</strong> Use health checks so nothing talks to a database that can’t answer yet.</p><p>3. <strong>Add infrastructure for a real requirement, not because every tutorial does.</strong></p><p>4. <strong>Make config fail loudly at startup. </strong>One Zod schema gives you validation and types together.</p><p>5. <strong>Validation only works if nothing goes around it.</strong></p><p><strong>Next up</strong></p><p>Connecting Drizzle to NestJS and building a health endpoint that actually queries the database. After that comes the first big design decision of the project: how one backend serves many tailor shops while keeping their data apart.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=538dac16575b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>