In modern web development, managing your database efficiently and ensuring optimized connections are crucial for building robust applications. This tutorial will guide you step-by-step through integrating Drizzle ORM into your Node.js project, while also establishing a connection to a Supabase database using pgBounce. By the end of this post, you'll have a clear understanding of setting up a performant database connection, which can improve the efficiency and scalability of your application.
Drizzle ORM is a lightweight and type-safe ORM (Object-Relational Mapping) tool for JavaScript and TypeScript that simplifies database queries. Its flexibility, paired with its type safety, makes it an excellent choice for handling database operations in modern web applications.
Start by creating a new Node.js project. Open your terminal and run the following commands:
# Create a new Node.js project
mkdir drizzle-supabase-pgbounce
cd drizzle-supabase-pgbounce
npm init -y
# Install required dependencies
npm install drizzle-orm pg dotenv
Here’s what each package does:
drizzle-orm
: The ORM that will manage your PostgreSQL database connections and queries.pg
: PostgreSQL client for Node.js.dotenv
: To load environment variables from a .env
file, which will store your database credentials securely.Before proceeding, you'll need to set up a Supabase account if you haven't already. After creating a project in Supabase, you'll have access to the necessary connection details.
In the Supabase dashboard, enable pgBounce by navigating to the database settings and enabling it under the connection pooler section. Once enabled, make note of your connection string for pgBounce.
For security purposes, you'll store your database credentials in a .env
file. Create this file in your project root directory:
touch .env
Add the following lines to your .env
file, replacing the placeholders with your actual Supabase connection details:
# .env
SUPABASE_URL=<your-supabase-url>
SUPABASE_PASSWORD=<your-supabase-password>
SUPABASE_USER=<your-supabase-user>
SUPABASE_DATABASE=<your-database-name>
SUPABASE_PORT=<your-database-port>
Now, let's configure Drizzle ORM and connect it to Supabase via pgBounce. Create a new db.js
file to handle the database connection.
// db.js
require('dotenv').config();
const { Pool } = require('pg');
const { drizzle } = require('drizzle-orm');
// Setup the connection pool for pgBounce
const pool = new Pool({
host: process.env.SUPABASE_URL,
port: process.env.SUPABASE_PORT,
user: process.env.SUPABASE_USER,
password: process.env.SUPABASE_PASSWORD,
database: process.env.SUPABASE_DATABASE,
ssl: { rejectUnauthorized: false } // For SSL connections
});
// Initialize Drizzle ORM
const db = drizzle(pool);
module.exports = db;
Here’s a breakdown of the code:
Pool
: This comes from the pg
package and is responsible for managing the connections to the Supabase database, passing through pgBounce.drizzle(pool)
: This initializes Drizzle ORM with the connection pool you created.In Drizzle ORM, you define your database structure using models. Let's create a basic User
model.
Create a models.js
file:
// models.js
const { defineTable } = require('drizzle-orm');
// Define the User model
const User = defineTable('users', {
id: 'serial primary key',
name: 'text',
email: 'text unique',
});
module.exports = { User };
This code defines a User
table with id
, name
, and email
fields.
Now that your database and models are set up, let’s write some code to interact with the database. Create a main.js
file and add some basic CRUD operations.
// main.js
const db = require('./db');
const { User } = require('./models');
async function main() {
// Insert a new user
await db.insert(User, { name: 'John Doe', email: 'john@example.com' });
// Fetch all users
const users = await db.select(User);
console.log('All Users:', users);
// Update a user
await db.update(User, { name: 'Jane Doe' }, { where: { id: 1 } });
// Delete a user
await db.delete(User, { where: { id: 1 } });
}
main().catch(console.error);
To run the application, simply execute the following command:
node main.js
Once executed, this script will:
By integrating Drizzle ORM with a Supabase database and using pgBounce, you've set up a powerful, efficient, and optimized connection for your Node.js project. Drizzle ORM's type safety and lightweight nature make it ideal for managing your database, while pgBounce ensures smooth handling of multiple database connections, especially under heavy loads.
Incorporating these tools into your project can significantly enhance both performance and scalability, making them an excellent choice for developers building modern applications.
This page content is most likely AI generated. Use it with caution.