@zuzjs/orm 0.1.3 → 0.1.5
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Repository, ObjectLiteral
|
|
1
|
+
import { Repository, ObjectLiteral } from "typeorm";
|
|
2
2
|
import { QueryAction } from "../types";
|
|
3
3
|
import { QueryResult } from "mysql2";
|
|
4
4
|
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
|
@@ -28,6 +28,7 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
28
28
|
* @returns The current instance of ZormQueryBuilder.
|
|
29
29
|
*/
|
|
30
30
|
select(fields: (keyof T)[]): this;
|
|
31
|
+
private applyCondition;
|
|
31
32
|
/**
|
|
32
33
|
* Adds a WHERE condition to the query.
|
|
33
34
|
* @param condition - The condition to be added.
|
|
@@ -39,7 +40,7 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
|
|
|
39
40
|
* @param condition - The condition to be added.
|
|
40
41
|
* @returns The current instance of ZormQueryBuilder.
|
|
41
42
|
*/
|
|
42
|
-
or(condition:
|
|
43
|
+
or(condition: Partial<Record<keyof T, any>>): this;
|
|
43
44
|
/**
|
|
44
45
|
* Adds an ORDER BY clause to the query.
|
|
45
46
|
* @param field - The field to order by.
|
|
@@ -57,7 +57,12 @@ class ZormQueryBuilder extends Promise {
|
|
|
57
57
|
return this;
|
|
58
58
|
}
|
|
59
59
|
_delete() {
|
|
60
|
-
this.
|
|
60
|
+
if (this.whereCount > 0) {
|
|
61
|
+
this.queryBuilder = this.queryBuilder.delete();
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw (0, core_1.stackTrace)(`○ Delete must have at least one WHERE condition. You forgot to call .where({ condition: value })`);
|
|
65
|
+
}
|
|
61
66
|
return this;
|
|
62
67
|
}
|
|
63
68
|
_getRawQuery() {
|
|
@@ -82,6 +87,24 @@ class ZormQueryBuilder extends Promise {
|
|
|
82
87
|
.select(fields.map(field => `${this.entityAlias}.${String(field)}`));
|
|
83
88
|
return this;
|
|
84
89
|
}
|
|
90
|
+
applyCondition(qb, condition, type) {
|
|
91
|
+
Object.entries(condition).forEach(([key, value], index) => {
|
|
92
|
+
const paramKey = `${key}Param${index}_${this.whereCount}`; // Unique parameter name
|
|
93
|
+
if (typeof value === "string") {
|
|
94
|
+
const match = value.match(/^(!=|>=|<=|>|<|=)\s*(.+)$/); // Improved regex
|
|
95
|
+
if (match) {
|
|
96
|
+
const [, operator, rawValue] = match;
|
|
97
|
+
const sqlOperator = operator; // Directly use the matched operator
|
|
98
|
+
const parsedValue = !isNaN(Number(rawValue)) ? Number(rawValue) : rawValue.trim(); // Convert to number if possible
|
|
99
|
+
qb[type](`${qb.alias}.${key} ${sqlOperator} :${paramKey}`, { [paramKey]: parsedValue });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Default case (normal equality condition)
|
|
104
|
+
qb[type](`${this.entityAlias}.${key} = :${paramKey}`, { [paramKey]: value });
|
|
105
|
+
this.whereCount++;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
85
108
|
/**
|
|
86
109
|
* Adds a WHERE condition to the query.
|
|
87
110
|
* @param condition - The condition to be added.
|
|
@@ -89,32 +112,17 @@ class ZormQueryBuilder extends Promise {
|
|
|
89
112
|
*/
|
|
90
113
|
where(condition) {
|
|
91
114
|
const qb = this.queryBuilder;
|
|
92
|
-
|
|
93
|
-
if (typeof value === "string" && value.startsWith("!")) {
|
|
94
|
-
qb.andWhere(`${this.entityAlias}.${key} != :${key}`, { [key]: value.slice(1) });
|
|
95
|
-
}
|
|
96
|
-
else if (typeof value === "boolean" || typeof value === "number") {
|
|
97
|
-
qb.andWhere(`${this.entityAlias}.${key} = :${key}`, { [key]: value });
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
qb.andWhere(`${this.entityAlias}.${key} = :${key}`, { [key]: value });
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
this.whereCount++;
|
|
115
|
+
this.applyCondition(qb, condition, `andWhere`);
|
|
104
116
|
return this;
|
|
105
117
|
}
|
|
106
|
-
// where(condition: FindOptionsWhere<T>): this {
|
|
107
|
-
// (this.queryBuilder as SelectQueryBuilder<T> | UpdateQueryBuilder<T>).where(condition);
|
|
108
|
-
// this.whereCount++
|
|
109
|
-
// return this;
|
|
110
|
-
// }
|
|
111
118
|
/**
|
|
112
119
|
* Adds an OR condition to the query.
|
|
113
120
|
* @param condition - The condition to be added.
|
|
114
121
|
* @returns The current instance of ZormQueryBuilder.
|
|
115
122
|
*/
|
|
116
123
|
or(condition) {
|
|
117
|
-
this.queryBuilder
|
|
124
|
+
const qb = this.queryBuilder;
|
|
125
|
+
this.applyCondition(qb, condition, `orWhere`);
|
|
118
126
|
return this;
|
|
119
127
|
}
|
|
120
128
|
/**
|
|
@@ -285,14 +293,17 @@ class ZormQueryBuilder extends Promise {
|
|
|
285
293
|
.where(whereQuery, _updateQuery.getParameters()); // Use the same parameters
|
|
286
294
|
const _updated = await _get.getMany();
|
|
287
295
|
return {
|
|
288
|
-
updated:
|
|
296
|
+
updated: _update.affected ? _update.affected > 0 : false,
|
|
289
297
|
record: _updated[0],
|
|
290
|
-
records: _updated.length > 1 ? _updated :
|
|
298
|
+
records: _updated.length > 1 ? _updated : []
|
|
291
299
|
};
|
|
292
300
|
case "delete":
|
|
293
301
|
this._delete();
|
|
294
302
|
const _delete = await this.queryBuilder.execute();
|
|
295
|
-
return {
|
|
303
|
+
return {
|
|
304
|
+
deleted: _delete.affected ? _delete.affected > 0 : false,
|
|
305
|
+
count: _delete.affected || 0
|
|
306
|
+
};
|
|
296
307
|
case "select":
|
|
297
308
|
default:
|
|
298
309
|
const _select = await this.queryBuilder.getMany();
|
|
@@ -300,7 +311,7 @@ class ZormQueryBuilder extends Promise {
|
|
|
300
311
|
hasRows: _select.length > 0,
|
|
301
312
|
count: _select.length,
|
|
302
313
|
row: _select.length > 0 ? _select[0] : null,
|
|
303
|
-
rows: _select
|
|
314
|
+
rows: _select.length > 1 ? _select : []
|
|
304
315
|
};
|
|
305
316
|
}
|
|
306
317
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EntitySchema, EntityTarget, MixedList, ObjectLiteral, Repository } from "typeorm";
|
|
2
2
|
import ZormQueryBuilder from "./drivers/queryBuilder.js";
|
|
3
|
-
import { InsertQueryResult, SelectQueryResult, UpdateQueryResult } from "./types.js";
|
|
3
|
+
import { DeleteQueryResult, InsertQueryResult, SelectQueryResult, UpdateQueryResult } from "./types.js";
|
|
4
4
|
import "reflect-metadata";
|
|
5
5
|
/**
|
|
6
6
|
* Zorm is a lightweight ORM wrapper around TypeORM with support for MySQL.
|
|
@@ -85,7 +85,7 @@ declare class Zorm {
|
|
|
85
85
|
* @param {EntityTarget<T>} entity - The entity target.
|
|
86
86
|
* @returns {ZormQueryBuilder<T, DeleteQueryResult>} The query builder instance.
|
|
87
87
|
*/
|
|
88
|
-
delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T,
|
|
88
|
+
delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, DeleteQueryResult>;
|
|
89
89
|
}
|
|
90
90
|
export default Zorm;
|
|
91
91
|
export * from "./types.js";
|