All blog posts
Article

A Software Engineer's Tips and Tricks #1: Drizzle

4 min

Hey there! At Koyeb, we really like diving into technical stuff. But here’s the thing: not every cool thing we stumble upon or think about needs a massive blog post. And honestly, not everything we’re into is directly related to what Koyeb does or about infrastructure in general.

So, we’ve got an idea: what if we start sharing these bits and pieces with you in a series of really short blog posts? They’ll be about all sorts of things—some related to our work, some just because we think they’re neat.

Here’s the plan: we’ll post something new three times a week, at least for the next few weeks. These posts will be super short reads, just a couple of minutes tops. If you don’t like one of the posts, no problem! Just skip it and check out the next one.

But if you do find something you like, please share it, tweet it, or tell your friends about it. And more importantly, don’t forget to check out the "further reading" links we’ll include at the bottom of each post for more info to dive deeper into the topic.

Sound good? Keep an eye on our blog, and let’s see where this goes!

Drizzle: a lightweight ORM in TypeScript

In the world of software development, there are two kinds of developers: those who have never had to complain about ORMs and those who have actually used them. Whether it’s Django ORM for Python, Active Record for Ruby, GORM for Golang, Doctrine for PHP, or Prisma for TypeScript, a common issue persists: writing simple queries is straightforward, but constructing complex or optimized queries can take hours, if not days.

Enter Drizzle, a lightweight typesafe ORM for TypeScript that comes with one promise: If you know SQL — you know Drizzle.

Here is a quick example to demonstrate how to use Drizzle. First, create a PostgreSQL database (on Koyeb!), and create a table with some data:

koyebdb=> create table users(id serial, name varchar unique);
CREATE TABLE
koyebdb=> insert into users(name) values('nico');
INSERT 0 1
koyebdb=> insert into users(name) values('julien');
INSERT 0 1
koyebdb=> insert into users(name) values('alisdair');
INSERT 0 1
koyebdb=> insert into users(name) values('thomas');
INSERT 0 1
koyebdb=> select * from users;
 id |   name
----+----------
  1 | nico
  2 | julien
  3 | alisdair
  4 | thomas
(4 rows)

Now, let’s install Drizzle:

npm i drizzle-orm @neondatabase/serverless

Finally, let’s open myapp.ts and:

  1. Add the imports
import { neon } from '@neondatabase/serverless'
import { like } from 'drizzle-orm'
import { drizzle } from 'drizzle-orm/neon-http'
import { pgTable, serial, text } from 'drizzle-orm/pg-core'
  1. Declare the database and the table we previously created:
const sql = neon(process.env.NEON_DATABASE_URL!)
const db = drizzle(sql)

const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name'),
})
  1. Fetch the entry from users starting with j:
db.select()
  .from(users)
  .where(like(users.name, 'j%'))
  .execute()
  .then((rows) => {
    console.log(rows)
  })
  1. Finally, run everything (assuming node and tsc are installed):
> export POSTGRES_DATABASE_URL=postgres://<your connection string>
> tsc --skipLibCheck myapp.ts && node myapp.js
// Ouptut: [ { id: 2, name: 'julien' } ]

For your important application running in production, you should consider using a framework (Vite, NextJS) and not replicate this example setup. You probably also want to set up database migrations and use Drizzle Studio to explore your data. The goal of this example is to demonstrate it is possible to have a typesafe ORM, and write queries in the best language that exists to query relational data: SQL!

Further reading:

Drizzle on Koyeb

Koyeb makes your apps Drizzle globally in minutes

Deploy Apps using Drizzle

SIGKILL

That’s it for today! We hope you enjoyed these tips and tricks. If you have any feedback or suggestions for future posts, feel free to reach out to us on Twitter (or X) at @gokoyeb, Koyeb's LinkedIn, or on the Koyeb Community.

Koyeb

Welcome to Koyeb

Koyeb is a developer-friendly serverless platform to deploy any apps globally.

  • Start for free, pay as you grow
  • Deploy your first app in no time
Start for free
The fastest way to deploy applications globally.