Skip to content

Commit

Permalink
Add Pool connection sample (#8)
Browse files Browse the repository at this point in the history
* Add Pool connection sample

* parse connection string to configure neonConfig.useSecureWebSocket

---------

Co-authored-by: Timo Wilhelm <[email protected]>
  • Loading branch information
dhanushreddy291 and TimoWilhelm authored Nov 15, 2024
1 parent f5ef43b commit c7bc06d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"license": "MIT",
"dependencies": {
"@neondatabase/serverless": "^0.6.0"
"@neondatabase/serverless": "^0.6.0",
"ws": "^8.18.0"
}
}
19 changes: 18 additions & 1 deletion sample.mjs
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`;
Expand All @@ -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();

0 comments on commit c7bc06d

Please sign in to comment.