Skip to content

Commit

Permalink
Merge branch 'main' into separate-benchmark-assignment-modal-from-trpc
Browse files Browse the repository at this point in the history
  • Loading branch information
canjalal committed Dec 19, 2024
2 parents 3ac8514 + f25a333 commit 4f45f88
Show file tree
Hide file tree
Showing 22 changed files with 1,956 additions and 1,064 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
7 changes: 7 additions & 0 deletions src/backend/db/migrations/4_add-created-at-on-tasks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Each task should have a created_at date so that we can sort by assigned date
-- NOTE: This relies on the assigned date never changing; if the app allows changes, we will likely want create a dedicated assigned_on date in addition to (or instead of) this column
ALTER TABLE task
ADD COLUMN created_at TIMESTAMPTZ NOT NULL DEFAULT NOW();

-- Add index to allow easy queries of tasks by date
CREATE INDEX idx_created_at ON task(created_at);
Loading

0 comments on commit 4f45f88

Please sign in to comment.