How should I specify the DATABASE_URL in ormconfig.json?












0















I am trying to deploy my Vesper server to Heroku and Vesper requires an ormconfig.json file.



This works just fine when I use my local db because I can fill out all the fields that will combine into the connection string. However, when I add a db in Heroku I just get the full url and I can't find where to put it.



This is my ormconfig.json right now.



{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "password",
"database": "test",
"synchronize": true,
"entities": ["target/entity/**/*.js"],
"migrations": ["target/migrations/*.js"],
"cli": {
"migrationsDir": "src/migrations"
}
}


I'm hoping I could replace most fields with just the database_url but I can't find any documentation stating under what name I should put it.










share|improve this question



























    0















    I am trying to deploy my Vesper server to Heroku and Vesper requires an ormconfig.json file.



    This works just fine when I use my local db because I can fill out all the fields that will combine into the connection string. However, when I add a db in Heroku I just get the full url and I can't find where to put it.



    This is my ormconfig.json right now.



    {
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "postgres",
    "password": "password",
    "database": "test",
    "synchronize": true,
    "entities": ["target/entity/**/*.js"],
    "migrations": ["target/migrations/*.js"],
    "cli": {
    "migrationsDir": "src/migrations"
    }
    }


    I'm hoping I could replace most fields with just the database_url but I can't find any documentation stating under what name I should put it.










    share|improve this question

























      0












      0








      0








      I am trying to deploy my Vesper server to Heroku and Vesper requires an ormconfig.json file.



      This works just fine when I use my local db because I can fill out all the fields that will combine into the connection string. However, when I add a db in Heroku I just get the full url and I can't find where to put it.



      This is my ormconfig.json right now.



      {
      "type": "postgres",
      "host": "localhost",
      "port": 5432,
      "username": "postgres",
      "password": "password",
      "database": "test",
      "synchronize": true,
      "entities": ["target/entity/**/*.js"],
      "migrations": ["target/migrations/*.js"],
      "cli": {
      "migrationsDir": "src/migrations"
      }
      }


      I'm hoping I could replace most fields with just the database_url but I can't find any documentation stating under what name I should put it.










      share|improve this question














      I am trying to deploy my Vesper server to Heroku and Vesper requires an ormconfig.json file.



      This works just fine when I use my local db because I can fill out all the fields that will combine into the connection string. However, when I add a db in Heroku I just get the full url and I can't find where to put it.



      This is my ormconfig.json right now.



      {
      "type": "postgres",
      "host": "localhost",
      "port": 5432,
      "username": "postgres",
      "password": "password",
      "database": "test",
      "synchronize": true,
      "entities": ["target/entity/**/*.js"],
      "migrations": ["target/migrations/*.js"],
      "cli": {
      "migrationsDir": "src/migrations"
      }
      }


      I'm hoping I could replace most fields with just the database_url but I can't find any documentation stating under what name I should put it.







      typescript heroku typeorm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 12:53









      Stefan WullemsStefan Wullems

      76




      76
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use an url parser for heroku env var parsing such as pg-connection-string https://www.npmjs.com/package/pg-connection-string



          Then you use the createConnection function given by TypeOrm to initialize TypeOrm on the server side.



          import * as PostgressConnectionStringParser from "pg-connection-string";

          const databaseUrl: string = process.env.DATABASE_URL;
          const connectionOptions = PostgressConnectionStringParser.parse(databaseUrl);
          const typeOrmOptions: PostgresConnectionOptions = {
          type: "postgres",
          name: connectionOptions.name,
          host: connectionOptions.host,
          port: connectionOptions.port,
          username: connectionOptions.username,
          password: connectionOptions.password,
          database: connectionOptions.database,
          synchronize: true,
          entities: ["target/entity/**/*.js"],
          extra: {
          ssl: true
          }
          };
          const connection = createConnection(typeOrmOptions);
          ...


          If you manage different configurations, you will probably have to modify this snippet so you can enable / disable ssl depending on the environment for instance (no ssl in dev mode, ts-node fetches entities in .ts format, etc...).



          If you really need to generate ormconfig.json, then I'm afraid you have to generate the file from a script file, with the former code, just add a write part:



          ...
          const json = JSON.stringify(typeOrmOptions, null, 2);
          fs.writeFile("./target/ormconfig.json", json, (err) => {
          if (err) {
          console.error(err);
          return;
          }
          console.log("File has been created");
          });





          share|improve this answer
























          • Works like a charm

            – Stefan Wullems
            Jan 3 at 13:14











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006757%2fhow-should-i-specify-the-database-url-in-ormconfig-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use an url parser for heroku env var parsing such as pg-connection-string https://www.npmjs.com/package/pg-connection-string



          Then you use the createConnection function given by TypeOrm to initialize TypeOrm on the server side.



          import * as PostgressConnectionStringParser from "pg-connection-string";

          const databaseUrl: string = process.env.DATABASE_URL;
          const connectionOptions = PostgressConnectionStringParser.parse(databaseUrl);
          const typeOrmOptions: PostgresConnectionOptions = {
          type: "postgres",
          name: connectionOptions.name,
          host: connectionOptions.host,
          port: connectionOptions.port,
          username: connectionOptions.username,
          password: connectionOptions.password,
          database: connectionOptions.database,
          synchronize: true,
          entities: ["target/entity/**/*.js"],
          extra: {
          ssl: true
          }
          };
          const connection = createConnection(typeOrmOptions);
          ...


          If you manage different configurations, you will probably have to modify this snippet so you can enable / disable ssl depending on the environment for instance (no ssl in dev mode, ts-node fetches entities in .ts format, etc...).



          If you really need to generate ormconfig.json, then I'm afraid you have to generate the file from a script file, with the former code, just add a write part:



          ...
          const json = JSON.stringify(typeOrmOptions, null, 2);
          fs.writeFile("./target/ormconfig.json", json, (err) => {
          if (err) {
          console.error(err);
          return;
          }
          console.log("File has been created");
          });





          share|improve this answer
























          • Works like a charm

            – Stefan Wullems
            Jan 3 at 13:14
















          0














          You can use an url parser for heroku env var parsing such as pg-connection-string https://www.npmjs.com/package/pg-connection-string



          Then you use the createConnection function given by TypeOrm to initialize TypeOrm on the server side.



          import * as PostgressConnectionStringParser from "pg-connection-string";

          const databaseUrl: string = process.env.DATABASE_URL;
          const connectionOptions = PostgressConnectionStringParser.parse(databaseUrl);
          const typeOrmOptions: PostgresConnectionOptions = {
          type: "postgres",
          name: connectionOptions.name,
          host: connectionOptions.host,
          port: connectionOptions.port,
          username: connectionOptions.username,
          password: connectionOptions.password,
          database: connectionOptions.database,
          synchronize: true,
          entities: ["target/entity/**/*.js"],
          extra: {
          ssl: true
          }
          };
          const connection = createConnection(typeOrmOptions);
          ...


          If you manage different configurations, you will probably have to modify this snippet so you can enable / disable ssl depending on the environment for instance (no ssl in dev mode, ts-node fetches entities in .ts format, etc...).



          If you really need to generate ormconfig.json, then I'm afraid you have to generate the file from a script file, with the former code, just add a write part:



          ...
          const json = JSON.stringify(typeOrmOptions, null, 2);
          fs.writeFile("./target/ormconfig.json", json, (err) => {
          if (err) {
          console.error(err);
          return;
          }
          console.log("File has been created");
          });





          share|improve this answer
























          • Works like a charm

            – Stefan Wullems
            Jan 3 at 13:14














          0












          0








          0







          You can use an url parser for heroku env var parsing such as pg-connection-string https://www.npmjs.com/package/pg-connection-string



          Then you use the createConnection function given by TypeOrm to initialize TypeOrm on the server side.



          import * as PostgressConnectionStringParser from "pg-connection-string";

          const databaseUrl: string = process.env.DATABASE_URL;
          const connectionOptions = PostgressConnectionStringParser.parse(databaseUrl);
          const typeOrmOptions: PostgresConnectionOptions = {
          type: "postgres",
          name: connectionOptions.name,
          host: connectionOptions.host,
          port: connectionOptions.port,
          username: connectionOptions.username,
          password: connectionOptions.password,
          database: connectionOptions.database,
          synchronize: true,
          entities: ["target/entity/**/*.js"],
          extra: {
          ssl: true
          }
          };
          const connection = createConnection(typeOrmOptions);
          ...


          If you manage different configurations, you will probably have to modify this snippet so you can enable / disable ssl depending on the environment for instance (no ssl in dev mode, ts-node fetches entities in .ts format, etc...).



          If you really need to generate ormconfig.json, then I'm afraid you have to generate the file from a script file, with the former code, just add a write part:



          ...
          const json = JSON.stringify(typeOrmOptions, null, 2);
          fs.writeFile("./target/ormconfig.json", json, (err) => {
          if (err) {
          console.error(err);
          return;
          }
          console.log("File has been created");
          });





          share|improve this answer













          You can use an url parser for heroku env var parsing such as pg-connection-string https://www.npmjs.com/package/pg-connection-string



          Then you use the createConnection function given by TypeOrm to initialize TypeOrm on the server side.



          import * as PostgressConnectionStringParser from "pg-connection-string";

          const databaseUrl: string = process.env.DATABASE_URL;
          const connectionOptions = PostgressConnectionStringParser.parse(databaseUrl);
          const typeOrmOptions: PostgresConnectionOptions = {
          type: "postgres",
          name: connectionOptions.name,
          host: connectionOptions.host,
          port: connectionOptions.port,
          username: connectionOptions.username,
          password: connectionOptions.password,
          database: connectionOptions.database,
          synchronize: true,
          entities: ["target/entity/**/*.js"],
          extra: {
          ssl: true
          }
          };
          const connection = createConnection(typeOrmOptions);
          ...


          If you manage different configurations, you will probably have to modify this snippet so you can enable / disable ssl depending on the environment for instance (no ssl in dev mode, ts-node fetches entities in .ts format, etc...).



          If you really need to generate ormconfig.json, then I'm afraid you have to generate the file from a script file, with the former code, just add a write part:



          ...
          const json = JSON.stringify(typeOrmOptions, null, 2);
          fs.writeFile("./target/ormconfig.json", json, (err) => {
          if (err) {
          console.error(err);
          return;
          }
          console.log("File has been created");
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 10:43









          zenbenizenbeni

          4,50822248




          4,50822248













          • Works like a charm

            – Stefan Wullems
            Jan 3 at 13:14



















          • Works like a charm

            – Stefan Wullems
            Jan 3 at 13:14

















          Works like a charm

          – Stefan Wullems
          Jan 3 at 13:14





          Works like a charm

          – Stefan Wullems
          Jan 3 at 13:14




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006757%2fhow-should-i-specify-the-database-url-in-ormconfig-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$