typeorm-extensions 1.3.2 → 1.3.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.
package/README.md CHANGED
@@ -42,8 +42,8 @@ const query = myDataSource
42
42
  name: user.name,
43
43
  email: user.email,
44
44
  ]))
45
- .whereTyped(user => user.name, 'ILIKE', { search: 'John' })
46
- .andWhereTyped(user => user.metadata, ` ->> 'jsonField' =`, { value: 'some-value' })
45
+ .whereTyped(user => user.name, 'ILIKE', 'John')
46
+ .andWhereTyped(user => user.metadata, ` ->> 'jsonField' =`, 'some-value')
47
47
  // Order by type-safe own and relation properties
48
48
  .orderByTyped(user => user.images.url, 'ASC', 'NULLS LAST')
49
49
  // Use pagination as simple as that
@@ -19,13 +19,10 @@ async function getManyWithTotals(builder, paginationFilter, options) {
19
19
  const { limit, offset } = loadAll
20
20
  ? { limit: null, offset: null }
21
21
  : (0, pagination_utils_1.getLimitAndOffset)(paginationFilter);
22
- const requests = [];
22
+ const requests = [null, []];
23
23
  if (!disableTotalsCalculation) {
24
24
  requests[0] = await builder.getCount();
25
25
  }
26
- else {
27
- requests[0] = null;
28
- }
29
26
  if (loadAll) {
30
27
  requests[1] = await builder.getMany();
31
28
  }
@@ -30,18 +30,23 @@ const alwaysAliasWhiteList = [
30
30
  ];
31
31
  function getOrderOption(orderBy, defaultAlias, options) {
32
32
  const { useDoubleQuotes = constants_1.defaultUseDoubleQuotes, alias: customAlias, alwaysAliasFields = alwaysAliasWhiteList, } = options !== null && options !== void 0 ? options : {};
33
- const { field, direction = constants_1.defaultOrderDirection, nulls: nullsOrder = constants_1.defaultNullsOrder, } = orderBy;
34
- const alias = customAlias !== null && customAlias !== void 0 ? customAlias : (alwaysAliasFields.includes(field) ? defaultAlias : undefined);
33
+ const { field, direction, isReversed, nulls: nullsOrder, tableAlias: otherTableAlias, } = orderBy;
34
+ const alias = otherTableAlias || customAlias ||
35
+ (alwaysAliasFields.includes(field) ? defaultAlias : undefined);
35
36
  const identifier = new identifier_utils_1.Identifier(field, alias, useDoubleQuotes);
36
37
  const selector = identifier.fieldIdentifier();
37
- const order = typeof direction === 'number'
38
- ? direction === -1
39
- ? 'DESC'
40
- : 'ASC'
41
- : direction.toUpperCase();
42
- const nulls = nullsOrder && /first|last/i.test(nullsOrder)
38
+ const order = typeof isReversed === 'boolean' && (0, common_utils_1.isNil)(direction)
39
+ ? (isReversed ? 'DESC' : 'ASC')
40
+ : typeof direction === 'boolean'
41
+ ? (direction ? 'ASC' : 'DESC')
42
+ : typeof direction === 'number'
43
+ ? (direction === -1 ? 'DESC' : 'ASC')
44
+ : typeof direction === 'string' && /asc|desc/i.test(direction)
45
+ ? direction.toUpperCase()
46
+ : constants_1.defaultOrderDirection;
47
+ const nulls = typeof nullsOrder === 'string' && /first|last/i.test(nullsOrder)
43
48
  ? `NULLS ${nullsOrder.toUpperCase()}`
44
- : undefined;
49
+ : constants_1.defaultNullsOrder;
45
50
  return {
46
51
  selector,
47
52
  order,
@@ -1,4 +1,3 @@
1
- import { SelectQueryBuilder } from 'typeorm';
2
1
  import { PaginationFilter } from '../types/interfaces/pagination.interface';
3
2
  import { ObjectLiteral } from 'typeorm';
4
3
  import { ApplyPaginationOptions } from '../types/extensions/pagination.types';
@@ -7,4 +6,3 @@ declare module 'typeorm/query-builder/SelectQueryBuilder' {
7
6
  applyPaginationFilter(paginationFilter: PaginationFilter, options?: ApplyPaginationOptions): SelectQueryBuilder<Entity>;
8
7
  }
9
8
  }
10
- export declare const SelectQB: typeof SelectQueryBuilder;
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.SelectQB = void 0;
5
3
  /* eslint-disable @typescript-eslint/no-explicit-any */
6
- const typeorm_1 = require("typeorm");
4
+ const SelectQueryBuilder_1 = require("typeorm/query-builder/SelectQueryBuilder");
7
5
  const constants_1 = require("../constants");
8
6
  const pagination_utils_1 = require("../utils/pagination.utils");
9
7
  const prototype_utils_1 = require("../utils/prototype.utils");
@@ -15,9 +13,7 @@ const extension = {
15
13
  },
16
14
  },
17
15
  };
18
- (0, prototype_utils_1.patchPrototype)(typeorm_1.SelectQueryBuilder, extension);
19
- console.log('After patching:', ((_a = typeorm_1.SelectQueryBuilder.prototype.applyPaginationFilter) === null || _a === void 0 ? void 0 : _a.name) || 'not patched');
20
- exports.SelectQB = typeorm_1.SelectQueryBuilder;
16
+ (0, prototype_utils_1.patchPrototype)(SelectQueryBuilder_1.SelectQueryBuilder, extension);
21
17
  // implementation
22
18
  function applyPaginationFilter(builder, paginationFilter, options) {
23
19
  const { useTakeAndSkip = constants_1.defaultUseTakeAndSkip } = options || {};
@@ -28,6 +28,6 @@ typeorm_1.SelectQueryBuilder.prototype.innerJoinAndSelectTyped = function (selec
28
28
  return this;
29
29
  };
30
30
  function addJoin(query, method, selection, alias, condition, parameters) {
31
- const key = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(selection, query.alias);
31
+ const key = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(selection, query.alias, false);
32
32
  return query[method](key, alias, condition, parameters);
33
33
  }
@@ -13,12 +13,12 @@ typeorm_1.SelectQueryBuilder.prototype.orderByTyped = function (selection, order
13
13
  }
14
14
  return this;
15
15
  };
16
- typeorm_1.SelectQueryBuilder.prototype.addOrderByTyped = function (selection, order, nulls) {
16
+ typeorm_1.SelectQueryBuilder.prototype.addOrderByTyped = function (selection, order, nulls, useDoubleQuotes) {
17
17
  if (selection instanceof proxy_key_utils_1.KeyHolder) {
18
18
  return this.addOrderBy(selection.toSql(), order, nulls);
19
19
  }
20
20
  if (typeof selection === 'function') {
21
- const key = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(selection, this.alias);
21
+ const key = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(selection, this.alias, useDoubleQuotes);
22
22
  return this.addOrderBy(key, order, nulls);
23
23
  }
24
24
  return this;
@@ -7,28 +7,28 @@ declare module 'typeorm/query-builder/SelectQueryBuilder' {
7
7
  * Creates SELECT query.
8
8
  * Replaces all previous selections if they exist.
9
9
  */
10
- selectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: KeyProxyCallback<Type>, selectionAliasName?: string): SelectQueryBuilder<Entity>;
10
+ selectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: KeyProxyCallback<Type>, selectionAliasName?: string, useDoubleQuotes?: boolean): SelectQueryBuilder<Entity>;
11
11
  /**
12
12
  * Creates SELECT query and selects given data.
13
13
  * Replaces all previous selections if they exist.
14
14
  */
15
- selectTyped(this: SelectQueryBuilder<Entity>, selection: string | KeyHolder, selectionAliasName?: string): SelectQueryBuilder<Entity>;
15
+ selectTyped(this: SelectQueryBuilder<Entity>, selection: string | KeyHolder, selectionAliasName?: string, useDoubleQuotes?: boolean): SelectQueryBuilder<Entity>;
16
16
  /**
17
17
  * Creates SELECT query and selects given data.
18
18
  * Replaces all previous selections if they exist.
19
19
  */
20
- selectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: string[] | KeyHolder[] | Array<KeyProxyCallback<Type>>): SelectQueryBuilder<Entity>;
20
+ selectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: string[] | KeyHolder[] | Array<KeyProxyCallback<Type>>, useDoubleQuotes?: boolean): SelectQueryBuilder<Entity>;
21
21
  /**
22
22
  * Adds new selection to the SELECT query.
23
23
  */
24
- addSelectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: KeyProxyCallback<Type>, selectionAliasName?: string): SelectQueryBuilder<Entity>;
24
+ addSelectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: KeyProxyCallback<Type>, selectionAliasName?: string, useDoubleQuotes?: boolean): SelectQueryBuilder<Entity>;
25
25
  /**
26
26
  * Adds new selection to the SELECT query.
27
27
  */
28
- addSelectTyped(this: SelectQueryBuilder<Entity>, selection: string | KeyHolder, selectionAliasName?: string): SelectQueryBuilder<Entity>;
28
+ addSelectTyped(this: SelectQueryBuilder<Entity>, selection: string | KeyHolder, selectionAliasName?: string, useDoubleQuotes?: boolean): SelectQueryBuilder<Entity>;
29
29
  /**
30
30
  * Adds new selection to the SELECT query.
31
31
  */
32
- addSelectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: string[] | KeyHolder[] | Array<KeyProxyCallback<Type>>): SelectQueryBuilder<Entity>;
32
+ addSelectTyped<Type extends ObjectLiteral = Entity>(this: SelectQueryBuilder<Entity>, selection: string[] | KeyHolder[] | Array<KeyProxyCallback<Type>>, useDoubleQuotes?: boolean): SelectQueryBuilder<Entity>;
33
33
  }
34
34
  }
@@ -6,26 +6,30 @@ const prototype_utils_1 = require("../../../utils/prototype.utils");
6
6
  // patching
7
7
  const extension = {
8
8
  prototype: {
9
- selectTyped(selection, selectionAliasName) {
10
- return attachSelectMethod(this, 'select', selection, selectionAliasName);
9
+ selectTyped(selection, selectionAliasName, useDoubleQuotes) {
10
+ return attachSelectMethod(this, 'select', selection, typeof selectionAliasName === 'string' ? selectionAliasName : undefined, typeof selectionAliasName === 'boolean'
11
+ ? selectionAliasName
12
+ : useDoubleQuotes);
11
13
  },
12
- addSelectTyped(selection, selectionAliasName) {
13
- return attachSelectMethod(this, 'addSelect', selection, selectionAliasName);
14
+ addSelectTyped(selection, selectionAliasName, useDoubleQuotes) {
15
+ return attachSelectMethod(this, 'addSelect', selection, typeof selectionAliasName === 'string' ? selectionAliasName : undefined, typeof selectionAliasName === 'boolean'
16
+ ? selectionAliasName
17
+ : useDoubleQuotes);
14
18
  },
15
19
  },
16
20
  };
17
21
  (0, prototype_utils_1.patchPrototype)(typeorm_1.SelectQueryBuilder, extension);
18
22
  // implementation
19
- const attachSelectMethod = (query, method, selection, selectionAliasName) => {
23
+ const attachSelectMethod = (query, method, selection, selectionAliasName, useDoubleQuotes) => {
20
24
  if (selection instanceof proxy_key_utils_1.KeyHolder) {
21
- return query[method](selection.toSql(), selectionAliasName);
25
+ return query[method](selection.toSql(undefined, useDoubleQuotes), selectionAliasName);
22
26
  }
23
27
  if (Array.isArray(selection)) {
24
- const arrayOfKeys = selection.map((select) => (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(select, query.alias));
28
+ const arrayOfKeys = selection.map((select) => (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(select, query.alias, useDoubleQuotes));
25
29
  return query[method](arrayOfKeys);
26
30
  }
27
31
  else if (typeof selection === 'function') {
28
- const keyArrayOrSingleKey = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(selection, query.alias);
32
+ const keyArrayOrSingleKey = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(selection, query.alias, useDoubleQuotes);
29
33
  if (Array.isArray(keyArrayOrSingleKey)) {
30
34
  return query[method](keyArrayOrSingleKey);
31
35
  }
@@ -9,17 +9,17 @@ declare module 'typeorm/query-builder/WhereExpressionBuilder' {
9
9
  * calling this function will override previously set WHERE conditions.
10
10
  * Additionally you can add parameters used in where expression.
11
11
  */
12
- whereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
12
+ whereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
13
13
  /**
14
14
  * Adds new AND WHERE condition in the query builder.
15
15
  * Additionally you can add parameters used in where expression.
16
16
  */
17
- andWhereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
17
+ andWhereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
18
18
  /**
19
19
  * Adds new OR WHERE condition in the query builder.
20
20
  * Additionally you can add parameters used in where expression.
21
21
  */
22
- orWhereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
22
+ orWhereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
23
23
  }
24
24
  }
25
25
  declare module 'typeorm/query-builder/SelectQueryBuilder' {
@@ -30,17 +30,17 @@ declare module 'typeorm/query-builder/SelectQueryBuilder' {
30
30
  * calling this function will override previously set WHERE conditions.
31
31
  * Additionally you can add parameters used in where expression.
32
32
  */
33
- whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
33
+ whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
34
34
  /**
35
35
  * Adds new AND WHERE condition in the query builder.
36
36
  * Additionally you can add parameters used in where expression.
37
37
  */
38
- andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
38
+ andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
39
39
  /**
40
40
  * Adds new OR WHERE condition in the query builder.
41
41
  * Additionally you can add parameters used in where expression.
42
42
  */
43
- orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
43
+ orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
44
44
  }
45
45
  }
46
46
  declare module 'typeorm/query-builder/UpdateQueryBuilder' {
@@ -51,17 +51,17 @@ declare module 'typeorm/query-builder/UpdateQueryBuilder' {
51
51
  * calling this function will override previously set WHERE conditions.
52
52
  * Additionally you can add parameters used in where expression.
53
53
  */
54
- whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
54
+ whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
55
55
  /**
56
56
  * Adds new AND WHERE condition in the query builder.
57
57
  * Additionally you can add parameters used in where expression.
58
58
  */
59
- andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
59
+ andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
60
60
  /**
61
61
  * Adds new OR WHERE condition in the query builder.
62
62
  * Additionally you can add parameters used in where expression.
63
63
  */
64
- orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
64
+ orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
65
65
  }
66
66
  }
67
67
  declare module 'typeorm/query-builder/DeleteQueryBuilder' {
@@ -72,16 +72,16 @@ declare module 'typeorm/query-builder/DeleteQueryBuilder' {
72
72
  * calling this function will override previously set WHERE conditions.
73
73
  * Additionally you can add parameters used in where expression.
74
74
  */
75
- whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
75
+ whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
76
76
  /**
77
77
  * Adds new AND WHERE condition in the query builder.
78
78
  * Additionally you can add parameters used in where expression.
79
79
  */
80
- andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
80
+ andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
81
81
  /**
82
82
  * Adds new OR WHERE condition in the query builder.
83
83
  * Additionally you can add parameters used in where expression.
84
84
  */
85
- orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: ObjectLiteral, options?: TypedWhereOptions): this;
85
+ orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
86
86
  }
87
87
  }
@@ -1,14 +1,19 @@
1
1
  import { NonFunctionPropertyNames } from "../util-types";
2
- export type OrderDirection = 'asc' | 'desc' | 'ASC' | 'DESC' | 1 | -1;
2
+ export type OrderDirection = 'asc' | 'desc' | 'ASC' | 'DESC' | 1 | -1 | true | false;
3
3
  export type NullsOrder = 'first' | 'last' | 'FIRST' | 'LAST';
4
4
  export type OrderParam<Entity> = {
5
- field: NonFunctionPropertyNames<Required<Entity>> | string;
5
+ field: NonFunctionPropertyNames<Required<Entity>>;
6
+ /** -1 = DESC, false = DESC */
6
7
  direction?: OrderDirection;
7
8
  nulls?: NullsOrder;
9
+ /** true = DESC, false = ASC */
10
+ isReversed?: boolean;
11
+ /** order by related table */
12
+ tableAlias?: string;
8
13
  };
9
14
  export type OrderType = {
10
15
  selector: string;
11
- order: 'ASC' | 'DESC';
16
+ order?: 'ASC' | 'DESC';
12
17
  nulls?: 'NULLS FIRST' | 'NULLS LAST';
13
18
  };
14
19
  export interface OrderFilter<Entity> {
@@ -3,7 +3,7 @@ import { KeyProxyCallback } from '../types/modules/proxy-callback.types';
3
3
  export declare class KeyHolder {
4
4
  private _keys;
5
5
  constructor(_keys?: string[]);
6
- toSql(length?: number, addDobleQuotes?: boolean): string;
6
+ toSql(length?: number, useDoubleQuotes?: boolean): string;
7
7
  toString(length?: number): string;
8
8
  valueOf(): string;
9
9
  [Symbol.toPrimitive](): string;
@@ -21,4 +21,4 @@ export declare class KeyProxy {
21
21
  }
22
22
  export declare const getKeyHolder: <Type extends ObjectLiteral>(callback: KeyHolder | KeyProxyCallback<Type>) => KeyHolder | Array<KeyHolder>;
23
23
  export declare const getKey: <Type extends ObjectLiteral>(callback: string | KeyHolder | KeyProxyCallback<Type>) => string;
24
- export declare const getSqlKeyFromProxyCallback: <Type extends ObjectLiteral>(callback: string | KeyHolder | KeyProxyCallback<Type>, addPrefix?: string, addDobleQuotes?: boolean) => string | string[];
24
+ export declare const getSqlKeyFromProxyCallback: <Type extends ObjectLiteral>(callback: string | KeyHolder | KeyProxyCallback<Type>, addPrefix?: string, useDoubleQuotes?: boolean) => string | string[];
@@ -7,9 +7,9 @@ class KeyHolder {
7
7
  this._keys = _keys;
8
8
  this._keys = [..._keys];
9
9
  }
10
- toSql(length = 2, addDobleQuotes = true) {
10
+ toSql(length = 2, useDoubleQuotes) {
11
11
  const string = this.toString(length);
12
- const sql = addDobleQuotes ? (0, sql_utils_1.stringToSQLIdentifier)(string) : string;
12
+ const sql = useDoubleQuotes ? (0, sql_utils_1.stringToSQLIdentifier)(string) : string;
13
13
  return sql;
14
14
  }
15
15
  toString(length = 2) {
@@ -86,7 +86,7 @@ const getKey = (callback) => {
86
86
  return keyHolder.toString();
87
87
  };
88
88
  exports.getKey = getKey;
89
- const getSqlKeyFromProxyCallback = (callback, addPrefix, addDobleQuotes = true) => {
89
+ const getSqlKeyFromProxyCallback = (callback, addPrefix, useDoubleQuotes) => {
90
90
  if (typeof callback === 'string')
91
91
  return callback;
92
92
  const keyHolder = (0, exports.getKeyHolder)(callback);
@@ -96,14 +96,14 @@ const getSqlKeyFromProxyCallback = (callback, addPrefix, addDobleQuotes = true)
96
96
  // if select is like this: (proxy) => proxy.relation.column
97
97
  if (keyHolder.getKeys().length > 1) {
98
98
  // return unchanged
99
- return keyHolder.toSql(undefined, addDobleQuotes);
99
+ return keyHolder.toSql(undefined, useDoubleQuotes);
100
100
  }
101
101
  // else if select is one layer depth like this: (proxy) => proxy.column
102
102
  // then add alias before the key
103
103
  if (typeof addPrefix === 'string' && addPrefix.length >= 1) {
104
104
  keyHolder.unshift(addPrefix);
105
105
  }
106
- return keyHolder.toSql(undefined, addDobleQuotes);
106
+ return keyHolder.toSql(undefined, useDoubleQuotes);
107
107
  };
108
108
  exports.getSqlKeyFromProxyCallback = getSqlKeyFromProxyCallback;
109
109
  // example of typed select
@@ -1,6 +1,6 @@
1
1
  import type { WhereExpressionBuilder } from 'typeorm';
2
2
  import type { WhereMethods } from '../types/extensions/where.types';
3
3
  export declare function getValuePlaceholder<T>(value: T, paramName?: string): string;
4
- export declare function attachWhere<QB extends WhereExpressionBuilder>(builder: QB, method: WhereMethods, field: string, conditions: string, value?: unknown, options?: {
4
+ export declare function attachWhere<QB extends WhereExpressionBuilder>(builder: QB, method: WhereMethods, field: string, conditions: string, params?: unknown, options?: {
5
5
  addValue?: boolean;
6
6
  }): QB;
@@ -12,22 +12,29 @@ function getValuePlaceholder(value, paramName) {
12
12
  return `:${paramName}`;
13
13
  }
14
14
  exports.getValuePlaceholder = getValuePlaceholder;
15
- function attachWhere(builder, method, field, conditions, value, options) {
15
+ function attachWhere(builder, method, field, conditions, params, options) {
16
16
  const { addValue = true } = options || {};
17
17
  if (method && typeof field === 'string' && typeof conditions === 'string') {
18
18
  field = (0, sql_utils_1.stringToSQLIdentifier)(field);
19
- if (addValue) {
20
- const firstValue = value !== null && typeof value === 'object' ? Object.values(value)[0] : value;
21
- const valueIsValid = firstValue !== undefined;
19
+ const hasParams = typeof params !== 'undefined';
20
+ if (addValue && hasParams) {
21
+ // pick first value from params object or pass as is
22
+ const firstValue = params !== null && typeof params === 'object' && !Array.isArray(params)
23
+ ? Object.values(params)[0]
24
+ : params;
25
+ // 0, false, '' are valid values like any other
26
+ // null, undefined and NaN are equal to NULL
27
+ const valueIsValid = firstValue !== undefined &&
28
+ (typeof firstValue === 'number' ? !isNaN(firstValue) : true);
22
29
  const paramName = valueIsValid
23
30
  ? (0, sql_utils_1.createUniqueParameterName)(method)
24
31
  : undefined;
25
- const params = paramName && valueIsValid ? { [paramName]: firstValue } : undefined;
32
+ const queryParams = paramName && valueIsValid ? { [paramName]: firstValue } : undefined;
26
33
  const valuePlaceholder = getValuePlaceholder(firstValue, paramName);
27
- return builder[method](`${field} ${conditions} ${valuePlaceholder}`, params);
34
+ return builder[method](`${field} ${conditions} ${valuePlaceholder}`, queryParams);
28
35
  }
29
36
  else {
30
- return builder[method](`${field} ${conditions}`, value);
37
+ return builder[method](`${field} ${conditions}`, params);
31
38
  }
32
39
  }
33
40
  return builder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeorm-extensions",
3
- "version": "1.3.2",
3
+ "version": "1.3.5",
4
4
  "description": "typeorm commonly used extensions",
5
5
  "main": "./dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -13,8 +13,7 @@
13
13
  },
14
14
  "scripts": {
15
15
  "build": "tsc -p .",
16
- "lint": "eslint . --ext .ts",
17
- "test-nest-build": "cd test/nest_v11 && npm install && npm run build"
16
+ "lint": "eslint . --ext .ts"
18
17
  },
19
18
  "keywords": [
20
19
  "typeorm",