DEV Community

Sami Salih İbrahimbaş
Sami Salih İbrahimbaş

Posted on

With Dynamic SQL Queries on TypeScript & NodeJS

In this article, I will show you how to write a more dynamic, fast and sustainable SQL queries with @ssibrahimbas/query package, which is the npm package I wrote with my own keyboard.

This package creates you SQL queries. But it doesn't run them. We usually use ready-made SQL clients (like sequelize, pg) to run SQL queries in NodeJS. I developed this package only as a query-builder by making use of the Single Responsibility pattern. It's the job of sequelize or pg to execute the query. Whichever you want!

Let's install the npm package

npm install @ssibrahimbas/query

for yarn yarn add @ssibrahimbas/query

Okey, now let's create an index.js file and code it

const { Query } = require("@ssibrahimbas/query");

  Query.table("users").select("id", "name").where("id", "=", 1).getAll()
Enter fullscreen mode Exit fullscreen mode

The process here is to create the query that returns the id and name field if id 1 from the users table.

When we run the code we will get the following result:
SELECT id FROM users WHERE id = '1'

Better install a driver and give it a try on the real database! It was developed entirely with TypeScript, full support for TypeScript & JavaScript and has +50 unit tests. And the documentation is pretty good too!

I would be grateful if you share your experiences as comments.

Npm Package

Top comments (1)

barathrum54 profile image

Looks great, i'll be glad to test and use it on my next prots.
I'll put my feedback both here and repo, thanks for sharing.