Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active March 17, 2020 18:22
Show Gist options
  • Save rueian/7875061d0832563be89fe6467de5fefa to your computer and use it in GitHub Desktop.
Save rueian/7875061d0832563be89fe6467de5fefa to your computer and use it in GitHub Desktop.
node-postgres prep test
'use strict';
const crypto = require('crypto');
const { Client } = require('pg');
const url = 'postgres://postgres@localhost:5433/postgres?sslmode=disable';
const text = 'INSERT INTO kv (key, value, updated_at) VALUES ($1::text, $2::bytea, $3::timestamptz) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at RETURNING *';
const values = ['foo', crypto.randomBytes(10), new Date()];
(async function() {
const client = new Client({ connectionString: url });
await client.connect();
await client.query({ name: '1', text, values });
await client.end();
})().catch(err => console.error(err));
// Proxy Output:
//
// Parse Query: name=1 query=INSERT INTO kv (key, value, updated_at) VALUES ($1::text, $2::bytea, $3::timestamptz) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at RETURNING *
// Bind Parameter: 0 format=text len=3 value=foo
// Bind Parameter: 1 format=byte len=10 hex=15122af1302635072a2e
// Bind Parameter: 2 format=text len=29 value=2020-03-18T01:54:31.367+08:00
// Bind ResultFormatCodes: no result format codes
// Result DataRaw: column=0 format=text len=3 value=foo
// Result DataRaw: column=1 format=text len=22 value=\x15122af1302635072a2e
// Result DataRaw: column=2 format=text len=26 value=2020-03-17 17:54:31.367+00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment