3

I currently use postgresql-node in my lambda with

import { Client } from 'pg'

I want to instrument the Postgresql lib with AWS X-ray. The Nodejs example has this line:

var AWSXRay = require('aws-xray-sdk');
var pg = AWSXRay.capturePostgres(require('pg'));

How would I convert the second line in that to proper Typescript. All the variations I come up with produce some errors or warnings. For example I would guess this would work:

const pg = AWSXRay.capturePostgres(require('pg'))

but not only you get ESlint warning for require being used without import but after that pg.Client says pg namespace not found.

vertti
  • 7,539
  • 4
  • 51
  • 81

1 Answers1

6

Well, it's slightly ugly but this seems to work:

import * as pg from 'pg'
const patchedPg = AWSXRay.capturePostgres(pg)
vertti
  • 7,539
  • 4
  • 51
  • 81
  • 1
    I have only seen this method. – makeitmorehuman Nov 27 '19 at 11:22
  • 1
    The snippet in this answer is correct. Here's the TS [definition](https://github.com/willarmiros/aws-xray-sdk-node/blob/master/packages/postgres/lib/postgres_p.d.ts#L4) where `capturePostgres` is defined - it uses the wildcard import to define `pg`. I'll get that documentation updated. – William Armiros Dec 05 '19 at 18:41