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.
- package/dist/utils/where.utils.d.ts +1 -1
- package/dist/utils/where.utils.js +11 -10
- package/package.json +1 -1
|
@@ -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,
|
|
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,
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
|
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
|
|
32
|
+
const queryParams = paramName && valueIsValid ? { [paramName]: firstValue } : undefined;
|
|
32
33
|
const valuePlaceholder = getValuePlaceholder(firstValue, paramName);
|
|
33
|
-
return builder[method](`${field} ${conditions} ${valuePlaceholder}`,
|
|
34
|
+
return builder[method](`${field} ${conditions} ${valuePlaceholder}`, queryParams);
|
|
34
35
|
}
|
|
35
36
|
else {
|
|
36
|
-
return builder[method](`${field} ${conditions}`,
|
|
37
|
+
return builder[method](`${field} ${conditions}`, params);
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
return builder;
|