-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathhelper.ts
302 lines (267 loc) · 11 KB
/
helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { mergeWith } from 'lodash'
import {
FindOperator,
FindOptionsRelationByString,
FindOptionsRelations,
Repository,
SelectQueryBuilder,
} from 'typeorm'
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata'
import { OrmUtils } from 'typeorm/util/OrmUtils'
/**
* Joins 2 keys as `K`, `K.P`, `K.(P` or `K.P)`
* The parenthesis notation is included for embedded columns
*/
type Join<K, P> = K extends string
? P extends string
? `${K}${'' extends P ? '' : '.'}${P | `(${P}` | `${P})`}`
: never
: never
/**
* Get the previous number between 0 and 10. Examples:
* Prev[3] = 2
* Prev[0] = never.
* Prev[20] = 0
*/
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]]
/**
* Unwrap Promise<T> to T
*/
type UnwrapPromise<T> = T extends Promise<infer U> ? UnwrapPromise<U> : T
/**
* Unwrap Array<T> to T
*/
type UnwrapArray<T> = T extends Array<infer U> ? UnwrapArray<U> : T
/**
* Find all the dotted path properties for a given column.
*
* T: The column
* D: max depth
*/
// v Have we reached max depth?
export type Column<T, D extends number = 2> = [D] extends [never]
? // yes, stop recursing
never
: // Are we extending something with keys?
T extends Record<string, any>
? {
// For every keyof T, find all possible properties as a string union
[K in keyof T]-?: K extends string
? // Is it string or number (includes enums)?
T[K] extends string | number
? // yes, add just the key
`${K}`
: // Is it a Date?
T[K] extends Date
? // yes, add just the key
`${K}`
: // no, is it an array?
T[K] extends Array<infer U>
? // yes, unwrap it, and recurse deeper
`${K}` | Join<K, Column<UnwrapArray<U>, Prev[D]>>
: // no, is it a promise?
T[K] extends Promise<infer U>
? // yes, try to infer its return type and recurse
U extends Array<infer V>
? `${K}` | Join<K, Column<UnwrapArray<V>, Prev[D]>>
: `${K}` | Join<K, Column<UnwrapPromise<U>, Prev[D]>>
: // no, we have no more special cases, so treat it as an
// object and recurse deeper on its keys
`${K}` | Join<K, Column<T[K], Prev[D]>>
: never
// Join all the string unions of each keyof T into a single string union
}[keyof T]
: ''
export type RelationColumn<T> = Extract<
Column<T>,
{
[K in Column<T>]: K extends `${infer R}.${string}` ? R : never
}[Column<T>]
>
export type Order<T> = [Column<T>, 'ASC' | 'DESC']
export type SortBy<T> = Order<T>[]
// eslint-disable-next-line @typescript-eslint/ban-types
export type MappedColumns<T, S> = { [key in Column<T> | (string & {})]: S }
export type JoinMethod = 'leftJoinAndSelect' | 'innerJoinAndSelect'
export type RelationSchemaInput<T = any> = FindOptionsRelations<T> | RelationColumn<T>[] | FindOptionsRelationByString
// eslint-disable-next-line @typescript-eslint/ban-types
export type RelationSchema<T = any> = { [relation in Column<T> | (string & {})]: true }
export function isEntityKey<T>(entityColumns: Column<T>[], column: string): column is Column<T> {
return !!entityColumns.find((c) => c === column)
}
export const positiveNumberOrDefault = (value: number | undefined, defaultValue: number, minValue: 0 | 1 = 0) =>
value === undefined || value < minValue ? defaultValue : value
export type ColumnProperties = { propertyPath?: string; propertyName: string; isNested: boolean; column: string }
export function getPropertiesByColumnName(column: string): ColumnProperties {
const propertyPath = column.split('.')
if (propertyPath.length > 1) {
const propertyNamePath = propertyPath.slice(1)
let isNested = false,
propertyName = propertyNamePath.join('.')
if (!propertyName.startsWith('(') && propertyNamePath.length > 1) {
isNested = true
}
propertyName = propertyName.replace('(', '').replace(')', '')
return {
propertyPath: propertyPath[0],
propertyName, // the join is in case of an embedded entity
isNested,
column: `${propertyPath[0]}.${propertyName}`,
}
} else {
return { propertyName: propertyPath[0], isNested: false, column: propertyPath[0] }
}
}
export function extractVirtualProperty(
qb: SelectQueryBuilder<unknown>,
columnProperties: ColumnProperties
): Partial<ColumnMetadata> {
const metadata = columnProperties.propertyPath
? qb?.expressionMap?.mainAlias?.metadata?.findColumnWithPropertyPath(columnProperties.propertyPath)
?.referencedColumn?.entityMetadata // on relation
: qb?.expressionMap?.mainAlias?.metadata
return (
metadata?.columns?.find((column) => column.propertyName === columnProperties.propertyName) || {
isVirtualProperty: false,
query: undefined,
}
)
}
export function includesAllPrimaryKeyColumns(qb: SelectQueryBuilder<unknown>, propertyPath: string[]): boolean {
if (!qb || !propertyPath) {
return false
}
return qb.expressionMap.mainAlias?.metadata?.primaryColumns
.map((column) => column.propertyPath)
.every((column) => propertyPath.includes(column))
}
export function hasColumnWithPropertyPath(
qb: SelectQueryBuilder<unknown>,
columnProperties: ColumnProperties
): boolean {
if (!qb || !columnProperties) {
return false
}
return !!qb.expressionMap.mainAlias?.metadata?.hasColumnWithPropertyPath(columnProperties.propertyName)
}
export function checkIsRelation(qb: SelectQueryBuilder<unknown>, propertyPath: string): boolean {
if (!qb || !propertyPath) {
return false
}
return !!qb?.expressionMap?.mainAlias?.metadata?.hasRelationWithPropertyPath(propertyPath)
}
export function checkIsNestedRelation(qb: SelectQueryBuilder<unknown>, propertyPath: string): boolean {
let metadata = qb?.expressionMap?.mainAlias?.metadata
for (const relationName of propertyPath.split('.')) {
const relation = metadata?.relations.find((relation) => relation.propertyPath === relationName)
if (!relation) {
return false
}
metadata = relation.inverseEntityMetadata
}
return true
}
export function checkIsEmbedded(qb: SelectQueryBuilder<unknown>, propertyPath: string): boolean {
if (!qb || !propertyPath) {
return false
}
return !!qb?.expressionMap?.mainAlias?.metadata?.hasEmbeddedWithPropertyPath(propertyPath)
}
export function checkIsArray(qb: SelectQueryBuilder<unknown>, propertyName: string): boolean {
if (!qb || !propertyName) {
return false
}
return !!qb?.expressionMap?.mainAlias?.metadata.findColumnWithPropertyName(propertyName)?.isArray
}
export function checkIsJsonb(qb: SelectQueryBuilder<unknown>, propertyName: string): boolean {
if (!qb || !propertyName) {
return false
}
if (propertyName.includes('.')) {
const parts = propertyName.split('.')
const dbColumnName = parts[parts.length - 2]
return qb?.expressionMap?.mainAlias?.metadata.findColumnWithPropertyName(dbColumnName)?.type === 'jsonb'
}
return qb?.expressionMap?.mainAlias?.metadata.findColumnWithPropertyName(propertyName)?.type === 'jsonb'
}
// This function is used to fix the column alias when using relation, embedded or virtual properties
export function fixColumnAlias(
properties: ColumnProperties,
alias: string,
isRelation = false,
isVirtualProperty = false,
isEmbedded = false,
query?: ColumnMetadata['query']
): string {
if (isRelation) {
if (isVirtualProperty && query) {
return `(${query(`${alias}_${properties.propertyPath}_rel`)})` // () is needed to avoid parameter conflict
} else if ((isVirtualProperty && !query) || properties.isNested) {
if (properties.propertyName.includes('.')) {
const propertyPath = properties.propertyName.split('.')
const nestedRelations = propertyPath
.slice(0, -1)
.map((v) => `${v}_rel`)
.join('_')
const nestedCol = propertyPath[propertyPath.length - 1]
return `${alias}_${properties.propertyPath}_rel_${nestedRelations}.${nestedCol}`
} else {
return `${alias}_${properties.propertyPath}_rel_${properties.propertyName}`
}
} else {
return `${alias}_${properties.propertyPath}_rel.${properties.propertyName}`
}
} else if (isVirtualProperty) {
return query ? `(${query(`${alias}`)})` : `${alias}_${properties.propertyName}`
} else if (isEmbedded) {
return `${alias}.${properties.propertyPath}.${properties.propertyName}`
} else {
return `${alias}.${properties.propertyName}`
}
}
export function getQueryUrlComponents(path: string): { queryOrigin: string; queryPath: string } {
const r = new RegExp('^(?:[a-z+]+:)?//', 'i')
let queryOrigin = ''
let queryPath = ''
if (r.test(path)) {
const url = new URL(path)
queryOrigin = url.origin
queryPath = url.pathname
} else {
queryPath = path
}
return { queryOrigin, queryPath }
}
const isoDateRegExp = new RegExp(
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/
)
export function isISODate(str: string): boolean {
return isoDateRegExp.test(str)
}
export function isRepository<T>(repo: unknown | Repository<T>): repo is Repository<T> {
if (repo instanceof Repository) return true
try {
if (Object.getPrototypeOf(repo).constructor.name === 'Repository') return true
return typeof repo === 'object' && !('connection' in repo) && 'manager' in repo
} catch {
return false
}
}
export function isFindOperator<T>(value: unknown | FindOperator<T>): value is FindOperator<T> {
if (value instanceof FindOperator) return true
try {
if (Object.getPrototypeOf(value).constructor.name === 'FindOperator') return true
return typeof value === 'object' && '_type' in value && '_value' in value
} catch {
return false
}
}
export function createRelationSchema<T>(configurationRelations: RelationSchemaInput<T>): RelationSchema<T> {
return Array.isArray(configurationRelations)
? OrmUtils.propertyPathsToTruthyObject(configurationRelations)
: (configurationRelations as RelationSchema<T>)
}
export function mergeRelationSchema(...schemas: RelationSchema[]) {
const noTrueOverride = (obj, source) => (source === true && obj !== undefined ? obj : undefined)
return mergeWith({}, ...schemas, noTrueOverride)
}