Is it possible to attach each array of images to their post using the foreign key placePlaceId
? For example, in the first object with place_id:3
, I want to attach all the images with the foreign key placePlaceId:3
.
This is my approach to achieving this goal
async getPlaces() {
let imagesList: Images[] = [];
const places = await this.repo.find();
const imageQuery = await this.imagesRepo
.createQueryBuilder()
.select('*')
.getRawMany();
places.forEach((place, index: number) => {
for(let i = 0; i < imageQuery.length; i++){
place.place_id === imageQuery[i].placePlaceId
? imagesList.push(imageQuery[i])
: 0;
}
place.images = imagesList;
this.repo.save(place);
console.log(place.place_id, imageQuery);
});
console.log(imagesList, 'imagesList');
return places;
}
Current Result
[
{
"place_id": 3,
"title": "البيك",
"description": "الذ مطعم سعودي",
"signature": "المسحب و البروست",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 4,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 5,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 6,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
{
"image_id": 7,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
]
]
Expected Result
[
{
"place_id": 3,
"title": "البيك",
"description": "الذ مطعم سعودي",
"signature": "المسحب و البروست",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 4,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
{
"image_id": 5,
"image": "http://eatnstays.com/Arabic/wp-content/uploads/2021/06/20180216_134034-12.jpg",
"image_owner": "google",
"placePlaceId": 3
},
],
{
"place_id": 5,
"title": "اثراء",
"description": "مركز الملك عبدالعزيز العالمي",
"signature": "شامل",
"isFavorite": false,
"approved": false,
"phone": null,
"website": null,
"instagram": null,
"Sunday": "4-11",
"Monday": "4-11",
"Tuesday": "4-11",
"Wednesday": "4-11",
"Thursday": "4-11",
"Friday": "4-11",
"Saturday": "4-11",
"images": [
{
"image_id": 6,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
{
"image_id": 7,
"image": "https://i.ytimg.com/vi/Fak54PuiW9g/maxresdefault.jpg",
"image_owner": "google",
"placePlaceId": 5
},
]
]