DEV Community

Discussion on: Built an unconventional database thing

Collapse
 
mindplay profile image
Rasmus Schultz • Edited

I've seen your squid project as well, and I'm glad to see someone exploring this direction. 😀

Running static analysis on the SQL bits is interesting - but appears to miss out on refactoring safety? I believe there is a much simpler and refactoring-safe way to approach this - have a look at this old thing I built in PHP some years back:

github.com/mindplay-dk/sql#select-...

That is, to use static references for all table and column symbols, e.g. interpolating schema/column objects and values into template literals:

sql`SELECT * FROM ${user} WHERE ${user.id} = ${id}`
Enter fullscreen mode Exit fullscreen mode

This approach would work much better with JS/TS than it did with PHP, since user and user.id would have types that the sql function can recognize. (in PHP the string interpolation happens before the call, which creates some corner cases and inconsistencies for e.g. INSERT statements in my library.)

Now you can rename user or user.id (with TS at least) and have automatic refactoring. You also get errors highlighted (by TS) for example after removing a column. Running static analysis on the SQL queries is probably much less necessary then, and the whole thing could be quite a bit simpler?

I know some users are averse to interpolation-heavy syntax - personally, considering how much this pays back in terms of safety and refactoring, I don't mind at all.

Prisma, TypeORM and Sequelize just don't do it for me - they're either too complex or too removed from SQL. I hope the idea of leveraging string interpolation catches on more widely in the future.

Cheers. 🙂