7

I'm newbie to typeorm and trying to create a connection to db. I read the typeorm's doc and found this code, it uses DataSource to create connection:

import "reflect-metadata"
import { DataSource } from "typeorm"
import { Photo } from "./entity/Photo"

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

AppDataSource.initialize()
    .then(() => {
        // here you can start to work with your database
    })
    .catch((error) => console.log(error))

But when searching for some references in other sources, they use createConnection instead:

import { createConnection } from "typeorm"

createConnection({
  type: "mysql",
  host: "localhost",
  port: 3306,
  username: "root",
  password: "mysql",
  database: "mysql",
  entities: [
     __dirname + "/entity/*.ts"
  ],
  synchronize: true,
  logging: false
}).then(async connection => {
…
…
}).catch(error => console.log(error));

I'm a bit confused. What approach should I use for creating connection to db between those two above?

1
  • 1
    I am also facing the same confusion. Did you get any reference for this? Commented Jun 5, 2022 at 9:34

1 Answer 1

9

For the next person that comes across this issue, createConnection has been deprecated in favor of using new DataSource. See here:

https://typeorm.io/changelog#030httpsgithubcomtypeormtypeormpull8616-2022-03-17

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.