3 ms·
I built a safe PostgreSQL driver for node from scratch with zero dependencies in about 1200 lines. I did this mostly because of the plumbing required to make th
by porsager 5y ago
I built a safe PostgreSQL driver for node from scratch with zero dependencies in about 1200 lines. I did this mostly because of the plumbing required to make the goto node driver tolerable to work with. In the process I made it 4-5 times faster as well.
https://github.com/porsager/postgres https://github.com/porsager/postgres
- megous 5y agoVery nice! I'd ditch pg-promise in a heartbeat for something simpler. I kinda dislike the tagged template literal helpers magic. I'd be more comfortable with explicit references to substituted arguments. Though it seems like I can just use the unsafe api all the time for that. The rest of the API is very nice, .stream, .cursor, notify support, all that.
- porsager 5y agoThanks! I'm curious - What's the reason you don't like the tagged template literal? It's those that give you implicit safe parameters and let's you avoid doing parameter counting when writing queries.
- megous 5y agoI don't like the sql() helpers part, and that they try to guess whether I want a helper for insert or update, based on some regexes and that helpers for different things all are sql(something) while doing very different things. https://github.com/porsager/postgres/blob/master/lib/index.js#L458 https://github.com/porsager/postgres/blob/master/lib/index.j... I also tend to compose queries out of smaller static parts dynamically, and tagged syntax does seem to make that hard. Patterns like: let params = {}; let conds = ['TRUE'] if (some_user_input) { conds.push('a = $test'); params.test = some_user_input; } db.query(` SELECT cols, ... FROM xxx WHERE abc AND ${conds.join(' AND ')} `, params); Not having a way to compose tagged literals and not having named parameters, only $1 just made it hard to port any of my existing projects based on pg-promise to your library. I use named parameters, so I don't do parameter counting. Connector does that for me, when rewriting the parameters to native postgresql syntax.
- porsager 5y agoThat makes perfect sense. Thanks a lot for the feedback.
- megous 5y agoAnyway, I'm a fan of suckless (https://suckless.org/ https://suckless.org/) and I try to use minimum set of dependencies (incl. transitive dependencies) in my node projects too. I like your project a lot from this perspective and that given its small size and lack of dependencies, it's also easily malleable to different needs, than the ones you anticipated.