Skip to content

Commit

Permalink
refactor: prefer pool over single client for pg
Browse files Browse the repository at this point in the history
  • Loading branch information
Battlesquid committed Jul 11, 2024
1 parent 5f28f1d commit d978785
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { sql } from "drizzle-orm";
import { NodePgDatabase, drizzle } from "drizzle-orm/node-postgres";
import { PgColumn } from "drizzle-orm/pg-core";
import { Client } from "pg";
import { Pool } from "pg";
import { config } from "../config";
import * as schema from "./schema";

let DATABASE: NodePgDatabase<typeof schema> | null = null;
let connection: Client | null = null;
let connection: Pool | null = null;

export const getPgConnection = async () => {
export const getPgPool = async () => {
if (connection === null) {
connection = new Client({
connection = new Pool({
host: config.getenv("DB_HOST"),
port: parseInt(config.getenv("DB_PORT")),
user: config.getenv("DB_USER"),
Expand All @@ -23,9 +23,9 @@ export const getPgConnection = async () => {

export const getDatabase = async (): Promise<NodePgDatabase<typeof schema>> => {
if (DATABASE === null) {
const client = await getPgConnection();
await client.connect();
DATABASE = drizzle(client, { schema });
const pool = await getPgPool();
await pool.connect();
DATABASE = drizzle(pool, { schema });
}
return DATABASE;
};
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LogLevel, SapphireClient, container } from "@sapphire/framework";
import { ActivityType, GatewayIntentBits, Partials } from "discord.js";
import { config } from "./config";
import { getDatabase, getPgConnection } from "./db";
import { getDatabase, getPgPool } from "./db";
import { getLoggerInstance } from "./logger";
import { PinoLoggerAdapter } from "./utils/bot";

Expand All @@ -27,19 +27,20 @@ const client = new SapphireClient({

const main = async () => {
container.drizzle = await getDatabase();
container.pg = await getPgConnection();
container.pool = await getPgPool();
await client.login(config.getenv("DISCORD_TOKEN"));
};

const cleanup = () => {
container.pg.end();
container.pool.end();
};

main().catch((error) => {
container.logger.error(error);
});

process.on("uncaughtException", () => {
process.on("uncaughtException", (error) => {
console.error(error);
cleanup();
});

Expand Down
4 changes: 2 additions & 2 deletions src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { getDatabase, getPgConnection } from "./db";
import { getDatabase, getPgPool } from "./db";

(async () => {
console.info("Getting database");
const db = await getDatabase();
console.info("Getting pg connection");
const connection = await getPgConnection();
const connection = await getPgPool();
console.info("Running migration");
await migrate(db, { migrationsFolder: "./drizzle" });
console.info("Closing pg connection");
Expand Down
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "@sapphire/framework";
import { NodePgDatabase } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as schema from "./db/schema";
import { Client } from "pg";

declare module "@sapphire/pieces" {
interface Container {
drizzle: NodePgDatabase<typeof schema>;
pg: Client;
pool: Pool;
}
}

0 comments on commit d978785

Please sign in to comment.