@zuzjs/orm 0.3.2 → 0.3.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.
@@ -1,19 +1,32 @@
1
1
  import { ObjectLiteral } from "typeorm";
2
2
  declare class ZormExprBuilder<T extends ObjectLiteral> {
3
- private _expression;
4
- private _param;
5
- private _paramIndex;
6
- field(column: keyof T | string): this;
3
+ private _parts;
4
+ private _params;
5
+ private _paramIdx;
6
+ private _alias;
7
+ private static globalParamIdx;
8
+ constructor(alias?: string, parent?: {
9
+ params: Record<string, any>;
10
+ idx: number;
11
+ });
12
+ field(col: keyof T | string): this;
13
+ equals(value: any): this;
14
+ append(extra: string): this;
15
+ wrap(wrapper: (expr: string) => string): this;
16
+ exists(sub: (q: ZormExprBuilder<T>) => ZormExprBuilder<T>): this;
17
+ select(expr: string): this;
18
+ from(table: string, alias: string): this;
19
+ where(cond: Record<string, any>): this;
20
+ or(): this;
21
+ group(): this;
7
22
  fromUnixTime(): this;
8
23
  date(): this;
9
24
  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): {
25
+ toExpression(): {
13
26
  expression: string;
14
27
  param: Record<string, any>;
15
28
  };
16
29
  buildExpression(): string;
17
- buildParam(): Record<string, any>;
30
+ buildParams(): Record<string, any>;
18
31
  }
19
32
  export default ZormExprBuilder;
@@ -1,46 +1,100 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class ZormExprBuilder {
4
- _expression = "";
5
- _param = {};
6
- _paramIndex = 0;
7
- field(column) {
8
- this._expression = String(column);
4
+ _parts = [];
5
+ _params = {};
6
+ _paramIdx = 0;
7
+ _alias;
8
+ static globalParamIdx = 0;
9
+ constructor(alias, parent) {
10
+ this._alias = alias ? `${alias}.` : ``;
11
+ this._paramIdx = parent ? parent.idx : 0;
12
+ this._params = parent ? parent.params : {};
13
+ }
14
+ field(col) {
15
+ this._parts.push(`${this._alias}${String(col)}`);
9
16
  return this;
10
17
  }
11
- fromUnixTime() {
12
- this._expression = `FROM_UNIXTIME(${this._expression})`;
18
+ equals(value) {
19
+ const key = `p${this._paramIdx++}`;
20
+ this.append(` = :${key}`);
21
+ // this._parts[this._parts.length - 1] += ` = :${key}`;
22
+ this._params[key] = value;
13
23
  return this;
14
24
  }
15
- date() {
16
- this._expression = `DATE(${this._expression})`;
25
+ append(extra) {
26
+ if (this._parts.length === 0)
27
+ throw new Error("Cannot append to empty expression");
28
+ this._parts[this._parts.length - 1] += extra;
17
29
  return this;
18
30
  }
19
- substring(column, delimiter, index) {
20
- this._expression = `SUBSTRING_INDEX(${String(column)}, '${delimiter}', ${index})`;
31
+ wrap(wrapper) {
32
+ if (this._parts.length === 0)
33
+ throw new Error("Cannot wrap empty expression");
34
+ this._parts[this._parts.length - 1] = wrapper(this._parts[this._parts.length - 1]);
21
35
  return this;
22
36
  }
23
- append(extra) {
24
- this._expression = `${this._expression}${extra}`;
37
+ exists(sub) {
38
+ const subQ = new ZormExprBuilder(undefined, { params: this._params, idx: this._paramIdx });
39
+ sub(subQ);
40
+ // const { expression, param } = subQ.toExpression();
41
+ this._parts.push(`EXISTS (${subQ.buildExpression()})`);
42
+ // Object.assign(this._params, param);
43
+ this._paramIdx = subQ._paramIdx;
25
44
  return this;
26
45
  }
27
- wrap(wrapper) {
28
- this._expression = wrapper(this._expression);
46
+ select(expr) {
47
+ this._parts.push(`SELECT ${expr}`);
29
48
  return this;
30
49
  }
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
- };
50
+ from(table, alias) {
51
+ this._parts.push(`FROM ${table} ${alias}`);
52
+ this._alias = alias;
53
+ return this;
54
+ }
55
+ where(cond) {
56
+ const whereParts = [];
57
+ for (const [k, v] of Object.entries(cond)) {
58
+ const key = `p${this._paramIdx++}`;
59
+ whereParts.push(`${k} = :${key}`);
60
+ this._params[key] = v;
61
+ }
62
+ this._parts.push(`WHERE ${whereParts.join(' AND ')}`);
63
+ return this;
64
+ }
65
+ // or(sub: ZormExprBuilder<any> | { expression: string; param: Record<string, any> }): this {
66
+ // const { expression, param } = 'toExpression' in sub ? sub.toExpression() : sub;
67
+ // this._parts.push(this._parts.length === 0 ? expression : `OR ${expression}`);
68
+ // Object.assign(this._params, param);
69
+ // return this;
70
+ // }
71
+ or() {
72
+ this._parts.push(`OR`);
73
+ return this;
74
+ }
75
+ group() {
76
+ return this.wrap(e => `(${e})`);
77
+ }
78
+ fromUnixTime() {
79
+ this.wrap(expr => `FROM_UNIXTIME(${expr})`);
80
+ return this;
81
+ }
82
+ date() {
83
+ this.wrap(expr => `DATE(${expr})`);
84
+ return this;
85
+ }
86
+ substring(column, delimiter, index) {
87
+ this._parts = [`SUBSTRING_INDEX(${this._alias}${String(column)}, '${delimiter}', ${index})`];
88
+ return this;
89
+ }
90
+ toExpression() {
91
+ return { expression: this._parts.join(' '), param: this._params };
38
92
  }
39
93
  buildExpression() {
40
- return this._expression;
94
+ return this.toExpression().expression;
41
95
  }
42
- buildParam() {
43
- return this._param;
96
+ buildParams() {
97
+ return this.toExpression().param;
44
98
  }
45
99
  }
46
100
  exports.default = ZormExprBuilder;
@@ -210,7 +210,7 @@ class MySqlDriver {
210
210
  if (foreignKeys[tableName]) {
211
211
  for (const fk of foreignKeys[tableName] || []) {
212
212
  const relatedEntity = (0, index_js_1.toPascalCase)(fk.REFERENCED_TABLE_NAME);
213
- if (entityCode.includes(`fk${relatedEntity}!: ${relatedEntity};`) === false) {
213
+ if (imports.includes(`import { ${relatedEntity} } from "./${fk.REFERENCED_TABLE_NAME}";`) === false) {
214
214
  entityCode.push(`\t@OneToOne(() => ${relatedEntity})`);
215
215
  entityCode.push(`\t@JoinColumn({ name: "${fk.COLUMN_NAME}" })`);
216
216
  entityCode.push(`\tfk${relatedEntity}!: ${relatedEntity};\n`);
@@ -45,10 +45,9 @@ declare class ZormQueryBuilder<T extends ObjectLiteral, R = QueryResult> extends
45
45
  * Adds a custom expression-based WHERE clause using a fluent builder.
46
46
  * @param fn - A callback that receives a ZormExprBuilder and returns an expression + param.
47
47
  */
48
- expression(exprFn: (q: ZormExprBuilder<T>) => {
49
- expression: string;
50
- param: Record<string, any>;
51
- } | ZormExprBuilder<T>): this;
48
+ expression(exprFn: (q: ZormExprBuilder<T>) => ZormExprBuilder<T>,
49
+ /** Add parentheses group to built expression */
50
+ group?: boolean): this;
52
51
  /**
53
52
  * Adds a WHERE condition to the query.
54
53
  * @param condition - The condition to be added.
@@ -179,19 +179,29 @@ class ZormQueryBuilder extends Promise {
179
179
  * Adds a custom expression-based WHERE clause using a fluent builder.
180
180
  * @param fn - A callback that receives a ZormExprBuilder and returns an expression + param.
181
181
  */
182
- expression(exprFn) {
182
+ expression(exprFn,
183
+ /** Add parentheses group to built expression */
184
+ group = true) {
183
185
  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
- }
186
+ const result = exprFn(new expressionBuilder_1.default(this.entityAlias));
187
+ const _expression = result.buildExpression();
188
+ qb.andWhere(group ? `(${_expression})` : _expression, result.buildParams());
192
189
  this.whereCount++;
193
190
  return this;
194
191
  }
192
+ // expression(exprFn: (q: ZormExprBuilder<T>) => ZormExprBuilder<T>): this {
193
+ // const qb = this.queryBuilder as SelectQueryBuilder<T> | UpdateQueryBuilder<T>;
194
+ // const result = exprFn(new ZormExprBuilder<T>(this.entityAlias));
195
+ // // const expression =
196
+ // qb.andWhere(result.buildExpression(), result.buildParams());
197
+ // // if ('expression' in result && 'param' in result) {
198
+ // // } else {
199
+ // // // fallback if only expression was built without .equals()
200
+ // // }
201
+ // // qb.andWhere(result.buildExpression(), result.buildParams());
202
+ // this.whereCount++;
203
+ // return this;
204
+ // }
195
205
  /**
196
206
  * Adds a WHERE condition to the query.
197
207
  * @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.2",
3
+ "version": "0.3.4",
4
4
  "keywords": [
5
5
  "orm",
6
6
  "zuz",