brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.23k stars 1.22k forks source link

Type of result.rows is any[][] instead of any[] ? #3275

Closed ipython3 closed 2 months ago

ipython3 commented 3 months ago

Operating System: Ubuntu jammy 22.04 x86_64 node: 20.15.0 vscode: 1.91.0 pg: 8.12.0

Hi,

Thank you for creating this amazing library! I've just started using pg and ran into a strange problem.

In pg's documentation, I can see that the type of result.rows should be any[].

image

I'm writing a Node.js server that need to get the total user count in the user table in my database. So I wrote JavaScript code like this:

const result = await pool.query('SELECT COUNT(*) FROM users;');
const rows = result.rows;
return parseInt(rows[0].count);

The code can run smoothly without any error, but my editor (VS Code) detects the type of result.rows as any[][]. image Thus I get an linting error which says Property 'count' does not exist on type 'any[]'. image

I'm wondering if I'm using pg incorrectly? Or if I have configured VS Code incorrectly? If the type of result.rows is any[] according to pg's documentation, why VS Code detects the type of it as any[][]? I'm not familiar with TypeScript and I'm wondering if there is a proper way to handle this linting error? Maybe writing some JSDoc comments?

My jsconfig.json file:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES2022",
    "checkJs": true,
    "allowJs": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true
  },
  "exclude": [
    "node_modules",
    "**/node_modules/*"
  ]
}
cesco69 commented 3 months ago

The problem is ;

const result = await pool.query('SELECT COUNT(*) FROM users;');
//---------------------------------------------------------^

the char ; is used for multi statement sintax, eg.:

const result = await pool.query('SELECT COUNT(*) FROM users; SELECT COUNT(*) FROM orders;');

see https://github.com/brianc/node-postgres/issues/3121