Squel: SQL Generator for Node.js

In this article, I will talk about the squel package, a SQL generator for node.js. To use a SQL generator is very productive, since we don’t need to directly write and debug the queries. Even though I will just show some simple queries, the package is very versatile.

Warning: You may also use squel on the browser, but you shouldn’t run any query in the server that was generated on the client, since it has serious security issues!

Installing and Configuring

In order to install and automatically add squel to your package.json dependencies, run

This package doesn’t need and configuration to be used.

Basic Usage

I took some examples from the official documentation.

Insert

Select

Update

Delete

Using Prepared Statements

Whenever is possible, you should use prepared statements. I wrote an article which shows how to use prepared statements with the `node-mysql2´ package, when you write your queries by yourself.

To do it with the squel package is simple.

And there is much more. It is possible to run subqueries, complex where conditions and generate queries specific to mysql or postgres databases. You really should take a look at the complete documentation!

Leave a Reply

Your email address will not be published. Required fields are marked *