- 从数据库加载
从数据库加载
让我们使用 Repository 尝试更多的加载操作:
import { createConnection } from "typeorm";import { Photo } from "./entity/Photo";createConnection(/*...*/).then(async connection => {/*...*/let allPhotos = await photoRepository.find();console.log("All photos from the db: ", allPhotos);let firstPhoto = await photoRepository.findOne(1);console.log("First photo from the db: ", firstPhoto);let meAndBearsPhoto = await photoRepository.findOne({ name: "Me and Bears" });console.log("Me and Bears photo from the db: ", meAndBearsPhoto);let allViewedPhotos = await photoRepository.find({ views: 1 });console.log("All viewed photos: ", allViewedPhotos);let allPublishedPhotos = await photoRepository.find({ isPublished: true });console.log("All published photos: ", allPublishedPhotos);let [allPhotos, photosCount] = await photoRepository.findAndCount();console.log("All photos: ", allPhotos);console.log("Photos count: ", photosCount);}).catch(error => console.log(error));
