Loading from the database

Let’s try more load operations using the Repository:

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