- 添加和插入 photo
添加和插入 photo
现在创建一个新的 photo 存到数据库:
import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";
createConnection(/*...*/)
.then(connection => {
let photo = new Photo();
photo.name = "Me and Bears";
photo.description = "I am near polar bears";
photo.filename = "photo-with-bears.jpg";
photo.views = 1;
photo.isPublished = true;
return connection.manager.save(photo).then(photo => {
console.log("Photo has been saved. Photo id is", photo.id);
});
})
.catch(error => console.log(error));
保存实体后,它将获得新生成的 ID。 save
方法返回传递给它的同一对象的实例。但它不是对象的新副本,只是修改了它的”id”并返回它。