4

I'm completely new to Express/Postgresql and I'm trying to learn them to create a web application. After some poking around, I decided that I wanted to develop my back-end with TypeScript. I successfully converted all my other files from JavaScript to TypeScript, but I still can't figure out how to initialize the pg-promise connection in TypeScript!

I've been trying to follow the TypeScript guidelines in this link here. https://github.com/vitaly-t/pg-promise/tree/master/typescript

// Initialize the PostGres database conneciton for use throughout 
the entire application
import {IMain, IDatabase} from 'pg-promise';
import * as pgPromise from 'pg-promise';

const pgp: IMain = pgPromise({
    query(e: any) {
        console.log('QUERY RESULT:', e.query);
    },
    receive(data: any, result: any, e: any) {
        console.log(`DATA FROM QUERY ${e.query} WAS RECEIVED.`);
    }
});

const connection: any = {
    host: 'localhost',
    port: 5432,
    database: 'RushHub',
    user: 'RyanArifin',
    password: null
}
const db: IDatabase<any> = pgp(connection);
export {
    db
};

I currently am getting the error "TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof pgPromise' has no compatible call signatures." This error is coming from when I try to set my initialization options. Any help would be appreciated, thanks!

3
  • Use pg-promise-demo as reference, because it works fine there. Commented Dec 26, 2018 at 4:43
  • Thanks! I was actually looking at that as well, and I was wondering how I could initialize the database without the "IExtensions" interface (or is it required for this to work)? The reason I ask is because the "IExtension", from what I understand, just extends the "UsersRepository" and "ProductsRepository" extensions, which I won't need in my smaller program (as of right now). Is there a way to create the initialization options without the IExtension interface? Thanks! Commented Dec 26, 2018 at 17:57
  • 2
    Nevermind, I figured out the problem. Apparently in Javascript ES6, you need to use "import pgPromise from 'pg-promise'" instead of "import * as pgPromise from 'pg-promise';". Commented Dec 26, 2018 at 20:42

1 Answer 1

5

This is standard TypeScript configuration flag - esModuleInterop. When set to true, the import syntax is import pgPromise from 'pg-promise';, and when false, which is the default, the syntax is import * as pgPromise from 'pg-promise';.

The library gives you example for the default TypeScript configuration.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.