brianc / node-postgres

PostgreSQL client for node.js.

Home Page:https://node-postgres.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why a little bit different request with the same meaning affects how fields are parsed

4nd3r5on opened this issue · comments

I'm trying to make a migration library and stuck with some problems

Schema created with:

export const createMigrationsTable = async (pool: pg.Pool) => {
  await pool.query(`
  CREATE TABLE IF NOT EXISTS applied_migrations (
    version BIGINT UNIQUE NOT NULL,
    label TEXT
  )`)
}

Trying to get all columns but specifying them:

let qresult = await client.query<{version: number, label: string}>("SELECT (version, label) FROM applied_migrations")
console.log(qresult.fields)

Log:

Log:
[
  Field {
    name: 'row',
    tableID: 0,
    columnID: 0,
    dataTypeID: 2249,
    dataTypeSize: -1,
    dataTypeModifier: -1,
    format: 'text'
  }
]

Trying to get all columns but using wildcard:

let qresult = await client.query<{version: number, label: string}>("SELECT * FROM applied_migrations")
console.log(qresult.fields)

Log:

Log:
[
  Field {
     name: 'version',
     tableID: 16389,
     columnID: 1,
     dataTypeID: 20,
     dataTypeSize: 8,
     dataTypeModifier: -1,
    format: 'text'
   },
   Field {
     name: 'label',
     tableID: 16389,
     columnID: 2,
     dataTypeID: 25,
     dataTypeSize: -1,
     dataTypeModifier: -1,
     format: 'text'
   }
]

But I would like to specify fields in the requests for a clarity reasons, and maybe more fields will be added latter so all the code that was using wildcard may brake. So I would like to know how properly parse rows in the case with specified fields in a query.

And why is it parsing version as a text? In the table version has bigint type and in generic I tried to specify it as a number

There was a mistake in query. The correct request is:

SELECT version, label FROM applied_migrations