porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.51k stars 275 forks source link

Unexpected Console output #546

Closed cfjello closed 1 year ago

cfjello commented 1 year ago

The following code:

// @deno-types="https://raw.githubusercontent.com/porsager/postgres/master/types/index.d.ts"
import postgres from 'https://deno.land/x/postgresjs/mod.js'
import { ghcnConfig } from "../GhcnConfig.ts"
//
// Client login for Postgresql
// 
function getClient() {
    return  postgres({
        username: ghcnConfig.dbConf.user,
        password: ghcnConfig.dbConf.password,
        database: ghcnConfig.dbConf.database,
        host: ghcnConfig.dbConf.hostname,
        port:  ghcnConfig.dbConf.port,
        ssl: false,
        // ssl: { rejectUnauthorized: false } ,
        max: ghcnConfig.dbConf.POOL_CONNECTIONS
    }) 
}
const sql = getClient()

try {
    // let result = await sql`DROP TABLE IF EXISTS Stations`
    // console.debug(`RESULT: ${result}`) 
    sql`CREATE TABLE IF NOT EXISTS Stations (ID VARCHAR(11),COUNTRY VARCHAR(2),NETC CHAR(1),STID VARCHAR(8),LATITUDE FLOAT8,LONGITUDE FLOAT8,GEO_POS GEOGRAPHY,ELEVATION FLOAT8,STATE VARCHAR(2),NAME VARCHAR(30),GSN_FLAG VARCHAR(3),HCN_CRN_FLAG VARCHAR(3),WMO_ID VARCHAR(5))`
    .then( result => {
        console.debug(`RESULT: ${result}`)
    }) 
} 
catch (err) {
    console.log(`ERROR: ${err}`)
}
sql.end()

produces the following console output when called the 2'nd and subsequent times:

{
  severity_local: "NOTICE",
  severity: "NOTICE",
  code: "42P07",
  message: 'relation "stations" already exists, skipping',
  file: "parse_utilcmd.c",
  line: "209",
  routine: "transformCreateStmt"
}
RESULT: 

Please note, that it is not put to the result, as I guess it should, and there is no error.

porsager commented 1 year ago

That's how PostgreSQL works, and silencing NOTICE by default would be too intrusive, so you have to do it explicitly using the onnotice option which by default is set to console.log.

benwilhelm commented 1 year ago

I was just cloning the repo to submit a PR that changed the default onnotice to noop. Thanks for clarifying :)

cfjello commented 1 year ago

Thanks for the hint! - I will use a callback function.