@zuzjs/orm 0.1.3 → 0.1.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.
@@ -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.
@@ -57,7 +57,12 @@ class ZormQueryBuilder extends Promise {
57
57
  return this;
58
58
  }
59
59
  _delete() {
60
- this.queryBuilder = this.queryBuilder.delete();
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,23 @@ 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]) => {
92
+ if (typeof value === "string") {
93
+ const match = value.match(/^(!=|>=|<=|>|<|=)\s*(.+)$/); // Improved regex
94
+ if (match) {
95
+ const [, operator, rawValue] = match;
96
+ const sqlOperator = operator; // Directly use the matched operator
97
+ const paramKey = `${key}Param`; // Unique parameter name
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} = :${key}`, { [key]: value });
105
+ });
106
+ }
85
107
  /**
86
108
  * Adds a WHERE condition to the query.
87
109
  * @param condition - The condition to be added.
@@ -89,32 +111,18 @@ class ZormQueryBuilder extends Promise {
89
111
  */
90
112
  where(condition) {
91
113
  const qb = this.queryBuilder;
92
- Object.entries(condition).forEach(([key, value]) => {
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
- });
114
+ this.applyCondition(qb, condition, `andWhere`);
103
115
  this.whereCount++;
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.orWhere(condition);
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: true,
296
+ updated: _update.affected ? _update.affected > 0 : false,
289
297
  record: _updated[0],
290
- records: _updated.length > 1 ? _updated : null
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 { deleted: true, count: _delete.affected || 0 };
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, UpdateQueryResult>;
88
+ delete<T extends ObjectLiteral>(entity: EntityTarget<T>): ZormQueryBuilder<T, DeleteQueryResult>;
89
89
  }
90
90
  export default Zorm;
91
91
  export * from "./types.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuzjs/orm",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "keywords": [
5
5
  "orm",
6
6
  "zuz",