Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM warns that createConnection is deprecated

Error'createConnection' is deprecated.

Short answer

TypeORM replaced Connection with DataSource. createConnection and the global helpers such as getConnection and getRepository are deprecated, and version 1.0 removes them. Create one DataSource, call initialize() once, export it, and take repositories from that object.

  • Applies to TypeORM 0.3, TypeORM 1.0, TypeScript 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every use of the old global functions so you can replace them in one pass.

    grep -rnE "createConnection|getConnection|getRepository|getManager" src
  2. 2Match what you see, then jump to its fix

    1. 1The deprecation hint sits on the createConnection call in your bootstrap fileThe connection is still opened with createConnectionFix
    2. 2Other files are flagged as deprecated, or fail after you upgrade to 1.0Code elsewhere calls getConnection, getRepository or getManagerFix

What does the error look like?

  • 'createConnection' is deprecated.

Cause 1 of 2

The connection is still opened with createConnection

1Check

The 0.3.0 release notes say createConnection and createConnections are deprecated because Connection is now called DataSource.

grep -rn "createConnection" src

2Fix

Build a DataSource from the same options and initialize it once. The docs say initialize must only be called once; a second call rejects with CannotConnectAlreadyConnectedError.

// data-source.ts
import { DataSource } from "typeorm";

export const AppDataSource = new DataSource({
  type: "mysql",
  host: "localhost",
  port: 3306,
  username: "test",
  password: "test",
  database: "test",
});

// main.ts
await AppDataSource.initialize();

3Verify

Start the app and watch for the connection to open without the hint in the editor.

npx tsc --noEmit

Confirmed by github.com

Did this fix it?

Cause 2 of 2

Code elsewhere calls getConnection, getRepository or getManager

1Check

The 0.3.0 notes deprecate these global helpers and say to use a globally accessible data source instead. The 1.0 upgrade guide says they were removed.

grep -rnE "getRepository|getConnection|getManager" src

2Fix

Import the exported data source and ask it for the repository. Replace close() with destroy() and connect() with initialize().

import { AppDataSource } from "./data-source";

const repo = AppDataSource.getRepository(User);
await AppDataSource.destroy();

3Verify

Compile the project and run the code path that reads a user.

npx tsc --noEmit

Confirmed by typeorm.io

Did this fix it?

How do I stop it coming back?

  • Keep the DataSource in one module and import it, rather than creating a second one in a test or a script.
  • Run tsc in CI so removed APIs fail the build at upgrade time.
  • Read the upgrade guide before moving to a new major version.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.