@zuzjs/orm 0.2.2 → 0.2.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/dist/drivers/queryBuilder.d.ts +5 -5
- package/dist/drivers/queryBuilder.js +40 -12
- package/dist/types.d.ts +52 -0
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { QueryResult } from "mysql2";
|
|
2
2
|
import { ObjectLiteral, Repository } from "typeorm";
|
|
3
3
|
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
|
4
|
-
import { QueryAction } from "../types";
|
|
4
|
+
import { PartialConditions, QueryAction } from "../types";
|
|
5
5
|
declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends Promise<R> {
|
|
6
6
|
private repository;
|
|
7
7
|
private queryBuilder;
|
|
@@ -44,13 +44,13 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
44
44
|
* @param condition - The condition to be added.
|
|
45
45
|
* @returns The current instance of ZormQueryBuilder.
|
|
46
46
|
*/
|
|
47
|
-
where(condition:
|
|
47
|
+
where(condition: PartialConditions<T>): this;
|
|
48
48
|
/**
|
|
49
49
|
* Adds an OR condition to the query.
|
|
50
50
|
* @param condition - The condition to be added.
|
|
51
51
|
* @returns The current instance of ZormQueryBuilder.
|
|
52
52
|
*/
|
|
53
|
-
or(condition:
|
|
53
|
+
or(condition: PartialConditions<T>): this;
|
|
54
54
|
/**
|
|
55
55
|
* Adds an ORDER BY clause to the query.
|
|
56
56
|
* @param field - The field to order by.
|
|
@@ -118,13 +118,13 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
118
118
|
* @param value - A string or an array of strings to match.
|
|
119
119
|
* @returns The current instance of ZormQueryBuilder.
|
|
120
120
|
*/
|
|
121
|
-
like(
|
|
121
|
+
like(conditions: Partial<Record<keyof T, string | string[]>>): this;
|
|
122
122
|
/**
|
|
123
123
|
* Adds a DISTINCT clause to the query.
|
|
124
124
|
* @returns The current instance of ZormQueryBuilder.
|
|
125
125
|
*/
|
|
126
126
|
distinct(): this;
|
|
127
|
-
count(): Promise<number>;
|
|
127
|
+
count(field?: keyof T): Promise<number>;
|
|
128
128
|
sum(field: keyof T): Promise<number>;
|
|
129
129
|
avg(field: keyof T): Promise<number>;
|
|
130
130
|
min(field: keyof T): Promise<number>;
|
|
@@ -111,6 +111,7 @@ class ZormQueryBuilder extends Promise {
|
|
|
111
111
|
applyCondition(qb, condition, type) {
|
|
112
112
|
Object.entries(condition).forEach(([key, value], index) => {
|
|
113
113
|
const paramKey = `${key}Param${index}_${this.whereCount}`; // Unique parameter name
|
|
114
|
+
let sqlOperator = "="; // Default to "="
|
|
114
115
|
if (typeof value === "string") {
|
|
115
116
|
const match = value.match(/^(!=|>=|<=|>|<|=)\s*(.+)$/); // Improved regex
|
|
116
117
|
if (match) {
|
|
@@ -121,6 +122,24 @@ class ZormQueryBuilder extends Promise {
|
|
|
121
122
|
return;
|
|
122
123
|
}
|
|
123
124
|
}
|
|
125
|
+
else if (typeof value === "object" && value !== null) {
|
|
126
|
+
// Support object-based conditions: { age: { gt: 18, lt: 20 } }
|
|
127
|
+
const operators = {
|
|
128
|
+
gt: ">",
|
|
129
|
+
gte: ">=",
|
|
130
|
+
lt: "<",
|
|
131
|
+
lte: "<=",
|
|
132
|
+
ne: "!=",
|
|
133
|
+
eq: "="
|
|
134
|
+
};
|
|
135
|
+
Object.entries(value).forEach(([opKey, opValue]) => {
|
|
136
|
+
if (operators[opKey]) {
|
|
137
|
+
const paramKey = `${key}Param_${opKey}_${this.whereCount++}`; // Unique param key
|
|
138
|
+
qb[type](`${qb.alias}.${key} ${operators[opKey]} :${paramKey}`, { [paramKey]: opValue });
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
return; // Prevent default equality case for objects
|
|
142
|
+
}
|
|
124
143
|
// Default case (normal equality condition)
|
|
125
144
|
qb[type](`${this.entityAlias}.${key} = :${paramKey}`, { [paramKey]: value });
|
|
126
145
|
this.whereCount++;
|
|
@@ -251,20 +270,26 @@ class ZormQueryBuilder extends Promise {
|
|
|
251
270
|
* @param value - A string or an array of strings to match.
|
|
252
271
|
* @returns The current instance of ZormQueryBuilder.
|
|
253
272
|
*/
|
|
254
|
-
like(
|
|
255
|
-
if (!
|
|
273
|
+
like(conditions) {
|
|
274
|
+
if (!conditions || Object.keys(conditions).length === 0)
|
|
256
275
|
return this;
|
|
257
276
|
const qb = this.queryBuilder;
|
|
258
|
-
const
|
|
259
|
-
const params = {};
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
|
|
277
|
+
const orConditions = [];
|
|
278
|
+
const params = {};
|
|
279
|
+
Object.entries(conditions).forEach(([field, value]) => {
|
|
280
|
+
const values = Array.isArray(value) ? value : [value];
|
|
281
|
+
const fieldConditions = [];
|
|
282
|
+
values.forEach((val, index) => {
|
|
283
|
+
const paramKey = `${field}LikeParam${index}_${this.whereCount}`;
|
|
284
|
+
fieldConditions.push(`${this.entityAlias}.${String(field)} LIKE :${paramKey}`);
|
|
285
|
+
params[paramKey] = val; // Directly use the value (supports %xyz% pattern)
|
|
286
|
+
});
|
|
287
|
+
if (fieldConditions.length > 0) {
|
|
288
|
+
orConditions.push(`(${fieldConditions.join(" OR ")})`);
|
|
289
|
+
}
|
|
265
290
|
});
|
|
266
|
-
if (
|
|
267
|
-
qb.andWhere(`(${
|
|
291
|
+
if (orConditions.length > 0) {
|
|
292
|
+
qb.andWhere(`(${orConditions.join(" OR ")})`, params);
|
|
268
293
|
this.whereCount++;
|
|
269
294
|
}
|
|
270
295
|
return this;
|
|
@@ -277,7 +302,10 @@ class ZormQueryBuilder extends Promise {
|
|
|
277
302
|
this.queryBuilder.distinct(true);
|
|
278
303
|
return this;
|
|
279
304
|
}
|
|
280
|
-
async count() {
|
|
305
|
+
async count(field) {
|
|
306
|
+
if (field) {
|
|
307
|
+
this.queryBuilder.select(`COUNT(${field})`);
|
|
308
|
+
}
|
|
281
309
|
return await this.queryBuilder.getCount();
|
|
282
310
|
}
|
|
283
311
|
async sum(field) {
|
package/dist/types.d.ts
CHANGED
|
@@ -17,6 +17,58 @@ export type ConnectionDetails = {
|
|
|
17
17
|
};
|
|
18
18
|
export type QueryAction = "create" | "upsert" | "select" | "update" | "delete";
|
|
19
19
|
export type QueryResult = InsertQueryResult | SelectQueryResult | UpdateQueryResult | DeleteQueryResult;
|
|
20
|
+
/**
|
|
21
|
+
* Defines supported comparison operators for filtering queries.
|
|
22
|
+
* Enables IntelliSense support in `.where()` conditions.
|
|
23
|
+
*
|
|
24
|
+
* @template T The type of the value being compared (e.g., number, string, Date).
|
|
25
|
+
*/
|
|
26
|
+
export type WhereOperators<T> = {
|
|
27
|
+
/**
|
|
28
|
+
* Greater than (`>`) operator.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* query.where({ age: { gt: 18 } }) // WHERE age > 18
|
|
32
|
+
*/
|
|
33
|
+
gt?: T;
|
|
34
|
+
/**
|
|
35
|
+
* Greater than or equal to (`>=`) operator.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* query.where({ price: { gte: 100 } }) // WHERE price >= 100
|
|
39
|
+
*/
|
|
40
|
+
gte?: T;
|
|
41
|
+
/**
|
|
42
|
+
* Less than (`<`) operator.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* query.where({ age: { lt: 30 } }) // WHERE age < 30
|
|
46
|
+
*/
|
|
47
|
+
lt?: T;
|
|
48
|
+
/**
|
|
49
|
+
* Less than or equal to (`<=`) operator.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* query.where({ date: { lte: '2024-01-01' } }) // WHERE date <= '2024-01-01'
|
|
53
|
+
*/
|
|
54
|
+
lte?: T;
|
|
55
|
+
/**
|
|
56
|
+
* Not equal (`!=`) operator.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* query.where({ status: { ne: 'inactive' } }) // WHERE status != 'inactive'
|
|
60
|
+
*/
|
|
61
|
+
ne?: T;
|
|
62
|
+
/**
|
|
63
|
+
* Equal (`=`) operator.
|
|
64
|
+
* This is typically unnecessary since `.where({ key: value })` already handles equality.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* query.where({ id: { eq: 1 } }) // WHERE id = 1
|
|
68
|
+
*/
|
|
69
|
+
eq?: T;
|
|
70
|
+
};
|
|
71
|
+
export type PartialConditions<T> = Partial<Record<keyof T, string | number | boolean | WhereOperators<any>>>;
|
|
20
72
|
export type QueryError = {
|
|
21
73
|
code: number | string;
|
|
22
74
|
message: string;
|