TypeormからHerokuのPostgressに繋ぐときに色々と詰まったところ
Version情報
おおよそ2022年9月ごろ時点のお話
"pg": "^8.8.0", "typeorm": "^0.3.9" heroku/7.63.0 wsl-x64 node-v14.19.0
error no pg_hba.conf entry for host
どういうタイミングで出力されていたかは定かじゃないが、error no pg_hba.conf entry for host
というエラーがでてアプリが立ち上がらなかった。
ssl: true
だけだと今度はself signed certificate
と表示されエラーとなった。
参考になったサイト
sslの設定のほかにrejectUnauthorized: false
の設定が必要だったようだ(これで本当にいいのかはちょっとわからんが。。。)
ssl: true
にすると今度はローカルでのテスト時に影響があったので僕は以下のような形にしてみた。本番環境ではNODE_ENVをprodcution
にするということを前提があるので注意。
const option: DataSourceOptions = { ... - ssl: false, + ssl: process.env.NODE_ENV === "production", + extra: + process.env.NODE_ENV === "production" + ? { ssl: { rejectUnauthorized: false } } + : undefined, ...
syntaxerror: cannot use import statement outside a module
migration:run
を実施するタイミングでsyntaxerror: cannot use import statement outside a module
というエラーが出た。エラー吐くタイミングで落ちるので当然アプリとして立ちあらがず。
参考になったサイト
typescriptで開発していると起きうるケースのようだ。ビルド後のマイグレーションファイル(js)を読みにいくように設定する必要があるとのこと。ts-nodeの設定を変えて解決する方法もあるようだ。
syntaxerror: cannot use import statement outside a module typeorm
でぐぐるとたくさん出てくるので参考にしてみてほしい。
僕はこういう風に変えた
const option: DataSourceOptions = {
...
migrations: [
__dirname + "/migrations/**/*.ts",
+ __dirname + "/migrations/**/*.js",
],
...