目的
- Typeormを使って、環境ごとに接続するDBを切り分けます。
- 補足のところに書きましたが、もっといい方法があると思っているので、もしあれば教えていただきたいです。
環境
- Node: v10.15.2
- nodeenvを利用し、プロジェクトごとにバージョンを指定することがおすすめです。
利用するパッケージ
typeorm
使用方法
1. packageをinstall
今回はyarnでinstallします。
$ yarn add --dev typeorm
2. 設定ファイルを作成(任意)
環境ごとに ormconfig.{環境名}.json
を作成します。
- ormconfig.develop.json
{
"type": "mysql",
"host": "YOUR_DB_HOST",
"port": 3306,
"username": "YOUR_DB_USERNAME",
"password": "YOUR_DB_PASSWORD",
"database":"YOUR_DB_PASSWORD",
// migrationfileで世代管理するためには、ここをfalseにする必要があります。
"synchronize": false,
"logging": false,
"entities": [
"src/entities/*.ts"
],
"migrations": [
"src/migrations/*.ts"
],
"subscribers": [
"src/subscribers/*.ts"
],
"cli": {
"entitiesDir": "src/entities",
"migrationsDir": "src/migrations",
"subscribersDir": "src/subscribers"
}
}
- ormconfig.production.json
{
"type": "mysql",
"host": "YOUR_DB_HOST",
"port": 3306,
"username": "YOUR_DB_USERNAME",
"password": "YOUR_DB_PASSWORD",
"database":"YOUR_DB_PASSWORD",
// migrationfileで世代管理するためには、ここをfalseにする必要があります。
"synchronize": false,
"logging": false,
"entities": [
"src/entities/*.ts"
],
"migrations": [
"src/migrations/*.ts"
],
"subscribers": [
"src/subscribers/*.ts"
],
"cli": {
"entitiesDir": "src/entities",
"migrationsDir": "src/migrations",
"subscribersDir": "src/subscribers"
}
}
3. package.jsonの修正(script追記)
環境ごとのyarnスクリプトを実行し、ExpressプロジェクトにENV_SETTINGS
環境変数を渡します。
{
"scripts": {
"dev": "tsc && ts-node-dev src/index.ts",
"start:local": "yarn migration:run && tsc && ts-node src/index.ts",
"start:develop": "yarn migration:run:develop &&tsc && ENV_SETTINGS=\"develop\" ts-node src/index.ts",
"start:production": "yarn migration:run:production && tsc && ENV_SETTINGS=\"production\" ts-node src/index.ts",
"//": "use -n option. it defines migration file name. example: yarn migration -n createTableUser",
"migration": "ts-node ./node_modules/.bin/typeorm migration:generate -f ormconfig.local.json",
"migration:run": "ts-node ./node_modules/.bin/typeorm migration:run -f ormconfig.local.json",
"migration:run:develop": "ts-node ./node_modules/.bin/typeorm migration:run -f ormconfig.develop.json",
"migration:run:production": "ts-node ./node_modules/.bin/typeorm migration:run -f ormconfig.production.json",
"migration:revert": "ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.local.json",
"migration:revert:develop": "ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.develop.json",
"migration:revert:production": "ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.production.json"
}
}
4. 環境変数によって読み込むconfigファイルを切り分ける
- index.ts
import {createConnection} from "typeorm";
import App from './app';
// 環境変数にセットされているENV_SETTINGSを格納する
var env: string = (process.env.ENV_SETTINGS) ? process.env.ENV_SETTINGS : "local";
// 環境ごとにconfigファイルを読み込む
export const connectOption = require(`../ormconfig.${env}.json`);
createConnection(connectOption)
.then(async connection => {
const app = new App();
app.start();
})
.catch(error => console.log(error));
(補足)やりたかったこと
- ormconfigの設定ファイルは
ormconfig.ts
のみにして、各値をconfig/{環境}.tsにまとめ、export/importで行いたかったのですが、migration:runの時にエラーが発生し、断念しております。 (https://typeorm.io/#/using-ormconfig)
before
├── config
│ ├── develop.ts
│ ├── production.ts
│ └── undefined.ts
├── ormconfig.develop.json
├── ormconfig.local.json
├── ormconfig.production.json
├── package.json
├── src
│ ├── app.ts
│ ├── index.ts
after
├── config
│ ├── develop.ts
│ ├── production.ts
│ └── undefined.ts
├── ormconfig.ts
├── package.json
├── src
│ ├── app.ts
│ ├── index.ts