After examining the source code of typeorm
(found here, here, here, here), it appears that you can access various information using the global variable typeormMetadataArgsStorage
(such as
window.typeormMetadataArgsStorage
or
global.typeormMetadataArgsStorage
). Dive into it, and you may discover what you're looking for with a script like this:
const global_context = ??? // depending on your environment
const property_to_look_for = `areaCode`
const name = global_context.typeormMetadataArgsStorage
.columns
.filter(col => col.propertyName === property_to_look_for && col.target === LocationStatus)
.options
.name
console.log(name)
UPDATED
For those utilizing Nest
framework like myself, here's my current approach.
import { getMetadataArgsStorage, InsertResult } from 'typeorm';
export class PinLocationStatusRepository extends Repository<PinLocationStatus> {
// List of column names in this array should not be altered.
// For example, the value of "location_id" as "location_ko_01" remains unchanged.
private static immutableColumnNames = ['location_id', ...];
private static mutableColumnFound(name: string): boolean {
return PinLocationStatusRepository.immutableColumnNames.every(colName => colName !== name);
}
savePinLocation(state: PinLocationStatus): Promise<InsertResult> {
const columns = getMetadataArgsStorage()
.columns.filter(({ target }) => target === PinLocationStatus)
.map(({ options, propertyName }) => (!options.name ? propertyName : options.name))
.reduce((columns, name) => {
if (PinLocationStatusRepository.mutableColumnFound(name)) {
columns.push(name);
}
return columns;
}, []);
return this.createQueryBuilder()
.insert()
.into(PinLocationStatus)
.values(state)
.orUpdate({ overwrite: columns }) // ['area_code']
.execute();
}
}
- Retrieve the column name from the
@Column()
decorator.
- Exclude certain column names where values must remain constant.
- Provide the columns to the
overwrite
property.
The output will be accurate unless "area_code" is a fixed column.