最后活跃于 1729145553

TypeORM-readme-30.sh 原始文件
1import { Photo } from "./entity/Photo"
2import { PhotoMetadata } from "./entity/PhotoMetadata"
3
4// create a photo
5const photo = new Photo()
6photo.name = "Me and Bears"
7photo.description = "I am near polar bears"
8photo.filename = "photo-with-bears.jpg"
9photo.views = 1
10photo.isPublished = true
11
12// create a photo metadata
13const metadata = new PhotoMetadata()
14metadata.height = 640
15metadata.width = 480
16metadata.compressed = true
17metadata.comment = "cybershoot"
18metadata.orientation = "portrait"
19metadata.photo = photo // this way we connect them
20
21// get entity repositories
22const photoRepository = AppDataSource.getRepository(Photo)
23const metadataRepository = AppDataSource.getRepository(PhotoMetadata)
24
25// first we should save a photo
26await photoRepository.save(photo)
27
28// photo is saved. Now we need to save a photo metadata
29await metadataRepository.save(metadata)
30
31// done
32console.log(
33 "Metadata is saved, and the relation between metadata and photo is created in the database too",
34)