TypeORM エンティティ/テーブルの更新 質問する

TypeORM エンティティ/テーブルの更新 質問する

これは私のユーザー エンティティです。

@PrimaryGeneratedColumn()
userId: number;

@Column({type:"varchar", length:"300"})
userName: string;

@OneToOne(type => UserProfile, {cascadeAll:true})
@JoinColumn()
userProfile: UserProfile;

@OneToOne(type => UserCredential, {cascadeAll:true, eager:true})
@JoinColumn()
userCredential: UserCredential;

@OneToOne(type => BusinessUnit, {cascadeAll:true})
@JoinColumn()
businessUnit: BusinessUnit;

@ManyToMany(type => ProductCategory)
@JoinTable()
productCategory: ProductCategory[];

これは私が更新したい新しいデータです:

User {
  userName: '[email protected]',
  userProfile: 
   UserProfile {
     firstName: 'dcds',
     lastName: 'Faiz',
     mobile: '42423423',
     addressLine1: 'Delhi',
     addressLine2: 'Delhi',
     city: '-',
     country: '-',
     zipCode: '234243',
     homeTelephone: '-',
     dayOfBirth: 0,
     monthOfBirth: 0,
     yearOfBirth: 0 },
  userCredential: UserCredential { credential: 'abcd@123' } }

ユーザーをユーザーIDで検索しています。

return await getManager()
               .createQueryBuilder(User, "user")
               .where("user.userId = :id", {id})
               .getOne();

上記のクエリの結果は次のようになります:

User {
  createdDate: 2018-03-29T06:45:16.322Z,
  updatedDate: 2018-04-01T06:28:24.171Z,
  userId: 1,
  userName: '[email protected]' }

ユーザーテーブルと関連テーブルを更新したい

manager.save(user)

既存の行を更新する代わりに、テーブルに新しい行を挿入します。すべての列を手動で更新せずに、ユーザー テーブル全体と関連テーブルを更新する方法はありますか? このタスクを次のように実行したくありません。

let user = await userRepositiory.findOneById(1);
user.userName = "Me, my friends and polar bears";
await userRepository.save(user);

let userProfile = await userProfileRepository.findOneById(1);
 userProfile.firstName = "";
 userProfile.lastName = "";
    ....// etc
 await userRepository.save(userProfile);

 // and so on update other tables.

ベストアンサー1

次の方法を試してください:

await this. userRepository.update(
                user.id ,
                user
              );

const updatedUser = await this.repository.findOne(user.id);            

おすすめ記事