Skip to content

Commit

Permalink
[db] add ability to enable SSL for database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-u410 committed Aug 13, 2024
1 parent 87f5274 commit e1a6d43
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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

0 comments on commit e1a6d43

Please sign in to comment.