A fast, synchronized and dynamic query builder package.
In short, the query builder. You can write complex and parameterized queries fast, plain and dynamically using the Query class, which uses the chain of responsibility pattern
!
Generally, adding parameters to queries and parsing objects increases the distance of us as software developers to sql. This package was influenced by mongoose's Query class, aiming to make it usable in SQL databases, and that's what it ultimately does.
Run it on the drive you want! Sequelize can work with postgresql or any driver. All it needs is a driver that accepts sql code!
Looking forward to your pull requests and issues to make this package better.
Finally, all functions are gesture tested.
To include this package in your project, run the following command:
npm install @ssibrahimbas/query
or with yarn
yarn add @ssibrahimbas/query
And try this:
import { Query } from "@ssibrahimbas/query"
console.log(Query.table("users").getAll())
// SELECT * FROM users
If you want to contribute to the project or play with the codes on your local machine, follow the steps below:
npm install
or with yarn
yarn
npm run test
or with yarn
yarn test
Interface |
---|
ISsiQuery |
Abstract of the Query class. If you wish, you can use as follows:
import { ISsiQueue, SsiQuery } from "@ssibrahimbas/query"
const Query : ISsiQuery = new SsiQuery();
Although this doable, it is memory redundant as the new
key is constantly used. The Queue class has been developed to reset itself after each query. So you can use it with peace of mind as follows:
import { Query } from "@ssibrahimbas/query"
It is used to declare a table in SQL.
Abstract:
table(table: string | Array<string>) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").getAll();
// SELECT * FROM users
Using arrays:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table(["users", "products"]).analyze();
// ANALYZE TABLE users, products
Select the fields to use for the query
Abstract:
select(fields: Record<string,string> | Array<Record<string, string>> | string | Array<string>) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").select("id").getAll();
// SELECT id FROM users
Using arrays:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").select(["id As userId", "name As userName"]).analyze();
// SELECT id As userId, name As userName FROM users
Using Records:
import { Query } from "@ssibrahimbas/query"
const data = { Id: "userId", FirstName: "firstName" };
const query : string = Query.table("users").select(data).getAll();
// SELECT Id AS userId, FirstName AS firstName FROM users
Using Records With Arrays:
import { Query } from "@ssibrahimbas/query"
const data: Array<Record<string, string>> = [
{ Id: "userId" },
{ FirstName: "firstName" },
];
const query : string = Query.table("users").select(data).getAll();
// SELECT Id AS userId, FirstName AS firstName FROM users
Abstract:
least(fields: Array<string>, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").least(["point1", "point2", "point3"], "minPoint").getAll();
// "SELECT LEAST(point1, point2, point3) AS minPoint FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").least(["point1", "point2", "point3"]).getAll();
// "SELECT LEAST(point1, point2, point3) FROM users"
Abstract:
groupConcat(fields: string, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").groupConcat("UserId", "users").getAll();
// "SELECT GROUP_CONCAT(UserId) AS users FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").groupConcat("UserId").getAll();
// "SELECT GROUP_CONCAT(UserId) FROM users"
Abstract:
max(fields: string, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").max("id", "count").getAll();
// "SELECT MAX(id) AS count FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").max("id").getAll();
// "SELECT MAX(id) FROM users"
Abstract:
min(fields: string, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").min("id", "count").getAll();
// "SELECT MIN(id) AS count FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").min("id").getAll();
// "SELECT MIN(id) FROM users"
Abstract:
sum(fields: string, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").sum("id", "count").getAll();
// "SELECT SUM(id) AS count FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").sum("id").getAll();
// "SELECT SUM(id) FROM users"
Abstract:
count(fields: string, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").count("id", "count").getAll();
// "SELECT COUNT(id) AS count FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").count("id").getAll();
// "SELECT COUNT(id) FROM users"
Abstract:
avg(fields: string, name?: string | null) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").avg("id", "avg").getAll();
// "SELECT AVG(id) AS avg FROM users"
Single Parameter:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").avg("id").getAll();
// "SELECT AVG(id) FROM users"
Abstract:
innerJoin(table: string, field1: string, operator?: string, field2?: string) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("user_posts")
.select([
"user_posts.id as postId",
"user_posts.title as postTitle",
"users_username as username",
])
.innerJoin("users", "user_posts.userId", "users.id")
.getAll();
// "SELECT user_posts.id as postId, user_posts.title as postTitle, users_username as username FROM user_posts INNER JOIN users ON user_posts.userId = users.id"
Abstract:
leftJoin(table: string, field1: string, operator?: string, field2?: string) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.select(["users.id As userId", "user_posts.title As postTitle"])
.leftJoin("user_posts", "users.id", "user_posts.userId")
.getAll();
// "SELECT users.id As userId, user_posts.title As postTitle FROM users LEFT JOIN user_posts ON users.id = user_posts.userId"
Abstract:
rightJoin(table: string, field1: string, operator?: string, field2?: string) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("user_posts")
.select(["users.id As userId", "user_posts.title As postTitle"])
.rightJoin("users", "user_posts.userId", "users.id")
.getAll();
// "SELECT users.id As userId, user_posts.title As postTitle FROM user_posts RIGHT JOIN users ON user_posts.userId = users.id"
Abstract:
fullOuterJoin(table: string, field1: string, operator?: string, field2?: string) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.select(["users.id As userId", "user_posts.title As postTitle"])
.fullOuterJoin("user_posts", "users.id", "user_posts.userId")
.getAll();
// "SELECT users.id As userId, user_posts.title As postTitle FROM users FULL OUTER JOIN user_posts ON users.id = user_posts.userId"
Abstract:
leftOuterJoin(table: string, field1: string, operator?: string, field2?: string) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.select(["users.id As userId", "user_posts.title As postTitle"])
.leftOuterJoin("user_posts", "users.id", "user_posts.userId")
.getAll();
// "SELECT users.id As userId, user_posts.title As postTitle FROM users LEFT OUTER JOIN user_posts ON users.id = user_posts.userId"
Abstract:
rightOuterJoin(table: string, field1: string, operator?: string, field2?: string) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("user_posts")
.select(["users.id As userId", "user_posts.title As postTitle"])
.rightOuterJoin("users", "user_posts.userId", "users.id")
.getAll();
// "SELECT users.id As userId, user_posts.title As postTitle FROM user_posts RIGHT OUTER JOIN users ON user_posts.userId = users.id"
Abstract:
where(
where: string | Array<string>,
operator?: string | Array<string> | boolean | null,
val?: string | null | number,
type?: string,
andOr?: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.where("id", "=", "1")
.getAll();
// "SELECT * FROM users WHERE id = '1'"
Abstract:
orWhere(
where: string | Array<string>,
operator?: string | Array<string> | boolean | null,
val?: string | null | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.where("age", "20")
.orWhere("age", ">", "25")
.getAll();
// "SELECT * FROM users WHERE age = '20' OR age > '25'"
Abstract:
notWhere(
where: string | Array<string>,
operator?: string | Array<string> | boolean | null,
val?: string | null | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.where("age", "20")
.notWhere("age", ">", "25")
.getAll();
// "SELECT * FROM users WHERE age = '20' AND NOT age > '25'"
Abstract:
orNotWhere(
where: string | Array<string>,
operator?: string | Array<string> | boolean | null,
val?: string | null | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.where("age", "20")
.orNotWhere("age", ">", "25")
.getAll();
// "SELECT * FROM users WHERE age = '20' OR NOT age > '25'"
Abstract:
whereNull(
where: string,
not?: boolean
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").whereNull("email").getAll();
// "SELECT * FROM users WHERE email IS NULL"
Abstract:
whereNotNull(
where: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users").whereNotNull("email").getAll();
// "SELECT * FROM users WHERE email IS NOT NULL"
Abstract:
grouped(
callback: (q: ISsiQuery) => any
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.grouped((q) => {
q.where("country", "TURKEY").orWhere("country", "ENGLAND");
})
.getAll();
// "SELECT * FROM users WHERE (country = 'TURKEY' OR country = 'ENGLAND')"
Abstract:
in(
field: string,
keys: Array<string | number>,
type?: string,
andOr?: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("users")
.in("state", [1, 2, 3, 4])
.getAll();
// "SELECT * FROM users WHERE state IN ('1', '2', '3', '4')"
Abstract:
notIn(
field: string,
keys: Array<string | number>
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.notIn("id", [1, 2, 3])
.getAll();
// "SELECT * FROM test WHERE active = '1' AND id NOT IN ('1', '2', '3')"
Abstract:
orIn(
field: string,
keys: Array<string | number>
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orIn("id", [1, 2, 3])
.getAll();
// "SELECT * FROM test WHERE active = '1' OR id IN ('1', '2', '3')"
Abstract:
orNotIn(
field: string,
keys: Array<string | number>
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orNotIn("id", [1, 2, 3])
.getAll();
// "SELECT * FROM test WHERE active = '1' OR id NOT IN ('1', '2', '3')"
Abstract:
findInSet(
field: string,
key: string | number,
type?: string,
andOr?: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.findInSet("selected_tests", 1)
.getAll();
// "SELECT * FROM test WHERE active = '1' AND FIND_IN_SET (1, selected_tests)"
Abstract:
notFindInSet(
field: string,
key: string | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.notFindInSet("selected_tests", 1)
.getAll();
// "SELECT * FROM test WHERE active = '1' AND NOT FIND_IN_SET (1, selected_tests)"
Abstract:
orFindInSet(
field: string,
key: string | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orFindInSet("selected_tests", 1)
.getAll();
// "SELECT * FROM test WHERE active = '1' OR FIND_IN_SET (1, selected_tests)"
Abstract:
orNotFindInSet(
field: string,
key: string | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orNotFindInSet("selected_tests", 1)
.getAll();
// "SELECT * FROM test WHERE active = '1' OR NOT FIND_IN_SET (1, selected_tests)"
Abstract:
between(
field: string,
value1: string | number,
value2: string | number,
type?: string,
andOr?: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.between("age", 12, 35)
.getAll();
// "SELECT * FROM test WHERE active = '1' AND (age BETWEEN '12' AND '35')"
Abstract:
notBetween(
field: string,
value1: string | number,
value2: string | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.notBetween("age", 12, 35)
.getAll();
// "SELECT * FROM test WHERE active = '1' AND (age NOT BETWEEN '12' AND '35')"
Abstract:
orBetween(
field: string,
value1: string | number,
value2: string | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orBetween("age", 12, 35)
.getAll();
// "SELECT * FROM test WHERE active = '1' OR (age BETWEEN '12' AND '35')"
Abstract:
orNotBetween(
field: string,
value1: string | number,
value2: string | number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orNotBetween("age", 12, 35)
.getAll();
// "SELECT * FROM test WHERE active = '1' OR (age NOT BETWEEN '12' AND '35')"
Abstract:
like(
field: string,
data: string,
type?: string,
andOr?: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.like("title", "%nodeJS%")
.getAll();
// "SELECT * FROM test WHERE active = '1' AND title LIKE '%nodeJS%'"
Abstract:
orLike(
field: string,
data: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orLike("title", "%nodeJS%")
.getAll();
// "SELECT * FROM test WHERE active = '1' OR title LIKE '%nodeJS%'"
Abstract:
notLike(
field: string,
data: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.notLike("title", "%nodeJS%")
.getAll();
// "SELECT * FROM test WHERE active = '1' AND title NOT LIKE '%nodeJS%'"
Abstract:
orNotLike(
field: string,
data: string
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orNotLike("title", "%nodeJS%")
.getAll();
// "SELECT * FROM test WHERE active = '1' OR title NOT LIKE '%nodeJS%'"
Abstract:
limit(
limit: number,
limitEnd?: number |Â null
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test").where("active", "1").limit(20).getAll();
// "SELECT * FROM test WHERE active = '1' LIMIT 20"
Abstract:
offset(
offset: number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.limit(20)
.offset(200)
.getAll();
// "SELECT * FROM test WHERE active = '1' LIMIT 20 OFFSET 200"
Abstract:
pagination(
perPage: number,
page: number
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.pagination(20, 1)
.getAll();
// "SELECT * FROM test WHERE active = '1' LIMIT 20 OFFSET 20"
Abstract:
orderBy(
orderBy: string,
orderDir?: string | null
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.orderBy("date", "desc")
.getAll();
// "SELECT * FROM test WHERE active = '1' ORDER BY date DESC"
Abstract:
groupBy(
groupBy: string | Array<string>
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test").where("active", "1").groupBy("id").getAll();
// "SELECT * FROM test WHERE active = '1' GROUP BY id"
Abstract:
having(
field: string,
operator?: string | Array<string> | null,
val?: string | null
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const query : string = Query.table("test")
.where("active", "1")
.groupBy("place")
.having("AVG(salary)", ">=", "3000")
.getAll();
// "SELECT * FROM test WHERE active = '1' GROUP BY place HAVING AVG(salary) >= '3000'"
Abstract:
query(
query: string,
values?: Array<string | number> | null
) : ISsiQuery;
Example:
import { Query } from "@ssibrahimbas/query"
const _query = "SELECT * FROM users WHERE userId = ?";
const query = Query.query(_query, ["1"]).getQuery();
// "SELECT * FROM users WHERE userId = '1'"
Builds the select query with a limit of 1
Abstract:
get() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("users").select("id, firstName").get();
// "SELECT id, firstName FROM users LIMIT 1"
Builds the select query
Abstract:
getAll() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("users").select("id, firstName").getAll();
// "SELECT id, firstName FROM users"
Builds the insert query
Abstract:
insert(data: object) : string;
Example:
import { Query } from "@ssibrahimbas/query"
const data = {
id: "3",
firstName: "John",
};
const query = Query.table("test").insert(data);
// "INSERT INTO test (id, firstName) VALUES ('3', 'John')"
Builds the update query
Abstract:
update(data: object) : string;
Example:
import { Query } from "@ssibrahimbas/query"
const data = {
firstName: "John",
age: 22,
};
const query = Query.table("test").where("id", "3").update(data);
// "UPDATE test SET firstName='John', age='22' WHERE id = '3'"
Builds the delete query
Abstract:
delete() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("test").where("id", "54").delete();
// "DELETE FROM test WHERE id = '54'"
Builds the analyze query
Abstract:
analyze() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("test").analyze();
// "ANALYZE TABLE test"
Builds the check query
Abstract:
check() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("test").check();
// "CHECK TABLE test"
Builds the checksum query
Abstract:
checksum() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("test").checksum();
// "CHECKSUM TABLE test"
Builds the optimize query
Abstract:
optimize() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table("test").optimize();
// "OPTIMIZE TABLE test"
Builds the repair query
Abstract:
repair() : string;
Example:
import { Query } from "@ssibrahimbas/query"
const query = Query.table(["test", "test_2"]).repair();
// "REPAIR TABLE test, test_2"
It clears all the values of the query in the class, it is not recommended to use it externally.
Abstract:
reset() : void;
Example:
import { Query } from "@ssibrahimbas/query"
Query.reset();