I am looking to add metadata to properties within classes, specifically using abbreviations for property names.
By using annotations like @shortName(abbreviated), you can label each property as follows:
function shortName(shortName: string){
return function (target: Object, realName: string){
// Where should I store the relationship between realName <-> shortName ??
}
}
class Record{
@shortName("ts") typeOfStorage: string;
}
class Client extends Record{
@shortName("df") descriptiveField: string;
}
function mapNames(obj: any){ // Return object with shortened names
let ret = {};
for(let prop in obj){
//Here retrieve the short name and add it to ret
}
return ret;
}
let client = new Client(); // assuming: { typeOfStorage: "f", descriptiveField: "blah"}
let clientShortened = mapNames(client); // expected output: {ts: "f", df: "blah"}
The challenge lies in determining where and how to store these relationships so they are accessible in instances of derived classes.
Initially, I attempted creating a global map prefixed with target.constructor.name (which provides the class name). However, in an inherited class, the constructor.name is that of the inherited class (resulting in losing track of typeOfStorage
in the client
example).
(This approach aims to optimize storage space when storing objects in non-SQL databases such as Firestore by storing each property name of each object record)