简体   繁体   English

如何使用 heroku 和 nodejs 设置 postgresql 配置?

[英]How to set up postgresql config with heroku and nodejs?

This is my first time try to host nodeJS application - built with hapi.js, typeorm and postgresql - on heroku.这是我第一次尝试在 heroku 上托管 nodeJS 应用程序 - 使用 hapi.js、typeorm 和 postgresql 构建。 I've create two apps on heroku - for "staging" (server-staging) and "production" (server-prod) - that using same code but will use different configuration.我在 heroku 上创建了两个应用程序 - 用于“登台”(服务器登台)和“生产”(服务器产品)——它们使用相同的代码但将使用不同的配置。 Why different configuration?为什么不同的配置? because each application on heroku will use different postgres credential, as it's attached as an add-ons.因为 heroku 上的每个应用程序都将使用不同的 postgres 凭据,因为它作为附加组件附加。

objective客观的

My objective/main question is How and where I have to set the database config for my application?我的目标/主要问题是我必须如何以及在哪里为我的应用程序设置数据库配置?

I use .env file (which I ignore in .gitignore - I don't want to put the credential in my repo ) to connect the application to my local database.我使用.env文件(我在.gitignore中忽略它 -我不想将凭证放入我的 repo )将应用程序连接到我的本地数据库。 Here is how the .env looks like:这是.env的样子:

HOST=localhost
PORT=3001

TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=password
TYPEORM_DATABASE=database
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=true
TYPEORM_LOGGING=false

In the application, I never do/write code such process.env.TYPEORM_USERNAME since its done by the typeorm node_modules.在应用程序中,我从不编写/编写诸如process.env.TYPEORM_USERNAME之类的代码,因为它是由 typeorm node_modules 完成的。 What I do to start the connection is by doing this:我开始连接的方法是这样做:

const server = new Hapi.Server({
    port: process.env.PORT,
    host: process.env.HOST,
    routes: {
      cors: Cors,
    },
  });

  await server.register(Plugins);

  server.route(Router);

  await createConnection();
  await server.start();

And my application automatically connected to the specified database as defined in the .env .我的应用程序自动连接到.env中定义的指定数据库。 Now, in heroku, the credential is lies here:现在,在 heroku 中,凭证就在这里:

在此处输入图像描述

All information lies there, but, [Q1] I don't know how to tell my application (of course, without store the credential in my code/repo) that I have to use the config as defined in above picture?所有信息都在那里,但是,[Q1] 我不知道如何告诉我的应用程序(当然,没有在我的代码/存储库中存储凭据)我必须使用上图中定义的配置? Also, as stated in above image, "Heroku rotates credentials periodically and updates applications where this database is attached.".此外,如上图所述,“Heroku 会定期轮换凭据并更新附加此数据库的应用程序。”。 Does it means the credentials will changed periodically?这是否意味着凭据会定期更改? [Q2] If yes, is there any way to make my application auto recognise the new credential? [Q2] 如果是,有什么方法可以让我的应用程序自动识别新的凭证?

Sorry if my explanation make confused.对不起,如果我的解释混淆了。 If you did not understand what I am trying to achieve, please ask things that you don't understand, so I can fix/update my question to make it understandable.如果您不理解我想要达到的目标,请询问您不理解的内容,以便我可以修复/更新我的问题以使其易于理解。

Anyway, I found this example first-example and second-example .无论如何,我找到了这个例子first-examplesecond-example But, they are using process.env.DATABASE_URL , which contain credential.但是,他们正在使用包含凭据的process.env.DATABASE_URL I think, it means that they not ignore their .env file in their repo?我认为,这意味着他们不会在他们的仓库中忽略他们的.env文件?

*) Note: Q1 means Question 1, and so for the rest *) 注意:Q1 表示问题 1,rest 也是如此

Write a ormconfig.js file in the root of your repo.在 repo 的根目录中编写一个 ormconfig.js 文件。 This way you can access the environment variables like the url provided from heroku and you don't have credentials in your repo.这样,您可以访问从 heroku 提供的 url 等环境变量,并且您的存储库中没有凭据。

 require('dotenv').config(); module.exports = [ { name: 'default', type: 'postgres', url: process.env.DATABASE_URL, synchronize: false, logging: false, entities: ['dist/entities/*.*'], migrations: ['dist/database/migrations/**/*.js'], subscribers: ['dist/database/subscribers/**/*.js'], cli: { entitiesDir: 'dist/entities', migrationsDir: 'dist/database/migrations', subscribersDir: 'dist/database/subscribers', }, }, { name: 'development', type: 'postgres', host: process.env.POSTGRES_HOST, port: process.env.POSTGRES_PORT, username: process.env.POSTGRES_USER, password: process.env.POSTGRES_PASSWORD, database: process.env.POSTGRES_DB, synchronize: true, logging: true, entities: ['src/entities/*.*'], migrations: ['src/database/migrations/**/*.ts'], subscribers: ['src/database/subscribers/**/*.ts'], cli: { entitiesDir: 'src/entities', migrationsDir: 'src/database/migrations', subscribersDir: 'src/database/subscribers', }, }, ];

With this configuration you can then get a specific configuration in javascript/typescript:使用此配置,您可以在 javascript/typescript 中获取特定配置:

let connectionOptions: ConnectionOptions;

if(process.env.NODE_ENV ==='development') { 
  connectionOptions = await getConnectionOptions("development");
} else {
  connectionOptions = await getConnectionOptions("default");
}
await createConnection(connectionOptions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM