cknarker
DTDrizzle Team
•Created by Julio Barros on 11/3/2023 in #help
migrate with node-postgres
try this if you still want to use client instead of pool
import { drizzle } from "drizzle-orm/node-postgres";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { Client } from "pg";
// you can substitute this with process.env instead if you're not using bun
const connectionString = Bun.env.DB_CONNECTION_STRING;
if (!connectionString) {
throw new Error("DB_CONNECTION_STRING is missing");
}
const connection = new Client({
connectionString: connectionString,
});
export const db = drizzle(connection);
// this is required otherwise it will hang, nk mentioned this
await connection.connect();
await migrate(db, {
migrationsFolder: "./migrations",
})
.then(() => console.log("Migrations complete"))
.finally(async () => await connection.end());
// use this instead if you cant use promise.finally()
// await connection.end()
import { drizzle } from "drizzle-orm/node-postgres";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { Client } from "pg";
// you can substitute this with process.env instead if you're not using bun
const connectionString = Bun.env.DB_CONNECTION_STRING;
if (!connectionString) {
throw new Error("DB_CONNECTION_STRING is missing");
}
const connection = new Client({
connectionString: connectionString,
});
export const db = drizzle(connection);
// this is required otherwise it will hang, nk mentioned this
await connection.connect();
await migrate(db, {
migrationsFolder: "./migrations",
})
.then(() => console.log("Migrations complete"))
.finally(async () => await connection.end());
// use this instead if you cant use promise.finally()
// await connection.end()
10 replies