Skip to content

Instantly share code, notes, and snippets.

@shaneosullivan
Created December 23, 2022 18:11
Show Gist options
  • Save shaneosullivan/555419454e536823d6f56fbc04612096 to your computer and use it in GitHub Desktop.
Save shaneosullivan/555419454e536823d6f56fbc04612096 to your computer and use it in GitHub Desktop.
Reproduce issue with Postgres for Node when updating table
/*
This Node script reproduces an issue where, when passing numbers to an
UPDATE SQL query in "pg", it complains that the values are of type
text.
There is an existing open issue at https://github.com/brianc/node-postgres/issues/2159
which is similar but the query is different
*/
const pg = require("pg");
const pgConfig = {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: process.env.PGHOST,
port: process.env.PGPORT,
};
async function runTest() {
const pgClient = new pg.Client(pgConfig);
// Connect to the client
await pgClient.connect();
console.log("Connected to the database");
try {
await pgClient.query(`
DROP TABLE IF EXISTS test_repro;
CREATE TABLE test_repro (
id BIGSERIAL PRIMARY KEY,
num BIGINT
);
`);
await pgClient.query(
`
UPDATE test_repro
SET
num = (
CASE
WHEN id = $1 then $2
END
)
WHERE id in ($3)
`,
[1, 2, 1]
);
} finally {
// Clean up
await pgClient.query(`
DROP TABLE IF EXISTS test_repro;
`);
}
await pgClient.end();
}
runTest().then(() => {
console.log("Complete");
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment