chore: Typescript database models (#2886)

closes #2798
This commit is contained in:
Tom Moor
2022-01-06 18:24:28 -08:00
committed by GitHub
parent d3cbf250e6
commit b20a341f0c
207 changed files with 5624 additions and 5315 deletions

View File

@@ -0,0 +1,50 @@
/**
* A decorator that must be applied to every model definition to workaround
* babel <> typescript incompatibility. See the following issue:
* https://github.com/RobinBuschmann/sequelize-typescript/issues/612#issuecomment-491890977
*
* @param target model class
*/
export default function Fix(target: any): void {
return class extends target {
constructor(...args: any[]) {
super(...args);
const rawAttributes = Object.keys(new.target.rawAttributes);
const associations = Object.keys(new.target.associations);
rawAttributes.forEach((propertyKey) => {
// check if we already defined getter/setter if so, do not override
const desc = Object.getOwnPropertyDescriptor(
target.prototype,
propertyKey
);
if (desc) {
return;
}
Object.defineProperty(this, propertyKey, {
get() {
return this.getDataValue(propertyKey);
},
set(value) {
this.setDataValue(propertyKey, value);
},
});
});
associations.forEach((propertyKey) => {
Object.defineProperty(this, propertyKey, {
get() {
return this.dataValues[propertyKey];
},
set(value) {
// sets without changing the "changed" flag for associations
this.dataValues[propertyKey] = value;
},
});
});
}
} as any;
}