Skip to content

Commit

Permalink
Fix import statements in database scripts
Browse files Browse the repository at this point in the history
Following the updating of dependencies in PR #472
the database scripts in src/backend/db/lib stopped
working. This PR fixes these scripts through
updating the import statements for the pg package.

Co-authored-by: Nick Visutsithiwong <[email protected]>
Co-authored-by: Francis Li <[email protected]>
  • Loading branch information
3 people committed Dec 11, 2024
1 parent bebb8c3 commit 067e79a
Show file tree
Hide file tree
Showing 4 changed files with 1,014 additions and 1,000 deletions.
35 changes: 23 additions & 12 deletions src/backend/db/lib/get-db.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import { Kysely, PostgresDialect } from "kysely";
import { KyselyDatabaseInstance, KyselySchema } from "@/backend/lib";
import { Pool } from "pg";
import pg from "pg";

type DatabaseInstanceAndPool = { db: KyselyDatabaseInstance; pool: Pool };
const { Pool } = pg;

type DatabaseInstanceAndPool = {
db: KyselyDatabaseInstance;
pool: typeof Pool;
};

const databaseUrlToSingleton = new Map<string, DatabaseInstanceAndPool>();
const poolToSingleton = new WeakMap<Pool, DatabaseInstanceAndPool>();
const poolToSingleton = new WeakMap<typeof Pool, DatabaseInstanceAndPool>();

type GetDb = {
(databaseUrl: string): DatabaseInstanceAndPool;
(pool: Pool): DatabaseInstanceAndPool;
(pool: typeof Pool): DatabaseInstanceAndPool;
};

export const getDb: GetDb = (databaseUrlOrPool) => {
if (databaseUrlOrPool instanceof Pool) {
if (!poolToSingleton.has(databaseUrlOrPool)) {
if (!poolToSingleton.has(databaseUrlOrPool as typeof Pool)) {
const db = new Kysely<KyselySchema>({
dialect: new PostgresDialect({
pool: databaseUrlOrPool,
}),
});

const dbAndPool = { db, pool: databaseUrlOrPool };
poolToSingleton.set(databaseUrlOrPool, dbAndPool);
const dbAndPool = {
db,
pool: databaseUrlOrPool,
} as DatabaseInstanceAndPool;
poolToSingleton.set(databaseUrlOrPool as typeof Pool, dbAndPool);
}

return poolToSingleton.get(databaseUrlOrPool)!;
return poolToSingleton.get(databaseUrlOrPool as typeof Pool)!;
}
if (!databaseUrlToSingleton.has(databaseUrlOrPool)) {
if (!databaseUrlToSingleton.has(databaseUrlOrPool as string)) {
const pool = new Pool({
connectionString: databaseUrlOrPool,
connectionString: databaseUrlOrPool as string,
});

const db = new Kysely<KyselySchema>({
Expand All @@ -38,8 +46,11 @@ export const getDb: GetDb = (databaseUrlOrPool) => {
}),
});

databaseUrlToSingleton.set(databaseUrlOrPool, { db, pool });
databaseUrlToSingleton.set(databaseUrlOrPool as string, {
db,
pool: Pool as unknown as typeof Pool,
});
}

return databaseUrlToSingleton.get(databaseUrlOrPool)!;
return databaseUrlToSingleton.get(databaseUrlOrPool as string)!;
};
3 changes: 2 additions & 1 deletion src/backend/db/lib/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { parse } from "pg-connection-string";
import pgConnectionString from "pg-connection-string";
import * as postgresMigrations from "postgres-migrations";
import * as zg from "zapatos/generate";
import path from "node:path";
import { logger } from "@/backend/lib";

const { parse } = pgConnectionString;
interface MigrateOptions {
silent?: boolean;
shouldGenerateTypes?: boolean;
Expand Down
6 changes: 4 additions & 2 deletions src/backend/db/lib/reset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Client } from "pg";
import { parse } from "pg-connection-string";
import client from "pg";
import pgConnectionString from "pg-connection-string";
import { logger } from "@/backend/lib";
import { migrate } from "./migrate";

const { Client } = client;
const { parse } = pgConnectionString;
export const reset = async (databaseUrl: string) => {
const connectionConfig = parse(databaseUrl);

Expand Down
Loading

0 comments on commit 067e79a

Please sign in to comment.