-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix duplicate postgres database connection issue (#2961)
* refactor pg-promise to connect as a singleton * update import path for db functions * fix other imports * couple more spots that needed the change * fix typing * fix tests after merge * remove logs * iron out merge issues * remove unneeded export
- Loading branch information
1 parent
2febfbd
commit 03fb4b4
Showing
7 changed files
with
87 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
containers/ecr-viewer/src/app/api/fhir-data/fhir-data-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,41 @@ | ||
import pgp from "pg-promise"; | ||
|
||
export const db_url = process.env.DATABASE_URL || ""; | ||
export const database = pgp()(db_url); | ||
/** | ||
* Global scope singleton creator for pgPromise | ||
* @async | ||
* @function createSingleton | ||
* @param name A name for your singleton | ||
* @param create Anonymous function containing what you want singleton-ized | ||
* @returns A singleton of the provided object | ||
*/ | ||
const createSingleton = <T>(name: string, create: () => T): T => { | ||
const s = Symbol.for(name); | ||
let scope = (global as any)[s]; | ||
if (!scope) { | ||
scope = { ...create() }; | ||
(global as any)[s] = scope; | ||
} | ||
return scope; | ||
}; | ||
|
||
const pgPromise = pgp(); | ||
const db_url = process.env.DATABASE_URL || ""; | ||
|
||
interface IDatabaseScope { | ||
database: pgp.IDatabase<any>; | ||
pgPromise: pgp.IMain; | ||
} | ||
|
||
/** | ||
* Provides access to pgPromise DB singleton | ||
* @function getDB | ||
* @returns A singleton of the pgPromise DB connection | ||
*/ | ||
export const getDB = (): IDatabaseScope => { | ||
return createSingleton<IDatabaseScope>("my-app-database-space", () => { | ||
return { | ||
database: pgPromise(db_url), | ||
pgPromise, | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters