pg-query-sdk 1.0.3 → 1.0.4
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
|
|
@@ -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`);
|