favicon here hometagsblogmicrobio cvtech cvgpg keys

Part 3: So I am building a fullstack project

#mercado-series #rust #typescript #fresh #deno #fullstack

uncomfyhalomacro | 2026-03-10T00:51:55Z | reading time: ~3min

Now back to the progress. I was busy with other personal stuff lately.

I've decided to rework some of the SQL schemas of the tables.

CREATE TABLE users (
    id uuid PRIMARY KEY,
    roles INTEGER[] NOT NULL,
    first_name TEXT DEFAULT NULL,
    last_name TEXT DEFAULT NULL,
    email TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    CONSTRAINT unique_email UNIQUE (email)
);

CREATE TABLE user_passwords (
    id uuid PRIMARY KEY,
    user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    password_hash TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    CONSTRAINT FK_userId FOREIGN KEY (user_id) REFERENCES users(id),
    CONSTRAINT unique_user_id UNIQUE (user_id)
);

CREATE TABLE audit_logs (
    id UUID NOT NULL,
    table_oid OID, -- pertains to the oid of the table in the information schema. aliased to a u32 integer
    entity_id UUID NOT NULL, -- this pertains to the table row's id
    event_type TEXT NOT NULL, -- actions that happened such as adding products in stock. Removing them will remove this row as well
    data JSONB,
    created_at TIMESTAMPTZ DEFAULT now()
);

CREATE TABLE products (
    id UUID PRIMARY KEY,
    seller_id UUID NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    item_type TEXT NOT NULL, -- product, service
    unit_price NUMERIC(12,2),
    currency CHAR(3),
    metadata JSONB, -- extra metadata the seller or us wants to include without changing the table
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now()
);

I already made some migration scripts as well, except for products (I actually have forgotten I didn't implement a migration for products).

Switching over to Nix for Development Environment Management§

I made some changes as well on how I manage my development environment. I am using nix, specifically the one made by https://determinate.systems since they have the best configuration on a macOS environment as far as I know. Since I don't have time to relearn the Nix language, I decided to use what I can remember plus with the help of LLMs like ChatGPT and Gemini.

I also adjusted my shell environment with the help of direnv. Mmfallacy suggested to add some sort of indicator to my shell prompt that I am inside a Nix environment, so I came up with modifying the following lines to my ~/.zshrc.

export PROMPT=$'\$vcs_info_msg_0_%F{010} %B%F{blue}%n%b%f in %F{black}%K{7}<%3~>%k%f %F{yellow}[%y] %F{green}@ %B%U%F{white}%M%u%b %F{red}%T %F{white}%D{%b %d %Y (%A)}
%{\x1b[0m%}%(?.%F{green} .%F{red}%? )%%%F{none} '
export PROMPT='%F{#2685bd}${DIRENV_PROMPT_PREFIX:+$DIRENV_PROMPT_PREFIX}%f'$PROMPT

To get the DIRENV_PROMPT_PREFIX environment variable, I adjusted the project's .envrc that direnv uses

use flake

description=$(nix flake metadata --json | jq '.description' | tr -d '"')
fingerprint=$(nix flake metadata --json | jq '.fingerprint' | tr -d '"' | cut -c1-6)
export DIRENV_PROMPT_PREFIX="❄️(nix-${description}-${fingerprint})
"

This way, direnv will inject new environmental variables to my shell's RC. Now, my prompt looks like the screenshot below.

screenshot of tmux with four split panes with new prompt in the second pane

I am not sure how useful description and fingerprint fields are but I guess that's fine because I can determine which environment using their uniqueness whenever if I have multiple projects managed by flakes in the future.

Because of this, even I tested that it's possible to use PostgreSQL outside homebrew so I uninstalled it and adjusted my justfile now to look like this

set shell := ["bash", "-cu"]
set dotenv-load

serve: serve
test-user-models:
	just migrate-down || true
	just migrate-up || true
	cargo test

migrate-up:
	cargo run --bin up_0001_users || true
	cargo run --bin up_0002_audit_logs

migrate-down:
	cargo run --bin down_0001_users || true
	cargo run --bin down_0002_audit_logs


build-backend:
	cargo build

serve-backend: build-backend
	cargo run

build-frontend:
	cd frontend && deno task build

serve-frontend:
	cd frontend && deno task dev

start-db:
	if [ ! -d "${PGDATA}" ]; then initdb -D ${PGDATA}; fi
	pg_ctl -o "${INIT_DB_OPTIONS}" -D ${PGDATA} start

stop-db:
	pg_ctl -D ${PGDATA} stop

Using Something Similar to Builder Pattern§

I am also using the builder pattern when I am creating new methods for each of my model structs.

For example, I made a separate method for save_to_db because I believe that it should be a separate concern.

As demonstration, here is the Users model

impl Users {
    pub fn new(first_name: Option<String>, last_name: Option<String>, email: String) -> Self {
        Self {
            id: Uuid::new_v4(),
            roles: Vec::new(),
            first_name,
            last_name,
            email,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        }
    }

    pub async fn save_to_db(&self, pool: &Pool) -> Result<Row, PoolError> {
        let client = pool.get().await?;
        let stmt = client.prepare_cached("INSERT INTO users (id, roles, first_name, last_name, email, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *").await?;
        let roles: Vec<i32> = self.roles.iter().map(|&x| x as i32).collect();
        let row = client
            .query_one(
                &stmt,
                &[
                    &self.id,
                    &roles,
                    &self.first_name,
                    &self.last_name,
                    &self.email,
                    &self.created_at,
                    &self.updated_at,
                ],
            )
            .await?;
        Ok(row)
    }

    pub fn hash_password(
        &self,
        plain_text_password: &str,
    ) -> Result<UserPasswords, password_hash::Error> {
        let salt = SaltString::generate(&mut OsRng);
        let password = plain_text_password.as_bytes();
        let argon2 = Argon2::default();
        let password_hash = argon2.hash_password(password, &salt)?.to_string();
        let id = Uuid::new_v4();
        Ok(UserPasswords {
            id,
            user_id: self.id,
            password_hash,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        })
    }

    pub async fn get_user_password_hash(&self, pool: &Pool) -> Result<UserPasswords, PoolError> {
        UserPasswords::from_user_id(pool, &self.id).await
    }
}

And the tests that go alongside it.

// snipped...
    #[tokio::test]
    async fn add_user() {
        dotenv().ok();
        let cfg = Config::from_env().unwrap();
        let pool = cfg.pg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
        assert!(table_exists(&pool, "users").await.unwrap());
        let new_user = Users::new(
            Some("David".to_string()),
            Some("Attenborough".to_string()),
            "contact@david_attenborough.com".to_string(),
        );
        let secure_password = "very_secure";

        new_user.save_to_db(&pool).await.unwrap();
        let password = new_user.hash_password(secure_password).unwrap();
        assert!(password.save_to_db(&pool).await.is_ok());
    }

    #[tokio::test]
    async fn verify_password() {
        dotenv().ok();
        let cfg = Config::from_env().unwrap();
        let pool = cfg.pg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
        assert!(table_exists(&pool, "users").await.unwrap());
        let new_user = Users::new(
            Some("Fred".to_string()),
            Some("Adila".to_string()),
            "contact@fredadila.com".to_string(),
        );
        let secure_password = "very_secure";

        new_user.save_to_db(&pool).await.unwrap();
        let password = new_user.hash_password(secure_password).unwrap();
        assert!(password.save_to_db(&pool).await.is_ok());
        assert!(password.verify_password("incorrect").is_err());
        assert!(password.verify_password("very_secure").is_ok());
    }
// snipped...

That means if we ever write something from initializing new data to saving it to a database, it would look like

let password_plain = "verysecure_password";
let user = Users::new(somedata);  // somedata is just placeholder
let user_password = user.hash_password(password_plain);
user.save_to_db(&pool).await.unwrap();
user_password.save_to_db(&pool).await.unwrap();

I believe I can wrap deadpool_postgres::Pool with a Mutex or Arc because I feel like I am repeating myself whenever I am passing a Pool to the method.

The reason that I am against it is because of the nature of the struct fields themselves.

They are clearly mirrored to the SQL schema that I made so if I will add a pool field, it would lose its representation and that will likely not communicate well with the model I made.

Hence, I am leaving it at that, to ensure they don't lose what they represent. But I think I can wrap the pool outside the structs themselves.

What's next§

I will continue adding the migration logic for products and proceed building the auth crate. The auth crate should have the following

  • JWT attestation logic
  • Basic Auth logic
  • OAuth2.0 logic

That's all for now. I'll go continue reading a new manhwa again. Binging never ends.

Articles from blogs I follow around the net

[WFD 36] the illusion of best practices

software engineering is full of rules that people follow without thinking. most of them are wrong for you.

via Ryana May Que — Writings for DiscussionMarch 09, 2026

Recently

The snow has been tough for my running schedule in February but it's starting to clear and temperatures have started to lift. Yesterday got in a solid 45 miles of cycling, including up to this point near the George Washington Bridge, and back on the Tappan…

via macwright.comMarch 01, 2026

Cryptography Engineering Has An Intrinsic Duty of Care

To understand my point, I need to first explain three different cryptography attack papers / blog posts. I promise this won’t be boring. Three Little Disclosures Misuse-Prone Ciphers For All In a blog post titled Carelessness versus craftsmanship in crypto…

via Dhole MomentsFebruary 25, 2026

What’s That String? That Time a Weird String Revealed a Whole Operation

How it felt to work on this post. Shikanoko Nokonoko Koshitantan is written by Takashi Aoshima and published by Wit Studio. It all started with a slack message from boB Rudis: “Hey, I keep seeing this string. Any ideas?” d2=%3D%3DQXisTKpcCd4RnLsF3ckN3LlR…

via GreyNoise LabsFebruary 24, 2026

Designing Odin's Casting Syntax

Odin;s declaration syntax becomes second nature to everyone who uses the language but I do sometimes get asked ;Why are there two ways to do type conversions?; Enough that I had to make an FAQ entry..The reason that there are two ways to do type conversio…

via gingerBill - ArticlesFebruary 23, 2026

Status update, February 2026

Hi all! Lars has contributed an implementation independent test suite for the scfg configuration file format. This is quite nice for implementors, they get a base test suite for free. I’ve added support for it for libscfg, the C implementation. I’ve spent …

via emersionFebruary 21, 2026

Investigating the SuperNote Notebook Format

I'm a big fan of eink tablets. I read a lot, I write a lot, I prefer handwritten notes, it's a match made in heaven. I've been using a Kindle Scribe for the past several years - I probably used it as much or more than my phone. Recently, I upgraded to a Su…

via Cracking the ShellFebruary 20, 2026

Luxe, ocaml et volupté

Luxe, ocaml et volupté by Clément Delafargue on February 16, 2026 Tagged as: ocaml. After a couple years using rust as my primary language, I’ve got a new job where I’m using a variety of languages (including rust and typescript), but mostly go 1. So…

via Clément Delafargue - RSS feedFebruary 16, 2026

How To Add DRM To Your Backend (easy) [2026 WORKING]

How KineMaster stopped some modded clients from accessing their asset market

via maia blogFebruary 14, 2026

Push comes to shove tools

Your tools are extensions of your skills

via Ishan WritesFebruary 09, 2026

The cults of TDD and GenAI

I’ve gotten a lot of flack throughout my career over my disdain towards test-driven development (TDD). I have met a lot of people who swear by it! And, I have also met a lot of people who insisted that I adopt it, too, often with the implied threat of appe…

via Drew DeVault's blogJanuary 29, 2026

2025 in review

Come along with me as I review the past year. Heh, I often start these kinds of posts right at the start of the year, but it takes a few weeks longer than I ever expect to think them through.1 Two years of being independent After a second year of operati…

via seanmonstarJanuary 27, 2026

The Birthday Paradox, simulated

I'm a fan of simulating counterintuitive statistics. I recently did this with the Monty Hall problem and I really enjoyed how it turned out. A similarly interesting statistical puzzle is the birthday paradox: you only need to get 23 people in a room a room…

via pcloadletterJanuary 23, 2026

Merry Christmas, Ya Filthy Animals (2025)

It’s my last day of writing for the year, so I’m going to try keep this one quick – it was knocked out over three hours, so I hope you can forgive me if it’s a bit clumsier than my usual writing. For some strange reason, one of the few clear memories I hav…

via LudicityDecember 27, 2025

Why are people migrating away from GitHub?

I noticed some people migrating away from GitHub recently. I was curious to understand the rationale. Is it a blip or is it a sign of prolonged exodus?

via Rob O'Leary | BlogDecember 22, 2025

Yep, Passkeys Still Have Problems

It's now late into 2025, and just over a year since I wrote my last post on Passkeys. The prevailing dialogue that I see from thought leaders is "addressing common misconceptions" around Passkeys, the implication being that "you just don't understand it co…

via Firstyear's blog-a-logDecember 17, 2025

Hacking the World Poker Tour: Inside ClubWPT Gold’s Back Office

In June, 2025, Shubs Shah and I discovered a vulnerability in the online poker website ClubWPT Gold which would have allowed an attacker to fully access the core back office application that is used for all administrative site functionality.

via Blog | Sam CurryOctober 12, 2025

Testing multiple versions of Python in parallel

Daniel Roy Greenfeld wrote about how to test your code for multiple versions of Python using `uv`. I follow up with a small improvement to the Makefile.

via Technically PersonalJuly 21, 2025

Generated by openring-rs

favicon here hometagsblogmicrobio cvtech cvgpg keys