Skip to content

Commit

Permalink
allow conecting to database that does not support ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Nov 29, 2024
1 parent f5ffaac commit 7d2525b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ export * from './components.js'
export type PrismaTransaction = Omit<PrismaClient, ITXClientDenyList>

let connectionString: string | null = null
let ssl: { rejectUnauthorized: boolean; ca?: string } | null = null
let ssl: { rejectUnauthorized: boolean; ca?: string } | null | undefined
let singleton: PrismaClient | null = null
export const init = (
_connectionString: string,
rejectUnauthorized: boolean,
ca: string | null
) => {
connectionString = _connectionString
ssl = {
rejectUnauthorized,
ca: ca ?? undefined,
if (rejectUnauthorized && ca === null) {
// these are the default values, so the same as not providing them
ssl = null
} else {
ssl = {
rejectUnauthorized,
ca: ca ?? undefined,
}
}
singleton = new PrismaClient({ datasourceUrl: connectionString })
}
Expand Down Expand Up @@ -73,16 +78,19 @@ export async function getPGInstance(): Promise<{
pubSubClient: pg.Client
pool: pg.Pool
}> {
if (!connectionString || !ssl) {
if (!connectionString || ssl === undefined) {
throw new Error(`Access db before calling init()`)
}

if (pgInstance) {
return pgInstance
}

const pgPool = new pg.Pool({ connectionString, ssl })
const pubSubClient = new pg.Client({ connectionString, ssl })
const pgPool = new pg.Pool({ connectionString, ssl: ssl ?? undefined })
const pubSubClient = new pg.Client({
connectionString,
ssl: ssl ?? undefined,
})
await pubSubClient.connect()

pubSubClient.on('notification', (notification) => {
Expand Down

0 comments on commit 7d2525b

Please sign in to comment.