• 从数据库加载

    从数据库加载

    让我们使用 Repository 尝试更多的加载操作:

    1. import { createConnection } from "typeorm";
    2. import { Photo } from "./entity/Photo";
    3. createConnection(/*...*/)
    4. .then(async connection => {
    5. /*...*/
    6. let allPhotos = await photoRepository.find();
    7. console.log("All photos from the db: ", allPhotos);
    8. let firstPhoto = await photoRepository.findOne(1);
    9. console.log("First photo from the db: ", firstPhoto);
    10. let meAndBearsPhoto = await photoRepository.findOne({ name: "Me and Bears" });
    11. console.log("Me and Bears photo from the db: ", meAndBearsPhoto);
    12. let allViewedPhotos = await photoRepository.find({ views: 1 });
    13. console.log("All viewed photos: ", allViewedPhotos);
    14. let allPublishedPhotos = await photoRepository.find({ isPublished: true });
    15. console.log("All published photos: ", allPublishedPhotos);
    16. let [allPhotos, photosCount] = await photoRepository.findAndCount();
    17. console.log("All photos: ", allPhotos);
    18. console.log("Photos count: ", photosCount);
    19. })
    20. .catch(error => console.log(error));