News for hackers. Stuff that matters.
Re:DenoDB by hn_throwaway_99 on Thursday June 17, 2021 @11:50PM (#27547182)
You're being downvoted, but I completely agree. If you are on Node and Postgres I highly recommend using slonik - it makes it easy to just write SQL but at the same time makes it almost impossible to have mistakes like SQL injections:

https://gajus.medium.com/stop-using-knex-js-and-earn-30-bf41...

https://github.com/gajus/slonik

Re:DenoDB by bhj on Friday June 18, 2021 @12:12AM (#27547278)
Along similar lines but more simple/agnostic, check out node-sql-template-strings[0] or sqlate[1].

[0]: https://github.com/felixfbecker/node-sql-template-strings

[1]: https://github.com/moll/js-sqlate

Re:DenoDB by mirekrusin on Friday June 18, 2021 @01:53AM (#27547738)
Re:DenoDB by girvo on Friday June 18, 2021 @02:11AM (#27547813)
> it makes it easy to just write SQL but at the same time makes it almost impossible to have mistakes like SQL injections

While I think this is totally the way to go, it is kind of amusing that PHP's PDO system was the "right" way to go all along in some ways

Re:DenoDB by hn_throwaway_99 on Friday June 18, 2021 @01:56PM (#27552997)
Meh, not really. Some of the biggest benefits from slonik are only possible because of Javascript's support for tagged template literals [1], e.g. you write a statement like

  sql`SELECT id FROM foo WHERE bar = ${barValue}`
Tagged template literals make it feel like you're just doing string concatenation, but it's done in such a way where under the covers it's actually creating prepared statements, and it is literally impossible to have SQL injection bugs unless you deliberately go out of your way to make them.

1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:DenoDB by girvo on Saturday June 19, 2021 @07:19PM (#27565455)
That is exactly what PDO’s prepared statements do. It’s not string concatenation, and it’s explicitly to prevent SQL injection?

I must be misunderstanding I guess.

Re:DenoDB by zomglings on Friday June 18, 2021 @06:40AM (#27549078)
The "Stop using knex.js" blog post misses a very important point in favor of using knex.js: database migrations.

Migrations allow a development team to synchronize database schema changes across multiple branches of development.

It is the main reason I end up using tools like knex.js (node) and sqlalchemy (python).

As far as knex is concerned, you can use it to manage migrations without using it in your application code.

Re:DenoDB by hn_throwaway_99 on Friday June 18, 2021 @01:43PM (#27552877)
I don't really think that blog post misses that, I just think that DB migrations functionality is very different from SQL builder functionality, and that blog post is referring to the SQL builder part of this.

FWIW, I do use knex for migrations, but I hate the SQL builder part of it, so most of my migrations are littered with knex.raw statements.

This was discussed at length in this slonik issue, https://github.com/gajus/slonik/issues/42 , with some recommendations for migrations functionality.

Re:DenoDB by ttfkam on Friday June 18, 2021 @11:16PM (#27557424)
Idempotent SQL updates are my preference.

``` ALTER TABLE IF EXISTS ADD COLUMN IF NOT EXISTS foo varchar; CREATE TABLE IF NOT EXISTS sample ( -- all the fields including foo ); ```

Run it on a clean DB, you get the table. Run it on an existing DB, it adds the column. Run it on a current DB, it does nothing. Run it once or a hundred times, the DB ends up in the same target state.

Can only be used on some databases though. The DBs without transactional DDL are a risk. Then again, they were a risk no matter what method you use. (I'm looking at you, MySQL.)

Re:DenoDB by ttfkam on Friday June 18, 2021 @11:07PM (#27557376)
I prefer the postgres module. 0 dependencies. Easy to use API. Wicked fast. Also really hard to fall victim to SQL injection attacks unless you actually put some extra effort into doing something foolish.

https://github.com/porsager/postgres#readme

Re:DenoDB by golergka on Friday June 18, 2021 @04:30AM (#27548472)
Thanks for the link to the slonik, never heard about it before. But if I'm not mistaken, if doesn't generate types for result days from the queries, unlike pgtyped?
Re:DenoDB by hn_throwaway_99 on Friday June 18, 2021 @02:00PM (#27553043)
> But if I'm not mistaken, if doesn't generate types for result days from the queries, unlike pgtyped?

You are correct. But note that pgtyped is really only able to generate types because it actually needs to run your queries against a running instance of your DB. Some contributors have commented they are working on something similar for Slonik, https://github.com/gajus/slonik/pull/267#issuecomment-840559...