TypeORM-readme-30.sh
· 989 B · Bash
Orginalformat
import { Photo } from "./entity/Photo"
import { PhotoMetadata } from "./entity/PhotoMetadata"
// create a photo
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
// create a photo metadata
const metadata = new PhotoMetadata()
metadata.height = 640
metadata.width = 480
metadata.compressed = true
metadata.comment = "cybershoot"
metadata.orientation = "portrait"
metadata.photo = photo // this way we connect them
// get entity repositories
const photoRepository = AppDataSource.getRepository(Photo)
const metadataRepository = AppDataSource.getRepository(PhotoMetadata)
// first we should save a photo
await photoRepository.save(photo)
// photo is saved. Now we need to save a photo metadata
await metadataRepository.save(metadata)
// done
console.log(
"Metadata is saved, and the relation between metadata and photo is created in the database too",
)
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 | ) |