在数据库中更新

让我们从数据库加载出 photo,更新并保存到数据库:

  1. import { createConnection } from "typeorm";
  2. import { Photo } from "./entity/Photo";
  3. createConnection(/*...*/)
  4. .then(async connection => {
  5. /*...*/
  6. let photoToUpdate = await photoRepository.findOne(1);
  7. photoToUpdate.name = "Me, my friends and polar bears";
  8. await photoRepository.save(photoToUpdate);
  9. })
  10. .catch(error => console.log(error));

这个id = 1的 photo 在数据库中就成功更新了。