Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
-
TypeORM
ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
I like the idea and project goals, but the execution...
https://github.com/gajus/slonik/blob/master/src/routines/exe...
There may be an explanation for this horrifying code, but it should probably be right above in the form of:
/*
Any pitfalls that you encountered?
I'm trying to decide between this, or writing my own helpers around https://github.com/felixfbecker/node-sql-template-strings.
Also, have you found anything the solves typechecking of embedded expressions inside the SQL template?
For example, Webstorm will help you autocomplete your SQL, but you can still type:
SELECT * FROM foo WHERE barInt = ${bazStr}
Great to see this package here. We have been using slonik for over a year now with few regrets. I am not a fan of query builders. Query builders work for simple inserts or updates. For really complex queries, nothing beats writing SQL inside of the sql template tag.
Another nice thing about slonik is that it is built on top of https://github.com/brianc/node-postgres. One gets all the benefit of node-pg with the nicer dev experience of slonik.
The template tag solution is cute but really dangerous if you can accidentally leave off the `SQL` tag and the query still works.
Here's a solution that goes the long route of wrapping node-postgres' classes to ensure that you can never accidentally pass a basic string into query(): https://github.com/danneu/pg-extra/blob/be0e1a9fd513296e9bed...
slonik has no knowledge of the pg schema, so it cannot help with this.
You may want to look at something like https://github.com/adelsz/pgtyped. The downside is that dynamic queries are more challenging.
We have been looking at moving over to pgtyped, but it will be a hard transition because we do a fair amount of building up queries at runtime.
Have to say I'm pretty happy with TypeORM[0], this is going to have to be really good to pry that from my hands.
Slonik does have a wonderfully smaller API surface and I sure do like writing SQL manually and getting to take advantage of various lower level benefits (ex. postgres JSON operations) without too much fuss/work arounds, but TypeORM also lets you drop to raw (but parametrized) SQL fairly quickly, and it's query builder is actually pretty good...
[0]: https://typeorm.io/
Related posts
- What is the best PostgreSQL library for Node.js / JavaScript?
- Drizzle is just as unready for prime-time as Prisma, what else is there?
- An effective way to build a heavy CRUD Rest API?
- Authentication part 3 using NestJS and Postgres database neon.tech
- PostgresJs: The Fastest full featured PostgreSQL client for Node.js and Deno