Skip to content

Commit 603ffbc

Browse files
authored
docs: add supabase guide (#297)
* docs: add supabase guide * feat: add support for external connection poolers * chore: use .env symlink in database package * chore: remove database .env.example * docs: finish supabase pooling section * docs: fix small typo * docs: replace prisma with app wording * docs: fix up supabase pooling section * fix: deleted too much in contributing docs * docs: change pooling heading * docs: reduce linux shaming * docs: include direct connection url in fly.io
1 parent beef776 commit 603ffbc

File tree

17 files changed

+87
-17
lines changed

17 files changed

+87
-17
lines changed

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ SESSION_SECRET=abcdef1234
33
MAGIC_LINK_SECRET=abcdef1234
44
ENCRYPTION_KEY=ae13021afef0819c3a307ad487071c06 # Must be a random 16 byte hex string. You can generate an encryption key by running `openssl rand -hex 16` in your terminal
55
LOGIN_ORIGIN=http://localhost:3030
6-
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres?schema=public
6+
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres?schema=public
7+
# This sets the URL used for direct connections to the database and should only be needed in limited circumstances
8+
# See: https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#fields:~:text=the%20shadow%20database.-,directUrl,-No
9+
DIRECT_URL=${DATABASE_URL}
710
REMIX_APP_PORT=3030
811
APP_ENV=development
912
APP_ORIGIN=http://localhost:3030

.github/workflows/publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ jobs:
122122
# Setup environment variables
123123
cp ./.env.example ./.env
124124
cp ./examples/nextjs-test/.env.example ./examples/nextjs-test/.env.local
125-
cp ./packages/database/.env.example ./packages/database/.env
126125
127126
# Build packages
128127
pnpm run build --filter @examples/nextjs-test^...

CONTRIBUTING.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ branch are tagged into a release monthly.
3737
```
3838
pnpm i
3939
```
40-
4. Create your `.env` files
40+
4. Create your `.env` file
4141
```
42-
cp .env.example .env && cp packages/database/.env.example packages/database/.env
42+
cp .env.example .env
4343
```
44-
5. Open the root `.env` file and generate a new value for `ENCRYPTION_KEY`:
44+
5. Open it and generate a new value for `ENCRYPTION_KEY`:
4545

4646
`ENCRYPTION_KEY` is used to two-way encrypt OAuth access tokens and so you'll probably want to actually generate a unique value, and it must be a random 16 byte hex string. You can generate one with the following command:
4747

@@ -178,7 +178,6 @@ To run the end-to-end tests, follow the steps below:
178178
```sh
179179
cp ./.env.example ./.env
180180
cp ./examples/nextjs-test/.env.example ./examples/nextjs-test/.env.local
181-
cp ./packages/database/.env.example ./packages/database/.env
182181
```
183182
184183
2. Set up dependencies

apps/webapp/app/db.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function getClient() {
9494
datasources: {
9595
db: {
9696
url: DATABASE_URL,
97+
// We can't set directUrl here, and we don't have to
9798
},
9899
},
99100
log: [

apps/webapp/app/env.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SecretStoreOptionsSchema } from "./services/secrets/secretStore.server"
44
const EnvironmentSchema = z.object({
55
NODE_ENV: z.union([z.literal("development"), z.literal("production"), z.literal("test")]),
66
DATABASE_URL: z.string(),
7+
DIRECT_URL: z.string(),
78
SESSION_SECRET: z.string(),
89
MAGIC_LINK_SECRET: z.string(),
910
ENCRYPTION_KEY: z.string(),

apps/webapp/app/services/worker.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function getWorkerQueue() {
9494
connectionString: env.DATABASE_URL,
9595
concurrency: 5,
9696
pollInterval: 1000,
97+
noPreparedStatements: env.DATABASE_URL !== env.DIRECT_URL,
9798
},
9899
schema: workerCatalog,
99100
recurringTasks: {

docker/dev-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ services:
3232
- db
3333
environment:
3434
DATABASE_URL: postgres://postgres:postgres@db:5432/postgres?schema=public
35+
DIRECT_URL: postgres://postgres:postgres@db:5432/postgres?schema=public
3536
SESSION_SECRET: secret123
3637
MAGIC_LINK_SECRET: secret123
3738
REMIX_APP_PORT: 3030

docs/documentation/guides/self-hosting/flyio.mdx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ You should fork the repository before starting so you can make changes and commi
1919
brew install flyctl
2020
```
2121

22+
```sh Linux
23+
curl -L https://fly.io/install.sh | sh
24+
```
25+
2226
```sh Windows
2327
pwsh -Command "iwr https://fly.io/install.ps1 -useb | iex"
2428
```
@@ -41,6 +45,8 @@ fly launch
4145

4246
2. Follow the prompts by `fly launch` and make sure to answer them in the following way:
4347

48+
<Warning>Do NOT get rid of your terminal output, we will need your database connection string shortly.</Warning>
49+
4450
```sh
4551
? Would you like to copy its configuration to the new app? Yes
4652
? Choose an app name (leaving blank will default to 'trigger-v2-fly-demo') <enter your preferred app name here or leave blank>
@@ -54,18 +60,26 @@ fly launch
5460

5561
### Required
5662

57-
`MAGIC_LINK_SECRET`, `SESSION_SECRET` and `ENCRYPTION_KEY`
63+
1. `MAGIC_LINK_SECRET`, `SESSION_SECRET` and `ENCRYPTION_KEY`
5864

5965
All of these secrets should be 16-byte random strings, which you can easily generate (and copy into your pasteboard) with the following command:
6066

6167
```sh
6268
openssl rand -hex 16 | pbcopy
6369
```
6470

65-
`LOGIN_ORIGIN` and `APP_ORIGIN`
71+
2. `LOGIN_ORIGIN` and `APP_ORIGIN`
6672

6773
Both of these secrets should be set to the base URL of your fly application. For example `https://trigger-v2-fly-demo.fly.dev`
6874

75+
3. `DIRECT_URL`
76+
77+
This needs to be set to the database connection string that was printed to your terminal after the creation step above:
78+
79+
```sh
80+
Connection string: postgres://postgres:<PASSWORD>@<fly db name>.flycast:5432
81+
```
82+
6983
### Optional
7084

7185
`AUTH_GITHUB_CLIENT_ID` and `AUTH_GITHUB_CLIENT_SECRET`
@@ -97,6 +111,7 @@ fly secrets set \
97111
SESSION_SECRET=<random string> \
98112
LOGIN_ORIGIN="https://<fly app name>.fly.dev" \
99113
APP_ORIGIN="https://<fly app name>.fly.dev" \
114+
DIRECT_URL="postgres://postgres:<PASSWORD>@<fly db name>.flycast:5432" \
100115
FROM_EMAIL="Acme Inc. <[email protected]>" \
101116
REPLY_TO_EMAIL="Acme Inc. <[email protected]>" \
102117
RESEND_API_KEY=<your API Key> \

docs/documentation/guides/self-hosting/supabase.mdx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ENCRYPTION_KEY=1189c93e399856a2a9a1454496171b2e
5353
<img src="/images/supabase-project-setup.png" alt="Supabase - Project Setup" />
5454
</Frame>
5555

56-
5. Navigate to Project `Settings -> Database` and copy your database connection string
56+
5. Navigate to `Project Settings -> Database` and copy your database connection string
5757

5858
<Frame>
5959
<img src="/images/supabase-db-url.png" alt="Supabase - Database URL" />
@@ -82,6 +82,7 @@ SESSION_SECRET=8d92078940c89588fc8b6f5481f2c6e0
8282
ENCRYPTION_KEY=1189c93e399856a2a9a1454496171b2e
8383

8484
DATABASE_URL=postgresql://postgres:<PASSWORD>@db.<ID>.supabase.co:5432/postgres?schema=triggerdotdev
85+
DIRECT_URL=${DATABASE_URL}
8586

8687
NODE_ENV=development
8788
RUNTIME_PLATFORM=docker-compose
@@ -136,15 +137,53 @@ docker compose up
136137

137138
8. Congratulations, you're all set up and ready to go with Supabase and Docker! 🚀
138139

139-
{/* TODO: add pooling setup */}
140-
141140
## Bonus: Connection pooling
142141

142+
### What is connection pooling?
143+
143144
<Frame caption="Connection Pool - © 2023 Supabase, Apache License 2.0">
144145
<img src="/images/supabase-connection-pool.png" alt="Supabase - Connection Pool" />
145146
</Frame>
146147

147-
...coming soon!
148+
When running on a single server, our app can be trusted to manage its own connections - it knows how many are open and can process its internal queue as needed.
149+
150+
<Tooltip tip="🐝">Serverless</Tooltip> changes things. Here, many concurrent app instances are started, each unaware of the others' connections. This can lead to a huge - potentially fatal - increase in connections straight to your DB.
151+
152+
<Info>
153+
PostgreSQL has a [default connection limit](https://www.postgresql.org/docs/current/runtime-config-connection.html#:~:text=at%20server%20start.-,max_connections,-(integer)) of 100.
154+
</Info>
155+
156+
**External connection pools** solve this by putting an additional layer between client and server, which essentially works like a message queue that gets processed as limits allow. The clients are blind to this and can keep sending their requests as before.
157+
158+
### Enable connection pooling
159+
160+
1. Copy and paste the connection string from earlier to `DIRECT_URL`
161+
162+
```sh .env (excerpt)
163+
...
164+
DIRECT_URL=postgresql://postgres:<PASSWORD>@db.<ID>.supabase.co:5432/postgres?schema=triggerdotdev
165+
...
166+
```
167+
168+
2. Navigate to `Project Settings -> Database` and copy your connection pool URL
169+
170+
<Frame>
171+
<img src="/images/supabase-pool-url.png" alt="Supabase - Connection Pool URL" />
172+
</Frame>
173+
174+
3. Paste it into your `.env` file next to `DATABASE_URL`
175+
176+
4. Append `?schema=triggerdotdev&pgbouncer=true` and insert your password
177+
178+
```sh .env (excerpt)
179+
...
180+
# notice the differing ports and additional query param
181+
DATABASE_URL=postgresql://postgres:<PASSWORD>@db.<ID>.supabase.co:6543/postgres?schema=triggerdotdev&pgbouncer=true
182+
DIRECT_URL=postgresql://postgres:<PASSWORD>@db.<ID>.supabase.co:5432/postgres?schema=triggerdotdev
183+
...
184+
```
185+
186+
5. All done! You can now enjoy the benefits of connection pooling ⚡️
148187

149188
## Next steps
150189

docs/documentation/guides/tunneling-platform.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ There are a few ways to do this, but we recommend using [ngrok](https://ngrok.co
1818
brew install ngrok/ngrok/ngrok
1919
```
2020

21+
```sh Linux
22+
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | \
23+
sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null && \
24+
echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | \
25+
sudo tee /etc/apt/sources.list.d/ngrok.list && \
26+
sudo apt update && sudo apt install ngrok
27+
```
28+
2129
```sh Windows
2230
choco install ngrok
2331
```

0 commit comments

Comments
 (0)