typeorm-extensions 1.3.4 → 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.
@@ -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,28 +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
- // is object (not primitive, null or array)
21
- const firstValue = value !== null && typeof value === 'object' && !Array.isArray(value)
22
- ? Object.values(value)[0]
23
- : value;
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;
24
25
  // 0, false, '' are valid values like any other
25
- // null is a valid value, but undefined and NaN are not
26
+ // null, undefined and NaN are equal to NULL
26
27
  const valueIsValid = firstValue !== undefined &&
27
28
  (typeof firstValue === 'number' ? !isNaN(firstValue) : true);
28
29
  const paramName = valueIsValid
29
30
  ? (0, sql_utils_1.createUniqueParameterName)(method)
30
31
  : undefined;
31
- const params = paramName && valueIsValid ? { [paramName]: firstValue } : undefined;
32
+ const queryParams = paramName && valueIsValid ? { [paramName]: firstValue } : undefined;
32
33
  const valuePlaceholder = getValuePlaceholder(firstValue, paramName);
33
- return builder[method](`${field} ${conditions} ${valuePlaceholder}`, params);
34
+ return builder[method](`${field} ${conditions} ${valuePlaceholder}`, queryParams);
34
35
  }
35
36
  else {
36
- return builder[method](`${field} ${conditions}`, value);
37
+ return builder[method](`${field} ${conditions}`, params);
37
38
  }
38
39
  }
39
40
  return builder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeorm-extensions",
3
- "version": "1.3.4",
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",