# Updated Deploy Instructions
## [Supabase](https://supabase.com/)
supabase offers a postgres PaaS we will be using to deploy.
### Setting up a supabase account and database
* sign up for supabase with your github https://app.supabase.com/
* click the green new project button:

* add a project name, and click generate password. This password is *important* as it will be used to access your database.
* click 'copy' to copy the password to your clipboard and paste in someplace safe.
* click `create new project` once you are finished

Your database will take a few minutes to spin up: , once it is complete, we can take the neccesary measures to connect our app to the database.
* On the lefthand side of the screen, click the gear cog icon to open up app settings, and select `Database` from the hamburger menu:

* scroll down to the `Connect info` and `Connection string` section. this is the information we can use to connect to our databases.

### Setting up sequelize and psql to connect to a supabase database
back in your project repo, we need to configure sequelize to use a `config.js` file and also modify the `confi.json`. We will additionally need to add the database's uri as an enrivoment veriable.
* `touch .sequelizerc` in the top level of your project (it should be a sibling to `package.json`). Add the following contents to it:
```javascript
const path = require('path');
module.exports = {
config: path.resolve('config', 'config.js')
}
```
* Make sure that the `.sequelizerc` file is saved
* `touch ./config/config.js`, we are going to adapt the data in your `config.json` into this file and add an additional key:
```javascript
// in ./config/config.js
const fs = require('fs')
require('dotenv').config()
module.exports = {
development: {
// add the key/values pairs from your config.json here
},
production: {
use_env_variable: 'DATABASE_URI'
}
}
```
* Also, update your config json so it looks like this:
```javascript
{
"development": {
...
},
"production": {
"use_env_variable": "DATABASE_URI"
}
}
```
(FYI our apps will use the `config.js` when we use the sequelize cli and the `config.json` on deployment/nodemon. Optionally you could edit the `./models/index.js` file to load the `config.js` instead of the `config.json` delete the `config.json` completely. We will not be doing so, however.)
* if you don't have the `dotenv` package installed, install it now and `touch .env` file for enviromental variables
* head back to the `config string` area on supabase, and click on the `URI` tab. We are going to put in the `.env` file

* in your `.env` file add `DATABASE_URI` key and paste in the URI you copied from supabase as a string (surrounded by `''`). Edit the URI to have your password (the one your created when setting up the database) into the URI where it says `[YOUR-PASSWORD]`. Make sure to remove the `[]`.
**_NOTE: TO AVOID BUGS DO NOT FORGET THE QUOTES_**
```bash
# in your .env file
DATABASE_URI='postgresql://postgres:XXXXXXXXXXXXXXXX@db.wjyerwtbxazojugztrjz.supabase.co:5432/postgres'
```
* you can use that URI to connect to your database with the `psql` shell as well by running `psql 'postgresql://postgres:XXXXXXXXXXXXXXXX@db.wjyerwtbxazojugztrjz.supabase.co:5432/postgres'` :

_use quotes around the above command to prevent bugs with zsh reading unescaped characters: `psql '< your database uri >'`_
* use the command `sequelize db:migrate --env production` to migrate tables to your supabase database.
* `sequelize db:migrate:undo:all --env production` can be used to unmigrate in the future if needed
* using the `psql` shell, connect to your supabase database and `\dt` and `\d < table_name >` to verify that your database has migrated successfully

* when you deploy your app, be sure to set the `DATABASE_URI` as an envirment variable, and also set `NODE_ENV=production` so that your app connects to supabase as the db. Explore the boilerplate in the file `./modes/index.js` for more info on how this works.
## Koyeb
koyeb allows us to deploy our servers.
#### notes
* free tier does not need credit card
* apps take 10min to spin up at least
* new accounts can take 24hrs to activate
* CLI is buggy, but web UI is intuitive
* [pricing info](https://www.koyeb.com/pricing)
* following [these](https://www.koyeb.com/docs/quickstart/deploy-with-git) docs
### Create an account and sign up
1. sign up [here](https://app.koyeb.com/auth/signup) with github OAuth
* authorization can take up to 24hrs -- this step may need to happend the day before
* I had to refresh the page to see my dashboard
### setup your app locally
1. `touch Procfile` and add `web: node .` to it
1. add, commit and push
### create app on koyeb
1. create new app on koyeb
* add unique url, click next
1. click 'create github app' to link github
1. after the app is installed, you should see a dropdown of your repos, find the repo you would like to deploy and select it along with the branch you are deploying
* if you cannot find the repo you would like to deploy, click `Re-synconice repositories` and refresh the page
* if you _STILL_ cannot find the repo you wish to deploy, click on `edit GitHub app permissions`, which will take you to your github app settings for koyeb. Under 'Repository Access' click `Only select repositories`, find the repository you would like to deploy, and click the 'save'. You may need to refresh your page on Koyeb.

1. add your needed enviromental variables such as `DATABASE_URI`, `NODE_ENV`, and your api keys.
* **_NOTE:_** if using sequelize: unlike your local repo, do not add quotes around your `DATABASE_URI`, it will crash your app! 🥴
1. add a service name, such as `server`
1. click create app
1. wait for the app to start (can take up to 10-15 min) and then navigate to the public url so see your app
1. if your app errors out, check the `Runtime Logs` tab
## Install the CLI (optional -- not needed)
1. install the cli https://www.koyeb.com/docs/cli/installation
* `brew install koyeb/tap/koyeb`
* `brew upgrade koyeb`
* `koyeb completion bash > /usr/local/etc/bash_completion.d/koyeb`
* `koyeb completion zsh > "${fpath[1]}/_koyeb"`
* `echo "autoload -U compinit; compinit" >> ~/.zshrc`
* restart zsh
1. run `koyeb login`
* say `y` to create a new config file
* create a token at https://app.koyeb.com/account/profile
* paste token into zsh
more info on the koyeb CLI [here](https://www.koyeb.com/docs/cli/reference)
* use `koyeb apps list` to see deployments
* use `koyed deployments logs NAME` to see server logs in case of error
* you can also veiw the server logs on the `Runtime Logs` tab of a service instances dashboard
## [Flyio](https://fly.io/) -- server alternative (potential backup for server deployments)
fly.io can be used to deploy servers. They have a postgres PaaS too, but I couldn't get migrations to run.
Info on pricing here (requires credit card on free tier) [here](https://fly.io/pricing )
#### pros:
* Deployment is turbo easy from the CLI, and apps spin up real fast.
* CLI is easy to use and does everything (including signing up for a new account)
* free tier allots 100GB of data transfer per month (plenty) and charges are not made unless plan is upgraded from free
#### cons:
* needs credit card
* deploying is super easy, but there is no node instance on the vm so running ad hoc scripts is not possible after deployment (only true for node apps? rails and django apps can do this). These scripts could be added to the `fly.toml` file under a `[deploy]` section, however:
```dockerfile
[deploy]
release_command = "echo hello_world"
```
#### setup flyio
https://fly.io/docs/hands-on/install-flyctl/
```bash=
# install the cli
brew install flyctl
# sign up for an account
flyctl auth signup
# sign up w github
# add credit card
flyctl auth login
```
### deploy app
https://fly.io/docs/languages-and-frameworks/node/
[ref docs on cli](https://fly.io/docs/flyctl/)
* in the project directory, run `flyctl launch`
* create app name
* select server near you
* Once your app is deployed, you can edit the `fly.toml` under the `[env]` section by adding your apps enviroment variables that are non sensitive (ie `NODE_ENV=production`. Sensitive variables can be set with `fly secrets set ENC_SECRET='my super secret enc key'`, for example.
* use `flyctl deploy` to redeploy your app
more info on `flyctl` [here](https://fly.io/docs/flyctl/)
* if needed run `fly logs` to see your server's standard output logs in the event of an error