I understand that this is not related to nestjs
but can someonw help me
import {
AllowNull,
AutoIncrement,
Column,
DataType,
Default,
Length,
Model,
PrimaryKey,
Table,
} from 'sequelize-typescript';
@Table({ tableName: 'country', timestamps: false })
export class CountryModel extends Model<CountryModel> {
public static keys: Record<CountryModelKeys, CountryModelKeys> =
CountryModel.generateKeys();
@PrimaryKey
@AutoIncrement
@Column(DataType.INTEGER)
id: number;
@AllowNull(false)
@Column(DataType.STRING)
name: string;
@AllowNull(false)
@Length({ max: 4 })
@Column(DataType.STRING)
phone_code: string;
@AllowNull(true)
@Length({ max: 4 })
@Column(DataType.STRING)
short_code: string;
@AllowNull(false)
@Column(DataType.STRING)
slug: string;
constructor(values?: any, options?: any) {
super(values, options);
CountryModel.initializeKeys();
}
public static initializeKeys(): void {
CountryModel.keys = CountryModel.generateKeys();
}
private static generateKeys(): Record<CountryModelKeys, CountryModelKeys> {
const keysObject = {} as Record<CountryModelKeys, CountryModelKeys>;
const modelKeys = Object.getOwnPropertyNames(Model.prototype);
Object.getOwnPropertyNames(CountryModel.prototype).forEach((key) => {
if (
!modelKeys.includes(key) &&
!key.startsWith('_')
) {
keysObject[key as CountryModelKeys] = key as CountryModelKeys;
}
});
return keysObject;
}
}
export type CountryModelKeys = 'id' | Exclude<keyof CountryModel, keyof Model>;
so basically i am trying to get the keys as a static variable so when writting attributes
i want to be somewhat safe
by using the keys
instead of using direct string literals