typeorm-extensions 1.3.5 → 2.1.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.
Files changed (107) hide show
  1. package/README.md +12 -10
  2. package/dist/constants/index.d.ts +1 -1
  3. package/dist/constants/index.js +2 -2
  4. package/dist/extensions/cursor-pagination.extension.d.ts +18 -0
  5. package/dist/extensions/cursor-pagination.extension.js +108 -0
  6. package/dist/extensions/index.d.ts +7 -0
  7. package/dist/extensions/index.js +7 -0
  8. package/dist/extensions/order-by-array-position.extension.d.ts +24 -0
  9. package/dist/extensions/order-by-array-position.extension.js +18 -0
  10. package/dist/extensions/order.extension.d.ts +3 -2
  11. package/dist/extensions/order.extension.js +16 -13
  12. package/dist/extensions/period-filter.extension.d.ts +29 -0
  13. package/dist/extensions/period-filter.extension.js +23 -0
  14. package/dist/extensions/typed/index.d.ts +10 -0
  15. package/dist/extensions/typed/index.js +10 -0
  16. package/dist/extensions/typed/typed-distinct/typed-distinct.extension.js +2 -2
  17. package/dist/extensions/typed/typed-joins/typed-joins.extension.js +2 -1
  18. package/dist/extensions/typed/typed-order-by/typed-order-by-array-position.extension.d.ts +20 -0
  19. package/dist/extensions/typed/typed-order-by/typed-order-by-array-position.extension.js +21 -0
  20. package/dist/extensions/typed/typed-order-by/typed-order-by.extension.d.ts +4 -3
  21. package/dist/extensions/typed/typed-order-by/typed-order-by.extension.js +7 -6
  22. package/dist/extensions/typed/typed-returning/typed-returning.extension.d.ts +27 -0
  23. package/dist/extensions/typed/typed-returning/typed-returning.extension.js +38 -0
  24. package/dist/extensions/typed/typed-select/typed-select.extension.d.ts +6 -6
  25. package/dist/extensions/typed/typed-select/typed-select.extension.js +11 -8
  26. package/dist/extensions/typed/typed-where/typed-period-filter.extension.d.ts +31 -0
  27. package/dist/extensions/typed/typed-where/typed-period-filter.extension.js +25 -0
  28. package/dist/extensions/typed/typed-where/typed-where-array.extension.d.ts +68 -0
  29. package/dist/extensions/typed/typed-where/typed-where-array.extension.js +41 -0
  30. package/dist/extensions/typed/typed-where/typed-where-ilike.extension.d.ts +62 -0
  31. package/dist/extensions/typed/typed-where/typed-where-ilike.extension.js +84 -0
  32. package/dist/extensions/typed/typed-where/typed-where-is-any.extension.d.ts +53 -0
  33. package/dist/extensions/typed/typed-where/typed-where-is-any.extension.js +38 -0
  34. package/dist/extensions/typed/typed-where/typed-where-is-distinct-from.extension.d.ts +53 -0
  35. package/dist/extensions/typed/typed-where/typed-where-is-distinct-from.extension.js +38 -0
  36. package/dist/extensions/typed/typed-where/typed-where-is-in.extension.d.ts +36 -119
  37. package/dist/extensions/typed/typed-where/typed-where-is-in.extension.js +14 -29
  38. package/dist/extensions/typed/typed-where/typed-where-json.extension.d.ts +53 -0
  39. package/dist/extensions/typed/typed-where/typed-where-json.extension.js +39 -0
  40. package/dist/extensions/typed/typed-where/typed-where-lower.extension.d.ts +59 -0
  41. package/dist/extensions/typed/typed-where/typed-where-lower.extension.js +86 -0
  42. package/dist/extensions/typed/typed-where/typed-where-similar.extension.d.ts +54 -0
  43. package/dist/extensions/typed/typed-where/typed-where-similar.extension.js +73 -0
  44. package/dist/extensions/typed/typed-where/typed-where.extension.d.ts +30 -74
  45. package/dist/extensions/typed/typed-where/typed-where.extension.js +13 -21
  46. package/dist/extensions/where-array.extension.d.ts +70 -0
  47. package/dist/extensions/where-array.extension.js +47 -0
  48. package/dist/extensions/where-is-any.extension.d.ts +142 -0
  49. package/dist/extensions/where-is-any.extension.js +44 -0
  50. package/dist/extensions/where-is-distinct-from.extension.d.ts +56 -0
  51. package/dist/extensions/where-is-distinct-from.extension.js +55 -0
  52. package/dist/extensions/where-json.extension.d.ts +55 -0
  53. package/dist/extensions/where-json.extension.js +64 -0
  54. package/dist/helpers.d.ts +2 -0
  55. package/dist/helpers.js +23 -0
  56. package/dist/index.d.ts +3 -0
  57. package/dist/index.js +4 -0
  58. package/dist/types/extensions/cursor-pagination.types.d.ts +16 -0
  59. package/dist/types/extensions/cursor-pagination.types.js +2 -0
  60. package/dist/types/extensions/get-many-with-totals.types.d.ts +3 -1
  61. package/dist/types/extensions/index.d.ts +4 -0
  62. package/dist/types/extensions/index.js +4 -0
  63. package/dist/types/extensions/order.types.d.ts +1 -1
  64. package/dist/types/extensions/period.types.d.ts +15 -0
  65. package/dist/types/extensions/period.types.js +2 -0
  66. package/dist/types/extensions/search.types.d.ts +68 -0
  67. package/dist/types/extensions/search.types.js +2 -0
  68. package/dist/types/extensions/typed-order-by.types.d.ts +3 -0
  69. package/dist/types/extensions/typed-order-by.types.js +2 -0
  70. package/dist/types/extensions/typed-where.types.d.ts +4 -4
  71. package/dist/types/extensions/where.types.d.ts +1 -0
  72. package/dist/types/interfaces/config.interface.d.ts +17 -0
  73. package/dist/types/interfaces/config.interface.js +2 -0
  74. package/dist/types/interfaces/cursor-pagination.interface.d.ts +16 -0
  75. package/dist/types/interfaces/cursor-pagination.interface.js +2 -0
  76. package/dist/types/interfaces/index.d.ts +3 -0
  77. package/dist/types/interfaces/index.js +3 -0
  78. package/dist/types/interfaces/period.interface.d.ts +12 -0
  79. package/dist/types/interfaces/period.interface.js +2 -0
  80. package/dist/types/modules/proxy-callback.types.d.ts +50 -3
  81. package/dist/types/modules/typed-params.types.d.ts +45 -0
  82. package/dist/types/modules/typed-params.types.js +2 -0
  83. package/dist/types/util-types/index.d.ts +3 -1
  84. package/dist/utils/cursor.utils.d.ts +31 -0
  85. package/dist/utils/cursor.utils.js +130 -0
  86. package/dist/utils/json-path.utils.d.ts +22 -0
  87. package/dist/utils/json-path.utils.js +57 -0
  88. package/dist/utils/order.utils.d.ts +15 -0
  89. package/dist/utils/order.utils.js +45 -0
  90. package/dist/utils/period.utils.d.ts +9 -0
  91. package/dist/utils/period.utils.js +82 -0
  92. package/dist/utils/proxy-key.utils.d.ts +19 -3
  93. package/dist/utils/proxy-key.utils.js +40 -15
  94. package/dist/utils/query-builder.utils.d.ts +48 -0
  95. package/dist/utils/query-builder.utils.js +257 -0
  96. package/dist/utils/search.utils.d.ts +27 -0
  97. package/dist/utils/search.utils.js +83 -0
  98. package/dist/utils/sql.utils.d.ts +29 -1
  99. package/dist/utils/sql.utils.js +91 -9
  100. package/dist/utils/table.utils.d.ts +86 -0
  101. package/dist/utils/table.utils.js +124 -0
  102. package/dist/utils/where.utils.d.ts +18 -1
  103. package/dist/utils/where.utils.js +62 -9
  104. package/helpers/package.json +7 -0
  105. package/package.json +5 -4
  106. package/dist/utils/identifier.utils.d.ts +0 -15
  107. package/dist/utils/identifier.utils.js +0 -45
@@ -1,87 +1,43 @@
1
1
  import { ObjectLiteral } from 'typeorm';
2
- import { ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
3
- import { TypedWhereOptions } from '../../../types/extensions/typed-where.types';
2
+ import { DeepUnwrapArray, ProxyColumnValueType } from '../../../types/modules/proxy-callback.types';
3
+ import { TypedWhereArgs, TypeOrDefault, UnsetType } from '../../../types/modules/typed-params.types';
4
+ /**
5
+ * Typesafe WHERE conditions: the column comes from an entity property.
6
+ * `Type` defaults to the query builder's entity; with the `strictParams` option (see `TypeormExtensionsConfig`)
7
+ * the parameter is checked against the type of the selected column.
8
+ */
9
+ export interface TypedWhereMethods<Entity extends ObjectLiteral> {
10
+ /**
11
+ * Sets WHERE condition in the query builder.
12
+ * If you had previously WHERE expression defined,
13
+ * calling this function will override previously set WHERE conditions.
14
+ * Additionally you can add parameters used in where expression.
15
+ */
16
+ whereTyped<Type extends ObjectLiteral = UnsetType, Value extends ProxyColumnValueType = ProxyColumnValueType, Operator extends string = string>(where: (proxy: DeepUnwrapArray<TypeOrDefault<Type, Entity>>) => Value, conditions: Operator, ...args: TypedWhereArgs<Type, Operator, Value>): this;
17
+ /**
18
+ * Adds new AND WHERE condition in the query builder.
19
+ * Additionally you can add parameters used in where expression.
20
+ */
21
+ andWhereTyped<Type extends ObjectLiteral = UnsetType, Value extends ProxyColumnValueType = ProxyColumnValueType, Operator extends string = string>(where: (proxy: DeepUnwrapArray<TypeOrDefault<Type, Entity>>) => Value, conditions: Operator, ...args: TypedWhereArgs<Type, Operator, Value>): this;
22
+ /**
23
+ * Adds new OR WHERE condition in the query builder.
24
+ * Additionally you can add parameters used in where expression.
25
+ */
26
+ orWhereTyped<Type extends ObjectLiteral = UnsetType, Value extends ProxyColumnValueType = ProxyColumnValueType, Operator extends string = string>(where: (proxy: DeepUnwrapArray<TypeOrDefault<Type, Entity>>) => Value, conditions: Operator, ...args: TypedWhereArgs<Type, Operator, Value>): this;
27
+ }
4
28
  declare module 'typeorm/query-builder/WhereExpressionBuilder' {
5
- interface WhereExpressionBuilder {
6
- /**
7
- * Sets WHERE condition in the query builder.
8
- * If you had previously WHERE expression defined,
9
- * calling this function will override previously set WHERE conditions.
10
- * Additionally you can add parameters used in where expression.
11
- */
12
- whereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
13
- /**
14
- * Adds new AND WHERE condition in the query builder.
15
- * Additionally you can add parameters used in where expression.
16
- */
17
- andWhereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
18
- /**
19
- * Adds new OR WHERE condition in the query builder.
20
- * Additionally you can add parameters used in where expression.
21
- */
22
- orWhereTyped<Type extends ObjectLiteral>(subQuery: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
29
+ interface WhereExpressionBuilder extends TypedWhereMethods<ObjectLiteral> {
23
30
  }
24
31
  }
25
32
  declare module 'typeorm/query-builder/SelectQueryBuilder' {
26
- interface SelectQueryBuilder<Entity> {
27
- /**
28
- * Sets WHERE condition in the query builder.
29
- * If you had previously WHERE expression defined,
30
- * calling this function will override previously set WHERE conditions.
31
- * Additionally you can add parameters used in where expression.
32
- */
33
- whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
34
- /**
35
- * Adds new AND WHERE condition in the query builder.
36
- * Additionally you can add parameters used in where expression.
37
- */
38
- andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
39
- /**
40
- * Adds new OR WHERE condition in the query builder.
41
- * Additionally you can add parameters used in where expression.
42
- */
43
- orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
33
+ interface SelectQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereMethods<Entity> {
44
34
  }
45
35
  }
46
36
  declare module 'typeorm/query-builder/UpdateQueryBuilder' {
47
- interface UpdateQueryBuilder<Entity> {
48
- /**
49
- * Sets WHERE condition in the query builder.
50
- * If you had previously WHERE expression defined,
51
- * calling this function will override previously set WHERE conditions.
52
- * Additionally you can add parameters used in where expression.
53
- */
54
- whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
55
- /**
56
- * Adds new AND WHERE condition in the query builder.
57
- * Additionally you can add parameters used in where expression.
58
- */
59
- andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
60
- /**
61
- * Adds new OR WHERE condition in the query builder.
62
- * Additionally you can add parameters used in where expression.
63
- */
64
- orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
37
+ interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereMethods<Entity> {
65
38
  }
66
39
  }
67
40
  declare module 'typeorm/query-builder/DeleteQueryBuilder' {
68
- interface DeleteQueryBuilder<Entity> {
69
- /**
70
- * Sets WHERE condition in the query builder.
71
- * If you had previously WHERE expression defined,
72
- * calling this function will override previously set WHERE conditions.
73
- * Additionally you can add parameters used in where expression.
74
- */
75
- whereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
76
- /**
77
- * Adds new AND WHERE condition in the query builder.
78
- * Additionally you can add parameters used in where expression.
79
- */
80
- andWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
81
- /**
82
- * Adds new OR WHERE condition in the query builder.
83
- * Additionally you can add parameters used in where expression.
84
- */
85
- orWhereTyped<Type extends ObjectLiteral = Entity>(where: (proxy: Type) => ProxyColumnValueType, conditions: string, parameters?: unknown, options?: TypedWhereOptions): this;
41
+ interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends TypedWhereMethods<Entity> {
86
42
  }
87
43
  }
@@ -1,21 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const typeorm_1 = require("typeorm");
4
- const proxy_key_utils_1 = require("../../../utils/proxy-key.utils");
4
+ const query_builder_utils_1 = require("../../../utils/query-builder.utils");
5
5
  const prototype_utils_1 = require("../../../utils/prototype.utils");
6
6
  const where_utils_1 = require("../../../utils/where.utils");
7
+ /* eslint-enable @typescript-eslint/no-empty-interface */
7
8
  // patching
9
+ const createWhereTypedMethod = (method) => function (where, conditions, parameters, options) {
10
+ return attachTypedWhere(this, method, where, conditions, parameters, options);
11
+ };
8
12
  const extension = {
9
13
  prototype: {
10
- whereTyped: function (where, operator, parameters, options) {
11
- return attachTypedWhere(this, 'where', where, operator, parameters, options);
12
- },
13
- andWhereTyped: function (where, operator, parameters, options) {
14
- return attachTypedWhere(this, 'andWhere', where, operator, parameters, options);
15
- },
16
- orWhereTyped: function (where, operator, parameters, options) {
17
- return attachTypedWhere(this, 'orWhere', where, operator, parameters, options);
18
- },
14
+ whereTyped: createWhereTypedMethod('where'),
15
+ andWhereTyped: createWhereTypedMethod('andWhere'),
16
+ orWhereTyped: createWhereTypedMethod('orWhere'),
19
17
  },
20
18
  };
21
19
  const queryBuilders = [
@@ -28,18 +26,12 @@ queryBuilders.forEach((builder) => {
28
26
  });
29
27
  // implementation
30
28
  function attachTypedWhere(builder, method, where, conditions, parameters, options) {
31
- const { useDoubleQuotes, addValue = true } = options || {};
29
+ const { addValue } = options || {};
32
30
  if (typeof where === 'function') {
33
- const alias = builder.alias;
34
- const key = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(where, alias, useDoubleQuotes);
35
- if (Array.isArray(key)) {
36
- for (const k of key) {
37
- builder = (0, where_utils_1.attachWhere)(builder, method, k, conditions, parameters, {
38
- addValue,
39
- });
40
- }
41
- return builder;
42
- }
31
+ // several columns have no single meaning (AND? OR?), and `where…` kept only the last one
32
+ // a path into a JSON column is cast to the type of the value it is compared with
33
+ const value = (0, where_utils_1.isParamsObject)(parameters) ? Object.values(parameters)[0] : parameters;
34
+ const key = (0, query_builder_utils_1.getTypedSingleColumnKey)(builder, where, `${method}Typed`, { value });
43
35
  return (0, where_utils_1.attachWhere)(builder, method, key, conditions, parameters, {
44
36
  addValue,
45
37
  });
@@ -0,0 +1,70 @@
1
+ import { ObjectLiteral } from 'typeorm';
2
+ import { ArgsType } from '../types/util-types';
3
+ /**
4
+ * Conditions on PostgreSQL array columns (`text[]`, `int[]`, `uuid[]`, …).
5
+ * The values are bound as one array parameter; `arrayType` (e.g. `'uuid[]'`) casts it:
6
+ * `CAST(:values AS uuid[])`. The operators can use a GIN index on the column.
7
+ */
8
+ export interface WhereArrayMethods {
9
+ /**
10
+ * Sets WHERE `field @> :values` condition (PostgreSQL): the array column contains every value.
11
+ * An empty array matches every row with a non-null column.
12
+ * If you had previously WHERE expression defined,
13
+ * calling this function will override previously set WHERE conditions.
14
+ */
15
+ whereArrayContains(field: string, values: ArgsType, arrayType?: string): this;
16
+ /**
17
+ * Adds new AND WHERE `field @> :values` condition (PostgreSQL): the array column contains every value.
18
+ */
19
+ andWhereArrayContains(field: string, values: ArgsType, arrayType?: string): this;
20
+ /**
21
+ * Adds new OR WHERE `field @> :values` condition (PostgreSQL): the array column contains every value.
22
+ */
23
+ orWhereArrayContains(field: string, values: ArgsType, arrayType?: string): this;
24
+ /**
25
+ * Sets WHERE `field <@ :values` condition (PostgreSQL): every element of the array column is one of the values.
26
+ * An empty column array always matches.
27
+ * If you had previously WHERE expression defined,
28
+ * calling this function will override previously set WHERE conditions.
29
+ */
30
+ whereArrayContainedBy(field: string, values: ArgsType, arrayType?: string): this;
31
+ /**
32
+ * Adds new AND WHERE `field <@ :values` condition (PostgreSQL): every element of the array column is one of the values.
33
+ */
34
+ andWhereArrayContainedBy(field: string, values: ArgsType, arrayType?: string): this;
35
+ /**
36
+ * Adds new OR WHERE `field <@ :values` condition (PostgreSQL): every element of the array column is one of the values.
37
+ */
38
+ orWhereArrayContainedBy(field: string, values: ArgsType, arrayType?: string): this;
39
+ /**
40
+ * Sets WHERE `field && :values` condition (PostgreSQL): the array column has at least one of the values.
41
+ * An empty array matches nothing.
42
+ * If you had previously WHERE expression defined,
43
+ * calling this function will override previously set WHERE conditions.
44
+ */
45
+ whereArrayOverlaps(field: string, values: ArgsType, arrayType?: string): this;
46
+ /**
47
+ * Adds new AND WHERE `field && :values` condition (PostgreSQL): the array column has at least one of the values.
48
+ */
49
+ andWhereArrayOverlaps(field: string, values: ArgsType, arrayType?: string): this;
50
+ /**
51
+ * Adds new OR WHERE `field && :values` condition (PostgreSQL): the array column has at least one of the values.
52
+ */
53
+ orWhereArrayOverlaps(field: string, values: ArgsType, arrayType?: string): this;
54
+ }
55
+ declare module 'typeorm/query-builder/WhereExpressionBuilder' {
56
+ interface WhereExpressionBuilder extends WhereArrayMethods {
57
+ }
58
+ }
59
+ declare module 'typeorm/query-builder/SelectQueryBuilder' {
60
+ interface SelectQueryBuilder<Entity extends ObjectLiteral> extends WhereArrayMethods {
61
+ }
62
+ }
63
+ declare module 'typeorm/query-builder/UpdateQueryBuilder' {
64
+ interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends WhereArrayMethods {
65
+ }
66
+ }
67
+ declare module 'typeorm/query-builder/DeleteQueryBuilder' {
68
+ interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends WhereArrayMethods {
69
+ }
70
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typeorm_1 = require("typeorm");
4
+ const prototype_utils_1 = require("../utils/prototype.utils");
5
+ const query_builder_utils_1 = require("../utils/query-builder.utils");
6
+ const sql_utils_1 = require("../utils/sql.utils");
7
+ const createWhereArrayMethod = (method, operator) => function (field, values, arrayType) {
8
+ return attachWhereArray(this, method, field, operator, values, arrayType);
9
+ };
10
+ const extension = {
11
+ prototype: {
12
+ whereArrayContains: createWhereArrayMethod('where', '@>'),
13
+ andWhereArrayContains: createWhereArrayMethod('andWhere', '@>'),
14
+ orWhereArrayContains: createWhereArrayMethod('orWhere', '@>'),
15
+ whereArrayContainedBy: createWhereArrayMethod('where', '<@'),
16
+ andWhereArrayContainedBy: createWhereArrayMethod('andWhere', '<@'),
17
+ orWhereArrayContainedBy: createWhereArrayMethod('orWhere', '<@'),
18
+ whereArrayOverlaps: createWhereArrayMethod('where', '&&'),
19
+ andWhereArrayOverlaps: createWhereArrayMethod('andWhere', '&&'),
20
+ orWhereArrayOverlaps: createWhereArrayMethod('orWhere', '&&'),
21
+ },
22
+ };
23
+ const queryBuilders = [
24
+ typeorm_1.SelectQueryBuilder,
25
+ typeorm_1.UpdateQueryBuilder,
26
+ typeorm_1.DeleteQueryBuilder,
27
+ ];
28
+ queryBuilders.forEach((builder) => {
29
+ (0, prototype_utils_1.patchPrototype)(builder, extension);
30
+ });
31
+ // implementation
32
+ function attachWhereArray(builder, method, field, operator, values, arrayType) {
33
+ if (!method || typeof field !== 'string') {
34
+ return builder;
35
+ }
36
+ if (!Array.isArray(values)) {
37
+ // a missing array has no safe meaning here: `@> '{}'` would match every row
38
+ throw new TypeError(`Array condition on "${field}" expects an array of values, got ${values}`);
39
+ }
40
+ const castTo = arrayType === undefined ? undefined : (0, sql_utils_1.assertArrayType)(arrayType);
41
+ const paramName = (0, sql_utils_1.createUniqueParameterName)(method);
42
+ // CAST(... AS ...) rather than `::type`: TypeORM would read `::text` as a `:text` parameter
43
+ const parameter = castTo ? `CAST(:${paramName} AS ${castTo})` : `:${paramName}`;
44
+ // the array is bound as is, empty arrays included: PostgreSQL's own semantics apply
45
+ // ("table"."tags" @> :param, where :param is the whole array)
46
+ return builder[method](`${(0, query_builder_utils_1.toFieldSql)(builder, field)} ${operator} ${parameter}`, { [paramName]: values });
47
+ }
@@ -0,0 +1,142 @@
1
+ import { ObjectLiteral } from 'typeorm';
2
+ import { ArgsType } from '../types/util-types';
3
+ declare module 'typeorm/query-builder/WhereExpressionBuilder' {
4
+ interface WhereExpressionBuilder {
5
+ /**
6
+ * Sets WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
7
+ * The array is bound as a single parameter instead of one parameter per value.
8
+ * `arrayType` (e.g. `'uuid[]'`) casts the parameter: `= ANY(CAST(:array AS uuid[]))`.
9
+ * If you had previously WHERE expression defined,
10
+ * calling this function will override previously set WHERE conditions.
11
+ */
12
+ whereIsAny(field: string, array?: ArgsType, arrayType?: string): this;
13
+ /**
14
+ * Adds new AND WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
15
+ */
16
+ andWhereIsAny(field: string, array?: ArgsType, arrayType?: string): this;
17
+ /**
18
+ * Adds new OR WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
19
+ */
20
+ orWhereIsAny(field: string, array?: ArgsType, arrayType?: string): this;
21
+ /**
22
+ * Sets WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL):
23
+ * the field differs from every value of the array.
24
+ * If you had previously WHERE expression defined,
25
+ * calling this function will override previously set WHERE conditions.
26
+ */
27
+ whereIsNotAny(field: string, array?: ArgsType, arrayType?: string): this;
28
+ /**
29
+ * Adds new AND WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
30
+ */
31
+ andWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): this;
32
+ /**
33
+ * Adds new OR WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
34
+ */
35
+ orWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): this;
36
+ }
37
+ }
38
+ declare module 'typeorm/query-builder/SelectQueryBuilder' {
39
+ interface SelectQueryBuilder<Entity extends ObjectLiteral> {
40
+ /**
41
+ * Sets WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
42
+ * The array is bound as a single parameter instead of one parameter per value.
43
+ * `arrayType` (e.g. `'uuid[]'`) casts the parameter: `= ANY(CAST(:array AS uuid[]))`.
44
+ * If you had previously WHERE expression defined,
45
+ * calling this function will override previously set WHERE conditions.
46
+ */
47
+ whereIsAny(field: string, array?: ArgsType, arrayType?: string): SelectQueryBuilder<Entity>;
48
+ /**
49
+ * Adds new AND WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
50
+ */
51
+ andWhereIsAny(field: string, array?: ArgsType, arrayType?: string): SelectQueryBuilder<Entity>;
52
+ /**
53
+ * Adds new OR WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
54
+ */
55
+ orWhereIsAny(field: string, array?: ArgsType, arrayType?: string): SelectQueryBuilder<Entity>;
56
+ /**
57
+ * Sets WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL):
58
+ * the field differs from every value of the array.
59
+ * If you had previously WHERE expression defined,
60
+ * calling this function will override previously set WHERE conditions.
61
+ */
62
+ whereIsNotAny(field: string, array?: ArgsType, arrayType?: string): SelectQueryBuilder<Entity>;
63
+ /**
64
+ * Adds new AND WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
65
+ */
66
+ andWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): SelectQueryBuilder<Entity>;
67
+ /**
68
+ * Adds new OR WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
69
+ */
70
+ orWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): SelectQueryBuilder<Entity>;
71
+ }
72
+ }
73
+ declare module 'typeorm/query-builder/UpdateQueryBuilder' {
74
+ interface UpdateQueryBuilder<Entity extends ObjectLiteral> {
75
+ /**
76
+ * Sets WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
77
+ * The array is bound as a single parameter instead of one parameter per value.
78
+ * `arrayType` (e.g. `'uuid[]'`) casts the parameter: `= ANY(CAST(:array AS uuid[]))`.
79
+ * If you had previously WHERE expression defined,
80
+ * calling this function will override previously set WHERE conditions.
81
+ */
82
+ whereIsAny(field: string, array?: ArgsType, arrayType?: string): UpdateQueryBuilder<Entity>;
83
+ /**
84
+ * Adds new AND WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
85
+ */
86
+ andWhereIsAny(field: string, array?: ArgsType, arrayType?: string): UpdateQueryBuilder<Entity>;
87
+ /**
88
+ * Adds new OR WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
89
+ */
90
+ orWhereIsAny(field: string, array?: ArgsType, arrayType?: string): UpdateQueryBuilder<Entity>;
91
+ /**
92
+ * Sets WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL):
93
+ * the field differs from every value of the array.
94
+ * If you had previously WHERE expression defined,
95
+ * calling this function will override previously set WHERE conditions.
96
+ */
97
+ whereIsNotAny(field: string, array?: ArgsType, arrayType?: string): UpdateQueryBuilder<Entity>;
98
+ /**
99
+ * Adds new AND WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
100
+ */
101
+ andWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): UpdateQueryBuilder<Entity>;
102
+ /**
103
+ * Adds new OR WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
104
+ */
105
+ orWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): UpdateQueryBuilder<Entity>;
106
+ }
107
+ }
108
+ declare module 'typeorm/query-builder/DeleteQueryBuilder' {
109
+ interface DeleteQueryBuilder<Entity extends ObjectLiteral> {
110
+ /**
111
+ * Sets WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
112
+ * The array is bound as a single parameter instead of one parameter per value.
113
+ * `arrayType` (e.g. `'uuid[]'`) casts the parameter: `= ANY(CAST(:array AS uuid[]))`.
114
+ * If you had previously WHERE expression defined,
115
+ * calling this function will override previously set WHERE conditions.
116
+ */
117
+ whereIsAny(field: string, array?: ArgsType, arrayType?: string): DeleteQueryBuilder<Entity>;
118
+ /**
119
+ * Adds new AND WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
120
+ */
121
+ andWhereIsAny(field: string, array?: ArgsType, arrayType?: string): DeleteQueryBuilder<Entity>;
122
+ /**
123
+ * Adds new OR WHERE `field = ANY(:array)` condition in the query builder (PostgreSQL).
124
+ */
125
+ orWhereIsAny(field: string, array?: ArgsType, arrayType?: string): DeleteQueryBuilder<Entity>;
126
+ /**
127
+ * Sets WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL):
128
+ * the field differs from every value of the array.
129
+ * If you had previously WHERE expression defined,
130
+ * calling this function will override previously set WHERE conditions.
131
+ */
132
+ whereIsNotAny(field: string, array?: ArgsType, arrayType?: string): DeleteQueryBuilder<Entity>;
133
+ /**
134
+ * Adds new AND WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
135
+ */
136
+ andWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): DeleteQueryBuilder<Entity>;
137
+ /**
138
+ * Adds new OR WHERE `field <> ALL(:array)` condition in the query builder (PostgreSQL).
139
+ */
140
+ orWhereIsNotAny(field: string, array?: ArgsType, arrayType?: string): DeleteQueryBuilder<Entity>;
141
+ }
142
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typeorm_1 = require("typeorm");
4
+ const prototype_utils_1 = require("../utils/prototype.utils");
5
+ const query_builder_utils_1 = require("../utils/query-builder.utils");
6
+ const sql_utils_1 = require("../utils/sql.utils");
7
+ const createWhereIsAnyMethod = (method, comparison) => function (field, array, arrayType) {
8
+ return attachWhereIsAny(this, method, field, comparison, array, arrayType);
9
+ };
10
+ const extension = {
11
+ prototype: {
12
+ whereIsAny: createWhereIsAnyMethod('where', '= ANY'),
13
+ andWhereIsAny: createWhereIsAnyMethod('andWhere', '= ANY'),
14
+ orWhereIsAny: createWhereIsAnyMethod('orWhere', '= ANY'),
15
+ whereIsNotAny: createWhereIsAnyMethod('where', '<> ALL'),
16
+ andWhereIsNotAny: createWhereIsAnyMethod('andWhere', '<> ALL'),
17
+ orWhereIsNotAny: createWhereIsAnyMethod('orWhere', '<> ALL'),
18
+ },
19
+ };
20
+ const queryBuilders = [
21
+ typeorm_1.SelectQueryBuilder,
22
+ typeorm_1.UpdateQueryBuilder,
23
+ typeorm_1.DeleteQueryBuilder,
24
+ ];
25
+ queryBuilders.forEach((builder) => {
26
+ (0, prototype_utils_1.patchPrototype)(builder, extension);
27
+ });
28
+ // implementation
29
+ function attachWhereIsAny(builder, method, field, comparison, array, arrayType) {
30
+ if (!method || typeof field !== 'string') {
31
+ return builder;
32
+ }
33
+ const castTo = arrayType === undefined ? undefined : (0, sql_utils_1.assertArrayType)(arrayType);
34
+ if (!(array === null || array === void 0 ? void 0 : array.length)) {
35
+ // don't bind an empty array, same as IN / NOT IN:
36
+ // `= ANY('{}')` matches nothing, `<> ALL('{}')` matches everything
37
+ return builder[method](comparison === '= ANY' ? 'FALSE' : 'TRUE');
38
+ }
39
+ const paramName = (0, sql_utils_1.createUniqueParameterName)(method);
40
+ // CAST(... AS ...) rather than `::type`: TypeORM would read `::text` as a `:text` parameter
41
+ const parameter = castTo ? `CAST(:${paramName} AS ${castTo})` : `:${paramName}`;
42
+ // "table"."column" = ANY(:param) / <> ALL(:param), where :param is the whole array
43
+ return builder[method](`${(0, query_builder_utils_1.toFieldSql)(builder, field)} ${comparison}(${parameter})`, { [paramName]: array });
44
+ }
@@ -0,0 +1,56 @@
1
+ import { ObjectLiteral } from 'typeorm';
2
+ /**
3
+ * Null-safe comparisons: `NULL` is a value like any other, so `NULL` equals `NULL`
4
+ * and differs from `1`, where `=` / `<>` would give `NULL` and drop the row.
5
+ * `null` / `undefined` compare with `NULL`.
6
+ *
7
+ * PostgreSQL and most databases: `IS [NOT] DISTINCT FROM`; MySQL / MariaDB: `<=>`; SQLite: `IS [NOT]`.
8
+ */
9
+ export interface WhereDistinctMethods {
10
+ /**
11
+ * Sets WHERE `field IS DISTINCT FROM :value` condition: the field differs from the value,
12
+ * `NULL` included (`NULL` differs from `1`, but not from `NULL`).
13
+ * If you had previously WHERE expression defined,
14
+ * calling this function will override previously set WHERE conditions.
15
+ */
16
+ whereIsDistinctFrom(field: string, value: unknown): this;
17
+ /**
18
+ * Adds new AND WHERE `field IS DISTINCT FROM :value` condition: the field differs from the value, `NULL` included.
19
+ */
20
+ andWhereIsDistinctFrom(field: string, value: unknown): this;
21
+ /**
22
+ * Adds new OR WHERE `field IS DISTINCT FROM :value` condition: the field differs from the value, `NULL` included.
23
+ */
24
+ orWhereIsDistinctFrom(field: string, value: unknown): this;
25
+ /**
26
+ * Sets WHERE `field IS NOT DISTINCT FROM :value` condition: the field equals the value,
27
+ * `NULL` included (`NULL` equals `NULL`).
28
+ * If you had previously WHERE expression defined,
29
+ * calling this function will override previously set WHERE conditions.
30
+ */
31
+ whereIsNotDistinctFrom(field: string, value: unknown): this;
32
+ /**
33
+ * Adds new AND WHERE `field IS NOT DISTINCT FROM :value` condition: the field equals the value, `NULL` included.
34
+ */
35
+ andWhereIsNotDistinctFrom(field: string, value: unknown): this;
36
+ /**
37
+ * Adds new OR WHERE `field IS NOT DISTINCT FROM :value` condition: the field equals the value, `NULL` included.
38
+ */
39
+ orWhereIsNotDistinctFrom(field: string, value: unknown): this;
40
+ }
41
+ declare module 'typeorm/query-builder/WhereExpressionBuilder' {
42
+ interface WhereExpressionBuilder extends WhereDistinctMethods {
43
+ }
44
+ }
45
+ declare module 'typeorm/query-builder/SelectQueryBuilder' {
46
+ interface SelectQueryBuilder<Entity extends ObjectLiteral> extends WhereDistinctMethods {
47
+ }
48
+ }
49
+ declare module 'typeorm/query-builder/UpdateQueryBuilder' {
50
+ interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends WhereDistinctMethods {
51
+ }
52
+ }
53
+ declare module 'typeorm/query-builder/DeleteQueryBuilder' {
54
+ interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends WhereDistinctMethods {
55
+ }
56
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typeorm_1 = require("typeorm");
4
+ const prototype_utils_1 = require("../utils/prototype.utils");
5
+ const query_builder_utils_1 = require("../utils/query-builder.utils");
6
+ const sql_utils_1 = require("../utils/sql.utils");
7
+ /* eslint-enable @typescript-eslint/no-empty-interface, @typescript-eslint/no-unused-vars */
8
+ // patching
9
+ const createWhereDistinctMethod = (method, distinct) => function (field, value) {
10
+ return attachWhereDistinct(this, method, field, distinct, value);
11
+ };
12
+ const extension = {
13
+ prototype: {
14
+ whereIsDistinctFrom: createWhereDistinctMethod('where', true),
15
+ andWhereIsDistinctFrom: createWhereDistinctMethod('andWhere', true),
16
+ orWhereIsDistinctFrom: createWhereDistinctMethod('orWhere', true),
17
+ whereIsNotDistinctFrom: createWhereDistinctMethod('where', false),
18
+ andWhereIsNotDistinctFrom: createWhereDistinctMethod('andWhere', false),
19
+ orWhereIsNotDistinctFrom: createWhereDistinctMethod('orWhere', false),
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
+ /** `field IS [NOT] DISTINCT FROM value` in the database's own null-safe syntax. */
32
+ function buildDistinctCondition(driver, field, distinct, value) {
33
+ if ((0, sql_utils_1.isMysqlLikeDriver)(driver)) {
34
+ // `<=>` is MySQL's null-safe equality
35
+ return distinct ? `NOT (${field} <=> ${value})` : `${field} <=> ${value}`;
36
+ }
37
+ if ((0, sql_utils_1.isSqliteLikeDriver)(driver)) {
38
+ // SQLite's `IS` / `IS NOT` compare any values null-safely (IS DISTINCT FROM needs SQLite 3.39+)
39
+ return `${field} ${distinct ? 'IS NOT' : 'IS'} ${value}`;
40
+ }
41
+ return `${field} ${distinct ? 'IS DISTINCT FROM' : 'IS NOT DISTINCT FROM'} ${value}`;
42
+ }
43
+ function attachWhereDistinct(builder, method, field, distinct, value) {
44
+ if (!method || typeof field !== 'string') {
45
+ return builder;
46
+ }
47
+ const driver = (0, sql_utils_1.getDriverType)(builder);
48
+ const column = (0, query_builder_utils_1.toFieldSql)(builder, field);
49
+ if (value === null || value === undefined) {
50
+ // compare with the NULL literal, no parameter to bind
51
+ return builder[method](buildDistinctCondition(driver, column, distinct, 'NULL'));
52
+ }
53
+ const paramName = (0, sql_utils_1.createUniqueParameterName)(method);
54
+ return builder[method](buildDistinctCondition(driver, column, distinct, `:${paramName}`), { [paramName]: value });
55
+ }
@@ -0,0 +1,55 @@
1
+ import { ObjectLiteral } from 'typeorm';
2
+ /**
3
+ * Conditions on PostgreSQL `jsonb` / `json` / `simple-json` columns. The column is cast to `jsonb`
4
+ * (a no-op for `jsonb` columns, so a GIN index on them is still used).
5
+ * `field` can also be a JSON expression, e.g. `tableHelper('u').jsonColumn('metadata', '->', 'settings')`.
6
+ */
7
+ export interface WhereJsonMethods {
8
+ /**
9
+ * Sets WHERE `field @> :value` condition (PostgreSQL): the JSON document contains `value`,
10
+ * e.g. `{ role: 'admin' }` matches `{ "role": "admin", "age": 30 }`.
11
+ * `value` is serialized with `JSON.stringify`.
12
+ * If you had previously WHERE expression defined,
13
+ * calling this function will override previously set WHERE conditions.
14
+ */
15
+ whereJsonContains(field: string, value: unknown): this;
16
+ /**
17
+ * Adds new AND WHERE `field @> :value` condition (PostgreSQL): the JSON document contains `value`.
18
+ */
19
+ andWhereJsonContains(field: string, value: unknown): this;
20
+ /**
21
+ * Adds new OR WHERE `field @> :value` condition (PostgreSQL): the JSON document contains `value`.
22
+ */
23
+ orWhereJsonContains(field: string, value: unknown): this;
24
+ /**
25
+ * Sets WHERE `field ? :key` condition (PostgreSQL): the JSON object has the top-level key
26
+ * (or the JSON array has the string element).
27
+ * If you had previously WHERE expression defined,
28
+ * calling this function will override previously set WHERE conditions.
29
+ */
30
+ whereJsonHasKey(field: string, key: string): this;
31
+ /**
32
+ * Adds new AND WHERE `field ? :key` condition (PostgreSQL): the JSON object has the top-level key.
33
+ */
34
+ andWhereJsonHasKey(field: string, key: string): this;
35
+ /**
36
+ * Adds new OR WHERE `field ? :key` condition (PostgreSQL): the JSON object has the top-level key.
37
+ */
38
+ orWhereJsonHasKey(field: string, key: string): this;
39
+ }
40
+ declare module 'typeorm/query-builder/WhereExpressionBuilder' {
41
+ interface WhereExpressionBuilder extends WhereJsonMethods {
42
+ }
43
+ }
44
+ declare module 'typeorm/query-builder/SelectQueryBuilder' {
45
+ interface SelectQueryBuilder<Entity extends ObjectLiteral> extends WhereJsonMethods {
46
+ }
47
+ }
48
+ declare module 'typeorm/query-builder/UpdateQueryBuilder' {
49
+ interface UpdateQueryBuilder<Entity extends ObjectLiteral> extends WhereJsonMethods {
50
+ }
51
+ }
52
+ declare module 'typeorm/query-builder/DeleteQueryBuilder' {
53
+ interface DeleteQueryBuilder<Entity extends ObjectLiteral> extends WhereJsonMethods {
54
+ }
55
+ }