pg-query-sdk 1.0.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -42,8 +42,6 @@ const db = new Database({
|
|
|
42
42
|
});
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
---
|
|
46
|
-
|
|
47
45
|
## 🛠 Core Functionalities
|
|
48
46
|
|
|
49
47
|
### 1️⃣ QueryBuilder: Fluent SQL `SELECT` Query Construction
|
|
@@ -79,27 +77,27 @@ The `ConditionBuilder` facilitates the creation of complex conditional logic wit
|
|
|
79
77
|
import {Database} from 'pg-query-sdk';
|
|
80
78
|
|
|
81
79
|
const db = new Database({
|
|
82
|
-
|
|
80
|
+
connectionString: 'postgres://user:pass@localhost:5432/your_database',
|
|
83
81
|
});
|
|
84
82
|
|
|
85
83
|
async function complexWhereExample() {
|
|
86
|
-
const products = await db.table('products')
|
|
87
|
-
.select(['name', 'price'])
|
|
88
|
-
.where(conditions => {
|
|
89
|
-
conditions
|
|
90
|
-
.where({ category: 'electronics' })
|
|
91
|
-
.orGroup(group => {
|
|
92
|
-
group
|
|
93
|
-
.where({ price: { op: '<', value: 100 } })
|
|
94
|
-
.where({ stock: { op: '>', value: 0 } });
|
|
95
|
-
});
|
|
96
|
-
})
|
|
97
|
-
.execute();
|
|
98
84
|
|
|
99
|
-
|
|
85
|
+
const products = await db
|
|
86
|
+
.table('products')
|
|
87
|
+
.select(['name', 'price'])
|
|
88
|
+
.where(conditions => {
|
|
89
|
+
conditions
|
|
90
|
+
.where({category: 'electronics'})
|
|
91
|
+
.orGroup(group => {
|
|
92
|
+
group.where({price: {op: '<', value: 100}})
|
|
93
|
+
.where({stock: {op: '>', value: 0}})
|
|
94
|
+
})
|
|
95
|
+
}).execute()
|
|
96
|
+
|
|
97
|
+
console.log('Complex WHERE Products:', products)
|
|
100
98
|
}
|
|
101
99
|
|
|
102
|
-
complexWhereExample()
|
|
100
|
+
complexWhereExample()
|
|
103
101
|
```
|
|
104
102
|
|
|
105
103
|
### 3️⃣ QueryExecutor: Direct Query Execution
|
|
@@ -138,8 +136,6 @@ const db = new Database({
|
|
|
138
136
|
async function transactionExample() {
|
|
139
137
|
try {
|
|
140
138
|
const result = await db.transaction(async trxDb => {
|
|
141
|
-
// 'trxDb' is a Database instance bound to the current transaction.
|
|
142
|
-
// Use 'trxDb.executor.execute()' for DML operations within the transaction.
|
|
143
139
|
await trxDb.executor.execute(
|
|
144
140
|
'UPDATE accounts SET balance = balance - $1 WHERE id = $2',
|
|
145
141
|
[100.00, 1]
|
|
@@ -183,9 +179,6 @@ class UserRepository extends Repository<User> {
|
|
|
183
179
|
.where({ name })
|
|
184
180
|
.execute();
|
|
185
181
|
}
|
|
186
|
-
|
|
187
|
-
// Implement DML operations as needed.
|
|
188
|
-
// Example: async insert(data: Partial<User>): Promise<User> { /* ... */ }
|
|
189
182
|
}
|
|
190
183
|
|
|
191
184
|
const db = new Database({
|
|
@@ -214,7 +207,7 @@ The package provides support for both CommonJS and ES Modules environments.
|
|
|
214
207
|
### CommonJS
|
|
215
208
|
|
|
216
209
|
```javascript
|
|
217
|
-
const Database = require('pg-query-sdk')
|
|
210
|
+
const {Database} = require('pg-query-sdk');
|
|
218
211
|
const db = new Database({ /* ... */ });
|
|
219
212
|
```
|
|
220
213
|
|
|
@@ -227,32 +220,6 @@ const db = new Database({ /* ... */ });
|
|
|
227
220
|
|
|
228
221
|
---
|
|
229
222
|
|
|
230
|
-
## 🧱 Project Structure
|
|
231
|
-
|
|
232
|
-
The project is organized into distinct modules, each responsible for a specific aspect of database interaction:
|
|
233
|
-
|
|
234
|
-
```
|
|
235
|
-
pg-query-sdk/
|
|
236
|
-
src/
|
|
237
|
-
core/
|
|
238
|
-
Database.ts # Central database interface
|
|
239
|
-
ParamContext.ts # Manages query parameters
|
|
240
|
-
QueryExecutor.ts # Executes SQL queries
|
|
241
|
-
TransactionManager.ts # Handles database transactions
|
|
242
|
-
builders/
|
|
243
|
-
ConditionBuilder.ts # Builds WHERE/HAVING clauses
|
|
244
|
-
QueryBuilder.ts # Builds SELECT queries
|
|
245
|
-
orm/
|
|
246
|
-
EntityManager.ts # (Planned) Entity management
|
|
247
|
-
Repository.ts # Abstract base for data access
|
|
248
|
-
dialects/
|
|
249
|
-
Dialect.ts # Interface for database dialects
|
|
250
|
-
PostgresDialect.ts # PostgreSQL specific dialect implementation
|
|
251
|
-
index.ts # Main entry point for module exports
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
---
|
|
255
|
-
|
|
256
223
|
## 📌 Responsibilities of Layers
|
|
257
224
|
|
|
258
225
|
| Layer | Responsibility |
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import ParamContext from '../core/ParamContext';
|
|
2
|
-
type Operator = '=' | '>' | '<' | '>=' | '<=' | '!=' | '<>' | 'LIKE' | 'ILIKE' | 'IN' | 'NOT IN' | 'BETWEEN' | 'EXISTS';
|
|
3
|
-
type ConditionValue = any | {
|
|
4
|
-
op: Operator;
|
|
5
|
-
value: any;
|
|
6
|
-
};
|
|
7
2
|
/**
|
|
8
3
|
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
9
4
|
*/
|
|
@@ -11,7 +6,7 @@ export default class ConditionBuilder {
|
|
|
11
6
|
private ctx;
|
|
12
7
|
private parts;
|
|
13
8
|
constructor(ctx: ParamContext);
|
|
14
|
-
where(obj: Record<string,
|
|
9
|
+
where(obj: Record<string, any> | ((qb: ConditionBuilder) => void)): this;
|
|
15
10
|
private handleOperator;
|
|
16
11
|
private add;
|
|
17
12
|
raw(expression: string): this;
|
|
@@ -20,4 +15,3 @@ export default class ConditionBuilder {
|
|
|
20
15
|
clone(): ConditionBuilder;
|
|
21
16
|
build(prefix?: string): string;
|
|
22
17
|
}
|
|
23
|
-
export {};
|
|
@@ -13,6 +13,15 @@ class ConditionBuilder {
|
|
|
13
13
|
this.parts = [];
|
|
14
14
|
}
|
|
15
15
|
where(obj) {
|
|
16
|
+
if (typeof obj === 'function') {
|
|
17
|
+
const nested = new ConditionBuilder(this.ctx);
|
|
18
|
+
obj(nested);
|
|
19
|
+
const built = nested.build();
|
|
20
|
+
if (built) {
|
|
21
|
+
this.add(`(${built.replace(/^WHERE\s/, '')})`);
|
|
22
|
+
}
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
16
25
|
Object.entries(obj).forEach(([key, condition]) => {
|
|
17
26
|
if (condition === null) {
|
|
18
27
|
this.add(`${key} IS NULL`);
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import ParamContext from '../core/ParamContext';
|
|
2
|
-
type Operator = '=' | '>' | '<' | '>=' | '<=' | '!=' | '<>' | 'LIKE' | 'ILIKE' | 'IN' | 'NOT IN' | 'BETWEEN' | 'EXISTS';
|
|
3
|
-
type ConditionValue = any | {
|
|
4
|
-
op: Operator;
|
|
5
|
-
value: any;
|
|
6
|
-
};
|
|
7
2
|
/**
|
|
8
3
|
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
9
4
|
*/
|
|
@@ -11,7 +6,7 @@ export default class ConditionBuilder {
|
|
|
11
6
|
private ctx;
|
|
12
7
|
private parts;
|
|
13
8
|
constructor(ctx: ParamContext);
|
|
14
|
-
where(obj: Record<string,
|
|
9
|
+
where(obj: Record<string, any> | ((qb: ConditionBuilder) => void)): this;
|
|
15
10
|
private handleOperator;
|
|
16
11
|
private add;
|
|
17
12
|
raw(expression: string): this;
|
|
@@ -20,4 +15,3 @@ export default class ConditionBuilder {
|
|
|
20
15
|
clone(): ConditionBuilder;
|
|
21
16
|
build(prefix?: string): string;
|
|
22
17
|
}
|
|
23
|
-
export {};
|
|
@@ -8,6 +8,15 @@ export default class ConditionBuilder {
|
|
|
8
8
|
this.parts = [];
|
|
9
9
|
}
|
|
10
10
|
where(obj) {
|
|
11
|
+
if (typeof obj === 'function') {
|
|
12
|
+
const nested = new ConditionBuilder(this.ctx);
|
|
13
|
+
obj(nested);
|
|
14
|
+
const built = nested.build();
|
|
15
|
+
if (built) {
|
|
16
|
+
this.add(`(${built.replace(/^WHERE\s/, '')})`);
|
|
17
|
+
}
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
11
20
|
Object.entries(obj).forEach(([key, condition]) => {
|
|
12
21
|
if (condition === null) {
|
|
13
22
|
this.add(`${key} IS NULL`);
|
package/package.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pg-query-sdk",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "PostgreSQL SDK with Query Builder and
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "PostgreSQL SDK with Query Builder, Query Executor and Transaction Manager",
|
|
5
|
+
"type": "commonjs",
|
|
5
6
|
"main": "dist/cjs/index.js",
|
|
6
7
|
"types": "dist/esm/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18"
|
|
11
|
+
},
|
|
7
12
|
"exports": {
|
|
8
13
|
".": {
|
|
9
14
|
"require": "./dist/cjs/index.js",
|
|
@@ -15,11 +20,29 @@
|
|
|
15
20
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
16
21
|
"build:esm": "tsc -p tsconfig.esm.json",
|
|
17
22
|
"build": "npm run build:cjs && npm run build:esm",
|
|
23
|
+
"lint": "echo \"Add lint later\"",
|
|
24
|
+
"test": "echo \"Add tests\"",
|
|
18
25
|
"prepublishOnly": "npm run build"
|
|
19
26
|
},
|
|
20
27
|
"files": [
|
|
21
28
|
"dist"
|
|
22
29
|
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"postgresql",
|
|
32
|
+
"postgres",
|
|
33
|
+
"typescript",
|
|
34
|
+
"nodejs",
|
|
35
|
+
"database",
|
|
36
|
+
"sql",
|
|
37
|
+
"query-builder",
|
|
38
|
+
"sdk",
|
|
39
|
+
"orm",
|
|
40
|
+
"transaction",
|
|
41
|
+
"cte",
|
|
42
|
+
"repository-pattern",
|
|
43
|
+
"query-engine",
|
|
44
|
+
"prepared-statement"
|
|
45
|
+
],
|
|
23
46
|
"author": {
|
|
24
47
|
"name": "Guilherme",
|
|
25
48
|
"email": "sguii5147@gmail.com",
|
|
@@ -27,19 +50,21 @@
|
|
|
27
50
|
},
|
|
28
51
|
"repository": {
|
|
29
52
|
"type": "git",
|
|
30
|
-
"url": "https://github.com/guio11221/pg-query-sdk.git"
|
|
53
|
+
"url": "git+https://github.com/guio11221/pg-query-sdk.git"
|
|
31
54
|
},
|
|
32
55
|
"bugs": {
|
|
33
56
|
"url": "https://github.com/guio11221/pg-query-sdk/issues"
|
|
34
57
|
},
|
|
35
58
|
"homepage": "https://github.com/guio11221/pg-query-sdk#readme",
|
|
36
59
|
"license": "MIT",
|
|
37
|
-
"dependencies": {
|
|
38
|
-
|
|
60
|
+
"dependencies": {},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"pg": "^8.0.0"
|
|
39
63
|
},
|
|
40
64
|
"devDependencies": {
|
|
41
65
|
"typescript": "^5.3.3",
|
|
42
66
|
"@types/node": "^20.10.5",
|
|
43
|
-
"@types/pg": "^8.16.0"
|
|
67
|
+
"@types/pg": "^8.16.0",
|
|
68
|
+
"pg": "^8.18.0"
|
|
44
69
|
}
|
|
45
|
-
}
|
|
70
|
+
}
|