-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsample.mjs
31 lines (21 loc) · 972 Bytes
/
sample.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 =
connectionStringUrl.hostname === 'db.localtest.me' ? (host) => `${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();