-
Notifications
You must be signed in to change notification settings - Fork 9
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 #73 from daeisbae/59-support-ssl-based-db-connection
Support SSL based DB connection (#59)
- Loading branch information
Showing
5 changed files
with
86 additions
and
58 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
Empty file.
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,10 +1,11 @@ | ||
import dotenv from 'dotenv'; | ||
dotenv.config(); | ||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
|
||
export const DBConfig = { | ||
host: process.env.DB_HOST, | ||
port: parseInt(process.env.DB_PORT), | ||
database: process.env.DB_NAME, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD | ||
}; | ||
host: process.env.DB_HOST, | ||
port: parseInt(process.env.DB_PORT), | ||
database: process.env.DB_NAME, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
certificate: process.env.DB_CERTIFICATE, | ||
} |
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,51 +1,70 @@ | ||
import { createRequire } from "module"; | ||
const require = createRequire(import.meta.url); | ||
const { Client } = require("pg"); | ||
const { readFile } = require("fs/promises"); | ||
import { fileURLToPath } from "url"; | ||
import { dirname, join } from "path"; | ||
import { DBConfig } from "../config/config.js"; | ||
import { createRequire } from 'module' | ||
const require = createRequire(import.meta.url) | ||
const { Client } = require('pg') | ||
const { readFile } = require('fs/promises') | ||
import { fileURLToPath } from 'url' | ||
import { dirname, join } from 'path' | ||
import { DBConfig } from '../config/config.js' | ||
import { promises as fs } from 'fs' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
async function initDatabase() { | ||
console.log("Initializing database with config:", { | ||
host: DBConfig.host, | ||
port: DBConfig.port, | ||
database: DBConfig.database, | ||
user: DBConfig.user, | ||
}); | ||
|
||
const pgClient = new Client({ | ||
...DBConfig, | ||
}); | ||
pgClient.connect(); | ||
|
||
await pgClient | ||
.query(`CREATE DATABASE ${DBConfig.database};`) | ||
.catch((error) => { | ||
console.log("❌ Database is already created"); | ||
}) | ||
.then(() => { | ||
console.log("✅ Database created successfully"); | ||
}); | ||
|
||
const schemaPath = join(__dirname, "../migrations/create_tables.sql"); | ||
const sql = await readFile(schemaPath, "utf8"); | ||
|
||
await pgClient.query(sql).catch((error) => { | ||
console.error("❌ Schema loading failed:", error); | ||
}); | ||
console.log("✅ Schema loaded successfully"); | ||
pgClient.end(); | ||
console.log('Initializing database with config:', { | ||
host: DBConfig.host, | ||
port: DBConfig.port, | ||
database: DBConfig.database, | ||
user: DBConfig.user, | ||
}) | ||
|
||
const { certificate, config } = DBConfig | ||
|
||
let pgClient = undefined | ||
|
||
if (!certificate) { | ||
pgClient = new Client({ | ||
...config, | ||
}) | ||
} else { | ||
pgClient = new Client({ | ||
...config, | ||
ssl: { | ||
rejectUnauthorized: false, | ||
ca: fs | ||
.readFile(process.cwd() + '/certificates/' + certificate) | ||
.toString(), | ||
sslmode: 'require', | ||
}, | ||
}) | ||
} | ||
|
||
pgClient.connect() | ||
|
||
await pgClient | ||
.query(`CREATE DATABASE ${DBConfig.database};`) | ||
.catch((error) => { | ||
console.log('❌ Database is already created') | ||
}) | ||
.then(() => { | ||
console.log('✅ Database created successfully') | ||
}) | ||
|
||
const schemaPath = join(__dirname, '../migrations/create_tables.sql') | ||
const sql = await readFile(schemaPath, 'utf8') | ||
|
||
await pgClient.query(sql).catch((error) => { | ||
console.error('❌ Schema loading failed:', error) | ||
}) | ||
console.log('✅ Schema loaded successfully') | ||
pgClient.end() | ||
} | ||
|
||
initDatabase() | ||
.then(() => { | ||
console.log("✅ Database initialization complete"); | ||
process.exit(0); | ||
}) | ||
.catch((error) => { | ||
console.error("❌ Fatal error:", error); | ||
process.exit(1); | ||
}); | ||
.then(() => { | ||
console.log('✅ Database initialization complete') | ||
process.exit(0) | ||
}) | ||
.catch((error) => { | ||
console.error('❌ Fatal error:', error) | ||
process.exit(1) | ||
}) |
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