mirror of
https://github.com/supabase/supabase.git
synced 2026-10-14 21:59:16 +03:00
- Link the libpq pipeline-mode doc from the opening sentence.
- Show `{ prepare: false }` in the runnable code samples (troubleshooting
page's fix and the postgres.js quickstart's client setup), not just in
prose, per CodeRabbit review.
- Tighten the pipelining spelling-dictionary entry so it requires a real
suffix instead of matching bare "pipelin". CodeRabbit's own suggested
pattern, [Pp]ipeline(s|d|ing)?, would have broken matching for
"pipelining" (pipeline + ing concatenates to "pipelineing", not
"pipelining"), so kept the existing, correct alternation and only
dropped the trailing `?` that made it too permissive.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KpmtZWQTia6nfJfrYoh4gn
94 lines
2.4 KiB
Plaintext
94 lines
2.4 KiB
Plaintext
---
|
|
id: 'postgres-js'
|
|
title: 'Postgres.js'
|
|
description: 'Postgres.js Quickstart'
|
|
breadcrumb: 'ORM Quickstarts'
|
|
hideToc: true
|
|
---
|
|
|
|
## Connecting with Postgres.js
|
|
|
|
[Postgres.js](https://github.com/porsager/postgres) is a full-featured Postgres client for Node.js and Deno.
|
|
|
|
<StepHikeCompact>
|
|
|
|
<StepHikeCompact.Step step={1}>
|
|
|
|
<StepHikeCompact.Details title="Install">
|
|
|
|
Install Postgres.js and related dependencies.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```shell
|
|
npm i postgres
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={2}>
|
|
|
|
<StepHikeCompact.Details title="Connect">
|
|
|
|
Create a `db.js` file with the connection details.
|
|
|
|
To get your connection details, go to the [**Connect** panel](/dashboard/project/_?showConnect=true). Choose [**Transaction pooler**](/dashboard/project/_?showConnect=true&method=transaction) if you're on a platform with transient connections, such as a serverless function, and [**Session pooler**](/dashboard/project/_?showConnect=true&method=session) if you have a long-lived connection. Copy the URI and save it as the environment variable `DATABASE_URL`.
|
|
|
|
<Admonition type="caution">
|
|
|
|
`postgres.js` pipelines queries by default. Combined with transaction mode, this can hang queries or return mismatched rows. Bind at least one parameter in every query, or use session mode instead. See [postgres.js pipelining issues](/docs/guides/troubleshooting/postgres-js-pipelining-transaction-mode-issues).
|
|
|
|
</Admonition>
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```ts
|
|
// db.js
|
|
import postgres from 'postgres'
|
|
|
|
const connectionString = process.env.DATABASE_URL
|
|
const sql = postgres(connectionString, { prepare: false })
|
|
|
|
export default sql
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={3}>
|
|
|
|
<StepHikeCompact.Details title="Execute commands">
|
|
|
|
Use the connection to execute commands.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```ts
|
|
import sql from './db.js'
|
|
|
|
async function getUsersOver(age) {
|
|
const users = await sql`
|
|
select name, age
|
|
from users
|
|
where age > ${ age }
|
|
`
|
|
// users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
|
|
return users
|
|
}
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
</StepHikeCompact>
|