Skip to content

Commit

Permalink
db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
lauti7 committed Apr 4, 2024
1 parent a323dc1 commit 7e220fd
Show file tree
Hide file tree
Showing 5 changed files with 723 additions and 351 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"scripts": {
"build": "tsc -p tsconfig.json",
"start": "node --trace-warnings --abort-on-uncaught-exception --unhandled-rejections=strict dist/index.js",
"test": "jest --forceExit --detectOpenHandles --coverage --verbose"
"test": "jest --forceExit --detectOpenHandles --coverage --verbose",
"migrate": "node-pg-migrate --database-url-var PG_COMPONENT_PSQL_CONNECTION_STRING --envPath .env -j ts --tsconfig tsconfig.json -m ./src/migrations"
},
"devDependencies": {
"@types/node": "^20.11.28",
"@well-known-components/test-helpers": "^1.5.6",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
},
"prettier": {
Expand All @@ -22,6 +24,10 @@
"@well-known-components/http-server": "^2.1.0",
"@well-known-components/interfaces": "^1.4.3",
"@well-known-components/logger": "^3.1.3",
"@well-known-components/metrics": "^2.1.0"
"@well-known-components/metrics": "^2.1.0",
"@well-known-components/pg-component": "^0.2.2",
"@well-known-components/uws-http-server": "^0.0.1-20240209194742.commit-9feae40",
"node-pg-migrate": "^6.2.2",
"sql-template-strings": "^2.2.2"
}
}
7 changes: 7 additions & 0 deletions src/adapters/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AppComponents } from '../types'

export interface IDBComponent {}

export function createDBComponent({ pg, logs }: Pick<AppComponents, 'pg' | 'logs'>): IDBComponent {
return {}
}
32 changes: 31 additions & 1 deletion src/components.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'path'
import { createDotEnvConfigComponent } from '@well-known-components/env-config-provider'
import {
createServerComponent,
Expand All @@ -8,6 +9,8 @@ import { createLogComponent } from '@well-known-components/logger'
import { createMetricsComponent } from '@well-known-components/metrics'
import { AppComponents, GlobalContext } from './types'
import { metricDeclarations } from './metrics'
import { createPgComponent } from '@well-known-components/pg-component'
import { createDBComponent } from './adapters/db'

// Initialize all the components of the app
export async function initComponents(): Promise<AppComponents> {
Expand All @@ -17,13 +20,40 @@ export async function initComponents(): Promise<AppComponents> {
const server = await createServerComponent<GlobalContext>({ config, logs }, {})
const statusChecks = await createStatusCheckComponent({ server, config })

let databaseUrl: string | undefined = await config.getString('PG_COMPONENT_PSQL_CONNECTION_STRING')
if (!databaseUrl) {
const dbUser = await config.requireString('PG_COMPONENT_PSQL_USER')
const dbDatabaseName = await config.requireString('PG_COMPONENT_PSQL_DATABASE')
const dbPort = await config.requireString('PG_COMPONENT_PSQL_PORT')
const dbHost = await config.requireString('PG_COMPONENT_PSQL_HOST')
const dbPassword = await config.requireString('PG_COMPONENT_PSQL_PASSWORD')
databaseUrl = `postgres://${dbUser}:${dbPassword}@${dbHost}:${dbPort}/${dbDatabaseName}`
}

const pg = await createPgComponent(
{ logs, config, metrics },
{
migration: {
databaseUrl,
dir: resolve(__dirname, 'migrations'),
migrationsTable: 'pgmigrations',
ignorePattern: '.*\\.map',
direction: 'up'
}
}
)

const db = createDBComponent({ pg, logs })

await instrumentHttpServerWithPromClientRegistry({ metrics, server, config, registry: metrics.registry! })

return {
config,
logs,
server,
statusChecks,
metrics
metrics,
pg,
db
}
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import type {
IMetricsComponent,
IFetchComponent
} from '@well-known-components/interfaces'
import { IPgComponent } from '@well-known-components/pg-component'
import { metricDeclarations } from './metrics'
import { IDBComponent } from './adapters/db'

export type GlobalContext = {
components: BaseComponents
Expand All @@ -18,6 +20,8 @@ export type BaseComponents = {
logs: ILoggerComponent
server: IHttpServerComponent<GlobalContext>
metrics: IMetricsComponent<keyof typeof metricDeclarations>
pg: IPgComponent
db: IDBComponent
}

// components used in runtime
Expand Down
Loading

0 comments on commit 7e220fd

Please sign in to comment.