AbstractOptionalexecutor: SqlExecutor<T>Protected AbstractbuildOptionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Optionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Optionaloptions: RepositoryOptionsProtected ReadonlyexecutorProtected OptionaloptionsProtected ReadonlytableOptionalpayload: QueryRowsPayload<T>Optionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Optionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]AbstractOptionalexecutor: SqlExecutor<T>Protected AbstractbuildOptionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Optionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Runs parameterized SQL from sqlkit against a pg pool.
-sqlkit generates PostgreSQL-style placeholders ($1, $2, …).
Protected ReadonlyexecutorProtected ReadonlyloggingProtected ReadonlyreadProtected ReadonlywriteOptionalwhere: WhereCondition<T>Optionalpayload: QueryRowsPayload<T>Return the first matching row, or null.
-Pass a where condition or a full query payload (must include where).
Paginate the result of a query
-Optionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Optionalexecutor: SqlExecutor<T>Optionalcolumns: (keyof T)[]Creates a logical AND condition
-Array of conditions to combine with AND
-A CompositeWhere condition
-Builds a SET clause for UPDATE SQL statements with proper type handling
-Builds a WHERE clause for SQL queries
-Creates a greater than or equal condition
-A SimpleWhere condition
-Creates a case-insensitive pattern matching condition
-The column name
-The pattern to match against
-A SimpleWhere condition
-Creates a condition to check if a value is in a list
-A SimpleWhere condition
-Creates a condition to check if a value matches a case-insensitive regular expression
-The column name
-The regular expression pattern
-A SimpleWhere condition
-Creates a condition to check if a value is not null
-The column name
-A SimpleWhere condition
-Creates a condition to check if a value is null
-The column name
-A SimpleWhere condition
-Creates a case-sensitive pattern matching condition
-The column name
-The pattern to match against
-A SimpleWhere condition
-Creates a less than or equal condition
-A SimpleWhere condition
-Creates a logical NOT condition
-The condition to negate
-A CompositeWhere condition
-Creates a condition to check if a value is not in a list
-A SimpleWhere condition
-Creates a logical OR condition
-Array of conditions to combine with OR
-A CompositeWhere condition
-Creates a condition to check if a value matches a regular expression
-The column name
-The regular expression pattern
-A SimpleWhere condition
-Creates a logical XOR condition
-Array of conditions to combine with XOR
-A CompositeWhere condition
-sqlkit is a zero-dependency TypeScript query builder and repository layer for PostgreSQL. Install the pg driver alongside it when you execute queries.
npm install sqlkit pg
-
-
-
-Define Your Domain Model
-interface User {
id: string;
name: string;
email: string;
age?: number;
}
-
-
-import { SelectQueryBuilder } from "sqlkit";
const builder = new SelectQueryBuilder<User>("users");
const { sql, values } = builder
.select(["id", "name"])
.where({ key: "age", operator: ">", value: 18 })
// OR .where(eq("age", 18))
.build();
console.log(sql);
// SELECT "users"."id","users"."name" FROM "users" WHERE "users"."age" > $1
console.log(values); // [18]
-
-
-import { PostgresAdapter, SelectQueryBuilder } from "sqlkit";
import { Pool } from "pg";
const pool = new Pool({
/* your config */
});
const executor = new PostgresAdapter(pool);
const builder = new SelectQueryBuilder<User>("users", executor);
const { rows: users } = await builder
.select(["id", "name"])
.where({ key: "age", operator: ">", value: 18 })
// OR .where(eq("age", 18))
.commit();
console.log(users);
// => [{ id: "1", name: "John Doe" }, ...]
-
-
-Repository is configured with a single RepositoryConfig object:
| Field | -Required | -Description | -
|---|---|---|
tableName |
-yes | -Table or view for reads (find, findOne, paginate, count) |
-
mutableTableName |
-no | -Base table for writes (insert, update, delete). Defaults to tableName |
-
executor |
-yes | -SqlExecutor instance (e.g. PostgresAdapter) |
-
logging |
-no | -Log SQL, values, and row counts to the console | -
import { Repository, PostgresAdapter, gt, like, and, asc } from "sqlkit";
import { Pool } from "pg";
const pool = new Pool({
/* your config */
});
const executor = new PostgresAdapter(pool);
const userRepo = new Repository<User>({
tableName: "users",
executor,
});
// Read from a view, write to the base table
const itemRepo = new Repository<Item>({
tableName: "inventory__v_items",
mutableTableName: "inventory_items",
executor,
logging: true,
});
// Find many
const users = await userRepo.find({
where: and(gt("age", 25), like("name", "%Doe%")),
});
// Paginate (offset is derived from page and limit inside paginate)
const result = await userRepo.paginate({
page: 1,
limit: 10,
where: gt("age", 18),
columns: ["age", "email"],
orderBy: [asc("age")],
});
console.log(result.nodes); // array of users
console.log(result.meta);
/*
{
totalCount: 100,
currentPage: 1,
totalPages: 10,
hasNextPage: true
}
*/
// Find one (returns T | null)
const user = await userRepo.findOne(like("email", "%@example.com"));
// Or: findOne({ where: like(...), columns: ["id", "email"], orderBy: [asc("email")] })
// Count (optional filter — omit `where` to count all rows)
const total = await userRepo.count();
const adults = await userRepo.count(gt("age", 30));
// Insert — one object or an array (returns QueryResult)
const one = await userRepo.insert({
name: "Rayhan",
email: "ray@example.com",
});
const many = await userRepo.insert([
{ name: "A", email: "a@example.com" },
{ name: "B", email: "b@example.com" },
]);
// one.rows[0], many.rows — inserted rows
// Update
const updated = await userRepo.update({
data: { name: "Ray" },
where: like("email", "%ray%"),
});
// Delete
const deleted = await userRepo.delete({
where: like("name", "Ray%"),
});
-
-
-The constructor no longer accepts positional (tableName, executor, options?). Use a config object:
// 1.x
new Repository("users", executor, { logging: true });
// 2.x
new Repository({ tableName: "users", executor, logging: true });
-
-
-All generated SQL quotes table and column identifiers ("users", "inventory__v_items"), so mixed-case names in PostgreSQL resolve correctly. The quoteTableName() helper is exported if you build raw SQL alongside sqlkit.
Comparison
-Logical
-Sorting
-Creates a logical AND condition
+Array of conditions to combine with AND
+A CompositeWhere condition
+Builds a SET clause for UPDATE SQL statements with proper type handling
+Builds a WHERE clause for SQL queries
+Creates a greater than or equal condition
+A SimpleWhere condition
+Creates a case-insensitive pattern matching condition
+The column name
+The pattern to match against
+A SimpleWhere condition
+Creates a condition to check if a value is in a list
+A SimpleWhere condition
+Creates a condition to check if a value matches a case-insensitive regular expression
+The column name
+The regular expression pattern
+A SimpleWhere condition
+Creates a condition to check if a value is not null
+The column name
+A SimpleWhere condition
+Creates a condition to check if a value is null
+The column name
+A SimpleWhere condition
+Creates a case-sensitive pattern matching condition
+The column name
+The pattern to match against
+A SimpleWhere condition
+Creates a less than or equal condition
+A SimpleWhere condition
+Creates a logical NOT condition
+The condition to negate
+A CompositeWhere condition
+Creates a condition to check if a value is not in a list
+A SimpleWhere condition
+Creates a logical OR condition
+Array of conditions to combine with OR
+A CompositeWhere condition
+Creates a condition to check if a value matches a regular expression
+The column name
+The regular expression pattern
+A SimpleWhere condition
+Creates a logical XOR condition
+Array of conditions to combine with XOR
+A CompositeWhere condition
+sqlkit is a zero-dependency lightweight and expressive SQL query builder and repository layer for TypeScript with PostgreSQL support.
+npm install sqlkit
+
+
+
+Define Your Domain Model
+interface User {
id: string;
name: string;
email: string;
age?: number;
}
+
+
+import { SelectQueryBuilder } from "sqlkit";
const builder = new SelectQueryBuilder<User>("users");
const { sql, values } = builder
.select(["id", "name"])
.where({ key: "age", operator: ">", value: 18 })
// OR .where(eq("age", 18))
.build();
console.log(sql);
// SELECT "users"."id","users"."name" FROM "users" WHERE "users"."age" > $1
console.log(values); // [18]
+
+
+import { PostgresAdapter, SelectQueryBuilder } from "sqlkit";
import { Pool } from "pg";
const pool = new Pool({
/* your config */
});
const executor = new PostgresAdapter(pool);
const builder = new SelectQueryBuilder<User>("users", executor);
const users = await builder
.select(["id", "name"])
.where({ key: "age", operator: ">", value: 18 })
// OR .where(eq("age", 18))
.commit();
console.log(users);
// => [{ id: "1", name: "John Doe" }, ...]
+
+
+import { Repository, gt, like, and } from "sqlkit";
import { PostgresAdapter } from "sqlkit";
import { Pool } from "pg";
const pool = new Pool({
/* your config */
});
const executor = new PostgresAdapter(pool);
const userRepo = new Repository<User>("users", executor);
// Find many
const users = await userRepo.find({
where: and(gt("age", 25), like("name", "%Doe%")),
});
// Paginate
const result = await userRepo.paginate({
page: 1,
limit: 10,
offset: 2,
where: gt("age", 18),
columns: ["age", "email"],
orderBy: [asc("age")],
});
console.log(result.nodes); // array of users
console.log(result.meta);
/*
{
totalCount: 100,
currentPage: 2,
totalPages: 10,
hasNextPage: true
}
*/
// Find one
const user = await userRepo.find(like("email", "%@example.com"));
// Count
const count = await userRepo.count(gt("age", 30));
// Insert
const newUser = await userRepo.insert({
name: "Rayhan",
email: "ray@example.com",
});
// Update
const updated = await userRepo.update(
{ name: "Ray" },
like("email", "%ray%"),
);
// Delete
const deleted = await userRepo.delete(like("name", "Ray%"));
+
+
+Comparison
+Logical
+Sorting
+
Paginate the result of a query
+