TypeORM-readme-25.ts
· 915 B · TypeScript
Orginalformat
import { Photo } from "./entity/Photo"
import { AppDataSource } from "./index"
const photoRepository = AppDataSource.getRepository(Photo)
const allPhotos = await photoRepository.find()
console.log("All photos from the db: ", allPhotos)
const firstPhoto = await photoRepository.findOneBy({
id: 1,
})
console.log("First photo from the db: ", firstPhoto)
const meAndBearsPhoto = await photoRepository.findOneBy({
name: "Me and Bears",
})
console.log("Me and Bears photo from the db: ", meAndBearsPhoto)
const allViewedPhotos = await photoRepository.findBy({ views: 1 })
console.log("All viewed photos: ", allViewedPhotos)
const allPublishedPhotos = await photoRepository.findBy({ isPublished: true })
console.log("All published photos: ", allPublishedPhotos)
const [photos, photosCount] = await photoRepository.findAndCount()
console.log("All photos: ", photos)
console.log("Photos count: ", photosCount)
1 | import { Photo } from "./entity/Photo" |
2 | import { AppDataSource } from "./index" |
3 | |
4 | const photoRepository = AppDataSource.getRepository(Photo) |
5 | const allPhotos = await photoRepository.find() |
6 | console.log("All photos from the db: ", allPhotos) |
7 | |
8 | const firstPhoto = await photoRepository.findOneBy({ |
9 | id: 1, |
10 | }) |
11 | console.log("First photo from the db: ", firstPhoto) |
12 | |
13 | const meAndBearsPhoto = await photoRepository.findOneBy({ |
14 | name: "Me and Bears", |
15 | }) |
16 | console.log("Me and Bears photo from the db: ", meAndBearsPhoto) |
17 | |
18 | const allViewedPhotos = await photoRepository.findBy({ views: 1 }) |
19 | console.log("All viewed photos: ", allViewedPhotos) |
20 | |
21 | const allPublishedPhotos = await photoRepository.findBy({ isPublished: true }) |
22 | console.log("All published photos: ", allPublishedPhotos) |
23 | |
24 | const [photos, photosCount] = await photoRepository.findAndCount() |
25 | console.log("All photos: ", photos) |
26 | console.log("Photos count: ", photosCount) |