April 2018
Beginner
536 pages
13h 21m
English
In our repository, we have used the getConnection function from TypeORM. Before we can invoke this function, we need to ensure that a connection has been created. The following function is used later to create a database connection:
import { createConnection } from "typeorm"; import { Movie } from "./entities/movie"; export async function getDbConnection() { const DATABASE_HOST = process.env.DATABASE_HOST || "localhost"; const DATABASE_USER = process.env.DATABASE_USER || ""; const DATABASE_PORT = 5432; const DATABASE_PASSWORD = process.env.DATABASE_PASSWORD || ""; const DATABASE_DB = "demo"; const entities = [ Movie ]; const conn = await createConnection({ type: "postgres", host: DATABASE_HOST, port: DATABASE_PORT, username: DATABASE_USER, ...