node.jstypescriptdeprecateddeprecation-warning

'createConnection' is deprecated


I am creating my first application in Node with Graphql, and for now I have only written this code:

import 'reflect-metadata';
const express = require('express');
import { Express } from 'express';
import { ApolloServer } from 'apollo-server-express';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
import { buildSchema } from 'type-graphql';
import { ActivityResolver } from './resolvers/activity';
import { createConnection } from 'typeorm';

const main = async () => {
    // Setup database connection
    const conn = await createConnection({
        type: 'postgres',
        database: 'deliveryapi',
        entities: [],
        logging: true,
        synchronize: true,
        username: process.env.USERNAME || 'postgres', 
        password: process.env.PASSWORD || 'Juventus',
        port: 5432,
    });

    // Create express application for GraphQL server
    const apollo: ApolloServer = new ApolloServer({
        schema: await buildSchema({
            resolvers: [ActivityResolver],
            validate: false,
        }),
        plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
    })
    const app: Express = express();

    // All application required varibles
    const PORT = process.env.PORT || 5000;

    // Middleware and routes
    await apollo.start();
    apollo.applyMiddleware({ app });
    app.use(express.json());
    app.get('/', (req, res) => {res.send("Hello World")});

    // Start server
    app.listen(PORT, () => {console.log(`Server started on port ${PORT} and connected to database`)});

}

main().catch(err => {console.error("Error: ", err)});

The problem is that, in VSCode there is a problem in this line: import { createConnection } from 'typeorm';. It is deleted and it says that it is deprecated, and I can't understand why, an if that affect the code. I am using Typescript.

Can someone explain please? Thank you


Solution

  • Since version 0.3.0 of typeorm, createConnection is deprecated in favour of the new DataSoure API. See the Changelog for 0.3.0 https://github.com/typeorm/typeorm/blob/master/CHANGELOG.md

    Also have a look at: https://typeorm.io/data-source