From 7bfab00869231f7f998cb61750afac9bf9765167 Mon Sep 17 00:00:00 2001 From: "dusan.knezevic" Date: Thu, 1 Dec 2022 15:26:42 +0100 Subject: [PATCH] auto-generator: For typescript generation `declare` all class fields as per https://sequelize.org/docs/v6/core-concepts/model-basics/#caveat-with-public-class-fields --- src/auto-generator.ts | 64 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/auto-generator.ts b/src/auto-generator.ts index 9d3b4252..afc11373 100644 --- a/src/auto-generator.ts +++ b/src/auto-generator.ts @@ -613,10 +613,10 @@ export class AutoGenerator { // current table is a child that belongsTo parent const pparent = _.upperFirst(rel.parentProp); str += `${sp}// ${rel.childModel} belongsTo ${rel.parentModel} via ${rel.parentId}\n`; - str += `${sp}${rel.parentProp}!: ${rel.parentModel};\n`; - str += `${sp}get${pparent}!: Sequelize.BelongsToGetAssociationMixin<${rel.parentModel}>;\n`; - str += `${sp}set${pparent}!: Sequelize.BelongsToSetAssociationMixin<${rel.parentModel}, ${rel.parentModel}Id>;\n`; - str += `${sp}create${pparent}!: Sequelize.BelongsToCreateAssociationMixin<${rel.parentModel}>;\n`; + str += `${sp}declare ${rel.parentProp}: ${rel.parentModel};\n`; + str += `${sp}declare get${pparent}: Sequelize.BelongsToGetAssociationMixin<${rel.parentModel}>;\n`; + str += `${sp}declare set${pparent}: Sequelize.BelongsToSetAssociationMixin<${rel.parentModel}, ${rel.parentModel}Id>;\n`; + str += `${sp}declare create${pparent}: Sequelize.BelongsToCreateAssociationMixin<${rel.parentModel}>;\n`; needed[rel.parentTable] ??= new Set(); needed[rel.parentTable].add(rel.parentModel); needed[rel.parentTable].add(rel.parentModel + 'Id'); @@ -626,10 +626,10 @@ export class AutoGenerator { if (rel.isOne) { // const hasModelSingular = singularize(hasModel); str += `${sp}// ${rel.parentModel} hasOne ${rel.childModel} via ${rel.parentId}\n`; - str += `${sp}${rel.childProp}!: ${rel.childModel};\n`; - str += `${sp}get${pchild}!: Sequelize.HasOneGetAssociationMixin<${rel.childModel}>;\n`; - str += `${sp}set${pchild}!: Sequelize.HasOneSetAssociationMixin<${rel.childModel}, ${rel.childModel}Id>;\n`; - str += `${sp}create${pchild}!: Sequelize.HasOneCreateAssociationMixin<${rel.childModel}>;\n`; + str += `${sp}declare ${rel.childProp}: ${rel.childModel};\n`; + str += `${sp}declare get${pchild}: Sequelize.HasOneGetAssociationMixin<${rel.childModel}>;\n`; + str += `${sp}declare set${pchild}: Sequelize.HasOneSetAssociationMixin<${rel.childModel}, ${rel.childModel}Id>;\n`; + str += `${sp}declare create${pchild}: Sequelize.HasOneCreateAssociationMixin<${rel.childModel}>;\n`; needed[rel.childTable].add(rel.childModel); needed[rel.childTable].add(`${rel.childModel}Id`); needed[rel.childTable].add(`${rel.childModel}CreationAttributes`); @@ -639,17 +639,17 @@ export class AutoGenerator { const lur = pluralize(rel.childProp); const plur = _.upperFirst(lur); str += `${sp}// ${rel.parentModel} hasMany ${rel.childModel} via ${rel.parentId}\n`; - str += `${sp}${lur}!: ${rel.childModel}[];\n`; - str += `${sp}get${plur}!: Sequelize.HasManyGetAssociationsMixin<${hasModel}>;\n`; - str += `${sp}set${plur}!: Sequelize.HasManySetAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}add${sing}!: Sequelize.HasManyAddAssociationMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}add${plur}!: Sequelize.HasManyAddAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}create${sing}!: Sequelize.HasManyCreateAssociationMixin<${hasModel}>;\n`; - str += `${sp}remove${sing}!: Sequelize.HasManyRemoveAssociationMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}remove${plur}!: Sequelize.HasManyRemoveAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}has${sing}!: Sequelize.HasManyHasAssociationMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}has${plur}!: Sequelize.HasManyHasAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; - str += `${sp}count${plur}!: Sequelize.HasManyCountAssociationsMixin;\n`; + str += `${sp}declare ${lur}: ${rel.childModel}[];\n`; + str += `${sp}declare get${plur}: Sequelize.HasManyGetAssociationsMixin<${hasModel}>;\n`; + str += `${sp}declare set${plur}: Sequelize.HasManySetAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare add${sing}: Sequelize.HasManyAddAssociationMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare add${plur}: Sequelize.HasManyAddAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare create${sing}: Sequelize.HasManyCreateAssociationMixin<${hasModel}>;\n`; + str += `${sp}declare remove${sing}: Sequelize.HasManyRemoveAssociationMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare remove${plur}: Sequelize.HasManyRemoveAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare has${sing}: Sequelize.HasManyHasAssociationMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare has${plur}: Sequelize.HasManyHasAssociationsMixin<${hasModel}, ${hasModel}Id>;\n`; + str += `${sp}declare count${plur}: Sequelize.HasManyCountAssociationsMixin;\n`; needed[rel.childTable].add(hasModel); needed[rel.childTable].add(`${hasModel}Id`); } @@ -666,17 +666,17 @@ export class AutoGenerator { const otherModelPlural = _.upperFirst(lotherModelPlural); const otherTable = isParent ? rel.childTable : rel.parentTable; str += `${sp}// ${thisModel} belongsToMany ${otherModel} via ${rel.parentId} and ${rel.childId}\n`; - str += `${sp}${lotherModelPlural}!: ${otherModel}[];\n`; - str += `${sp}get${otherModelPlural}!: Sequelize.BelongsToManyGetAssociationsMixin<${otherModel}>;\n`; - str += `${sp}set${otherModelPlural}!: Sequelize.BelongsToManySetAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}add${otherModelSingular}!: Sequelize.BelongsToManyAddAssociationMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}add${otherModelPlural}!: Sequelize.BelongsToManyAddAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}create${otherModelSingular}!: Sequelize.BelongsToManyCreateAssociationMixin<${otherModel}>;\n`; - str += `${sp}remove${otherModelSingular}!: Sequelize.BelongsToManyRemoveAssociationMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}remove${otherModelPlural}!: Sequelize.BelongsToManyRemoveAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}has${otherModelSingular}!: Sequelize.BelongsToManyHasAssociationMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}has${otherModelPlural}!: Sequelize.BelongsToManyHasAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; - str += `${sp}count${otherModelPlural}!: Sequelize.BelongsToManyCountAssociationsMixin;\n`; + str += `${sp}declare ${lotherModelPlural}: ${otherModel}[];\n`; + str += `${sp}declare get${otherModelPlural}: Sequelize.BelongsToManyGetAssociationsMixin<${otherModel}>;\n`; + str += `${sp}declare set${otherModelPlural}: Sequelize.BelongsToManySetAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare add${otherModelSingular}: Sequelize.BelongsToManyAddAssociationMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare add${otherModelPlural}: Sequelize.BelongsToManyAddAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare create${otherModelSingular}: Sequelize.BelongsToManyCreateAssociationMixin<${otherModel}>;\n`; + str += `${sp}declare remove${otherModelSingular}: Sequelize.BelongsToManyRemoveAssociationMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare remove${otherModelPlural}: Sequelize.BelongsToManyRemoveAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare has${otherModelSingular}: Sequelize.BelongsToManyHasAssociationMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare has${otherModelPlural}: Sequelize.BelongsToManyHasAssociationsMixin<${otherModel}, ${otherModel}Id>;\n`; + str += `${sp}declare count${otherModelPlural}: Sequelize.BelongsToManyCountAssociationsMixin;\n`; needed[otherTable] ??= new Set(); needed[otherTable].add(otherModel); needed[otherTable].add(`${otherModel}Id`); @@ -692,13 +692,13 @@ export class AutoGenerator { private addTypeScriptFields(table: string, isInterface: boolean) { const sp = this.space[1]; const fields = _.keys(this.tables[table]); - const notNull = isInterface ? '' : '!'; + const isClass = !isInterface; let str = ''; fields.forEach(field => { if (!this.options.skipFields || !this.options.skipFields.includes(field)){ const name = this.quoteName(recase(this.options.caseProp, field)); const isOptional = this.getTypeScriptFieldOptional(table, field); - str += `${sp}${name}${isOptional ? '?' : notNull}: ${this.getTypeScriptType(table, field)};\n`; + str += `${sp}${isClass ? 'declare ': ''}${name}${isOptional ? '?' : ''}: ${this.getTypeScriptType(table, field)};\n`; } }); return str;