[やってみた]自主学習 PlanetScale, Quick Start

## 参考、出典などのリンク

- [PlanetScaleというサーバレスDBが凄く勢いのあるサービスらしいのでQuick Startやってみ](https://qiita.com/tak001/items/cfbaa9dcb542929ff235)
- [PlanetScale](https://planetscale.com/)
- [PlanetScale quick start guide](https://docs.planetscale.com/tutorials/planetscale-quick-start-guide)
- [PlanetScale CLI](https://github.com/planetscale/cli#installation)


## やってみた、スタート

### アクセス

![20220320_IMG_0001.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/26c25637-352c-74c7-0564-2b9485b0a2d7.jpeg)

### GitHubでの登録とDB作成

![20220320_IMG_0002.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/62f54ec4-eabf-6204-8fbd-665763de3c30.jpeg)

![20220320_IMG_0003.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/3b91b250-f73a-a53d-dfb7-857b61d14e00.jpeg)

![20220320_IMG_0004.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/18412168-d750-d24f-8926-e59c1ded007c.jpeg)

![20220320_IMG_0005.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/ad84e6d0-56b2-cf8c-fedf-b2ab81111c40.jpeg)

![20220320_IMG_0006.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/353133b6-2831-891c-6b33-3bd8f403ba28.jpeg)

![20220320_IMG_0007.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/617c8344-5420-e490-36e3-108ac7f3f1be.jpeg)

![20220320_IMG_0008.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/d1b7765a-0739-dd5f-085d-78801491caa4.jpeg)

![20220320_IMG_0009.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/2455c9c1-7f5e-dd52-7809-040a9b3a0cf4.jpeg)

:::note info
DB名は、やっている途中で流していた、相対性理論のアルバム名から・・・
:::

### ブラウザ上でクエリを叩いてみる

![20220320_IMG_0010.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/280e1aa9-5c35-8ac5-7731-5db30c1c2cb4.jpeg)

![20220320_IMG_0011.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/4b7d8637-49fc-171e-8c1f-3226e46b48a0.jpeg)

![20220320_IMG_0012.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/5733febe-990f-809a-f3a7-2d94f759a97f.jpeg)

![20220320_IMG_0013.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/63bdb3c2-08b6-bf11-dedb-d630cb0e3b9b.jpeg)

![20220320_IMG_0014.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/12e915e1-9f30-7c35-9187-9ec5e69a73c9.jpeg)

### mainブランチを本番用にして保護する

![20220320_IMG_0015.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/b38a2b0e-a149-4812-313e-5c8e400bc617.jpeg)

![20220320_IMG_0016.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/eb7989b0-2435-cb11-b53d-1e7a8c30c905.jpeg)

### 新しい開発用ブランチを作成して、DBに接続する

![20220320_IMG_0017.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/7abd36ab-50cf-b0e2-35b7-d6e24c6d1973.jpeg)

![20220320_IMG_0018.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/71cb42d6-6703-7671-3ff7-ac3c5a456ef6.jpeg)

![20220320_IMG_0019.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/3570224e-524b-e3be-a4f9-4c58929dc6d9.jpeg)

![20220320_IMG_0020.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/2116f640-8a90-6e0e-f398-80580795eb95.jpeg)

```terminal
hogehoge@fugafugafuga develop % brew install planetscale/tap/pscale
==> Tapping planetscale/tap
Cloning into '/usr/local/Homebrew/Library/Taps/planetscale/homebrew-tap'...
remote: Enumerating objects: 504, done.
remote: Counting objects: 100% (504/504), done.
remote: Compressing objects: 100% (488/488), done.
remote: Total 504 (delta 162), reused 33 (delta 10), pack-reused 0
Receiving objects: 100% (504/504), 84.31 KiB | 2.28 MiB/s, done.
Resolving deltas: 100% (162/162), done.
Tapped 2 formulae (15 files, 106.5KB).
==> Downloading https://github.com/planetscale/cli/releases/download/v0.89.0/pscale_0.89.0_macOS_amd64.tar.gz
==> Downloading from https://objects.githubusercontent.com/github-production-release-asset-2e65be/317614271/05fc88b8-d84f-46ab-a10c-3498722e5314?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Cred
######################################################################## 100.0%
==> Installing pscale from planetscale/tap
==> Caveats
zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/pscale/0.89.0: 8 files, 10.2MB, built in 4 seconds
==> Running `brew cleanup pscale`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
hogehoge@fugafugafuga develop % 
```

:::note info
brew update, brew upgradeは事前にやっておきましょう・・・
:::

```terminal
hogehoge@fugafugafuga develop % pscale --version
pscale version 0.89.0 (build date: 2022-01-24T18:17:47Z commit: 6405542)
hogehoge@fugafugafuga develop %       
```

```terminal
hogehoge@fugafugafuga develop % pscale auth login

Confirmation Code: XXXXXXXX

If something goes wrong, copy and paste this URL into your browser: https://auth.planetscale.com/oauth/device?user_code=XXXXXXXX

Successfully logged in.
hogehoge@fugafugafuga develop %         
```

:::note info
Confirmation Codeがconsole上に表示されましたが、ブラウザ上で承認すれば問題なくログインできます。
:::

```terminal
hogehoge@fugafugafuga develop % pscale shell syncroniciteen dev1
Error: couldn't find the 'mysql' command-line tool required to run this command.
To install, run: brew install mysql-client
hogehoge@fugafugafuga develop % 
hogehoge@fugafugafuga develop % brew install mysql-client
==> Downloading https://ghcr.io/v2/homebrew/core/libcbor/manifests/0.9.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libcbor/blobs/sha256:d07e3853a1d2d1cabe8ac3f4c005a3d90226f9f5faa8d174f61c76b121a351cc
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d07e3853a1d2d1cabe8ac3f4c005a3d90226f9f5faa8d174f61c76b121a351cc?se=2022-03-20T01%3A40%3A00Z&sig=1SnOVN
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libfido2/manifests/1.10.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libfido2/blobs/sha256:61720be49bd0fdb87b7900122e22511514f87e0d5c73eb186664bfeee274f451
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:61720be49bd0fdb87b7900122e22511514f87e0d5c73eb186664bfeee274f451?se=2022-03-20T01%3A40%3A00Z&sig=SqLwiG
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql-client/manifests/8.0.28
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/mysql-client/blobs/sha256:fc037da725fac5450fd5246b0ba2854c963ca90f1b1ebff34f111dc4bcc41a80
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:fc037da725fac5450fd5246b0ba2854c963ca90f1b1ebff34f111dc4bcc41a80?se=2022-03-20T01%3A40%3A00Z&sig=W6ee3G
######################################################################## 100.0%
==> Installing dependencies for mysql-client: libcbor and libfido2
==> Installing mysql-client dependency: libcbor
==> Pouring libcbor--0.9.0.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/libcbor/0.9.0: 31 files, 161.7KB
==> Installing mysql-client dependency: libfido2
==> Pouring libfido2--1.10.0.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/libfido2/1.10.0: 510 files, 1MB
==> Installing mysql-client
==> Pouring mysql-client--8.0.28.monterey.bottle.tar.gz
==> Caveats
mysql-client is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with mysql (which contains client libraries).

If you need to have mysql-client first in your PATH, run:
  echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql-client you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql-client/include"

For pkg-config to find mysql-client you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/mysql-client/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/mysql-client/8.0.28: 168 files, 154.2MB
==> Running `brew cleanup mysql-client`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> mysql-client
mysql-client is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with mysql (which contains client libraries).

If you need to have mysql-client first in your PATH, run:
  echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql-client you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql-client/include"

For pkg-config to find mysql-client you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/mysql-client/lib/pkgconfig"

hogehoge@fugafugafuga develop %                                 
hogehoge@fugafugafuga develop % 
hogehoge@fugafugafuga develop % pscale shell syncroniciteen dev1
syncroniciteen/dev1> 
syncroniciteen/dev1> use users;
ERROR 1105 (HY000): unknown database 'users'
syncroniciteen/dev1> 
syncroniciteen/dev1> select count(*) from users;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
syncroniciteen/dev1> insert into `users` (id, email, first_name, last_name) values (1, 'fuga@test.com', 'ccccc', 'ddddd');
syncroniciteen/dev1> select count(*) from users;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
syncroniciteen/dev1> select * from users;
+----+---------------+------------+-----------+
| id | email         | first_name | last_name |
+----+---------------+------------+-----------+
|  1 | fuga@test.com | ccccc      | ddddd     |
+----+---------------+------------+-----------+
syncroniciteen/dev1> 
syncroniciteen/dev1> commit;
syncroniciteen/dev1> 
syncroniciteen/dev1> exit
hogehoge@fugafugafuga develop % 
```

:::note info
案の定mysqlがなかったので、インストールしてからリトライ。
:::


```terminal
hogehoge@fugafugafuga develop % git clone https://github.com/planetscale/express-example.git
Cloning into 'express-example'...
remote: Enumerating objects: 126, done.
remote: Counting objects: 100% (126/126), done.
remote: Compressing objects: 100% (92/92), done.
remote: Total 126 (delta 65), reused 78 (delta 32), pack-reused 0
Receiving objects: 100% (126/126), 13.31 MiB | 10.04 MiB/s, done.
Resolving deltas: 100% (65/65), done.
hogehoge@fugafugafuga develop % 
hogehoge@fugafugafuga develop % ls
express-example
hogehoge@fugafugafuga develop % 
hogehoge@fugafugafuga develop % cd express-example 
hogehoge@fugafugafuga express-example % 
hogehoge@fugafugafuga express-example % npm install       

added 65 packages, and audited 66 packages in 2s

found 0 vulnerabilities
npm notice 
npm notice New patch version of npm available! 8.5.2 -> 8.5.5
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.5.5
npm notice Run npm install -g npm@8.5.5 to update!
npm notice 
hogehoge@fugafugafuga express-example %
```

:::note warn
git cloneした後、express-exampleに移動してからnpm installを叩くのを忘れずに。
:::


```terminal
hogehoge@fugafugafuga express-example % pscale connect syncroniciteen dev1 --execute 'node app.js'
Secure connection to database syncroniciteen and branch dev1 is established!.

Local address to connect your application: 127.0.0.1:3306 (press ctrl-c to quit)
Example app listening at http://localhost:3000

```

:::note warn
'note app.jp'と打ち込んだら、そりゃ立ち上がらない。
:::

![20220320_IMG_0021.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/f802357a-cd44-ec7b-945d-36047f15c932.jpeg)

## 最後に

ローカルにDB置く必要もなく、自作アプリからも呼び出せるし、面白いなぁ。
入れるデータには気をつけなきゃいけないですが、色々と「あんなのこんなの」が想像できて、とても楽しいです。