@adminjs/mikroorm
Version:
MikroORM adapter for AdminJS
62 lines • 1.81 kB
JavaScript
import { ReferenceKind } from '@mikro-orm/core';
import { BaseProperty } from 'adminjs';
import { DATA_TYPES } from './utils/data-types.js';
export class Property extends BaseProperty {
column;
columnPosition;
constructor(column, columnPosition = 0) {
const path = column.name;
super({ path });
this.column = column;
this.columnPosition = columnPosition;
}
getColumnMetadata() {
return this.column;
}
isEditable() {
return !this.isId() && this.column.name !== 'createdAt' && this.column.name !== 'updatedAt';
}
isId() {
return !!this.column.primary;
}
isRequired() {
if (this.column.nullable === false)
return true;
return false;
}
isSortable() {
return this.type() !== 'reference';
}
reference() {
const isRef = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(this.column.kind);
if (isRef) {
return this.column.targetMeta?.name ?? this.column.type;
}
return null;
}
availableValues() {
const isEnum = !!this.column.enum && !!this.column.items;
if (isEnum)
return this.column.items?.map((i) => String(i)) ?? [];
return null;
}
position() {
return this.columnPosition || 0;
}
isEnum() {
return this.column.type === 'enum';
}
type() {
let type = DATA_TYPES[this.column.columnTypes[0]]
|| DATA_TYPES[this.column.type];
if (this.reference()) {
type = 'reference';
}
// eslint-disable-next-line no-console
if (!type) {
console.warn(`Unhandled type: ${this.column.type}`);
}
return type;
}
}
//# sourceMappingURL=Property.js.map