typeorm-extensions 1.2.6 → 1.3.1
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 +11 -3
- package/dist/extensions/get-many-with-totals.extension.d.ts +1 -16
- package/dist/extensions/get-many-with-totals.extension.js +22 -8
- package/dist/extensions/index.d.ts +1 -0
- package/dist/extensions/index.js +1 -0
- package/dist/extensions/order.extension.d.ts +1 -15
- package/dist/extensions/order.extension.js +39 -21
- package/dist/extensions/pagination.extension.d.ts +1 -10
- package/dist/extensions/pagination.extension.js +17 -4
- package/dist/extensions/typed/index.d.ts +1 -2
- package/dist/extensions/typed/index.js +1 -3
- package/dist/extensions/typed/typed-distinct/typed-distinct.extension.d.ts +1 -14
- package/dist/extensions/typed/typed-distinct/typed-distinct.extension.js +3 -1
- package/dist/extensions/typed/typed-joins/typed-joins.extension.d.ts +1 -10
- package/dist/extensions/typed/typed-joins/typed-joins.extension.js +3 -1
- package/dist/extensions/typed/typed-order-by/typed-order-by.extension.d.ts +1 -17
- package/dist/extensions/typed/typed-order-by/typed-order-by.extension.js +6 -5
- package/dist/extensions/typed/typed-select/typed-select.extension.d.ts +1 -33
- package/dist/extensions/typed/typed-select/typed-select.extension.js +17 -9
- package/dist/extensions/typed/typed-where/typed-where-is-in.extension.d.ts +2 -0
- package/dist/extensions/typed/typed-where/typed-where-is-in.extension.js +55 -0
- package/dist/extensions/typed/typed-where/typed-where.extension.d.ts +1 -89
- package/dist/extensions/typed/typed-where/typed-where.extension.js +32 -16
- package/dist/extensions/where-is-in.extension.d.ts +1 -0
- package/dist/extensions/where-is-in.extension.js +52 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types/extensions/get-many-with-totals.types.d.ts +10 -0
- package/dist/types/extensions/index.d.ts +4 -0
- package/dist/types/extensions/index.js +20 -0
- package/dist/types/extensions/order.types.d.ts +6 -0
- package/dist/types/extensions/pagination.types.d.ts +3 -0
- package/dist/types/extensions/pagination.types.js +2 -0
- package/dist/types/extensions/typed-where.types.d.ts +7 -0
- package/dist/types/extensions/typed-where.types.js +2 -0
- package/dist/types/extensions/where.types.d.ts +2 -0
- package/dist/types/extensions/where.types.js +2 -0
- package/dist/types/interfaces/ordering.interface.d.ts +1 -1
- package/dist/types/modules/proxy-callback.types.d.ts +7 -0
- package/dist/types/modules/proxy-callback.types.js +2 -0
- package/dist/types/util-types/index.d.ts +1 -0
- package/dist/types/util-types/index.js +1 -0
- package/dist/utils/common.utils.d.ts +1 -0
- package/dist/utils/common.utils.js +11 -1
- package/dist/utils/prototype.utils.d.ts +5 -0
- package/dist/utils/prototype.utils.js +10 -0
- package/dist/utils/proxy-key.utils.d.ts +7 -5
- package/dist/utils/proxy-key.utils.js +25 -14
- package/dist/utils/sql.utils.d.ts +1 -0
- package/dist/utils/sql.utils.js +7 -1
- package/dist/utils/where.utils.d.ts +6 -0
- package/dist/utils/where.utils.js +35 -0
- package/package.json +3 -3
- package/dist/extensions/ordering.extension.js +0 -48
- package/dist/extensions/typed/typeorm-querybuilder.types.d.ts +0 -22
- package/dist/extensions/typed/typeorm-querybuilder.types.js +0 -56
- package/dist/extensions/typed/typeorm.types.d.ts +0 -17
- package/dist/types/modules/proxy-types/proxy-callback.types.d.ts +0 -6
- package/dist/utils/string.utils.js +0 -11
- /package/dist/{extensions/typed/typeorm.types.js → types/extensions/get-many-with-totals.types.js} +0 -0
- /package/dist/types/{modules/proxy-types/proxy-callback.types.js → extensions/order.types.js} +0 -0
|
@@ -1,33 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("../../declarations/typed/typed-where.declaration");
|
|
3
4
|
const typeorm_1 = require("typeorm");
|
|
4
5
|
const proxy_key_utils_1 = require("../../../utils/proxy-key.utils");
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
typeorm_1.DeleteQueryBuilder,
|
|
9
|
-
];
|
|
6
|
+
const prototype_utils_1 = require("../../../utils/prototype.utils");
|
|
7
|
+
const where_utils_1 = require("../../../utils/where.utils");
|
|
8
|
+
// patching
|
|
10
9
|
const extension = {
|
|
11
10
|
prototype: {
|
|
12
|
-
whereTyped: function (where,
|
|
13
|
-
return
|
|
11
|
+
whereTyped: function (where, operator, parameters, options) {
|
|
12
|
+
return attachTypedWhere(this, 'where', where, operator, parameters, options);
|
|
14
13
|
},
|
|
15
|
-
andWhereTyped: function (where,
|
|
16
|
-
return
|
|
14
|
+
andWhereTyped: function (where, operator, parameters, options) {
|
|
15
|
+
return attachTypedWhere(this, 'andWhere', where, operator, parameters, options);
|
|
17
16
|
},
|
|
18
|
-
orWhereTyped: function (where,
|
|
19
|
-
return
|
|
17
|
+
orWhereTyped: function (where, operator, parameters, options) {
|
|
18
|
+
return attachTypedWhere(this, 'orWhere', where, operator, parameters, options);
|
|
20
19
|
},
|
|
21
20
|
},
|
|
22
21
|
};
|
|
22
|
+
const queryBuilders = [
|
|
23
|
+
typeorm_1.SelectQueryBuilder,
|
|
24
|
+
typeorm_1.UpdateQueryBuilder,
|
|
25
|
+
typeorm_1.DeleteQueryBuilder,
|
|
26
|
+
];
|
|
23
27
|
queryBuilders.forEach((builder) => {
|
|
24
|
-
|
|
28
|
+
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
25
29
|
});
|
|
26
|
-
|
|
30
|
+
// implementation
|
|
31
|
+
function attachTypedWhere(builder, method, where, conditions, parameters, options) {
|
|
32
|
+
const { useDoubleQuotes, addValue = true } = options || {};
|
|
27
33
|
if (typeof where === 'function') {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
const alias = builder.alias;
|
|
35
|
+
const key = (0, proxy_key_utils_1.getSqlKeyFromProxyCallback)(where, alias, useDoubleQuotes);
|
|
36
|
+
if (Array.isArray(key)) {
|
|
37
|
+
for (const k of key) {
|
|
38
|
+
builder = (0, where_utils_1.attachWhere)(builder, method, k, conditions, parameters, {
|
|
39
|
+
addValue,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return builder;
|
|
43
|
+
}
|
|
44
|
+
return (0, where_utils_1.attachWhere)(builder, method, key, conditions, parameters, {
|
|
45
|
+
addValue,
|
|
46
|
+
});
|
|
31
47
|
}
|
|
32
48
|
return builder;
|
|
33
49
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './declarations/where-is-in.daclaration';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("./declarations/where-is-in.daclaration");
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
const prototype_utils_1 = require("../utils/prototype.utils");
|
|
6
|
+
const where_utils_1 = require("../utils/where.utils");
|
|
7
|
+
// patching
|
|
8
|
+
const extension = {
|
|
9
|
+
prototype: {
|
|
10
|
+
whereIsIn: function (field, array) {
|
|
11
|
+
return attachWhereIsIn(this, 'where', field, 'IN', array);
|
|
12
|
+
},
|
|
13
|
+
andWhereIsIn: function (field, array) {
|
|
14
|
+
return attachWhereIsIn(this, 'andWhere', field, 'IN', array);
|
|
15
|
+
},
|
|
16
|
+
orWhereIsIn: function (field, array) {
|
|
17
|
+
return attachWhereIsIn(this, 'orWhere', field, 'IN', array);
|
|
18
|
+
},
|
|
19
|
+
whereIsNotIn: function (field, array) {
|
|
20
|
+
return attachWhereIsIn(this, 'where', field, 'NOT IN', array);
|
|
21
|
+
},
|
|
22
|
+
andWhereIsNotIn: function (field, array) {
|
|
23
|
+
return attachWhereIsIn(this, 'andWhere', field, 'NOT IN', array);
|
|
24
|
+
},
|
|
25
|
+
orWhereIsNotIn: function (field, array) {
|
|
26
|
+
return attachWhereIsIn(this, 'orWhere', field, 'NOT IN', array);
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
const queryBuilders = [
|
|
31
|
+
typeorm_1.SelectQueryBuilder,
|
|
32
|
+
typeorm_1.UpdateQueryBuilder,
|
|
33
|
+
typeorm_1.DeleteQueryBuilder,
|
|
34
|
+
];
|
|
35
|
+
queryBuilders.forEach((builder) => {
|
|
36
|
+
(0, prototype_utils_1.patchPrototype)(builder, extension);
|
|
37
|
+
});
|
|
38
|
+
// implementation
|
|
39
|
+
function attachWhereIsIn(builder, method, field, operator, array) {
|
|
40
|
+
if (method && typeof field === 'string' && typeof operator === 'string') {
|
|
41
|
+
if (!(array === null || array === void 0 ? void 0 : array.length)) {
|
|
42
|
+
// if the array is empty, we can use a simple condition
|
|
43
|
+
// IN (empty array) => FALSE
|
|
44
|
+
// NOT IN (empty array) => TRUE
|
|
45
|
+
return builder[method](operator === 'IN' ? 'FALSE' : 'TRUE');
|
|
46
|
+
}
|
|
47
|
+
return (0, where_utils_1.attachWhere)(builder, method, field, operator, { value: array }, {
|
|
48
|
+
addValue: true,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return builder;
|
|
52
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./extensions"), exports);
|
|
18
18
|
__exportStar(require("./types/interfaces"), exports);
|
|
19
|
+
__exportStar(require("./types/extensions"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./pagination.types"), exports);
|
|
18
|
+
__exportStar(require("./order.types"), exports);
|
|
19
|
+
__exportStar(require("./get-many-with-totals.types"), exports);
|
|
20
|
+
__exportStar(require("./where.types"), exports);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NonFunctionPropertyNames } from "../util-types";
|
|
2
|
-
export type OrderDirection = 'asc' | 'desc' | 'ASC' | 'DESC';
|
|
2
|
+
export type OrderDirection = 'asc' | 'desc' | 'ASC' | 'DESC' | 1 | -1;
|
|
3
3
|
export type NullsOrder = 'first' | 'last' | 'FIRST' | 'LAST';
|
|
4
4
|
export type OrderParam<Entity> = {
|
|
5
5
|
field: NonFunctionPropertyNames<Required<Entity>> | string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ObjectLiteral } from "typeorm";
|
|
2
|
+
import { EntityOnlyWithRelations, EntityWithoutRelations } from "./relations.types";
|
|
3
|
+
import { ColumnValueType } from "./column.types";
|
|
4
|
+
export type ProxyColumnValueType = ColumnValueType | ':value';
|
|
5
|
+
export type KeyProxyCallback<T extends ObjectLiteral> = (proxy: T) => ProxyColumnValueType | Array<ProxyColumnValueType>;
|
|
6
|
+
export type RelationalKeyProxyCallback<T extends ObjectLiteral> = (proxy: EntityOnlyWithRelations<T>) => ProxyColumnValueType;
|
|
7
|
+
export type NonRelationalKeyProxyCallback<T extends ObjectLiteral> = (proxy: EntityWithoutRelations<T>) => ProxyColumnValueType;
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isNil = void 0;
|
|
3
|
+
exports.randomString = exports.isNil = void 0;
|
|
4
4
|
const isNil = (val) => typeof val === 'undefined' || val === null;
|
|
5
5
|
exports.isNil = isNil;
|
|
6
|
+
function randomString(length = 21) {
|
|
7
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
8
|
+
let result = '';
|
|
9
|
+
for (let i = 0; i < length; i++) {
|
|
10
|
+
const index = Math.floor(Math.random() * chars.length);
|
|
11
|
+
result += chars.charAt(index);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
exports.randomString = randomString;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.patchPrototype = void 0;
|
|
5
|
+
function patchPrototype(target, source) {
|
|
6
|
+
for (const method in Object.keys(source.prototype)) {
|
|
7
|
+
target.prototype[method] = source.prototype[method];
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.patchPrototype = patchPrototype;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { ObjectLiteral } from 'typeorm';
|
|
2
|
-
import { KeyProxyCallback } from '../types/modules/proxy-
|
|
2
|
+
import { KeyProxyCallback } from '../types/modules/proxy-callback.types';
|
|
3
3
|
export declare class KeyHolder {
|
|
4
|
-
|
|
5
|
-
constructor(
|
|
4
|
+
private _keys;
|
|
5
|
+
constructor(_keys?: string[]);
|
|
6
6
|
toSql(length?: number, addDobleQuotes?: boolean): string;
|
|
7
7
|
toString(length?: number): string;
|
|
8
8
|
valueOf(): string;
|
|
9
9
|
[Symbol.toPrimitive](): string;
|
|
10
|
-
|
|
10
|
+
getKeys(): string[];
|
|
11
|
+
push(key: string | symbol): this;
|
|
12
|
+
unshift(key: string | symbol): this;
|
|
11
13
|
}
|
|
12
14
|
export declare class TypedProxy {
|
|
13
15
|
static defaultHandler: {};
|
|
@@ -19,4 +21,4 @@ export declare class KeyProxy {
|
|
|
19
21
|
}
|
|
20
22
|
export declare const getKeyHolder: <Type extends ObjectLiteral>(callback: KeyHolder | KeyProxyCallback<Type>) => KeyHolder | Array<KeyHolder>;
|
|
21
23
|
export declare const getKey: <Type extends ObjectLiteral>(callback: string | KeyHolder | KeyProxyCallback<Type>) => string;
|
|
22
|
-
export declare const
|
|
24
|
+
export declare const getSqlKeyFromProxyCallback: <Type extends ObjectLiteral>(callback: string | KeyHolder | KeyProxyCallback<Type>, addPrefix?: string, addDobleQuotes?: boolean) => string | string[];
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getSqlKeyFromProxyCallback = exports.getKey = exports.getKeyHolder = exports.KeyProxy = exports.TypedProxy = exports.KeyHolder = void 0;
|
|
4
4
|
const sql_utils_1 = require("./sql.utils");
|
|
5
5
|
class KeyHolder {
|
|
6
|
-
constructor(
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
6
|
+
constructor(_keys = []) {
|
|
7
|
+
this._keys = _keys;
|
|
8
|
+
this._keys = [..._keys];
|
|
9
9
|
}
|
|
10
10
|
toSql(length = 2, addDobleQuotes = true) {
|
|
11
11
|
const string = this.toString(length);
|
|
@@ -13,7 +13,7 @@ class KeyHolder {
|
|
|
13
13
|
return sql;
|
|
14
14
|
}
|
|
15
15
|
toString(length = 2) {
|
|
16
|
-
const keys = this.
|
|
16
|
+
const keys = this._keys.slice(0, length);
|
|
17
17
|
const string = keys.join('.');
|
|
18
18
|
return string;
|
|
19
19
|
}
|
|
@@ -23,11 +23,22 @@ class KeyHolder {
|
|
|
23
23
|
[Symbol.toPrimitive]() {
|
|
24
24
|
return this.toString();
|
|
25
25
|
}
|
|
26
|
-
|
|
26
|
+
getKeys() {
|
|
27
|
+
return this._keys.slice();
|
|
28
|
+
}
|
|
29
|
+
push(key) {
|
|
30
|
+
if (typeof key !== 'symbol' &&
|
|
31
|
+
!Object.getOwnPropertyNames(this).includes(key) && // check of getOwnPropertyNames(this) is questionable
|
|
32
|
+
!Object.getOwnPropertyNames(KeyHolder.prototype).includes(key)) {
|
|
33
|
+
this._keys.push(key);
|
|
34
|
+
}
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
unshift(key) {
|
|
27
38
|
if (typeof key !== 'symbol' &&
|
|
28
39
|
!Object.getOwnPropertyNames(this).includes(key) && // check of getOwnPropertyNames(this) is questionable
|
|
29
40
|
!Object.getOwnPropertyNames(KeyHolder.prototype).includes(key)) {
|
|
30
|
-
this.
|
|
41
|
+
this._keys.unshift(key);
|
|
31
42
|
}
|
|
32
43
|
return this;
|
|
33
44
|
}
|
|
@@ -53,8 +64,8 @@ KeyProxy.defaultHandler = {
|
|
|
53
64
|
return value;
|
|
54
65
|
}
|
|
55
66
|
// for each key create new independent layer of KeyProxy
|
|
56
|
-
const newKeyHolder = new KeyHolder(target.
|
|
57
|
-
const newLayer = TypedProxy.create(newKeyHolder, KeyProxy.defaultHandler);
|
|
67
|
+
const newKeyHolder = new KeyHolder(target.getKeys());
|
|
68
|
+
const newLayer = TypedProxy.create(newKeyHolder.push(key), KeyProxy.defaultHandler);
|
|
58
69
|
return newLayer;
|
|
59
70
|
},
|
|
60
71
|
};
|
|
@@ -75,26 +86,26 @@ const getKey = (callback) => {
|
|
|
75
86
|
return keyHolder.toString();
|
|
76
87
|
};
|
|
77
88
|
exports.getKey = getKey;
|
|
78
|
-
const
|
|
89
|
+
const getSqlKeyFromProxyCallback = (callback, addPrefix, addDobleQuotes = true) => {
|
|
79
90
|
if (typeof callback === 'string')
|
|
80
91
|
return callback;
|
|
81
92
|
const keyHolder = (0, exports.getKeyHolder)(callback);
|
|
82
93
|
if (Array.isArray(keyHolder)) {
|
|
83
|
-
return keyHolder.map((holder) => (0, exports.
|
|
94
|
+
return keyHolder.map((holder) => (0, exports.getSqlKeyFromProxyCallback)(holder, addPrefix));
|
|
84
95
|
}
|
|
85
96
|
// if select is like this: (proxy) => proxy.relation.column
|
|
86
|
-
if (keyHolder.
|
|
97
|
+
if (keyHolder.getKeys().length > 1) {
|
|
87
98
|
// return unchanged
|
|
88
99
|
return keyHolder.toSql(undefined, addDobleQuotes);
|
|
89
100
|
}
|
|
90
101
|
// else if select is one layer depth like this: (proxy) => proxy.column
|
|
91
102
|
// then add alias before the key
|
|
92
103
|
if (typeof addPrefix === 'string' && addPrefix.length >= 1) {
|
|
93
|
-
keyHolder.
|
|
104
|
+
keyHolder.unshift(addPrefix);
|
|
94
105
|
}
|
|
95
106
|
return keyHolder.toSql(undefined, addDobleQuotes);
|
|
96
107
|
};
|
|
97
|
-
exports.
|
|
108
|
+
exports.getSqlKeyFromProxyCallback = getSqlKeyFromProxyCallback;
|
|
98
109
|
// example of typed select
|
|
99
110
|
// const key = getSqlKey<UserEntity>((proxy) => proxy.sessions);
|
|
100
111
|
// console.log(key);
|
|
@@ -3,3 +3,4 @@ export declare const wrapWithDoubleQoutes: (str: string) => string;
|
|
|
3
3
|
export declare const wrapWithBackQoutes: (str: string) => string;
|
|
4
4
|
export declare const stringToSQLIdentifier: (str: string, driver?: DatabaseType) => string;
|
|
5
5
|
export declare const stringToSQLValue: (str: string) => string;
|
|
6
|
+
export declare const createUniqueParameterName: (prefix?: string) => string;
|
package/dist/utils/sql.utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringToSQLValue = exports.stringToSQLIdentifier = exports.wrapWithBackQoutes = exports.wrapWithDoubleQoutes = void 0;
|
|
3
|
+
exports.createUniqueParameterName = exports.stringToSQLValue = exports.stringToSQLIdentifier = exports.wrapWithBackQoutes = exports.wrapWithDoubleQoutes = void 0;
|
|
4
|
+
const common_utils_1 = require("./common.utils");
|
|
4
5
|
const wrapWithDoubleQoutes = (str) => str.includes('"') ? str : `"${str}"`;
|
|
5
6
|
exports.wrapWithDoubleQoutes = wrapWithDoubleQoutes;
|
|
6
7
|
const wrapWithBackQoutes = (str) => str.includes('`') ? str : '`' + str + '`';
|
|
@@ -16,3 +17,8 @@ const stringToSQLIdentifier = (str, driver) => str.includes('.')
|
|
|
16
17
|
exports.stringToSQLIdentifier = stringToSQLIdentifier;
|
|
17
18
|
const stringToSQLValue = (str) => `'${str}'`;
|
|
18
19
|
exports.stringToSQLValue = stringToSQLValue;
|
|
20
|
+
const createUniqueParameterName = (prefix) => {
|
|
21
|
+
const uniqueCode = (0, common_utils_1.randomString)(5);
|
|
22
|
+
return `${prefix || 'param'}_${uniqueCode}`;
|
|
23
|
+
};
|
|
24
|
+
exports.createUniqueParameterName = createUniqueParameterName;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { WhereExpressionBuilder } from 'typeorm';
|
|
2
|
+
import type { WhereMethods } from '../types/extensions/where.types';
|
|
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?: {
|
|
5
|
+
addValue?: boolean;
|
|
6
|
+
}): QB;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.attachWhere = exports.getValuePlaceholder = void 0;
|
|
4
|
+
const sql_utils_1 = require("./sql.utils");
|
|
5
|
+
function getValuePlaceholder(value, paramName) {
|
|
6
|
+
if (value === undefined || value === null || paramName === undefined) {
|
|
7
|
+
return 'NULL';
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
return `(:...${paramName})`;
|
|
11
|
+
}
|
|
12
|
+
return `:${paramName}`;
|
|
13
|
+
}
|
|
14
|
+
exports.getValuePlaceholder = getValuePlaceholder;
|
|
15
|
+
function attachWhere(builder, method, field, conditions, value, options) {
|
|
16
|
+
const { addValue = true } = options || {};
|
|
17
|
+
if (method && typeof field === 'string' && typeof conditions === 'string') {
|
|
18
|
+
field = (0, sql_utils_1.stringToSQLIdentifier)(field);
|
|
19
|
+
if (addValue) {
|
|
20
|
+
const firstValue = value !== null && typeof value === 'object' ? Object.values(value)[0] : value;
|
|
21
|
+
const valueIsValid = firstValue !== undefined;
|
|
22
|
+
const paramName = valueIsValid
|
|
23
|
+
? (0, sql_utils_1.createUniqueParameterName)(method)
|
|
24
|
+
: undefined;
|
|
25
|
+
const params = paramName && valueIsValid ? { [paramName]: firstValue } : undefined;
|
|
26
|
+
const valuePlaceholder = getValuePlaceholder(firstValue, paramName);
|
|
27
|
+
return builder[method](`${field} ${conditions} ${valuePlaceholder}`, params);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return builder[method](`${field} ${conditions}`, value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return builder;
|
|
34
|
+
}
|
|
35
|
+
exports.attachWhere = attachWhere;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typeorm-extensions",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "typeorm commonly used extensions",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": ["dist", "LICENSE", "README.md", "package.json"],
|
|
8
|
-
"type": "
|
|
8
|
+
"type": "commonjs",
|
|
9
9
|
"homepage": "https://koenigstag.github.io/typeorm-extensions/",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "tsc -p .",
|
|
16
16
|
"lint": "eslint . --ext .ts",
|
|
17
|
-
"test": "
|
|
17
|
+
"test-nest-build": "cd test/nest_v11 && npm install && npm run build"
|
|
18
18
|
},
|
|
19
19
|
"keywords": [
|
|
20
20
|
"typeorm",
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getOrderOption = void 0;
|
|
4
|
-
const SelectQueryBuilder_1 = require("typeorm/query-builder/SelectQueryBuilder");
|
|
5
|
-
const constants_1 = require("../constants");
|
|
6
|
-
const common_utils_1 = require("../utils/common.utils");
|
|
7
|
-
const identifier_utils_1 = require("../utils/identifier.utils");
|
|
8
|
-
const alwaysAliasWhiteList = [
|
|
9
|
-
'id',
|
|
10
|
-
'createdAt',
|
|
11
|
-
'created_at',
|
|
12
|
-
'updatedAt',
|
|
13
|
-
'updated_at',
|
|
14
|
-
'deletedAt',
|
|
15
|
-
'deleted_at',
|
|
16
|
-
];
|
|
17
|
-
SelectQueryBuilder_1.SelectQueryBuilder.prototype.applyOrder = function (orderBy, options) {
|
|
18
|
-
const { resetPreviousOrder = false } = options !== null && options !== void 0 ? options : {};
|
|
19
|
-
const orderOption = (0, exports.getOrderOption)(orderBy, this.alias, options);
|
|
20
|
-
const query = resetPreviousOrder ? this.orderBy() : this;
|
|
21
|
-
return query.addOrderBy(orderOption.selector, orderOption.order, orderOption.nulls);
|
|
22
|
-
};
|
|
23
|
-
SelectQueryBuilder_1.SelectQueryBuilder.prototype.applyOrderFilter = function (orderFilter, options) {
|
|
24
|
-
if (!(0, common_utils_1.isNil)(orderFilter === null || orderFilter === void 0 ? void 0 : orderFilter.orderBy)) {
|
|
25
|
-
return orderFilter.orderBy.reduce((acc, order, index) => acc.applyOrder(order, {
|
|
26
|
-
...options,
|
|
27
|
-
resetPreviousOrder: index === 0 ? options === null || options === void 0 ? void 0 : options.resetPreviousOrder : false,
|
|
28
|
-
}), this);
|
|
29
|
-
}
|
|
30
|
-
return this;
|
|
31
|
-
};
|
|
32
|
-
const getOrderOption = (orderBy, defaultAlias, options) => {
|
|
33
|
-
const { useDoubleQuotes = constants_1.defaultUseDoubleQuotes, alias: customAlias, alwaysAliasFields = alwaysAliasWhiteList, } = options !== null && options !== void 0 ? options : {};
|
|
34
|
-
const { field, direction = constants_1.defaultOrderDirection, nulls: nullsOrder = constants_1.defaultNullsOrder, } = orderBy;
|
|
35
|
-
const alias = customAlias !== null && customAlias !== void 0 ? customAlias : (alwaysAliasFields.includes(field) ? defaultAlias : undefined);
|
|
36
|
-
const identifier = new identifier_utils_1.Identifier(field, alias, useDoubleQuotes);
|
|
37
|
-
const selector = identifier.fieldIdentifier();
|
|
38
|
-
const order = direction.toUpperCase();
|
|
39
|
-
const nulls = nullsOrder
|
|
40
|
-
? `NULLS ${nullsOrder.toUpperCase()}`
|
|
41
|
-
: undefined;
|
|
42
|
-
return {
|
|
43
|
-
selector,
|
|
44
|
-
order,
|
|
45
|
-
nulls,
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
exports.getOrderOption = getOrderOption;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ObjectLiteral } from 'typeorm';
|
|
2
|
-
import { ColumnValueType, EntityOnlyWithRelations, EntityWithoutRelations } from './typeorm.types';
|
|
3
|
-
export declare class KeyHolder {
|
|
4
|
-
keys: string[];
|
|
5
|
-
constructor(keys?: string[]);
|
|
6
|
-
toSql(length?: number, addDobleQuotes?: boolean): string;
|
|
7
|
-
toString(length?: number): string;
|
|
8
|
-
valueOf(): string;
|
|
9
|
-
[Symbol.toPrimitive](): string;
|
|
10
|
-
add(key: string | symbol): this;
|
|
11
|
-
}
|
|
12
|
-
export declare class TypedProxy {
|
|
13
|
-
static defaultHandler: {};
|
|
14
|
-
static create<T extends ObjectLiteral>(obj: object, handler?: {}): T;
|
|
15
|
-
}
|
|
16
|
-
export declare class KeyProxy {
|
|
17
|
-
static defaultHandler: ProxyHandler<KeyHolder>;
|
|
18
|
-
static create<T extends ObjectLiteral>(): T;
|
|
19
|
-
}
|
|
20
|
-
export type KeyProxyCallback<T extends ObjectLiteral> = (proxy: T) => ColumnValueType | Array<ColumnValueType>;
|
|
21
|
-
export type RelationalKeyProxyCallback<T extends ObjectLiteral> = (proxy: EntityOnlyWithRelations<T>) => ColumnValueType;
|
|
22
|
-
export type NonRelationalKeyProxyCallback<T extends ObjectLiteral> = (proxy: EntityWithoutRelations<T>) => ColumnValueType;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KeyProxy = exports.TypedProxy = exports.KeyHolder = void 0;
|
|
4
|
-
class KeyHolder {
|
|
5
|
-
constructor(keys = []) {
|
|
6
|
-
this.keys = keys;
|
|
7
|
-
this.keys = [...keys];
|
|
8
|
-
}
|
|
9
|
-
toSql(length = 2, addDobleQuotes = true) {
|
|
10
|
-
const string = this.toString(length);
|
|
11
|
-
const sql = addDobleQuotes ? `"${string.replace(/\./g, '"."')}"` : string;
|
|
12
|
-
return sql;
|
|
13
|
-
}
|
|
14
|
-
toString(length = 2) {
|
|
15
|
-
const keys = this.keys.slice(0, length);
|
|
16
|
-
const string = keys.join('.');
|
|
17
|
-
return string;
|
|
18
|
-
}
|
|
19
|
-
valueOf() {
|
|
20
|
-
return this.toString();
|
|
21
|
-
}
|
|
22
|
-
[Symbol.toPrimitive]() {
|
|
23
|
-
return this.toString();
|
|
24
|
-
}
|
|
25
|
-
add(key) {
|
|
26
|
-
if (typeof key !== 'symbol' &&
|
|
27
|
-
!Object.getOwnPropertyNames(this).includes(key) && // check of getOwnPropertyNames(this) is questionable
|
|
28
|
-
!Object.getOwnPropertyNames(KeyHolder.prototype).includes(key)) {
|
|
29
|
-
this.keys.push(key);
|
|
30
|
-
}
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.KeyHolder = KeyHolder;
|
|
35
|
-
class TypedProxy {
|
|
36
|
-
static create(obj, handler = TypedProxy.defaultHandler) {
|
|
37
|
-
return new Proxy(obj, handler);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.TypedProxy = TypedProxy;
|
|
41
|
-
TypedProxy.defaultHandler = {};
|
|
42
|
-
class KeyProxy {
|
|
43
|
-
static create() {
|
|
44
|
-
return TypedProxy.create(new KeyHolder(), KeyProxy.defaultHandler);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.KeyProxy = KeyProxy;
|
|
48
|
-
KeyProxy.defaultHandler = {
|
|
49
|
-
get(target, key, receiver) {
|
|
50
|
-
var _a;
|
|
51
|
-
// for each key create new independent layer of KeyProxy
|
|
52
|
-
const newKeyHolder = new KeyHolder(target.keys).add(key);
|
|
53
|
-
const newLayer = TypedProxy.create(newKeyHolder, KeyProxy.defaultHandler);
|
|
54
|
-
return (_a = Reflect.get(target, key, receiver)) !== null && _a !== void 0 ? _a : newLayer;
|
|
55
|
-
},
|
|
56
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { BaseEntity, ObjectLiteral } from 'typeorm';
|
|
2
|
-
export type SimpleColumnType = string | number | boolean | Date | null;
|
|
3
|
-
export type ColumnValueType = SimpleColumnType | undefined | object | ObjectLiteral | ':value';
|
|
4
|
-
export type RelationColumnsType<ColumnType, Key> = ColumnType extends SimpleColumnType | ((...args: any[]) => any) ? never : ColumnType extends BaseEntity | BaseEntity[] | ObjectLiteral | ObjectLiteral[] ? Key : never;
|
|
5
|
-
export type NonRelationColumnsType<ColumnType, Key> = ColumnType extends SimpleColumnType ? Key : never;
|
|
6
|
-
export type RelationPropertyNames<Type> = {
|
|
7
|
-
[Key in keyof Type]: RelationColumnsType<Type[Key], Key>;
|
|
8
|
-
}[keyof Type] & string;
|
|
9
|
-
export type NonRelationPropertyNames<Type> = {
|
|
10
|
-
[Key in keyof Type]: NonRelationColumnsType<Type[Key], Key>;
|
|
11
|
-
}[keyof Type] & string;
|
|
12
|
-
export type EntityOnlyWithRelations<Entity extends ObjectLiteral> = {
|
|
13
|
-
[Key in RelationPropertyNames<Entity>]: Entity[Key];
|
|
14
|
-
};
|
|
15
|
-
export type EntityWithoutRelations<Entity extends ObjectLiteral> = {
|
|
16
|
-
[Key in NonRelationPropertyNames<Entity>]: Entity[Key];
|
|
17
|
-
};
|