Building an API with TypeScript, Bun.js, Hono, Drizzle, and Turso πŸš€

Axier PerlazaAxier Perlaza
3 min read

Introduction 🌟

Hey there, fellow developers! πŸ‘‹
For the past couple of days, I’ve been working on building an API using TypeScript along with some exciting technologies:

  • Bun.js 🏎️ – A fast JavaScript runtime, an alternative to Node.js

  • Hono.js ⚑ – A lightweight and efficient web framework, replacing Express

  • Drizzle ORM πŸ“Š – A modern and type-safe ORM for database interactions

  • Turso ☁️ – A high-performance SQL database hosting solution

This has been a rewarding experience, and I’d love to share my progress so far.

Why This Stack? πŸ€”

I chose these technologies because:

βœ… Bun.js is extremely fast compared to Node.js. It significantly improves execution speed and reduces startup time.
βœ… Hono.js is lightweight and perfect for building APIs without the overhead of Express.
βœ… Drizzle ORM provides type safety and better database schema management.
βœ… Turso offers a powerful SQL database with serverless hosting capabilities.

My Progress So Far 🎯

I’ve made great progress in key areas:

βœ… JWT Authentication – Implemented token-based authentication
βœ… Password Hashing – Ensured security with password encryption
βœ… Project Structuring – Organized the project following best practices

πŸ”Ή Project Structure

To keep the code clean and maintainable, I followed this folder structure:

/src
  β”œβ”€β”€ database
  β”‚   β”œβ”€β”€ queries
  β”‚   β”‚   β”œβ”€β”€ insert.query.ts
  β”‚   β”‚   β”œβ”€β”€ select.query.ts
  β”‚   β”‚   β”œβ”€β”€ index.ts
  β”‚   β”‚   └── schema.ts
  β”œβ”€β”€ routes
  β”‚   β”œβ”€β”€ auth.route.ts
  β”‚   └── post.route.ts
  β”œβ”€β”€ schemasValidator
  β”‚   └── schema.validator.ts
  β”œβ”€β”€ utils
  β”‚   β”œβ”€β”€ config.utils.ts
  β”‚   β”œβ”€β”€ hashing.utils.ts
  β”‚   β”œβ”€β”€ jwt.util.ts
  β”‚   └── index.ts
.env
.gitignore
bun.lock
LICENSE
README.md
tsconfig.json
package.json
drizzle.config.ts

Code Snippets πŸ› οΈ

πŸ” JWT Authentication with Hono.js

import { sign } from "hono/jwt";
import { JWT_SECRET } from "../utils/config.utils";

export async function jwtGenerate(id:number, username:string, email:string, password:string){
    const payload = {
        sub: username,
        id:id,
        email: email,
        password: password,
        exp: Math.floor(Date.now()/1000) + 60 * 5, // if you want expire in one day use Math.floor(Date.now()/1000) + 60 * 60 * 24 
    }

    if (JWT_SECRET === 'YOUR JWT_SECRET') {
        throw new Error('You must set the JWT_SECRET env variable')
    }

    const token = await sign(payload, JWT_SECRET) 
    return token
}

πŸ”‘ Hashing Passwords


export async function hashingPassword(pass: string){
    const hashedPassword = await Bun.password.hash(pass);
    return hashedPassword
}

export async function matchHashedPassword(pass: string, hashPass: string){
    const isMatch = await Bun.password.verify(pass, hashPass);
    return isMatch
}

Open Source Contribution 🌍

I’ll be sharing the GitHub repository, so if you’re interested in:

  • Checking out the code

  • Suggesting improvements

  • Contributing to the project

You’re more than welcome! πŸš€

Next Steps πŸš€

In the next updates, I plan to:

  • Implement role-based authorization (RBAC)

  • Add better error handling and logging

  • Optimize database queries using indexed searches in Turso

  • Deploy the API for public use

Let’s Connect! πŸ’¬

πŸ’‘ Got any suggestions or ideas? Drop them in the comments! Let’s build something great together.

Also, follow this blog for updates as I continue refining the API and adding new features! πŸš€

Social Media 🌐

Let’s stay connected! You can find me on:

  • LinkedIn

  • GitHub

  • X

βš™οΈ Api-Rest-TypeScript

Readme Card

Feel free to reach out or follow me for more updates and content!

2
Subscribe to my newsletter

Read articles from Axier Perlaza directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Axier Perlaza
Axier Perlaza

Hey there! I'm a passionate Software Developer who loves turning ideas into reality through code. With a strong enthusiasm for problem-solving and innovation, I thrive in the world of programming. I enjoy working with multiple languages, including Python, JavaScript, TypeScript, and Go (Golang), allowing me to build versatile and scalable applications. Whether it's crafting dynamic web applications, automating workflows, or optimizing system performance, I'm always eager to explore new possibilities. When it comes to data, I have hands-on experience with SQL and NoSQL databases, ensuring that applications run efficiently and handle data seamlessly. Beyond coding, I love sharing knowledge, collaborating with other developers, and continuously learning to stay ahead in the ever-evolving tech landscape. Feel free to connect with me let’s build something awesome together!