|
| 1 | +# sqlkit |
| 2 | + |
| 3 | +**sqlkit** is a lightweight and expressive SQL query builder and repository layer for TypeScript with PostgreSQL support. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🔧 Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install sqlkit |
| 11 | +``` |
| 12 | + |
| 13 | +## 🚀 Usage Examples |
| 14 | + |
| 15 | +### 1. Query Builder (Build Mode) |
| 16 | + |
| 17 | +```ts |
| 18 | +import { SelectQueryBuilder } from "sqlkit"; |
| 19 | + |
| 20 | +const builder = new SelectQueryBuilder("users"); |
| 21 | + |
| 22 | +const { sql, values } = builder |
| 23 | + .select(["id", "name"]) |
| 24 | + .where({ key: "age", operator: ">", value: 18 }) |
| 25 | + .build(); |
| 26 | + |
| 27 | +console.log(sql); |
| 28 | +// SELECT "users"."id","users"."name" FROM "users" WHERE "users"."age" > $1 |
| 29 | +console.log(values); // [18] |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Query Execution (Commit Mode) |
| 33 | + |
| 34 | +```ts |
| 35 | +import { PostgresAdapter, SelectQueryBuilder } from "sqlkit"; |
| 36 | +import { Pool } from "pg"; |
| 37 | + |
| 38 | +const pool = new Pool({ |
| 39 | + /* your config */ |
| 40 | +}); |
| 41 | +const executor = new PostgresAdapter(pool); |
| 42 | + |
| 43 | +const builder = new SelectQueryBuilder("users", executor); |
| 44 | + |
| 45 | +const users = await builder |
| 46 | + .select(["id", "name"]) |
| 47 | + .where({ key: "age", operator: ">", value: 18 }) |
| 48 | + .commit(); |
| 49 | + |
| 50 | +console.log(users); |
| 51 | +// => [{ id: "1", name: "John Doe" }, ...] |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Repository API 🔥 |
| 55 | + |
| 56 | +```ts |
| 57 | +import { Repository, gt, like, and } from "sqlkit"; |
| 58 | +import { PostgresAdapter } from "sqlkit"; |
| 59 | +import { Pool } from "pg"; |
| 60 | + |
| 61 | +const pool = new Pool({ |
| 62 | + /* your config */ |
| 63 | +}); |
| 64 | +const executor = new PostgresAdapter(pool); |
| 65 | +const userRepo = new Repository("users", executor); |
| 66 | + |
| 67 | +// Find many |
| 68 | +const users = await userRepo.findRows({ |
| 69 | + where: and(gt("age", 25), like("name", "%Doe%")), |
| 70 | +}); |
| 71 | + |
| 72 | +// Find one |
| 73 | +const user = await userRepo.findRow(like("email", "%@example.com")); |
| 74 | + |
| 75 | +// Count |
| 76 | +const count = await userRepo.count(gt("age", 30)); |
| 77 | + |
| 78 | +// Insert |
| 79 | +const newUser = await userRepo.insertOne({ |
| 80 | + name: "Rayhan", |
| 81 | + |
| 82 | +}); |
| 83 | + |
| 84 | +// Update |
| 85 | +const updated = await userRepo.update({ name: "Ray" }, like("email", "%ray%")); |
| 86 | + |
| 87 | +// Delete |
| 88 | +const deleted = await userRepo.delete(like("name", "Ray%")); |
| 89 | +``` |
| 90 | + |
| 91 | +### 🔍 Supported Operators |
| 92 | + |
| 93 | +**Comparison** |
| 94 | + |
| 95 | +- eq("field", value) – Equal (=) |
| 96 | +- neq("field", value) – Not Equal (!=) |
| 97 | +- gt("field", value) – Greater Than (>) |
| 98 | +- gte("field", value) – Greater Than or Equal (>=) |
| 99 | +- lt("field", value) – Less Than (<) |
| 100 | +- lte("field", value) – Less Than or Equal (<=) |
| 101 | +- between("field", min, max) – BETWEEN |
| 102 | +- like("field", pattern) – LIKE |
| 103 | +- ilike("field", pattern) – ILIKE (case-insensitive) |
| 104 | +- regexp("field", pattern) – REGEXP |
| 105 | +- iregexp("field", pattern) – Case-insensitive REGEXP |
| 106 | +- inArray("field", [a, b, c]) – IN |
| 107 | +- notInArray("field", [a, b]) – NOT IN |
| 108 | +- isNull("field") – IS NULL |
| 109 | +- isNotNull("field") – IS NOT NULL |
| 110 | + |
| 111 | +**Logical** |
| 112 | + |
| 113 | +- and(...conditions) |
| 114 | +- or(...conditions) |
| 115 | +- not(condition) |
| 116 | +- xor(condA, condB) [⚠️ -- Not tested properly] |
| 117 | + |
| 118 | +**Sorting** |
| 119 | + |
| 120 | +- asc("field") |
| 121 | +- desc("field") |
| 122 | +- nullsFirst("field") |
| 123 | +- nullsLast("field") |
0 commit comments