DEV Community 👩‍💻👨‍💻

Discussion on: Setup PostgreSQL with TypeORM in GraphQL Tutorial

Collapse
jimmymcbride profile image
Jimmy McBride • Edited on

Solved: Answer in reply below

I have a TypeORM project I am trying to deploy to Heroku. Heroku's Postgres addon just gives me one database URL. That URL locally on my end would look like: postgres://user_name:password@localhost:5432/project_table

TypeORM spreads those things out a bit, though. In knex I can just do:

require("dotenv").config();

module.exports = {
  development: {
    client: "pg",
    // 🔻 Points to our local Postgresql database
    connection: process.env.DATABASE_URL,
  }
};

But I don't know how to consolidate that down just one variable in the ormconfig.ts. Any ideas? I'm really loving using TypeORM so far and would love to get it hosted! Lol

Collapse
karladler profile image
Karl Adler

You can extract all data you need from the connection string. This module may help a lot, but it's also manually not that hard.

npmjs.com/package/pg-connection-st...

But, yeah would be really cool if typeorm supports this by default somehow.

Collapse
jimmymcbride profile image
Jimmy McBride

They do! :) Posted the answer above! 🔥

Collapse
jimmymcbride profile image
Jimmy McBride

This is how I have my TypeORM set up that works exactly the way I wanted it:

createConnection({
    name: "default",
    type: "postgres",
    url: process.env.DATABASE_URL,
    synchronize: true,
    logging: true,
    entities: ["src/entity/*.*"],
    extra: {
      ssl: process.env.SSL || false,
    },
  });