'How can I create a TypeORM Postgres connection with Heroku using Typegraphql?

I want to create a TypeORM Postgres connection with Heroku using Typegraphql, but I receiving the following error message:

'createConnection' is deprecated.ts(6385)
globals.d.ts(34, 4): The declaration was marked as deprecated here

Here is ormconfig.json:

{
    "name": "default" ,
    "type": "postgres",
    "host": "heroku host", 
    "port": 5432,
    "username": "heroku username", 
    "password": "heroku password",
    "database": "heroku database", 
    "uri": "heroku uri",
    "synchronize ": true,
    "logging": true,
    "entities": ["src/entity/*.*"]
}

Here is my index.ts:

    import "reflect-metadata"
    import { ApolloServer } from "apollo-server-express";
    import * as Express from "express";
    import { buildSchema, Resolver, Query } from "type-graphql";
    import { createConnection } from "typeorm";
    
    
    @Resolver()
    class HelloResolver {
        @Query(() => String)
        async hello() {
            return "Hello World!!";
        }
    }
    
    const main = async () => {
    
        await createConnection();
    
        const schema = await buildSchema({
            resolvers: [HelloResolver]
        });
    
        const apolloServer =  new ApolloServer({ schema }); 
    
        await apolloServer.start();
    
        const app = Express(); 
    
        apolloServer.applyMiddleware({ app }); 
    
        app.listen(4000, () => {
            console.log("server started on http://localhost:4000/graphql");
    
        });
    
    };

main();

How can I created this connection? Apparently the ormconfig.json connection method is already deprecated. I truly need help on this.



Solution 1:[1]

Since TypeORM 0.3.0 the correct way to establish connections is to use the below code.

const AppDataSource = new DataSource({
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "root",
    password: "admin",
    database: "test",
    entities: [Photo],
    synchronize: true,
    logging: false,
})


await AppDataSource.initialize()

Another way to solve your issue is to downgrade TypeORM version to 0.2.45 by running:

npm install [email protected]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcelo KortKamp