porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid URL on README (TypeScript + Cypress)

autofas opened this issue · comments

I'm using TypeScript with Cypress. Following the example on the README, I did:

import postgres from 'postgres';
const sql = postgres('postgres://username:password@host:port/database', {
    host: 'localhost', // Postgres ip address[s] or domain name[s]
    port: 5432, // Postgres server port[s]
    database: 'postgres', // Name of database to connect to
    username: 'postgres', // Username of database user
    password: 'postgres', // Password of database user
});
export default sql;

But when running the script from Node, I got this error on postgres/cjs/src/index.js:543:18:
const urlObj = new URL(url.replace(host, host.split(',')[0]))
TypeError [ERR_INVALID_URL]: Invalid URL

As a workaround, I added a comma after the host on the URL and it worked: postgres://username:password@host,:port/database

Nevertheless, this behavior struck me as very odd. This is an example code.

you have a url with the word "port" instead of numbers.. Just remove dthe url, it looks like you are using the options object.

well you're supposed to replace the connection detail placeholders with actual values.

I think the example code is not clear enough, but the explanation is, which makes it a bit confusing. You could split the code in two, one showing only the formatted URL and the other just the options object.

You're welcome to make a PR, but remember it should be understandable in most cases, not just the understandig you have.

I also think the error message you get is fairly clear, so not super high priority imo. The docs do require some level of understanding of hostnames, ports and urls.