-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Pool connection sample * parse connection string to configure neonConfig.useSecureWebSocket --------- Co-authored-by: Timo Wilhelm <[email protected]>
- Loading branch information
1 parent
f5ef43b
commit c7bc06d
Showing
4 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,17 +57,36 @@ When using the local proxy you need to add a custom fetchEndpoint function to th | |
local proxy listens on `http://db.localtest.me:4444/sql`. | ||
|
||
```js | ||
import { neon, neonConfig } from '@neondatabase/serverless'; | ||
import { neon, neonConfig, Pool } from '@neondatabase/serverless'; | ||
import ws from 'ws'; | ||
const connectionString = 'postgres://postgres:[email protected]:5432/main'; | ||
/* Using single SQL query */ | ||
neonConfig.fetchEndpoint = (host) => { | ||
const [protocol, port] = host === 'db.localtest.me' ? ['http', 4444] : ['https', 443]; | ||
return `${protocol}://${host}:${port}/sql`; | ||
}; | ||
|
||
const sql = neon(connectionString); | ||
const [result] = await sql`SELECT * FROM NOW()`; | ||
|
||
console.log(result); | ||
|
||
/* or using Pool */ | ||
|
||
const connectionStringUrl = new URL(connectionString); | ||
neonConfig.useSecureWebSocket = connectionStringUrl.hostname !== 'db.localtest.me'; | ||
neonConfig.wsProxy = (host) => (host === 'db.localtest.me' ? `${host}:4444/v1` : undefined); | ||
neonConfig.webSocketConstructor = ws; // when using Node.js | ||
|
||
const pool = new Pool({ connectionString }); | ||
const { rows } = await pool.query('SELECT * FROM NOW()'); | ||
|
||
console.log(rows[0]); | ||
|
||
await pool.end(); | ||
``` | ||
|
||
## Developing | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { neon, neonConfig } from '@neondatabase/serverless'; | ||
import { neon, neonConfig, Pool } from '@neondatabase/serverless'; | ||
import ws from 'ws'; | ||
|
||
const connectionString = 'postgres://postgres:[email protected]:5432/main'; | ||
|
||
/* Using single SQL query */ | ||
|
||
neonConfig.fetchEndpoint = (host) => { | ||
const [protocol, port] = host === 'db.localtest.me' ? ['http', 4444] : ['https', 443]; | ||
return `${protocol}://${host}:${port}/sql`; | ||
|
@@ -11,3 +14,17 @@ const sql = neon(connectionString); | |
const [result] = await sql`SELECT * FROM NOW()`; | ||
|
||
console.log(result); | ||
|
||
/* or using Pool */ | ||
|
||
const connectionStringUrl = new URL(connectionString); | ||
neonConfig.useSecureWebSocket = connectionStringUrl.hostname !== 'db.localtest.me'; | ||
neonConfig.wsProxy = (host) => (host === 'db.localtest.me' ? `${host}:4444/v1` : undefined); | ||
neonConfig.webSocketConstructor = ws; // when using Node.js | ||
|
||
const pool = new Pool({ connectionString }); | ||
const { rows } = await pool.query('SELECT * FROM NOW()'); | ||
|
||
console.log(rows[0]); | ||
|
||
await pool.end(); |