Skip to content

Commit

Permalink
upgrade deps and test without mocks (#134)
Browse files Browse the repository at this point in the history
* upgrade deps and test without mocks

* api-extractor
  • Loading branch information
menduz authored Jan 27, 2025
1 parent fbf830e commit bf05a38
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 402 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ LOCAL_ARG = --local --verbose --diagnostics
endif

test:
docker compose -f docker-compose.yaml up -d --wait
node_modules/.bin/jest --detectOpenHandles --colors --runInBand --coverage $(TESTARGS)

test-watch:
Expand Down
23 changes: 23 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

services:
postgres:
container_name: "test-db"
image: 'postgres:latest'
user: postgres
volumes:
- test_postgres_volume:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=pass1234
- POSTGRES_DB=test
ports:
- '15432:5432'
networks:
- test

volumes:
test_postgres_volume:

networks:
test:
name: 'test'
2 changes: 1 addition & 1 deletion etc/pg-component.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const metricDeclarations: IMetricsComponent_2.MetricsRecordDefinition<str
// @public (undocumented)
export type Options = Partial<{
pool: PoolConfig;
migration: RunnerOption;
migration: Omit<RunnerOption, 'databaseUrl' | 'dbClient'>;
}>;

// Warning: (ae-internal-missing-underscore) The name "QueryStreamWithCallback" should be prefixed with an underscore because the declaration is marked as @internal
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
"@well-known-components/metrics": "^2.1.0",
"@well-known-components/test-helpers": "^1.5.6",
"node-fetch": "2",
"typescript": "^5.4.5"
"typescript": "^5.7.3"
},
"dependencies": {
"@types/pg": "^8.6.5",
"@types/pg": "^8.11.11",
"@well-known-components/interfaces": "^1.4.3",
"node-pg-migrate": "^6.2.1",
"pg": "^8.7.3",
"pg-query-stream": "^4.2.3",
"node-pg-migrate": "^7.9.0",
"pg": "^8.13.1",
"pg-query-stream": "^4.7.1",
"sql-template-strings": "^2.2.2"
},
"files": [
Expand Down
51 changes: 39 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IBaseComponent, IConfigComponent, ILoggerComponent, IDatabase } from '@well-known-components/interfaces'
import { Client, Pool, PoolConfig } from 'pg'
import QueryStream from 'pg-query-stream'
import runner from 'node-pg-migrate'
import runner, { RunnerOption } from 'node-pg-migrate'
import { SQLStatement } from 'sql-template-strings'
import { setTimeout } from 'timers/promises'
import { runReportingQueryDurationMetric } from './utils'
Expand Down Expand Up @@ -49,33 +49,60 @@ export async function createPgComponent(

const finalOptions: PoolConfig = { ...defaultOptions, ...options.pool }

if (!finalOptions.log) {
finalOptions.log = logger.debug.bind(logger)
}

// Config
const pool: Pool = new Pool(finalOptions)

// Methods
async function start() {
try {
if (options.migration) {
logger.debug('Running migrations:')
await runner(options.migration)
}

const db = await pool.connect()
db.release()
} catch (error) {
logger.error(`An error occurred trying to open the database. Error: '${(error as Error).message}'`)

try {
if (options.migration) {
logger.debug('Running migrations:')

const opt: RunnerOption = {
...options.migration,
dbClient: db
}

if (!opt.logger) {
opt.logger = logger
}
await runner(opt)
}
} catch (err: any) {
logger.error(err)
throw err
} finally {
db.release()
}
} catch (error: any) {
logger.warn('Error starting pg-component:')
logger.error(error)
throw error
}
}

async function defaultQuery<T extends Record<string, any>>(sql: string | SQLStatement): Promise<IDatabase.IQueryResult<T>> {
async function defaultQuery<T extends Record<string, any>>(
sql: string | SQLStatement
): Promise<IDatabase.IQueryResult<T>> {
const result = await pool.query<T>(sql)
return { ...result, rowCount: result.rowCount ?? 0 }
}

async function measuredQuery<T extends Record<string, any>>(sql: string | SQLStatement, durationQueryNameLabel?: string): Promise<IDatabase.IQueryResult<T>> {
async function measuredQuery<T extends Record<string, any>>(
sql: string | SQLStatement,
durationQueryNameLabel?: string
): Promise<IDatabase.IQueryResult<T>> {
const result = durationQueryNameLabel
? await runReportingQueryDurationMetric({ metrics: components.metrics! }, durationQueryNameLabel, () => defaultQuery<T>(sql))
? await runReportingQueryDurationMetric({ metrics: components.metrics! }, durationQueryNameLabel, () =>
defaultQuery<T>(sql)
)
: await defaultQuery<T>(sql)

return result
Expand Down
19 changes: 11 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IDatabase, IMetricsComponent as IBaseMetricsComponent } from "@well-known-components/interfaces"
import { Pool, PoolConfig } from "pg"
import { RunnerOption } from "node-pg-migrate"
import { SQLStatement } from "sql-template-strings"
import QueryStream from "pg-query-stream"
import { metricDeclarations } from "./metrics"
import { IDatabase, IMetricsComponent as IBaseMetricsComponent } from '@well-known-components/interfaces'
import { Pool, PoolConfig } from 'pg'
import { RunnerOption } from 'node-pg-migrate'
import { SQLStatement } from 'sql-template-strings'
import QueryStream from 'pg-query-stream'
import { metricDeclarations } from './metrics'

/**
* @internal
Expand All @@ -13,7 +13,7 @@ export type QueryStreamWithCallback = QueryStream & { callback: Function }
/**
* @public
*/
export type Options = Partial<{ pool: PoolConfig; migration: RunnerOption }>
export type Options = Partial<{ pool: PoolConfig; migration: Omit<RunnerOption, 'databaseUrl' | 'dbClient'> }>

/**
* @public
Expand All @@ -22,7 +22,10 @@ export interface IPgComponent extends IDatabase {
start(): Promise<void>

query<T extends Record<string, any>>(sql: string): Promise<IDatabase.IQueryResult<T>>
query<T extends Record<string, any>>(sql: SQLStatement, durationQueryNameLabel?: string): Promise<IDatabase.IQueryResult<T>>
query<T extends Record<string, any>>(
sql: SQLStatement,
durationQueryNameLabel?: string
): Promise<IDatabase.IQueryResult<T>>
streamQuery<T = any>(sql: SQLStatement, config?: { batchSize?: number }): AsyncGenerator<T>

/**
Expand Down
Loading

0 comments on commit bf05a38

Please sign in to comment.