Last active 1729145553

knox revised this gist 1729145553. Go to revision

1 file changed, 34 insertions

TypeORM-readme-30.sh(file created)

@@ -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 + )
Newer Older