添加和插入 photo

现在创建一个新的 photo 存到数据库:

  1. import { createConnection } from "typeorm";
  2. import { Photo } from "./entity/Photo";
  3. createConnection(/*...*/)
  4. .then(connection => {
  5. let photo = new Photo();
  6. photo.name = "Me and Bears";
  7. photo.description = "I am near polar bears";
  8. photo.filename = "photo-with-bears.jpg";
  9. photo.views = 1;
  10. photo.isPublished = true;
  11. return connection.manager.save(photo).then(photo => {
  12. console.log("Photo has been saved. Photo id is", photo.id);
  13. });
  14. })
  15. .catch(error => console.log(error));

保存实体后,它将获得新生成的 ID。 save方法返回传递给它的同一对象的实例。但它不是对象的新副本,只是修改了它的”id”并返回它。