npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sql-select-query-generator

v1.1.7

Published

SQL-Query-Generator to generate select Query with joins and alias name for the fields to be selected, and the total record count using a input JSON.

Downloads

32

Readme

SQL SELECT QUERY GENERATOR

Version

This package can be used to generate sql select queries for your tables with a JSON input.

Installation

Install sql-select-query-generator with npm

npm install sql-select-query-generator  

Usage/Examples

const queryGenerator = require("sql-select-query-generator");

const sampleQuery = {
  tableName: "Orders", // this is the target table and is always considered as t
  searchField: "customer.name",
  customSearch: [{ field: "", value: "" }],
  selectColumns: [{ fieldName: "*", alias: "" }],
  customOrSearch: ["customer.address", "customer.phone"],
  customAndSearch: ["t.total_cost"],
  customColumnQuery: "",
  joins: [
    {
      tableName: "Customers",
      joinName: "customer",
      type: "LEFT",
      isCustomJoin: false,
      selectColumns: [
        { fieldName: "name", alias: "customer_name" },
        { fieldName: "address", alias: "" },
        { fieldName: "phone", alias: "phone" },
      ],
    },
  ],
  queryParams: {
    limit: 20,
    offset: 5,
    sortBy: "DESC",
    orderBy: "t.id",
    searchTerm: "'abc",
  },
};

const generateSelectQuery = async()=>{
  var sql_query = await queryGenerator(sampleQuery);
  console.log(sql_query)
}

generateSelectQuery();

Output

SELECT ( SELECT COUNT( 1 ) from Orders t   LEFT JOIN Customers customer on
t.customer_id = customer.id  where  (customer.name like '%'abc%' OR
t.id like '%'abc%' ) OR (customer.address like '%'abc%' ) OR
(customer.phone like '%'abc%' ) AND (t.total_cost like '%'abc%' )  AND  = ''  ) AS total_count,
( SELECT   t.*     ,   customer.name  as customer_name, customer.address  , customer.phone  as phone  from Orders t
LEFT JOIN Customers customer on
t.customer_id = customer.id   where   (customer.name like '%'abc%' OR t.id like '%'abc%' )
OR (customer.address like '%'abc%' ) OR
(customer.phone like '%'abc%' ) AND (t.total_cost like '%'abc%' )
order by t.id  DESC OFFSET 5 ROWS FETCH NEXT 20 ROWS ONLY FOR JSON PATH ) AS data

Input JSON Structure

The following is the structure of the input JSON object.

const QueryConfig = {
  tableName: "", // this is the target table and is always considered as t
  searchField: "",
  customSearch: [{ field: "", value: "" }],
  selectColumns: [{ fieldName: "", alias: "" }],
  customOrSearch: ["joinName.fieldName", "joinName.fieldName"],
  customAndSearch: ["joinName.fieldName", "joinName.fieldName"],
  nullCheckColumns: ["joinName.fieldName", "joinName.fieldName"],
  customColumnQuery: "",
  joins: [
    {
      tableName: "",
      joinName: "",
      type: "",
      isCustomJoin: false,
      selectColumns: [{ fieldName: "", alias: "" }],
    },
  ],
  queryParams: {
    limit: 0,
    offset: 0,
    sortBy: "",
    orderBy: "",
    searchTerm: "",
  },
};

Desciption of the JSON structure

The above JSON defines an object called "QueryConfig" which appears to be used for
querying a database. It includes various properties for specifying the details of the
query such as the table name, fields to be searched, conditions for searching, columns
to be selected, join details and query parameters.
  • tableName: This is a string variable which specifies the name of the table to be queried.

  • searchField: This is a string variable which specifies the field that should be searched in the table.

  • customOrSearch, customAndSearch, nullCheckColumns: These are arrays of strings specifying the fields that should be searched, and conditions for searching.

  • customColumnQuery: This is a string variable which contains custom query for selected columns.

  • customSearch: This is an array of objects, where each object contains two properties:

    • field: A string specifying the field name that should be searched.
    • value: A string specifying the value to be searched in the specified field.
  • selectColumns: This is an array of objects, where each object contains two properties:

    • fieldName: A string specifying the field name that should be selected.
    • alias: A string specifying the alias for the selected field.
  • joins: This is an array of objects, where each object contains several properties:

    • tableName: A string specifying the name of the table to be joined.
    • joinName: A string specifying the name of the join.
    • type: A string specifying the type of join.
    • isCustomJoin: A Boolean value indicating whether the join is custom or not.
    • selectColumns: An array of objects, where each object contains two properties:
      • fieldName: A string specifying the field name of the joined table that should be selected.
      • alias: A string specifying the alias for the selected field.
  • queryParams: This is an object which contains several properties:

    • limit: An integer specifying the maximum number of rows to be returned in the query.
    • offset: An integer specifying the number of rows to skip before starting to return rows.
    • orderBy: A string specifying the field name to sort the results by.
    • sortBy: A string specifying the order of sorting (e.g. "ASC" for ascending, "DESC" for descending).
    • searchTerm: A string specifying the term to search in the query.

Sample SQL creation queries to test the package

Sample Table Creation

Create the table Customers

CREATE TABLE Customers (
id INT PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
is_active [bit] NULL,
phone VARCHAR(20));
Create the table Orders

CREATE TABLE Orders (
id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_cost DECIMAL(10, 2),
is_active [bit] NULL,
FOREIGN KEY (customer_id) REFERENCES Customers(id))
Create the table Order_Details

CREATE TABLE Order_Details (
id INT,
product_id INT,
quantity INT,
price DECIMAL(10, 2),
FOREIGN KEY (order_id) REFERENCES Orders(id),
is_active [bit] NULL,
FOREIGN KEY (product_id) REFERENCES Products(id));
Create the table Products

CREATE TABLE Products (
id INT PRIMARY KEY,
name VARCHAR(255),
description VARCHAR(255),
is_active [bit] NULL,
price DECIMAL(10, 2));

Authors

License

MIT License

Contributing

Contributions are always welcome!

Please adhere to this project's code of conduct.

🚀 About Me

export class Info {

  name: string = 'Amrishkhan'
  occupation: string = 'Full Stack Developer'
  
}

export class ConnectViaSocial {

  linkedin: string = 'https://www.linkedin.com/in/amrishkhan/'
  github: string = 'amrishkhan05'
  instagram: string = '@aka_batman'

}