Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 21 21:44
    joshxyzhimself opened #146
  • Jan 20 13:50
    joshxyzhimself commented #121
  • Jan 20 12:51
    joshxyzhimself commented #121
  • Jan 19 09:23
    marcbachmann commented #123
  • Jan 19 09:23
    marcbachmann commented #123
  • Jan 19 09:08
    marcbachmann closed #123
  • Jan 19 08:42
    porsager commented #123
  • Jan 19 08:42
    porsager commented #142
  • Jan 19 08:41
    porsager commented #142
  • Jan 19 08:40

    porsager on master

    Please eslint 2.0.0-beta.3 (compare)

  • Jan 19 08:40

    porsager on v2.0.0-beta.3

    (compare)

  • Jan 19 08:37
    porsager commented #123
  • Jan 19 08:36
    porsager commented #123
  • Jan 19 08:35

    porsager on master

    Add timeout override feature to… Increase timeout for parallel s… (compare)

  • Jan 19 08:29

    porsager on master

    Fix SCRAM-SHA-256 authenticatio… (compare)

  • Jan 19 08:29
    porsager closed #145
  • Jan 19 08:29
    porsager commented #145
  • Jan 19 08:24
    mortifia commented #145
  • Jan 19 08:24
    mortifia commented #145
  • Jan 19 08:23
    mortifia commented #145
Rasmus Porsager
@porsager
The thing that I still haven't figured out is why it matters that I catch and rethrow the error.. If I don't do that, it will throw the error properly in userland.. Probably a race condition where the rethrow moves the userland throw to the next tick, and then it lands up behind the end promise that hangs.. Still not sure though, but shouldn't matter if I fix the hanging connection end promise..
Rasmus Porsager
@porsager
Also learned that socket.end(resolve) is not the right thing to use for our case, but instead socket.once('close', resolve)
Rasmus Porsager
@porsager
@spacejack ok, just released beta.2 which should fix these issues. Please let me know if everything is good for you now ;)
keb
@keb
@porsager no worries! I tried better-sqlite3 but ran into some Windows-specific build issues on installation. Ended up using sqlite3 + sqlite and sql-template-strings. It's all relatively comfortable (though not as tiny as postgres). :P Gonna save postgres for when I'm in production
Jeremy Cantrell
@jmcantrell
I really like this module so far. I'm curious about something related to json columns. The following query seems to work, and I'm trying to understand what sql.json() is for:
await sql`insert into users ${sql({
    username: "user",
    password: JSON.stringify({ salt: "foo", hash: "bar" }),
})}`;
Jeremy Cantrell
@jmcantrell
I'm curious if there's a way to write that query so that I don't have to stringify the password. Does this module know how to deal with json column types?
prasanna-k-s
@prasanna-k-s
" invalid input syntax for type double precision" when inserting for a float type of postgres, Is there a way to set the type of the column when inserting into DB.
spacejack
@spacejack
@porsager sadly, no, beta 2 still has an uncaught exception. I wonder if it has to do with the first query being in a transaction.
Are identifiers quoted when using sql({...})? I'm getting a syntax error for a column named "end": syntax error at or near "end" when I try to INSERT into foo ${sql(foo)}
spacejack
@spacejack
FYI the uncaught exception for incorrect DB credentials on Linux/Ubuntu is: error: uncaughtException: write CONNECTION_CLOSED localhost:5432
Artem Makoven
@makoven
Hello. Which extension do you prefer to highlight the query syntax of this library in the VSCode? It seems they are all not very popular and rather abandoned.
Rasmus Porsager
@porsager
@spacejack sorry, I've been real busy lately, but I'm gonna look closer at it tonight.. Thanks for the feedback
sql() is not in the final shape I have planned yet, but I will try to outline my ideas in an issue tonight unless I actually get it finished :P
@makoven I don't use vscode, so I don't have any, but maybe someone else here does?
spacejack
@spacejack

Having trouble getting IN or ANY to work for this query:

sql`SELECT id, foo_id FROM bar WHERE foo_id = ANY (${['2478455463672']})`

Which is logged as:

SELECT id, foo_id FROM bar WHERE foo_id = ANY ($1)
params: [ [ { type: 0, value: '2478455463672' } ] ]

I get the error:

malformed array literal: '2478455463672'

With just one array element, but it also happens with more than one.
The PG type is bigint so the array is string values representing ints.

I also tried an array of numbers, but get this error:

syntax error at or near ","

conn 1 query: `SELECT id, foo_id FROM bar WHERE foo_id = ANY ($1,$2)`
params: [ [ { type: 0, value: '234' }, { type: 0, value: '145' } ] ]

And with just one number in the array:

malformed array literal: "234"

conn 1 query: `SELECT id, foo_id FROM bar WHERE foo_id = ANY ($1)`
params: [ [ { type: 0, value: '234' } ] ]
spacejack
@spacejack
Ah, I had to write it this way:
WHERE foo_id = ANY(array[${['2478455463672']}]::bigint[])
^^ That might be handy for the docs. When I get a spare minute sometime I hope I can remember to make a PR for the docs.
Sergii Stotskyi
@stalniy

Hi @porsager

Thanks for the awesome lib! When can we expect v2 to be production ready?

Rasmus Porsager
@porsager
Hey, thanks 😊 I've unfortunately been swamped with work lately, and there is an essential part in dynamic queries i want to get done before release. I use v2 beta myself in production though, so besides a few edge cases I'd say it's stable
Sergii Stotskyi
@stalniy

Hi @porsager

Is there a way to get parameters which I sent to sql when an error happens?

To illustrate what I mean (silly example that generates error):

sql`
  SELECT CONT(id)::int as amount
  FROM "person"
  WHERE nationality = ${'UK'} and id = ${1}
`

This query fails because of typo and the error has parameters property. When I check this property, I see this:

[ { type: 0, value: 'UK' }, { type: 0, value: '1' } ]

I understand that this is serialized parameters with their data types in Postges but this is not what I passed to sql function. So, it's not fine to log something like this into log file (or even in stack trace) because it confuses. I also cannot map values to value property because then it looks like I passed string '1' to id but I passed a normal number. This becomes even more confusing when we deal with booleans and the value equals to f or t

debug option behaves the same way, I'd like to have a way to get back parameters I passed to sql function without writing additional logic on my end to unserialize all of those
Rasmus Porsager
@porsager
Hi @stalniy 🙂 I see, you want the parameters before they're serialized.. That seems like a good idea.
How about adding raw with that value to those objects
so it'd be
[ { type: 0, value: 'UK', raw: 'UK' }, { type: 0, value: '1', raw: 1 } ]
or perhaps input is a better name
Sergii Stotskyi
@stalniy
Yes, I think raw or original is fine
Sergii Stotskyi
@stalniy
let me know if I can do anything to help you release it soon!
Rasmus Porsager
@porsager
Thanks! It seems I finally have some time for Postgres.js again, so I'm hoping to look into this and the 2.0 release the coming days :)
Sergii Stotskyi
@stalniy
That's awesome!
Rasmus Porsager
@porsager
Issues on github need some cleanup, but I think most will be closed out by the 2.0 release, so I'm holding off on that for now
would love to discuss my thoughts around the improvements to dynamic queries.. I noticed you mentioned feeling sql`` was a bit overloaded, and that you thought it would affect V8 perf.
Sergii Stotskyi
@stalniy
Let's discuss it! Probably the best place is in comments for that issue. So, others can participate as well.
Rasmus Porsager
@porsager
Sure thing 😉 I'm gonna make some examples for how I imagined the API first
Minigugus
@Minigugus

:point_up: 17 octobre 2020 04:59

Maybe I'm a bit late... I wrote https://github.com/Minigugus/postgres-ts-plugin, a TypeScript plugin that executes your queries against a configured postgresql server. It is dedicated to the postgres library, only sql tags from this lib are checked ;) VSCode supports natively TypeScript plugins, as long as you use your typescript project version (should work with others IDEs by the way) ;)

Jeremy Cantrell
@jmcantrell
Are there any recommendations on how to mock this library for testing?
Rasmus Porsager
@porsager
@jmcantrell can you describe what kind of mocking you would like to do?
spacejack
@spacejack

Hey @porsager, happy new year! I finally had a bit of time to repro the uncaught exception I was seeing. This will reliably fail for me on Windows & WSL2:

'use strict'
const Postgres = require('postgres')

const sql = Postgres({
    host: 'localhost',
    port: 5432,
    database: 'test',
    username: 'test',
    password: 'test',
    debug: (c, q, ...params) => {
        console.log('conn ' + c + ' query: `' + q + '`\nparams:', params)
    }
})

async function run() {
    const rows = await sql.begin(tsql => tsql`SELECT table_name
        FROM information_schema.tables
        WHERE table_type='BASE TABLE' AND table_schema='public'
    `)
    console.log(`${rows.length} rows`)
}

run().then(() => {
    process.exit()
}).catch(err => {
    console.error('Caught exception:', err)
    process.exit(1)
})

Resulting in this uncaught exception (assuming the connection params used above are invalid):
UnhandledPromiseRejectionWarning: Error: write CONNECTION_CLOSED localhost:5432

Rasmus Porsager
@porsager
👏 Awesome @spacejack .. I'll take a look tomorrow evening 😊 Happy new year to you too 🎉
spacejack
@spacejack
Cool! Sorry I couldn't give you a better new year's gift :innocent:

Using the transaction is the thing. if I had just wrote

const rows = await sql`SELECT table_name...`

the exception would be caught.

Nicolas T.
@traedamatic
Hello everyone, I have a question about the sql.unsafe function. Is this function still unsafe if you use it this way:
db.unsafe(`UPDATE carts set (firstname) = ($1) where id = $2;`, [ firstName, email, ]);
Sergii Stotskyi
@stalniy
It’s safe if you use placeholders
Nicolas T.
@traedamatic
Maybe it is good idea to rename the function to "query" or "rawQuery" because "unsafe" is confusing name for new developers
Sergii Stotskyi
@stalniy
It’s still unsafe if used incorrectly. Thats why it has such a name. Because a junior dev without any back thought can interpolate variables in ES6 template string and pass them as parameters
In comparison to sql which is by default safe
Mortifia
@mortifia

Hello everybody,
I just discovered this very interesting project and I ask myself a question.
anyone ever thought about creating an apollo-datasource with postgres.js?
a bit like SQLDataSource

https://www.apollographql.com/docs/apollo-server/data/data-sources/#community-data-sources

John Yan
@yanjh007

I check the postgres git lib and I think it's coll, so I writed a sample code to learn it . The query is OK, but when I use the cursor, throw a error about the cursor function not exist, here is my code and Error, very simple , no cursor ,is ok, what happen to me?

qr = await db`
select stuid,xm from pt_student_all limit 10
`.cursor(async row=>{
    console.log(row[0]);
});

(node:7668) UnhandledPromiseRejectionWarning: TypeError: db(...).cursor is not a function
at Timeout._onTimeout (c:\Work\NodeDev\dmerge\t2.js:22:7)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use node --trace-warnings ... to show where the warning was created)

Rasmus Porsager
@porsager
Sorry, cursor is only in v2@beta. You'd need to do npm install postgres@beta
Sergii Stotskyi
@stalniy
@porsager do you have any ETA on the dynamic sql support and/or bug fixes? I'm specifically interested in getting raw parameter values: porsager/postgres#140