Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] add ability to enable SSL for database connection #119

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/common-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add ability to enable SSL to database connection with `sslEnabled` (maintain default of `false`)

## [2.0.7] - 2023-09-25
### Changed
Expand Down
41 changes: 41 additions & 0 deletions packages/common-ts/src/database/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,45 @@ describe('Database', () => {
const sequelize = await connectDatabase(__DATABASE__)
expect(sequelize).toBeDefined()
})

test('Connect with options set', async () => {
const sequelize = await connectDatabase({
host: 'localhost',
username: 'test',
password: 'test',
database: 'test',
sslEnabled: true,
logging: () => {},
poolMin: 1,
poolMax: 5,
})

expect(sequelize).toBeDefined()

const poolConfig = sequelize.config.pool
expect(poolConfig?.min).toBe(1)
expect(poolConfig?.max).toBe(5)

const sslConfig = sequelize.config.ssl
expect(sslConfig).toBe(true)
})

test('Connect with default options', async () => {
const sequelize = await connectDatabase({
host: 'localhost',
username: 'test',
password: 'test',
database: 'test',
logging: () => {},
})

expect(sequelize).toBeDefined()

const poolConfig = sequelize.config.pool
expect(poolConfig?.min).toBe(0)
expect(poolConfig?.max).toBe(10)

const sslConfig = sequelize.config.ssl
expect(sslConfig).toBe(false)
})
})
3 changes: 3 additions & 0 deletions packages/common-ts/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface ConnectOptions {
username: string
password: string
database: string
sslEnabled?: boolean
logging?: (sql: string, timing?: number) => void
poolMin?: number
poolMax?: number
Expand All @@ -18,6 +19,7 @@ export const connectDatabase = async (options: ConnectOptions): Promise<Sequeliz
const port = options.port || 5432
const poolMin = options.poolMin || 0
const poolMax = options.poolMax || 10
const sslEnabled = options.sslEnabled || false

// Connect to the database
const sequelize = new Sequelize({
Expand All @@ -27,6 +29,7 @@ export const connectDatabase = async (options: ConnectOptions): Promise<Sequeliz
username,
password,
database,
ssl: sslEnabled,
pool: {
max: poolMax,
min: poolMin,
Expand Down
Loading