herokuにnode.jsとprismaで環境作成

#アジェンダ
　表題通り、heroku環境にnodejs＋postgressの環境作成。
　環境作成時に、prismaでTBLのマイグレーションを実施する。

#1. アプリサーバの作成

```javascript:heroku環境作成
~/develop/postgress $ mkdir heroku
~/develop/postgress $ cd heroku 
~/develop/postgress/heroku $ curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=3 prisma-examples-latest/deployment-platforms/heroku
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  241k    0  241k    0     0   492k      0 --:--:-- --:--:-- --:--:--  492k

~/develop/postgress/heroku $ yarn 
yarn install v1.22.5
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
warning Your current version of Yarn is out of date. The latest version is "1.22.15", while you're on "1.22.5".
info To upgrade, run the following command:
$ curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
✨  Done in 13.60s.

~/develop/postgress/heroku $ git checkout -b main
Switched to a new branch 'main'


~/develop/postgress/prisma-heroku $ heroku apps:create
Creating app... done, ⬢ ＄{アプリ名}
https://＄{アプリ名}.herokuapp.com/ | https://git.heroku.com/＄{アプリ名}.git


heroku git:remote --app ＄{アプリ名}

~/develop/postgress/prisma-heroku $ git add .
~/develop/postgress/prisma-heroku $ git commit -m 'Initial commit'
On branch main
nothing to commit, working tree clean
~/develop/postgress/prisma-heroku $ git log -1
commit 7982a1567cebead920cb664876ddeba092e35f36 (HEAD -> main, heroku/main)
Author: XXXXXXXXXXXXXXXXXXX
Date:   Sat Dec 4 16:21:21 2021 +0900

    Initial commit
```

#2. DBサーバの作成
```javascript:DB環境作成

~/develop/postgress/prisma-heroku $ heroku addons:create heroku-postgresql:hobby-dev
Creating heroku-postgresql:hobby-dev on ⬢ ＄{アプリ名}... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-concave-63603 as HEROKU_POSTGRESQL_COBALT_URL
Use heroku addons:docs heroku-postgresql to view documentation


~/develop/postgress/prisma-heroku $ git push heroku
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 8 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (17/17), 9.40 KiB | 3.13 MiB/s, done.
Total 17 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Node.js app detected
remote:        
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:        
remote:        Resolving node version 14.x...
remote:        Downloading and installing node 14.18.2...
remote:        Using default npm version: 6.14.15
remote:        
remote: -----> Installing dependencies
remote:        Installing node modules
remote:        
remote:        > @prisma/client@3.5.0 postinstall /tmp/build_f22cda95/node_modules/@prisma/client
remote:        > node scripts/postinstall.js
remote:        
remote:        Prisma schema loaded from prisma/schema.prisma
remote:        
remote:        ✔ Generated Prisma Client (3.5.0) to ./node_modules/@prisma/client in 156ms
remote:        You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
remote:        ```
remote:        import { PrismaClient } from '@prisma/client'
remote:        const prisma = new PrismaClient()
remote:        ```
remote:        
remote:        > prisma@3.5.0 preinstall /tmp/build_f22cda95/node_modules/prisma
remote:        > node scripts/preinstall-entry.js
remote:        
remote:        
remote:        > @prisma/engines@3.5.0-38.78a5df6def6943431f4c022e1428dbc3e833cf8e postinstall /tmp/build_f22cda95/node_modules/@prisma/engines
remote:        > node download/index.js
remote:        
remote:        
remote:        > prisma@3.5.0 install /tmp/build_f22cda95/node_modules/prisma
remote:        > node scripts/install-entry.js
remote:        
remote:        added 54 packages in 6.592s
remote:        
remote: -----> Build
remote:        Running build
remote:        
remote:        > prisma-heroku@1.0.0 build /tmp/build_f22cda95
remote:        > prisma generate
remote:        
remote:        Prisma schema loaded from prisma/schema.prisma
remote:        
remote:        ✔ Generated Prisma Client (3.5.0) to ./node_modules/@prisma/client in 72ms
remote:        You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
remote:        ```
remote:        import { PrismaClient } from '@prisma/client'
remote:        const prisma = new PrismaClient()
remote:        ```
remote:        
remote: -----> Caching build
remote:        - node_modules
remote:        
remote: -----> Pruning devDependencies
remote:        audited 54 packages in 0.527s
remote:        found 0 vulnerabilities
remote:        
remote:        
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote:        Procfile declares types -> release, web
remote: 
remote: -----> Compressing...
remote:        Done: 104.9M
remote: -----> Launching...
remote:  !     Release command declared: this new release will not be available until the command succeeds.
remote:        Released v7
remote:        https://${アプリ名}.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
remote: Running release command...
remote: 
remote: Prisma schema loaded from prisma/schema.prisma
remote: Datasource "db": PostgreSQL database "XXXXXXXXXXXXX", schema "public" at "ec2-X-XXX-XX-XX.compute-1.amazonaws.com:5432"
remote: 
remote: 1 migration found in prisma/migrations
remote: 
remote: Applying migration `20210310152103_init`
remote: 
remote: The following migration have been applied:
remote: 
remote: migrations/
remote:   └─ 20210310152103_init/
remote:     └─ migration.sql
remote:       
remote: All migrations have been successfully applied.
To https://git.heroku.com/＄{アプリ名}.git
 * [new branch]      main -> main
~/develop/postgress/prisma-heroku $ 

```

#3. 結果の確認

![スクリーンショット 2021-12-04 17.38.44.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/26a494c3-6a00-032b-441f-b776db3f3306.png)

![スクリーンショット 2021-12-04 17.40.01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/ff396d9a-0466-2285-9203-3a99c53bb6a8.png)

#出典
https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-heroku