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')
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
npx esbuild db.ts --platform=node --bundle --log-level=warning --outfile=db.js
Then ran it like this:
node db.js
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
create pool
create db
main
list users
after main
[]
after list users
Any ideas what I'm doing wrong?
4 Replies
Dan
Dan2y ago
You're doing everything correct. Firstly, the order is not wrong, it's how event loop works in JS. When you invoke an async function, it runs asynchronously to the main thread, so console.log('after main') will run in parallel with the main() function execution. Secondly, the program is hanging because the event loop isn't empty. That's because you didn't close the DB connection before exit. You should do it either at the end of the main() function, or in the main().then(...) callback, so that it's invoked after the function execution is completed. Also, ideally you should wrap your main() function invocation in a try-catch block, or add a .catch() clause, to avoid uncaught promise rejections.
JT
JTOP2y ago
wow i'm a dolt thank you
Dan
Dan2y ago
np!
JT
JTOP2y ago
yeah this is just a test, not an actual app so i was trying to get quick and dirty success
Want results from more Discord servers?
Add your server