Naposledy aktivní 1729145553

knox revidoval tento gist 1729145553. Přejít na revizi

1 file changed, 34 insertions

TypeORM-readme-30.sh(vytvořil soubor)

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