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

@morivvv/json-sql-builder

v2.4.0

Published

The package is designed to convert JSON-specific queries to SQL. There is support for nested queries. By default, all queries are wrapped in a construct that imposes a check of access rights to table data To exclude schema names in which you do not

Downloads

55

Readme

The package is designed to convert JSON-specific queries to SQL.
There is support for nested queries.
By default, all queries are wrapped in a construct that imposes a check of access rights to table data To exclude schema names in which you do not need to do an access check, you can use a static variable in the FromTables class

import { FromTables } from "@morivvv/json-sql-builder/dist/queryBuilder/FromTables";

FromTables.accessTable = ["pg_catalog", "information_schema"];

Any query is built through the Query class. The class is automatically divided into sections. Depending on the JSON being passed, you can get the result for SELECT, INSERT, UPDATE, DELETE by calling different methods of the class

import { IJiraIssues } from "./schemas/jira";
import Query from "@morivvv/json-sql-builder";
import { ICreateTableFields } from "@morivvv/json-sql-builder/dist/types/restApi";
import {
  EBILLING,
  EJIRA,
} from "@morivvv/json-sql-builder/dist/const/schemaEnums";
import { FromTables } from "@morivvv/json-sql-builder/dist/queryBuilder/FromTables";
import { IBillingResources } from "./schemas/billing";

FromTables.accessTable = ["jira"];
const a = new Query<
  | ICreateTableFields<keyof IBillingResources, "e">
  | ICreateTableFields<keyof IJiraIssues, "au">,
  `${EBILLING | EJIRA}`
>(
  {
    fields: [
      "DISTINCT",
      "au.issuetype",
      "au.comments",
      "au.comments:alias_comm",
      "e.naimen",
    ],
    from: [
      "billing.addressees:u",
      "billing.approvals:e",
      "jira.issues:au",
      "jira.defects",
    ],
    join: ["e.id=au.created", "au.assignee_display_name=au.reporter_tabnum"],
    filter: {
      id: 1,
      test: { from: "test:a", fields: ["a.audit"], filter: { kod_user: 42 } },
    },
  },
  0,
  "token",
  "id"
);
console.log(a.getSelect(), a.getValues());

RESULT

SELECT DISTINCT au.issuetype, au.comments, au.comments AS alias_comm, e.naimen FROM (SELECT *
    FROM billing.approvals AS t
    WHERE
    NOT EXISTS (SELECT 1 FROM jira.rights_table as rt WHERE rt.naimen = 'billing.approvals' AND rt.active=true)
    OR EXISTS (SELECT 1 FROM jira.roles as r
      INNER JOIN jira.roles_users as ru ON r.id = ru.kod_role
      INNER JOIN jira.bz_users as u ON ru.kod_user = u.id
      INNER JOIN jira.bz_user_tokens as ut ON u.id = ut.kod_user
      WHERE ut.session_token = 'token'
      AND r.full_access = true
      AND u.active = true
      AND ut.active = true
      LIMIT 1)
    OR t.id in
    (
      SELECT table_identificator
      FROM jira.rights_elements as re
        INNER JOIN jira.rights_table as rt ON re.kod_table = rt.id
        INNER JOIN jira.roles as r ON re.kod_role = r.id
        INNER JOIN jira.roles_users as ru ON r.id = ru.kod_role
        INNER JOIN jira.bz_users as u ON ru.kod_user = u.id
        INNER JOIN jira.bz_user_tokens as ut ON u.id = ut.kod_user
      WHERE rt.naimen = 'billing.approvals'
        AND ut.session_token = 'token'
        AND u.active = true
        AND ut.active = true
    ) ) AS e
INNER JOIN jira.issues AS au
        ON e.id = au.created
        AND au.assignee_display_name = au.reporter_tabnum, (SELECT *
    FROM billing.addressees AS t
    WHERE
    NOT EXISTS (SELECT 1 FROM jira.rights_table as rt WHERE rt.naimen = 'billing.addressees' AND rt.active=true)
    OR EXISTS (SELECT 1 FROM jira.roles as r
      INNER JOIN jira.roles_users as ru ON r.id = ru.kod_role
      INNER JOIN jira.bz_users as u ON ru.kod_user = u.id
      INNER JOIN jira.bz_user_tokens as ut ON u.id = ut.kod_user
      WHERE ut.session_token = 'token'
      AND r.full_access = true
      AND u.active = true
      AND ut.active = true
      LIMIT 1)
    OR t.id in
    (
      SELECT table_identificator
      FROM jira.rights_elements as re
        INNER JOIN jira.rights_table as rt ON re.kod_table = rt.id
        INNER JOIN jira.roles as r ON re.kod_role = r.id
        INNER JOIN jira.roles_users as ru ON r.id = ru.kod_role
        INNER JOIN jira.bz_users as u ON ru.kod_user = u.id
        INNER JOIN jira.bz_user_tokens as ut ON u.id = ut.kod_user
      WHERE rt.naimen = 'billing.addressees'
        AND ut.session_token = 'token'
        AND u.active = true
        AND ut.active = true
    ) ) AS u, jira.defects AS at3
WHERE (id = $1
AND test in (SELECT a.audit FROM jira.test AS a
WHERE (kod_user = $2)   ))    [ 1, 42 ]

TS Intellij from interface

import {
  IJMQL,
  TMergeTInterface,
  IAliasTableFields,
} from "@morivvv/json-sql-builder/dist/types/restApi";
interface IBillingResources {
  table: "billing.resources";
  id: number;
  naimen: null | string;
  post: null | number;
  on_meter: null | boolean;
  on_people: null | boolean;
  date_add: null | string;
  active: boolean;
}

const сс: IJMQL<IBillingResources> = {
  fields: ["active", "naimen", "post:pt"],
  from: ["billing.resources"],
  filter: { naimen: "=:id" },
};

interface A1 {
  table: "blocl";
  key: 5 | 4 | 3 | 2 | 1;
  test: string;
  val: string;
  creatgdf: boolean;
  dte: number;
}
interface A2 {
  table: "blocl2";
  valu: number;
  red: false;
  dte: number;
}
interface A3 {
  table: "test.blocl3";
  valu: number;
  red: false;
  dte: number;
}

const a: IJMQL<
  TMergeTInterface<
    IAliasTableFields<A2, "a2">,
    IAliasTableFields<A1, "a1">,
    IAliasTableFields<A3, "a3">
  >
> = {
  from: ["test.blocl3:a3", "blocl:a1", "blocl2:a2", "test.blocl3:a3"],
  fields: ["DISTINCT", "a1.dte"],
  filter: [
    {
      "a1.creatgdf": "!=:false",
      "a3.valu": {
        from: ["test.blocl3:a3"],
        fields: ["a3.dte"],
        filter: { "a1.key": "@@<=:a3.red" },
      },
    },
  ],
};

Библиотека предназначена для преобразования JSON объекта в SQL.
Параметры для создания объекта максимально типизированы.
IJMQL - интерфейс всего объекта, который может принимать на вход класс new Query(a)
IBillingResources, A1-A3 - интерфейсы, описывающие все поля таблиц в базе данных. Обязательным ключом является поле table, в котором указывается название таблицы в БД
IAliasTableFields - вспомогательный тип, который для каждого поля добавляет указанный префикс, аналогично SQL алиаса
TMergeTInterface - вспомогательный тип, объединяющий несколько интерфейсов в один.
Все объекты могут быть вложены по правилам SQL: поля, условия фильтра, могут быть подзапросами