@zuzjs/orm 0.3.0 → 0.3.1

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.
@@ -0,0 +1,19 @@
1
+ import { ObjectLiteral } from "typeorm";
2
+ declare class ZormExprBuilder<T extends ObjectLiteral> {
3
+ private _expression;
4
+ private _param;
5
+ private _paramIndex;
6
+ field(column: keyof T | string): this;
7
+ fromUnixTime(): this;
8
+ date(): this;
9
+ substring(column: keyof T | string, delimiter: string, index: number): this;
10
+ append(extra: string): this;
11
+ wrap(wrapper: (expr: string) => string): this;
12
+ equals(value: string | number | boolean): {
13
+ expression: string;
14
+ param: Record<string, any>;
15
+ };
16
+ buildExpression(): string;
17
+ buildParam(): Record<string, any>;
18
+ }
19
+ export default ZormExprBuilder;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ZormExprBuilder {
4
+ _expression = "";
5
+ _param = {};
6
+ _paramIndex = 0;
7
+ field(column) {
8
+ this._expression = String(column);
9
+ return this;
10
+ }
11
+ fromUnixTime() {
12
+ this._expression = `FROM_UNIXTIME(${this._expression})`;
13
+ return this;
14
+ }
15
+ date() {
16
+ this._expression = `DATE(${this._expression})`;
17
+ return this;
18
+ }
19
+ substring(column, delimiter, index) {
20
+ this._expression = `SUBSTRING_INDEX(${String(column)}, '${delimiter}', ${index})`;
21
+ return this;
22
+ }
23
+ append(extra) {
24
+ this._expression = `${this._expression}${extra}`;
25
+ return this;
26
+ }
27
+ wrap(wrapper) {
28
+ this._expression = wrapper(this._expression);
29
+ return this;
30
+ }
31
+ equals(value) {
32
+ const paramKey = `param${this._paramIndex++}`;
33
+ this._param[paramKey] = value;
34
+ return {
35
+ expression: `${this._expression} = :${paramKey}`,
36
+ param: this._param,
37
+ };
38
+ }
39
+ buildExpression() {
40
+ return this._expression;
41
+ }
42
+ buildParam() {
43
+ return this._param;
44
+ }
45
+ }
46
+ exports.default = ZormExprBuilder;
@@ -2,6 +2,7 @@ import { QueryResult } from "mysql2";
2
2
  import { ObjectLiteral, Repository } from "typeorm";
3
3
  import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
4
4
  import { PartialConditions, QueryAction } from "../types";
5
+ import ZormExprBuilder from "./expressionBuilder";
5
6
  declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends Promise<R> {
6
7
  private repository;
7
8
  private queryBuilder;
@@ -40,6 +41,14 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
40
41
  */
41
42
  select(fields: (keyof T)[]): this;
42
43
  private applyCondition;
44
+ /**
45
+ * Adds a custom expression-based WHERE clause using a fluent builder.
46
+ * @param fn - A callback that receives a ZormExprBuilder and returns an expression + param.
47
+ */
48
+ expression(exprFn: (q: ZormExprBuilder<T>) => {
49
+ expression: string;
50
+ param: Record<string, any>;
51
+ } | ZormExprBuilder<T>): this;
43
52
  /**
44
53
  * Adds a WHERE condition to the query.
45
54
  * @param condition - The condition to be added.
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const core_1 = require("../core");
7
+ const expressionBuilder_1 = __importDefault(require("./expressionBuilder"));
4
8
  const index_js_1 = require("./mysql/index.js");
5
9
  class ZormQueryBuilder extends Promise {
6
10
  repository;
@@ -171,6 +175,23 @@ class ZormQueryBuilder extends Promise {
171
175
  this.whereCount++;
172
176
  });
173
177
  }
178
+ /**
179
+ * Adds a custom expression-based WHERE clause using a fluent builder.
180
+ * @param fn - A callback that receives a ZormExprBuilder and returns an expression + param.
181
+ */
182
+ expression(exprFn) {
183
+ const qb = this.queryBuilder;
184
+ const result = exprFn(new expressionBuilder_1.default());
185
+ if ('expression' in result && 'param' in result) {
186
+ qb.andWhere(result.expression, result.param);
187
+ }
188
+ else {
189
+ // fallback if only expression was built without .equals()
190
+ qb.andWhere(result.buildExpression(), result.buildParam());
191
+ }
192
+ this.whereCount++;
193
+ return this;
194
+ }
174
195
  /**
175
196
  * Adds a WHERE condition to the query.
176
197
  * @param condition - The condition to be added.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuzjs/orm",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "keywords": [
5
5
  "orm",
6
6
  "zuz",
@@ -31,21 +31,21 @@
31
31
  "sideEffects": ["reflect-metadata"],
32
32
  "dependencies": {
33
33
  "@types/md5": "^2.3.5",
34
- "commander": "^13.1.0",
34
+ "commander": "14.0.0",
35
35
  "md5": "^2.3.0",
36
- "mysql2": "^3.12.0",
37
- "nanoid": "^5.1.0",
36
+ "mysql2": "3.14.1",
37
+ "nanoid": "5.1.5",
38
38
  "picocolors": "^1.1.1",
39
39
  "reflect-metadata": "^0.2.2",
40
- "typeorm": "^0.3.20"
40
+ "typeorm": "^0.3.25"
41
41
  },
42
42
  "devDependencies": {
43
43
  "husky": "^9.1.7",
44
44
  "ts-node": "^10.9.2",
45
- "typescript": "^5.7.3"
45
+ "typescript": "^5.8.3"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "reflect-metadata": "^0.2.2",
49
- "typeorm": "^0.3.0"
49
+ "typeorm": "^0.3.25"
50
50
  }
51
51
  }