-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from brunofamiliar/main
feat: Convert database factory in singleton
- Loading branch information
Showing
3 changed files
with
45 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,40 @@ | ||
|
||
import { Pool } from "pg"; | ||
|
||
export default function DatabaseFactory() { | ||
const poolConfiguration = { | ||
user: process.env.POSTGRES_USER, | ||
host: process.env.POSTGRES_HOST, | ||
database: process.env.POSTGRES_DB, | ||
password: process.env.POSTGRES_PASSWORD, | ||
port: process.env.POSTGRES_PORT, | ||
ssl: { | ||
rejectUnauthorized: false, | ||
}, | ||
}; | ||
|
||
// https://github.com/filipedeschamps/tabnews.com.br/issues/84 | ||
if ( | ||
["test", "development"].includes(process.env.NODE_ENV) || | ||
process.env.CI | ||
) { | ||
delete poolConfiguration.ssl; | ||
} | ||
|
||
const pool = new Pool(poolConfiguration); | ||
|
||
async function query(query) { | ||
const results = await pool.query(query); | ||
return results; | ||
} | ||
|
||
async function getNewConnectedClient() { | ||
// When manually creating a new connection like this, | ||
// you need to make sure to close it afterward | ||
// with the .end() method. | ||
return await pool.connect(); | ||
} | ||
|
||
return { | ||
query, | ||
getNewConnectedClient, | ||
pool, | ||
}; | ||
const poolConfiguration = { | ||
user: process.env.POSTGRES_USER, | ||
host: process.env.POSTGRES_HOST, | ||
database: process.env.POSTGRES_DB, | ||
password: process.env.POSTGRES_PASSWORD, | ||
port: process.env.POSTGRES_PORT, | ||
ssl: { | ||
rejectUnauthorized: false, | ||
}, | ||
}; | ||
|
||
// https://github.com/filipedeschamps/tabnews.com.br/issues/84 | ||
if (["test", "development"].includes(process.env.NODE_ENV) || process.env.CI) { | ||
delete poolConfiguration.ssl; | ||
} | ||
|
||
const pool = new Pool(poolConfiguration); | ||
|
||
async function query(query, params) { | ||
return await pool.query(query, params);; | ||
} | ||
|
||
async function getNewConnectedClient() { | ||
// When manually creating a new connection like this, | ||
// you need to make sure to close it afterward | ||
// with the .end() method. | ||
return await pool.connect(); | ||
} | ||
|
||
const instance = { | ||
query, | ||
getNewConnectedClient, | ||
} | ||
|
||
Object.freeze(instance) | ||
|
||
export default instance; |
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