TypeORM-readme-46.ts
· 751 B · TypeScript
Ham
import { AppDataSource } from "./index"
// create a few albums
const album1 = new Album()
album1.name = "Bears"
await AppDataSource.manager.save(album1)
const album2 = new Album()
album2.name = "Me"
await AppDataSource.manager.save(album2)
// create a few photos
const 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
photo.albums = [album1, album2]
await AppDataSource.manager.save(photo)
// now our photo is saved and albums are attached to it
// now lets load them:
const loadedPhoto = await AppDataSource.getRepository(Photo).findOne({
where: {
id: 1,
},
relations: {
albums: true,
},
})
1 | import { AppDataSource } from "./index" |
2 | |
3 | // create a few albums |
4 | const album1 = new Album() |
5 | album1.name = "Bears" |
6 | await AppDataSource.manager.save(album1) |
7 | |
8 | const album2 = new Album() |
9 | album2.name = "Me" |
10 | await AppDataSource.manager.save(album2) |
11 | |
12 | // create a few photos |
13 | const photo = new Photo() |
14 | photo.name = "Me and Bears" |
15 | photo.description = "I am near polar bears" |
16 | photo.filename = "photo-with-bears.jpg" |
17 | photo.views = 1 |
18 | photo.isPublished = true |
19 | photo.albums = [album1, album2] |
20 | await AppDataSource.manager.save(photo) |
21 | |
22 | // now our photo is saved and albums are attached to it |
23 | // now lets load them: |
24 | const loadedPhoto = await AppDataSource.getRepository(Photo).findOne({ |
25 | where: { |
26 | id: 1, |
27 | }, |
28 | relations: { |
29 | albums: true, |
30 | }, |
31 | }) |