typeorm-extensions 1.3.5 → 2.0.0
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 +14 -10
- package/dist/constants/index.d.ts +1 -1
- package/dist/constants/index.js +2 -2
- package/dist/extensions/cursor-pagination.extension.d.ts +18 -0
- package/dist/extensions/cursor-pagination.extension.js +108 -0
- package/dist/extensions/index.d.ts +7 -0
- package/dist/extensions/index.js +7 -0
- package/dist/extensions/order-by-array-position.extension.d.ts +24 -0
- package/dist/extensions/order-by-array-position.extension.js +18 -0
- package/dist/extensions/order.extension.d.ts +3 -2
- package/dist/extensions/order.extension.js +16 -13
- package/dist/extensions/period-filter.extension.d.ts +29 -0
- package/dist/extensions/period-filter.extension.js +23 -0
- package/dist/extensions/typed/index.d.ts +10 -0
- package/dist/extensions/typed/index.js +10 -0
- package/dist/extensions/typed/typed-distinct/typed-distinct.extension.js +2 -2
- package/dist/extensions/typed/typed-joins/typed-joins.extension.js +2 -1
- package/dist/extensions/typed/typed-order-by/typed-order-by-array-position.extension.d.ts +20 -0
- package/dist/extensions/typed/typed-order-by/typed-order-by-array-position.extension.js +21 -0
- package/dist/extensions/typed/typed-order-by/typed-order-by.extension.d.ts +4 -3
- package/dist/extensions/typed/typed-order-by/typed-order-by.extension.js +7 -6
- package/dist/extensions/typed/typed-returning/typed-returning.extension.d.ts +27 -0
- package/dist/extensions/typed/typed-returning/typed-returning.extension.js +38 -0
- package/dist/extensions/typed/typed-select/typed-select.extension.d.ts +6 -6
- package/dist/extensions/typed/typed-select/typed-select.extension.js +11 -8
- package/dist/extensions/typed/typed-where/typed-period-filter.extension.d.ts +31 -0
- package/dist/extensions/typed/typed-where/typed-period-filter.extension.js +25 -0
- package/dist/extensions/typed/typed-where/typed-where-array.extension.d.ts +68 -0
- package/dist/extensions/typed/typed-where/typed-where-array.extension.js +41 -0
- package/dist/extensions/typed/typed-where/typed-where-ilike.extension.d.ts +62 -0
- package/dist/extensions/typed/typed-where/typed-where-ilike.extension.js +84 -0
- package/dist/extensions/typed/typed-where/typed-where-is-any.extension.d.ts +144 -0
- package/dist/extensions/typed/typed-where/typed-where-is-any.extension.js +37 -0
- package/dist/extensions/typed/typed-where/typed-where-is-distinct-from.extension.d.ts +53 -0
- package/dist/extensions/typed/typed-where/typed-where-is-distinct-from.extension.js +38 -0
- package/dist/extensions/typed/typed-where/typed-where-is-in.extension.d.ts +25 -26
- package/dist/extensions/typed/typed-where/typed-where-is-in.extension.js +16 -23
- package/dist/extensions/typed/typed-where/typed-where-json.extension.d.ts +53 -0
- package/dist/extensions/typed/typed-where/typed-where-json.extension.js +39 -0
- package/dist/extensions/typed/typed-where/typed-where-lower.extension.d.ts +59 -0
- package/dist/extensions/typed/typed-where/typed-where-lower.extension.js +86 -0
- package/dist/extensions/typed/typed-where/typed-where-similar.extension.d.ts +54 -0
- package/dist/extensions/typed/typed-where/typed-where-similar.extension.js +73 -0
- package/dist/extensions/typed/typed-where/typed-where.extension.d.ts +13 -13
- package/dist/extensions/typed/typed-where/typed-where.extension.js +4 -12
- package/dist/extensions/where-array.extension.d.ts +70 -0
- package/dist/extensions/where-array.extension.js +47 -0
- package/dist/extensions/where-is-any.extension.d.ts +142 -0
- package/dist/extensions/where-is-any.extension.js +44 -0
- package/dist/extensions/where-is-distinct-from.extension.d.ts +56 -0
- package/dist/extensions/where-is-distinct-from.extension.js +55 -0
- package/dist/extensions/where-json.extension.d.ts +55 -0
- package/dist/extensions/where-json.extension.js +64 -0
- package/dist/helpers.d.ts +2 -0
- package/dist/helpers.js +23 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/dist/types/extensions/cursor-pagination.types.d.ts +16 -0
- package/dist/types/extensions/cursor-pagination.types.js +2 -0
- package/dist/types/extensions/get-many-with-totals.types.d.ts +3 -1
- package/dist/types/extensions/index.d.ts +4 -0
- package/dist/types/extensions/index.js +4 -0
- package/dist/types/extensions/order.types.d.ts +1 -1
- package/dist/types/extensions/period.types.d.ts +15 -0
- package/dist/types/extensions/period.types.js +2 -0
- package/dist/types/extensions/search.types.d.ts +68 -0
- package/dist/types/extensions/search.types.js +2 -0
- package/dist/types/extensions/typed-order-by.types.d.ts +3 -0
- package/dist/types/extensions/typed-order-by.types.js +2 -0
- package/dist/types/extensions/typed-where.types.d.ts +4 -4
- package/dist/types/extensions/where.types.d.ts +1 -0
- package/dist/types/interfaces/cursor-pagination.interface.d.ts +16 -0
- package/dist/types/interfaces/cursor-pagination.interface.js +2 -0
- package/dist/types/interfaces/index.d.ts +2 -0
- package/dist/types/interfaces/index.js +2 -0
- package/dist/types/interfaces/period.interface.d.ts +12 -0
- package/dist/types/interfaces/period.interface.js +2 -0
- package/dist/types/modules/proxy-callback.types.d.ts +26 -3
- package/dist/types/util-types/index.d.ts +3 -1
- package/dist/utils/cursor.utils.d.ts +31 -0
- package/dist/utils/cursor.utils.js +130 -0
- package/dist/utils/order.utils.d.ts +15 -0
- package/dist/utils/order.utils.js +45 -0
- package/dist/utils/period.utils.d.ts +9 -0
- package/dist/utils/period.utils.js +82 -0
- package/dist/utils/proxy-key.utils.d.ts +13 -2
- package/dist/utils/proxy-key.utils.js +32 -14
- package/dist/utils/query-builder.utils.d.ts +46 -0
- package/dist/utils/query-builder.utils.js +208 -0
- package/dist/utils/search.utils.d.ts +27 -0
- package/dist/utils/search.utils.js +83 -0
- package/dist/utils/sql.utils.d.ts +27 -1
- package/dist/utils/sql.utils.js +87 -9
- package/dist/utils/table.utils.d.ts +86 -0
- package/dist/utils/table.utils.js +135 -0
- package/dist/utils/where.utils.d.ts +18 -1
- package/dist/utils/where.utils.js +62 -9
- package/helpers/package.json +7 -0
- package/package.json +3 -3
- package/dist/utils/identifier.utils.d.ts +0 -15
- package/dist/utils/identifier.utils.js +0 -45
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("../../where-is-distinct-from.extension");
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
const query_builder_utils_1 = require("../../../utils/query-builder.utils");
|
|
6
|
+
const prototype_utils_1 = require("../../../utils/prototype.utils");
|
|
7
|
+
/* eslint-enable @typescript-eslint/no-empty-interface */
|
|
8
|
+
// patching
|
|
9
|
+
const createWhereDistinctTypedMethod = (method) => function (where, value) {
|
|
10
|
+
return attachWhereDistinctTyped(this, method, where, value);
|
|
11
|
+
};
|
|
12
|
+
const extension = {
|
|
13
|
+
prototype: {
|
|
14
|
+
whereIsDistinctFromTyped: createWhereDistinctTypedMethod('whereIsDistinctFrom'),
|
|
15
|
+
andWhereIsDistinctFromTyped: createWhereDistinctTypedMethod('andWhereIsDistinctFrom'),
|
|
16
|
+
orWhereIsDistinctFromTyped: createWhereDistinctTypedMethod('orWhereIsDistinctFrom'),
|
|
17
|
+
whereIsNotDistinctFromTyped: createWhereDistinctTypedMethod('whereIsNotDistinctFrom'),
|
|
18
|
+
andWhereIsNotDistinctFromTyped: createWhereDistinctTypedMethod('andWhereIsNotDistinctFrom'),
|
|
19
|
+
orWhereIsNotDistinctFromTyped: createWhereDistinctTypedMethod('orWhereIsNotDistinctFrom'),
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
const queryBuilders = [
|
|
23
|
+
typeorm_1.SelectQueryBuilder,
|
|
24
|
+
typeorm_1.UpdateQueryBuilder,
|
|
25
|
+
typeorm_1.DeleteQueryBuilder,
|
|
26
|
+
];
|
|
27
|
+
queryBuilders.forEach((builder) => {
|
|
28
|
+
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
29
|
+
});
|
|
30
|
+
// implementation
|
|
31
|
+
function attachWhereDistinctTyped(builder, method, where, value) {
|
|
32
|
+
if (typeof where !== 'function') {
|
|
33
|
+
return builder;
|
|
34
|
+
}
|
|
35
|
+
const key = (0, query_builder_utils_1.getTypedSingleColumnKey)(builder, where, `${method}Typed`);
|
|
36
|
+
// .andWhereIsDistinctFrom(`"table"."column"`, value)
|
|
37
|
+
return builder[method](key, value);
|
|
38
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import '../../where-is-in.extension';
|
|
2
2
|
import { ObjectLiteral } from 'typeorm';
|
|
3
|
-
import { ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
|
|
3
|
+
import { DeepUnwrapArray, ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
|
|
4
4
|
import { ArgsType } from '../../../types/util-types';
|
|
5
|
-
import { TypedWhereIsInOptions } from '../../../types/extensions/typed-where.types';
|
|
6
5
|
declare module 'typeorm/query-builder/WhereExpressionBuilder' {
|
|
7
6
|
interface WhereExpressionBuilder {
|
|
8
7
|
/**
|
|
@@ -10,29 +9,29 @@ declare module 'typeorm/query-builder/WhereExpressionBuilder' {
|
|
|
10
9
|
* If you had previously WHERE expression defined,
|
|
11
10
|
* calling this function will override previously set WHERE conditions.
|
|
12
11
|
*/
|
|
13
|
-
whereIsInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
12
|
+
whereIsInTyped<Type extends ObjectLiteral>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
14
13
|
/**
|
|
15
14
|
* Adds new AND WHERE IN condition in the query builder.
|
|
16
15
|
*/
|
|
17
|
-
andWhereIsInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
16
|
+
andWhereIsInTyped<Type extends ObjectLiteral>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
18
17
|
/**
|
|
19
18
|
* Adds new OR WHERE IN condition in the query builder.
|
|
20
19
|
*/
|
|
21
|
-
orWhereIsInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
20
|
+
orWhereIsInTyped<Type extends ObjectLiteral>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
22
21
|
/**
|
|
23
22
|
* Sets WHERE NOT IN condition in the query builder.
|
|
24
23
|
* If you had previously WHERE expression defined,
|
|
25
24
|
* calling this function will override previously set WHERE conditions.
|
|
26
25
|
*/
|
|
27
|
-
whereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
26
|
+
whereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
28
27
|
/**
|
|
29
28
|
* Adds new AND WHERE NOT IN condition in the query builder.
|
|
30
29
|
*/
|
|
31
|
-
andWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
30
|
+
andWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
32
31
|
/**
|
|
33
32
|
* Adds new OR WHERE NOT IN condition in the query builder.
|
|
34
33
|
*/
|
|
35
|
-
orWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
34
|
+
orWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
declare module 'typeorm/query-builder/SelectQueryBuilder' {
|
|
@@ -42,29 +41,29 @@ declare module 'typeorm/query-builder/SelectQueryBuilder' {
|
|
|
42
41
|
* If you had previously WHERE expression defined,
|
|
43
42
|
* calling this function will override previously set WHERE conditions.
|
|
44
43
|
*/
|
|
45
|
-
whereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
44
|
+
whereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
46
45
|
/**
|
|
47
46
|
* Adds new AND WHERE IN condition in the query builder.
|
|
48
47
|
*/
|
|
49
|
-
andWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
48
|
+
andWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
50
49
|
/**
|
|
51
50
|
* Adds new OR WHERE IN condition in the query builder.
|
|
52
51
|
*/
|
|
53
|
-
orWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
52
|
+
orWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
54
53
|
/**
|
|
55
54
|
* Sets WHERE NOT IN condition in the query builder.
|
|
56
55
|
* If you had previously WHERE expression defined,
|
|
57
56
|
* calling this function will override previously set WHERE conditions.
|
|
58
57
|
*/
|
|
59
|
-
whereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
58
|
+
whereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
60
59
|
/**
|
|
61
60
|
* Adds new AND WHERE NOT IN condition in the query builder.
|
|
62
61
|
*/
|
|
63
|
-
andWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
62
|
+
andWhereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
64
63
|
/**
|
|
65
64
|
* Adds new OR WHERE NOT IN condition in the query builder.
|
|
66
65
|
*/
|
|
67
|
-
orWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
66
|
+
orWhereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
68
67
|
}
|
|
69
68
|
}
|
|
70
69
|
declare module 'typeorm/query-builder/UpdateQueryBuilder' {
|
|
@@ -74,29 +73,29 @@ declare module 'typeorm/query-builder/UpdateQueryBuilder' {
|
|
|
74
73
|
* If you had previously WHERE expression defined,
|
|
75
74
|
* calling this function will override previously set WHERE conditions.
|
|
76
75
|
*/
|
|
77
|
-
whereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
76
|
+
whereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
78
77
|
/**
|
|
79
78
|
* Adds new AND WHERE IN condition in the query builder.
|
|
80
79
|
*/
|
|
81
|
-
andWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
80
|
+
andWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
82
81
|
/**
|
|
83
82
|
* Adds new OR WHERE IN condition in the query builder.
|
|
84
83
|
*/
|
|
85
|
-
orWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
84
|
+
orWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
86
85
|
/**
|
|
87
86
|
* Sets WHERE NOT IN condition in the query builder.
|
|
88
87
|
* If you had previously WHERE expression defined,
|
|
89
88
|
* calling this function will override previously set WHERE conditions.
|
|
90
89
|
*/
|
|
91
|
-
whereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
90
|
+
whereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
92
91
|
/**
|
|
93
92
|
* Adds new AND WHERE NOT IN condition in the query builder.
|
|
94
93
|
*/
|
|
95
|
-
andWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
94
|
+
andWhereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
96
95
|
/**
|
|
97
96
|
* Adds new OR WHERE NOT IN condition in the query builder.
|
|
98
97
|
*/
|
|
99
|
-
orWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
98
|
+
orWhereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
100
99
|
}
|
|
101
100
|
}
|
|
102
101
|
declare module 'typeorm/query-builder/DeleteQueryBuilder' {
|
|
@@ -106,28 +105,28 @@ declare module 'typeorm/query-builder/DeleteQueryBuilder' {
|
|
|
106
105
|
* If you had previously WHERE expression defined,
|
|
107
106
|
* calling this function will override previously set WHERE conditions.
|
|
108
107
|
*/
|
|
109
|
-
whereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
108
|
+
whereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
110
109
|
/**
|
|
111
110
|
* Adds new AND WHERE IN condition in the query builder.
|
|
112
111
|
*/
|
|
113
|
-
andWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
112
|
+
andWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
114
113
|
/**
|
|
115
114
|
* Adds new OR WHERE IN condition in the query builder.
|
|
116
115
|
*/
|
|
117
|
-
orWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
116
|
+
orWhereIsInTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
118
117
|
/**
|
|
119
118
|
* Sets WHERE NOT IN condition in the query builder.
|
|
120
119
|
* If you had previously WHERE expression defined,
|
|
121
120
|
* calling this function will override previously set WHERE conditions.
|
|
122
121
|
*/
|
|
123
|
-
whereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
122
|
+
whereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
124
123
|
/**
|
|
125
124
|
* Adds new AND WHERE NOT IN condition in the query builder.
|
|
126
125
|
*/
|
|
127
|
-
andWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
126
|
+
andWhereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
128
127
|
/**
|
|
129
128
|
* Adds new OR WHERE NOT IN condition in the query builder.
|
|
130
129
|
*/
|
|
131
|
-
orWhereIsNotInTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, array?: ArgsType
|
|
130
|
+
orWhereIsNotInTyped<Type extends ObjectLiteral = Entity>(subQuery: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, array?: ArgsType): this;
|
|
132
131
|
}
|
|
133
132
|
}
|
|
@@ -2,28 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
require("../../where-is-in.extension");
|
|
4
4
|
const typeorm_1 = require("typeorm");
|
|
5
|
-
const
|
|
5
|
+
const query_builder_utils_1 = require("../../../utils/query-builder.utils");
|
|
6
6
|
const prototype_utils_1 = require("../../../utils/prototype.utils");
|
|
7
7
|
// patching
|
|
8
8
|
const extension = {
|
|
9
9
|
prototype: {
|
|
10
|
-
whereIsInTyped: function (where, array
|
|
11
|
-
return attachWhereIsIn(this, 'whereIsIn', where, array
|
|
10
|
+
whereIsInTyped: function (where, array) {
|
|
11
|
+
return attachWhereIsIn(this, 'whereIsIn', where, array);
|
|
12
12
|
},
|
|
13
|
-
andWhereIsInTyped: function (where, array
|
|
14
|
-
return attachWhereIsIn(this, 'andWhereIsIn', where, array
|
|
13
|
+
andWhereIsInTyped: function (where, array) {
|
|
14
|
+
return attachWhereIsIn(this, 'andWhereIsIn', where, array);
|
|
15
15
|
},
|
|
16
|
-
orWhereIsInTyped: function (where, array
|
|
17
|
-
return attachWhereIsIn(this, 'orWhereIsIn', where, array
|
|
16
|
+
orWhereIsInTyped: function (where, array) {
|
|
17
|
+
return attachWhereIsIn(this, 'orWhereIsIn', where, array);
|
|
18
18
|
},
|
|
19
|
-
whereIsNotInTyped: function (where, array
|
|
20
|
-
return attachWhereIsIn(this, 'whereIsNotIn', where, array
|
|
19
|
+
whereIsNotInTyped: function (where, array) {
|
|
20
|
+
return attachWhereIsIn(this, 'whereIsNotIn', where, array);
|
|
21
21
|
},
|
|
22
|
-
andWhereIsNotInTyped: function (where, array
|
|
23
|
-
return attachWhereIsIn(this, 'andWhereIsNotIn', where, array
|
|
22
|
+
andWhereIsNotInTyped: function (where, array) {
|
|
23
|
+
return attachWhereIsIn(this, 'andWhereIsNotIn', where, array);
|
|
24
24
|
},
|
|
25
|
-
orWhereIsNotInTyped: function (where, array
|
|
26
|
-
return attachWhereIsIn(this, 'orWhereIsNotIn', where, array
|
|
25
|
+
orWhereIsNotInTyped: function (where, array) {
|
|
26
|
+
return attachWhereIsIn(this, 'orWhereIsNotIn', where, array);
|
|
27
27
|
},
|
|
28
28
|
},
|
|
29
29
|
};
|
|
@@ -36,17 +36,10 @@ queryBuilders.forEach((builder) => {
|
|
|
36
36
|
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
37
37
|
});
|
|
38
38
|
// implementation
|
|
39
|
-
function attachWhereIsIn(builder, method, where, array
|
|
40
|
-
const { useDoubleQuotes } = options || {};
|
|
39
|
+
function attachWhereIsIn(builder, method, where, array) {
|
|
41
40
|
if (typeof where === 'function') {
|
|
42
|
-
|
|
43
|
-
const key = (0,
|
|
44
|
-
if (Array.isArray(key)) {
|
|
45
|
-
for (const k of key) {
|
|
46
|
-
builder = builder[method](k, array);
|
|
47
|
-
}
|
|
48
|
-
return builder;
|
|
49
|
-
}
|
|
41
|
+
// several columns have no single meaning (AND? OR?), and `where…` kept only the last one
|
|
42
|
+
const key = (0, query_builder_utils_1.getTypedSingleColumnKey)(builder, where, `${method}Typed`);
|
|
50
43
|
// .andWhere(`"table"."column" IN (:...values)`, { values: ['value'] })
|
|
51
44
|
return builder[method](key, array);
|
|
52
45
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import '../../where-json.extension';
|
|
2
|
+
import { ObjectLiteral } from 'typeorm';
|
|
3
|
+
import { DeepUnwrapArray, ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
|
|
4
|
+
/**
|
|
5
|
+
* Typesafe conditions on PostgreSQL `jsonb` / `json` columns: the column comes from an entity property.
|
|
6
|
+
* See {@link WhereJsonMethods}.
|
|
7
|
+
*/
|
|
8
|
+
export interface TypedWhereJsonMethods<Entity extends ObjectLiteral> {
|
|
9
|
+
/**
|
|
10
|
+
* Sets WHERE `column @> :value` condition (PostgreSQL): the JSON document contains `value`.
|
|
11
|
+
* If you had previously WHERE expression defined,
|
|
12
|
+
* calling this function will override previously set WHERE conditions.
|
|
13
|
+
*/
|
|
14
|
+
whereJsonContainsTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, value: unknown): this;
|
|
15
|
+
/**
|
|
16
|
+
* Adds new AND WHERE `column @> :value` condition (PostgreSQL): the JSON document contains `value`.
|
|
17
|
+
*/
|
|
18
|
+
andWhereJsonContainsTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, value: unknown): this;
|
|
19
|
+
/**
|
|
20
|
+
* Adds new OR WHERE `column @> :value` condition (PostgreSQL): the JSON document contains `value`.
|
|
21
|
+
*/
|
|
22
|
+
orWhereJsonContainsTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, value: unknown): this;
|
|
23
|
+
/**
|
|
24
|
+
* Sets WHERE `column ? :key` condition (PostgreSQL): the JSON object has the top-level key.
|
|
25
|
+
* If you had previously WHERE expression defined,
|
|
26
|
+
* calling this function will override previously set WHERE conditions.
|
|
27
|
+
*/
|
|
28
|
+
whereJsonHasKeyTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, key: string): this;
|
|
29
|
+
/**
|
|
30
|
+
* Adds new AND WHERE `column ? :key` condition (PostgreSQL): the JSON object has the top-level key.
|
|
31
|
+
*/
|
|
32
|
+
andWhereJsonHasKeyTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, key: string): this;
|
|
33
|
+
/**
|
|
34
|
+
* Adds new OR WHERE `column ? :key` condition (PostgreSQL): the JSON object has the top-level key.
|
|
35
|
+
*/
|
|
36
|
+
orWhereJsonHasKeyTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, key: string): this;
|
|
37
|
+
}
|
|
38
|
+
declare module 'typeorm/query-builder/WhereExpressionBuilder' {
|
|
39
|
+
interface WhereExpressionBuilder extends TypedWhereJsonMethods<ObjectLiteral> {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
declare module 'typeorm/query-builder/SelectQueryBuilder' {
|
|
43
|
+
interface SelectQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereJsonMethods<Entity> {
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
declare module 'typeorm/query-builder/UpdateQueryBuilder' {
|
|
47
|
+
interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereJsonMethods<Entity> {
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
declare module 'typeorm/query-builder/DeleteQueryBuilder' {
|
|
51
|
+
interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereJsonMethods<Entity> {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("../../where-json.extension");
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
const query_builder_utils_1 = require("../../../utils/query-builder.utils");
|
|
6
|
+
const prototype_utils_1 = require("../../../utils/prototype.utils");
|
|
7
|
+
/* eslint-enable @typescript-eslint/no-empty-interface */
|
|
8
|
+
// patching
|
|
9
|
+
const createWhereJsonTypedMethod = (method) => function (where, value) {
|
|
10
|
+
return attachWhereJsonTyped(this, method, where, value);
|
|
11
|
+
};
|
|
12
|
+
const extension = {
|
|
13
|
+
prototype: {
|
|
14
|
+
whereJsonContainsTyped: createWhereJsonTypedMethod('whereJsonContains'),
|
|
15
|
+
andWhereJsonContainsTyped: createWhereJsonTypedMethod('andWhereJsonContains'),
|
|
16
|
+
orWhereJsonContainsTyped: createWhereJsonTypedMethod('orWhereJsonContains'),
|
|
17
|
+
whereJsonHasKeyTyped: createWhereJsonTypedMethod('whereJsonHasKey'),
|
|
18
|
+
andWhereJsonHasKeyTyped: createWhereJsonTypedMethod('andWhereJsonHasKey'),
|
|
19
|
+
orWhereJsonHasKeyTyped: createWhereJsonTypedMethod('orWhereJsonHasKey'),
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
const queryBuilders = [
|
|
23
|
+
typeorm_1.SelectQueryBuilder,
|
|
24
|
+
typeorm_1.UpdateQueryBuilder,
|
|
25
|
+
typeorm_1.DeleteQueryBuilder,
|
|
26
|
+
];
|
|
27
|
+
queryBuilders.forEach((builder) => {
|
|
28
|
+
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
29
|
+
});
|
|
30
|
+
// implementation
|
|
31
|
+
function attachWhereJsonTyped(builder, method, where, value) {
|
|
32
|
+
if (typeof where !== 'function') {
|
|
33
|
+
return builder;
|
|
34
|
+
}
|
|
35
|
+
const key = (0, query_builder_utils_1.getTypedSingleColumnKey)(builder, where, `${method}Typed`);
|
|
36
|
+
// .andWhereJsonContains(`"table"."column"`, { role: 'admin' }) / .andWhereJsonHasKey(`"table"."column"`, 'role')
|
|
37
|
+
// (one signature for both: `value` is the JSON value or the key, each method checks its own)
|
|
38
|
+
return builder[method](key, value);
|
|
39
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ObjectLiteral } from 'typeorm';
|
|
2
|
+
import { DeepUnwrapArray, ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
|
|
3
|
+
import type { TypedLowerOptions } from '../../../types/extensions/search.types';
|
|
4
|
+
/**
|
|
5
|
+
* Case-insensitive equality: `LOWER(column) = LOWER(:value)`. `options.normalize` changes the
|
|
6
|
+
* wrapping of both sides, e.g. `(sql) => \`LOWER(TRIM(${sql}))\``, to match an expression index.
|
|
7
|
+
*/
|
|
8
|
+
export interface TypedWhereLowerMethods<Entity extends ObjectLiteral> {
|
|
9
|
+
/**
|
|
10
|
+
* Sets WHERE `LOWER(column) = LOWER(:value)` condition in the query builder.
|
|
11
|
+
* `null` / `undefined` matches nothing (`FALSE`).
|
|
12
|
+
* If you had previously WHERE expression defined,
|
|
13
|
+
* calling this function will override previously set WHERE conditions.
|
|
14
|
+
*/
|
|
15
|
+
whereLowerEqualsTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, value: string | null | undefined, options?: TypedLowerOptions): this;
|
|
16
|
+
/**
|
|
17
|
+
* Adds new AND WHERE `LOWER(column) = LOWER(:value)` condition in the query builder.
|
|
18
|
+
* `null` / `undefined` matches nothing (`FALSE`).
|
|
19
|
+
*/
|
|
20
|
+
andWhereLowerEqualsTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, value: string | null | undefined, options?: TypedLowerOptions): this;
|
|
21
|
+
/**
|
|
22
|
+
* Adds new OR WHERE `LOWER(column) = LOWER(:value)` condition in the query builder.
|
|
23
|
+
* `null` / `undefined` matches nothing (`FALSE`).
|
|
24
|
+
*/
|
|
25
|
+
orWhereLowerEqualsTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, value: string | null | undefined, options?: TypedLowerOptions): this;
|
|
26
|
+
/**
|
|
27
|
+
* Sets WHERE `LOWER(column) IN (LOWER(:v1), LOWER(:v2), …)` condition in the query builder.
|
|
28
|
+
* An empty or missing array matches nothing (`FALSE`), like `whereIsIn`.
|
|
29
|
+
* If you had previously WHERE expression defined,
|
|
30
|
+
* calling this function will override previously set WHERE conditions.
|
|
31
|
+
*/
|
|
32
|
+
whereIsInLowerTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, values: ReadonlyArray<string> | null | undefined, options?: TypedLowerOptions): this;
|
|
33
|
+
/**
|
|
34
|
+
* Adds new AND WHERE `LOWER(column) IN (LOWER(:v1), LOWER(:v2), …)` condition in the query builder.
|
|
35
|
+
* An empty or missing array matches nothing (`FALSE`).
|
|
36
|
+
*/
|
|
37
|
+
andWhereIsInLowerTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, values: ReadonlyArray<string> | null | undefined, options?: TypedLowerOptions): this;
|
|
38
|
+
/**
|
|
39
|
+
* Adds new OR WHERE `LOWER(column) IN (LOWER(:v1), LOWER(:v2), …)` condition in the query builder.
|
|
40
|
+
* An empty or missing array matches nothing (`FALSE`).
|
|
41
|
+
*/
|
|
42
|
+
orWhereIsInLowerTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, values: ReadonlyArray<string> | null | undefined, options?: TypedLowerOptions): this;
|
|
43
|
+
}
|
|
44
|
+
declare module 'typeorm/query-builder/WhereExpressionBuilder' {
|
|
45
|
+
interface WhereExpressionBuilder extends TypedWhereLowerMethods<ObjectLiteral> {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
declare module 'typeorm/query-builder/SelectQueryBuilder' {
|
|
49
|
+
interface SelectQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereLowerMethods<Entity> {
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
declare module 'typeorm/query-builder/UpdateQueryBuilder' {
|
|
53
|
+
interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereLowerMethods<Entity> {
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
declare module 'typeorm/query-builder/DeleteQueryBuilder' {
|
|
57
|
+
interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereLowerMethods<Entity> {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const typeorm_1 = require("typeorm");
|
|
4
|
+
const query_builder_utils_1 = require("../../../utils/query-builder.utils");
|
|
5
|
+
const prototype_utils_1 = require("../../../utils/prototype.utils");
|
|
6
|
+
const sql_utils_1 = require("../../../utils/sql.utils");
|
|
7
|
+
/* eslint-enable @typescript-eslint/no-empty-interface */
|
|
8
|
+
// patching
|
|
9
|
+
const createWhereLowerEqualsTypedMethod = (method) => function (where, value, options) {
|
|
10
|
+
if (typeof where !== 'function') {
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
const methodName = `${method}LowerEqualsTyped`;
|
|
14
|
+
const column = (0, query_builder_utils_1.getTypedSingleColumnKey)(this, where, methodName);
|
|
15
|
+
const normalize = getNormalize(options, methodName);
|
|
16
|
+
if (value === null || value === undefined) {
|
|
17
|
+
// `LOWER(col) = LOWER(NULL)` is never true either; don't bind a parameter for it
|
|
18
|
+
return this[method]('FALSE');
|
|
19
|
+
}
|
|
20
|
+
const paramName = (0, sql_utils_1.createUniqueParameterName)(method);
|
|
21
|
+
return this[method](`${normalize(column)} = ${normalize(`:${paramName}`)}`, {
|
|
22
|
+
[paramName]: value,
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
const createWhereIsInLowerTypedMethod = (method) => function (where, values, options) {
|
|
26
|
+
if (typeof where !== 'function') {
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
const methodName = `${method}IsInLowerTyped`;
|
|
30
|
+
const column = (0, query_builder_utils_1.getTypedSingleColumnKey)(this, where, methodName);
|
|
31
|
+
const normalize = getNormalize(options, methodName);
|
|
32
|
+
if (values !== null && values !== undefined && !Array.isArray(values)) {
|
|
33
|
+
throw new TypeError(`${methodName} expects an array of values, got ${typeof values}`);
|
|
34
|
+
}
|
|
35
|
+
if (!(values === null || values === void 0 ? void 0 : values.length)) {
|
|
36
|
+
// don't bind an empty list, same as whereIsIn
|
|
37
|
+
return this[method]('FALSE');
|
|
38
|
+
}
|
|
39
|
+
// one parameter per value: each one is normalized by the database, like the column
|
|
40
|
+
const paramName = (0, sql_utils_1.createUniqueParameterName)(method);
|
|
41
|
+
const parameters = {};
|
|
42
|
+
const placeholders = values.map((value, index) => {
|
|
43
|
+
parameters[`${paramName}_${index}`] = value;
|
|
44
|
+
return normalize(`:${paramName}_${index}`);
|
|
45
|
+
});
|
|
46
|
+
return this[method](`${normalize(column)} IN (${placeholders.join(', ')})`, parameters);
|
|
47
|
+
};
|
|
48
|
+
const extension = {
|
|
49
|
+
prototype: {
|
|
50
|
+
whereLowerEqualsTyped: createWhereLowerEqualsTypedMethod('where'),
|
|
51
|
+
andWhereLowerEqualsTyped: createWhereLowerEqualsTypedMethod('andWhere'),
|
|
52
|
+
orWhereLowerEqualsTyped: createWhereLowerEqualsTypedMethod('orWhere'),
|
|
53
|
+
whereIsInLowerTyped: createWhereIsInLowerTypedMethod('where'),
|
|
54
|
+
andWhereIsInLowerTyped: createWhereIsInLowerTypedMethod('andWhere'),
|
|
55
|
+
orWhereIsInLowerTyped: createWhereIsInLowerTypedMethod('orWhere'),
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
const queryBuilders = [
|
|
59
|
+
typeorm_1.SelectQueryBuilder,
|
|
60
|
+
typeorm_1.UpdateQueryBuilder,
|
|
61
|
+
typeorm_1.DeleteQueryBuilder,
|
|
62
|
+
];
|
|
63
|
+
queryBuilders.forEach((builder) => {
|
|
64
|
+
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
65
|
+
});
|
|
66
|
+
// implementation
|
|
67
|
+
const lower = (sql) => `LOWER(${sql})`;
|
|
68
|
+
/**
|
|
69
|
+
* The wrapping for both sides: `options.normalize` or `LOWER(…)`. Its result must contain the
|
|
70
|
+
* expression it was given, or the column / the parameter would be dropped from the condition.
|
|
71
|
+
*/
|
|
72
|
+
function getNormalize(options, methodName) {
|
|
73
|
+
var _a;
|
|
74
|
+
const normalize = (_a = options === null || options === void 0 ? void 0 : options.normalize) !== null && _a !== void 0 ? _a : lower;
|
|
75
|
+
if (typeof normalize !== 'function') {
|
|
76
|
+
throw new TypeError(`${methodName} expects options.normalize to be a function`);
|
|
77
|
+
}
|
|
78
|
+
return (sql) => {
|
|
79
|
+
const result = normalize(sql);
|
|
80
|
+
if (typeof result !== 'string' || !result.includes(sql)) {
|
|
81
|
+
throw new TypeError(`${methodName}: options.normalize must return an SQL expression containing its argument ` +
|
|
82
|
+
`"${sql}", got ${JSON.stringify(result)}`);
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ObjectLiteral } from 'typeorm';
|
|
2
|
+
import { DeepUnwrapArray, ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
|
|
3
|
+
import type { SimilarityScoreOptions, TypedSimilarOptions } from '../../../types/extensions/search.types';
|
|
4
|
+
/**
|
|
5
|
+
* Trigram similarity conditions (PostgreSQL, `pg_trgm` extension). The column is the left operand,
|
|
6
|
+
* the form a `gin_trgm_ops` / `gist_trgm_ops` index on the column can be used for.
|
|
7
|
+
*/
|
|
8
|
+
export interface TypedWhereSimilarMethods<Entity extends ObjectLiteral> {
|
|
9
|
+
/**
|
|
10
|
+
* Sets WHERE `column % :term` condition in the query builder (PostgreSQL, pg_trgm).
|
|
11
|
+
* `options.operator`: `'%'` (default), `'%>'` (`column %> :term`, same as `:term <% column`)
|
|
12
|
+
* or `'<%'` (`column <% :term`, can't use an index). A blank term adds no condition.
|
|
13
|
+
* If you had previously WHERE expression defined,
|
|
14
|
+
* calling this function will override previously set WHERE conditions.
|
|
15
|
+
*/
|
|
16
|
+
whereSimilarTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, term: string | null | undefined, options?: TypedSimilarOptions): this;
|
|
17
|
+
/**
|
|
18
|
+
* Adds new AND WHERE `column % :term` condition in the query builder (PostgreSQL, pg_trgm).
|
|
19
|
+
* A blank term adds no condition.
|
|
20
|
+
*/
|
|
21
|
+
andWhereSimilarTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, term: string | null | undefined, options?: TypedSimilarOptions): this;
|
|
22
|
+
/**
|
|
23
|
+
* Adds new OR WHERE `column % :term` condition in the query builder (PostgreSQL, pg_trgm).
|
|
24
|
+
* A blank term adds no condition.
|
|
25
|
+
*/
|
|
26
|
+
orWhereSimilarTyped<Type extends ObjectLiteral = Entity>(where: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, term: string | null | undefined, options?: TypedSimilarOptions): this;
|
|
27
|
+
}
|
|
28
|
+
/** Trigram similarity score selection (PostgreSQL, `pg_trgm` extension). */
|
|
29
|
+
export interface TypedSimilarityScoreMethods<Entity extends ObjectLiteral> {
|
|
30
|
+
/**
|
|
31
|
+
* Adds `similarity(column, :term) AS alias` to the selection (PostgreSQL, pg_trgm), to order by:
|
|
32
|
+
* `.addSimilarityScoreTyped(u => u.name, term, 'score').orderBy('score', 'DESC')`.
|
|
33
|
+
* `options.function`: `'similarity'` (default), `'word_similarity'` or `'strict_word_similarity'`
|
|
34
|
+
* (`word_similarity(:term, column)`, the score of `column %> :term`).
|
|
35
|
+
* Use a lower-case alias: PostgreSQL folds an unquoted alias in ORDER BY.
|
|
36
|
+
*/
|
|
37
|
+
addSimilarityScoreTyped<Type extends ObjectLiteral = Entity>(selection: (proxy: DeepUnwrapArray<Type>) => ProxyColumnValueType, term: string | null | undefined, alias: string, options?: SimilarityScoreOptions): this;
|
|
38
|
+
}
|
|
39
|
+
declare module 'typeorm/query-builder/WhereExpressionBuilder' {
|
|
40
|
+
interface WhereExpressionBuilder extends TypedWhereSimilarMethods<ObjectLiteral> {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
declare module 'typeorm/query-builder/SelectQueryBuilder' {
|
|
44
|
+
interface SelectQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereSimilarMethods<Entity>, TypedSimilarityScoreMethods<Entity> {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
declare module 'typeorm/query-builder/UpdateQueryBuilder' {
|
|
48
|
+
interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereSimilarMethods<Entity> {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
declare module 'typeorm/query-builder/DeleteQueryBuilder' {
|
|
52
|
+
interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereSimilarMethods<Entity> {
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const typeorm_1 = require("typeorm");
|
|
4
|
+
const query_builder_utils_1 = require("../../../utils/query-builder.utils");
|
|
5
|
+
const prototype_utils_1 = require("../../../utils/prototype.utils");
|
|
6
|
+
const sql_utils_1 = require("../../../utils/sql.utils");
|
|
7
|
+
const search_utils_1 = require("../../../utils/search.utils");
|
|
8
|
+
/* eslint-enable @typescript-eslint/no-empty-interface */
|
|
9
|
+
// patching
|
|
10
|
+
const TRIGRAM_OPERATORS = ['%', '%>', '<%'];
|
|
11
|
+
const createWhereSimilarTypedMethod = (method) => function (where, term, options) {
|
|
12
|
+
if (typeof where !== 'function') {
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
const methodName = `${method}SimilarTyped`;
|
|
16
|
+
const column = (0, query_builder_utils_1.getTypedSingleColumnKey)(this, where, methodName);
|
|
17
|
+
const { operator = '%' } = options !== null && options !== void 0 ? options : {};
|
|
18
|
+
if (!TRIGRAM_OPERATORS.includes(operator)) {
|
|
19
|
+
// the operator is inserted into the SQL as is
|
|
20
|
+
throw new TypeError(`${methodName}: invalid operator "${operator}", expected "%", "%>" or "<%"`);
|
|
21
|
+
}
|
|
22
|
+
const value = (0, search_utils_1.toSearchTerm)(term, methodName);
|
|
23
|
+
if (value === undefined) {
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
const paramName = (0, sql_utils_1.createUniqueParameterName)(method);
|
|
27
|
+
// the column on the left: `col % :term`, `col %> :term` can use a trigram index on `col`
|
|
28
|
+
return this[method](`${column} ${operator} :${paramName}`, { [paramName]: value });
|
|
29
|
+
};
|
|
30
|
+
const SCORE_FUNCTIONS = [
|
|
31
|
+
'similarity',
|
|
32
|
+
'word_similarity',
|
|
33
|
+
'strict_word_similarity',
|
|
34
|
+
];
|
|
35
|
+
const extension = {
|
|
36
|
+
prototype: {
|
|
37
|
+
whereSimilarTyped: createWhereSimilarTypedMethod('where'),
|
|
38
|
+
andWhereSimilarTyped: createWhereSimilarTypedMethod('andWhere'),
|
|
39
|
+
orWhereSimilarTyped: createWhereSimilarTypedMethod('orWhere'),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const scoreExtension = {
|
|
43
|
+
prototype: {
|
|
44
|
+
addSimilarityScoreTyped: function (selection, term, alias, options) {
|
|
45
|
+
var _a;
|
|
46
|
+
const methodName = 'addSimilarityScoreTyped';
|
|
47
|
+
const column = (0, query_builder_utils_1.getTypedSingleColumnKey)(this, selection, methodName);
|
|
48
|
+
const { function: scoreFunction = 'similarity' } = options !== null && options !== void 0 ? options : {};
|
|
49
|
+
if (!SCORE_FUNCTIONS.includes(scoreFunction)) {
|
|
50
|
+
throw new TypeError(`${methodName}: invalid function "${scoreFunction}", expected "similarity", "word_similarity" or "strict_word_similarity"`);
|
|
51
|
+
}
|
|
52
|
+
if (typeof alias !== 'string' || alias.length === 0) {
|
|
53
|
+
throw new TypeError(`${methodName} expects an alias for the score`);
|
|
54
|
+
}
|
|
55
|
+
// always select the score, a query may order by its alias: a blank term scores 0
|
|
56
|
+
const value = (_a = (0, search_utils_1.toSearchTerm)(term, methodName)) !== null && _a !== void 0 ? _a : '';
|
|
57
|
+
const paramName = (0, sql_utils_1.createUniqueParameterName)('similarity');
|
|
58
|
+
const expression = scoreFunction === 'similarity'
|
|
59
|
+
? `similarity(${column}, :${paramName})`
|
|
60
|
+
: `${scoreFunction}(:${paramName}, ${column})`;
|
|
61
|
+
return this.addSelect(expression, alias).setParameter(paramName, value);
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const queryBuilders = [
|
|
66
|
+
typeorm_1.SelectQueryBuilder,
|
|
67
|
+
typeorm_1.UpdateQueryBuilder,
|
|
68
|
+
typeorm_1.DeleteQueryBuilder,
|
|
69
|
+
];
|
|
70
|
+
queryBuilders.forEach((builder) => {
|
|
71
|
+
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
72
|
+
});
|
|
73
|
+
(0, prototype_utils_1.patchPrototype)(typeorm_1.SelectQueryBuilder, scoreExtension);
|