TypeORM-readme-38.ts
· 702 B · TypeScript
Неформатований
import { AppDataSource } from "./index"
// create photo object
const photo = new Photo()
photo.name = "Me and Bears"
photo.description = "I am near polar bears"
photo.filename = "photo-with-bears.jpg"
photo.isPublished = true
// create photo metadata object
const metadata = new PhotoMetadata()
metadata.height = 640
metadata.width = 480
metadata.compressed = true
metadata.comment = "cybershoot"
metadata.orientation = "portrait"
photo.metadata = metadata // this way we connect them
// get repository
const photoRepository = AppDataSource.getRepository(Photo)
// saving a photo also save the metadata
await photoRepository.save(photo)
console.log("Photo is saved, photo metadata is saved too.")
1 | import { AppDataSource } from "./index" |
2 | |
3 | // create photo object |
4 | const photo = new Photo() |
5 | photo.name = "Me and Bears" |
6 | photo.description = "I am near polar bears" |
7 | photo.filename = "photo-with-bears.jpg" |
8 | photo.isPublished = true |
9 | |
10 | // create photo metadata object |
11 | const metadata = new PhotoMetadata() |
12 | metadata.height = 640 |
13 | metadata.width = 480 |
14 | metadata.compressed = true |
15 | metadata.comment = "cybershoot" |
16 | metadata.orientation = "portrait" |
17 | |
18 | photo.metadata = metadata // this way we connect them |
19 | |
20 | // get repository |
21 | const photoRepository = AppDataSource.getRepository(Photo) |
22 | |
23 | // saving a photo also save the metadata |
24 | await photoRepository.save(photo) |
25 | |
26 | console.log("Photo is saved, photo metadata is saved too.") |