Having a problem just connecting with drizzle

Howdy, I just found out about drizzle and thought I might give it a try, but unfortunately I'm having trouble just getting the example working from the documentation.

I've created this sample file:

import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import { mysqlTable,  serial,  text, varchar} from 'drizzle-orm/mysql-core';

const users = mysqlTable('users', {
    id: serial('id').primaryKey(),
    fullName: text('full_name'),
    phone: varchar('phone', { length: 256 }),
});

console.log('create pool');
const poolConnection = mysql.createPool({
    host: 'localhost',
    user: 'user',
    password: 'pass',
    database: 'drizzle',
});

console.log('create db');
export const db = drizzle(poolConnection);
async function main() {
    console.log('list users');
    const allUsers = await db.select().from(users);
    console.log(allUsers);
    console.log('after list users');
    return
}
console.log('main')
main();
console.log('after main')


Then compiled it into JS like this:

npx esbuild db.ts --platform=node --bundle --log-level=warning --outfile=db.js


Then ran it like this:

node db.js


The execution order is wrong, and the program just hangs at the end rather than exiting cleanly.

Here's the output:

create pool
create db
main
list users
after main
[]
after list users


Any ideas what I'm doing wrong?
Was this page helpful?