diff --git a/README.md b/README.md index 75526e2..8f4d513 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,13 @@ 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:postgres@db.localtest.me: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`; @@ -68,6 +71,22 @@ neonConfig.fetchEndpoint = (host) => { 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 diff --git a/package-lock.json b/package-lock.json index a144c1d..ac95817 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "@neondatabase/serverless": "^0.6.0" + "@neondatabase/serverless": "^0.6.0", + "ws": "^8.18.0" } }, "node_modules/@neondatabase/serverless": { @@ -98,6 +99,27 @@ "node": ">=0.10.0" } }, + "node_modules/ws": { + "version": "8.18.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index 69e2305..4a4638c 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ }, "license": "MIT", "dependencies": { - "@neondatabase/serverless": "^0.6.0" + "@neondatabase/serverless": "^0.6.0", + "ws": "^8.18.0" } } diff --git a/sample.mjs b/sample.mjs index d8b4b35..9eacb7b 100644 --- a/sample.mjs +++ b/sample.mjs @@ -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:postgres@db.localtest.me: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();