Skip to content

Commit

Permalink
feat: towards decoupling from metrics component - make it optional (#131
Browse files Browse the repository at this point in the history
)

* feat: decouple from metrics component making it optional

* chore: update api
  • Loading branch information
aleortega authored Oct 28, 2024
1 parent 545f1a2 commit fbf830e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ To create the component you have the option of:

- Supplying any `PoolConfig` options you want. This are related 1:1 with [node-postgres](https://node-postgres.com/api/pool) implementation, which in turn are all variables [Postgres](https://www.postgresql.org/) supports
- Supplying any `RunnerOption`. This are related 1:1 with [node-pg-migrate](https://github.com/salsita/node-pg-migrate) implementation and will be used for [migrations](#migrations)
- Supplying a Metrics component ([any `IMetricsComponent`](https://github.com/well-known-components/metrics)). This component will enable additional functionality for collecting metrics on querys.

```ts
// src/components.ts

await createPgComponent({ config, logs, metrics } /* optional config here */)
await createPgComponent({ config, logs, metrics: metrics | undefined } /* optional config here */)

// A possible optional'd look like:
const { config } = createConfigComponent()
Expand Down Expand Up @@ -51,6 +52,7 @@ the API looks like this

```ts
query<T>(sql: string): Promise<IDatabase.IQueryResult<T>>
// If it has been built with the metrics component this contract is also exposed:
query<T>(sql: SQLStatement, durationQueryNameLabel?: string): Promise<IDatabase.IQueryResult<T>>
```

Expand All @@ -62,7 +64,7 @@ const id = getIdFromUnreliableSource()
pg.query<TableType>(SQL`SELECT * FROM table WHERE id = ${id}`) // this results in ['SELECT * FROM table WHERE id = $1', id]
```

suppliying a `durationQueryNameLabel` will trigger a metrics increment with that name.
if `metrics` component has been provided, suppliying a `durationQueryNameLabel` will trigger a metrics increment with that name.

**Streaming a query**

Expand Down
2 changes: 1 addition & 1 deletion etc/pg-component.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export namespace createPgComponent {
export type NeededComponents = {
logs: ILoggerComponent;
config: IConfigComponent;
metrics: IMetricsComponent;
metrics?: IMetricsComponent;
};
}

Expand Down
32 changes: 13 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,17 @@ export async function createPgComponent(
}
}

async function query<T extends Record<string, any>>(sql: string): Promise<IDatabase.IQueryResult<T>>
async function query<T extends Record<string, any>>(
sql: SQLStatement,
durationQueryNameLabel?: string
): Promise<IDatabase.IQueryResult<T>>
async function query<T extends Record<string, any>>(
sql: string | SQLStatement,
durationQueryNameLabel?: string
): Promise<IDatabase.IQueryResult<T>> {
const rows = durationQueryNameLabel
? await runReportingQueryDurationMetric(components, durationQueryNameLabel, () => pool.query<T>(sql))
: await pool.query<T>(sql)

return {
rows: rows.rows,
rowCount: rows.rowCount ?? 0
}
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>> {
const result = durationQueryNameLabel
? await runReportingQueryDurationMetric({ metrics: components.metrics! }, durationQueryNameLabel, () => defaultQuery<T>(sql))
: await defaultQuery<T>(sql)

return result
}

async function* streamQuery<T>(sql: SQLStatement, config?: { batchSize?: number }): AsyncGenerator<T> {
Expand Down Expand Up @@ -169,7 +163,7 @@ export async function createPgComponent(
}

return {
query,
query: components.metrics ? measuredQuery : defaultQuery,
streamQuery,
getPool,
start,
Expand All @@ -184,6 +178,6 @@ export namespace createPgComponent {
export type NeededComponents = {
logs: ILoggerComponent
config: IConfigComponent
metrics: IMetricsComponent
metrics?: IMetricsComponent
}
}

0 comments on commit fbf830e

Please sign in to comment.