forked from Arteha/admin-bro-typeorm
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathProperty.ts
More file actions
68 lines (53 loc) · 1.75 KB
/
Copy pathProperty.ts
File metadata and controls
68 lines (53 loc) · 1.75 KB
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
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata'
import { BaseProperty, PropertyType } from 'adminjs'
import { DATA_TYPES } from './utils/data-types'
export class Property extends BaseProperty {
public column: ColumnMetadata;
protected columnPosition: number;
constructor(column: ColumnMetadata, columnPosition = 0) {
const path = column.propertyPath
super({ path })
this.column = column
this.columnPosition = columnPosition
}
public isEditable(): boolean {
return !this.isId()
&& !this.column.isCreateDate
&& !this.column.isUpdateDate
}
public isId(): boolean {
return this.column.isPrimary
}
public isSortable(): boolean {
return this.type() !== 'reference'
}
public reference(): string | null {
const ref = this.column.referencedColumn
if (ref) return ref.entityMetadata.name
return null
}
public availableValues(): Array<any> | null {
const values = this.column.enum
if (values) { return values.map((val) => val.toString()) }
return null
}
public position(): number {
return this.columnPosition || 0
}
public type(): PropertyType {
let type: PropertyType = DATA_TYPES[this.column.type as any]
if (typeof this.column.type === 'function') {
if (this.column.type === Number) { type = 'number' }
if (this.column.type === String) { type = 'string' }
if (this.column.type === Date) { type = 'datetime' }
if (this.column.type === Boolean) { type = 'boolean' }
}
if (this.reference()) { type = 'reference' }
// eslint-disable-next-line no-console
if (!type) { console.warn(`Unhandled type: ${this.column.type}`) }
return type
}
public isArray(): boolean {
return this.column.isArray
}
}