【问题标题】:How to set up postgresql config with heroku and nodejs?如何使用 heroku 和 nodejs 设置 postgresql 配置?
【发布时间】:2020-03-28 03:34:52
【问题描述】:

这是我第一次尝试在 heroku 上托管 nodeJS 应用程序——使用 hapi.js、typeorm 和 postgresql 构建。我在 heroku 上创建了两个应用程序——用于“staging”(服务器暂存)和“production”(server-prod)——它们使用相同的代码但将使用不同的配置。为什么不同的配置?因为 heroku 上的每个应用程序都将使用不同的 postgres 凭据,因为它是作为附加组件附加的。

目标

我的目标/主要问题是我必须如何以及在何处为我的应用程序设置数据库配置?

我使用.env 文件(我在.gitignore 中忽略了它 - 我不想将凭据放入我的存储库中)将应用程序连接到我的本地数据库。以下是.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

在应用程序中,我从不编写/编写 process.env.TYPEORM_USERNAME 这样的代码,因为它是由 typeorm node_modules 完成的。我开始连接的方法是这样做:

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();

并且我的应用程序自动连接到.env 中定义的指定数据库。现在,在 heroku 中,凭证就在这里:

所有信息都在那里,但是,[Q1] 我不知道如何告诉我的应用程序(当然,没有在我的代码/存储库中存储凭据)我必须使用上图中定义的配置?此外,如上图所述,“Heroku 会定期轮换凭据并更新附加此数据库的应用程序。”。这是否意味着凭据会定期更改? [Q2] 如果是,有什么方法可以让我的应用程序自动识别新的凭据?

对不起,如果我的解释混淆了。如果您不理解我要达到的目标,请提出您不理解的问题,以便我可以修复/更新我的问题以使其易于理解。

无论如何,我找到了这个例子first-example 和second-example。但是,他们使用的是process.env.DATABASE_URL,其中包含凭据。我认为,这意味着他们不会忽略他们的仓库中的.env 文件?

*) 注意:Q1 表示问题 1,其余的也是如此

【问题讨论】:

标签: node.js postgresql heroku


【解决方案1】:

在 repo 的根目录中编写一个 ormconfig.js 文件。这样您就可以访问环境变量,例如从 heroku 提供的 url,并且您的 repo 中没有凭据。

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',
        },
    },
];

通过此配置,您可以在 javascript/typescript 中获取特定配置:

let connectionOptions: ConnectionOptions;

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

【讨论】:

    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 2023-04-03
    • 2012-09-01
    • 2013-05-07
    • 2013-03-21
    • 1970-01-01
    • 2018-01-28
    • 2020-11-28
    相关资源
    最近更新 更多