How to import pg-promise using ES6 syntax?
Asked Answered
A

3

8

I am trying to set up a Postgres database in a nodejs server using ES6 syntax, but I don't think I'm importing and initializing pg-promise properly. If I were using common js sytax I would do the below:

// Create Database Connection
const pgp = require('pg-promise')({});
const db = pgp(config.db);

// Test connection
db.connect()
  .then((obj) => {
    console.log('Connected to database');
    obj.done(); // success, release connection;
  })
  .catch((error) => {
    console.error('ERROR:', error.message);
  });

Using ES6 I am trying to do the below but the connection just hangs and doesn't complete or error out.

import pgPromise from 'pg-promise';

// Create Database Connection
const pgp = pgPromise({});
const db = pgp(config.db);

// Test connection
db.connect()
  .then((obj) => {
    console.log('Connected to database');
    obj.done(); // success, release connection;
  })
  .catch((error) => {
    console.error('ERROR:', error.message);
  });

I've searched through the pg-promise docs and can't find anything about using it with ES6 syntax. Any ideas on what I should change?

Airdry answered 1/7, 2020 at 4:46 Comment(0)
A
1

Alright this is pretty stupid, but I found out my problem was just that I needed to update the pg-promise dependency. I was using version 8.5.1 and upgrading to 10.5.7 fixed this issue. For anyone else running into this issue you can use the code for ES6 as written in the question just make sure your pg-promise dependency is at the latest version.

Airdry answered 1/7, 2020 at 13:32 Comment(0)
M
8

The correct as I can see in the documentation.

Loading and initializing the library with Initialization Options:

const initOptions = {/* initialization options */};
const pgp = require('pg-promise')(initOptions);

or without Initialization Options:

const pgp = require('pg-promise')();

Create your Database object from the connection as pgp(connection, [dc]):

const db = pgp(connection);

For ES6 or TypeScript syntax

import pgPromise from 'pg-promise';

const pgp = pgPromise({/* Initialization Options */});

const db = pgp('postgres://username:password@host:port/database');

Marlow answered 1/7, 2020 at 4:55 Comment(2)
Using the * notation in the import gives a TypeError stating that pgPromise is not a function.Airdry
I solved this issue (see my own answer), but it turns out pg-promise returns a default export so you don't need the * notation.Airdry
N
3

Is there any error message? Nodejs needs specific conditions to support es module, first make sure you have introduced the module correctly.

// index.mjs
import pgPromise from 'pg-promise';
const pgp = pgPromise({});
console.log(pgp);

Then execute with a --experimental-modules

node --experimental-modules index.mjs

More details https://blog.logrocket.com/es-modules-in-node-js-12-from-experimental-to-release/

Neiman answered 1/7, 2020 at 5:16 Comment(1)
There are no errors that are thrown. I've already done the set up for ES modules in node. I'm using Node 14.3.0 and have set "type": "module" in the package.json.Airdry
A
1

Alright this is pretty stupid, but I found out my problem was just that I needed to update the pg-promise dependency. I was using version 8.5.1 and upgrading to 10.5.7 fixed this issue. For anyone else running into this issue you can use the code for ES6 as written in the question just make sure your pg-promise dependency is at the latest version.

Airdry answered 1/7, 2020 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.