0

I'm creating an Import function for our Software, where Excel Files can be used to import Data to the System. We work with objection.js (create Tool) and I'm using insertGraph() to insert content. My problem is I only want to import Relations when there is data provided. How can I make insertGraph conditinal?

.insertGraph(
                  [
                      {
                          '#id': 'boat',
                          ...arrayList[i],
                          insertedBy: req.user.sub,
                          berthAssignments: [
                              {
                                  insertedBy: req.user.sub,
                                  from: relationsData[i].ownershipSince,
  
                                  boat: [
                                      {
                                          id: '#ref{boat.id}'
                                      }
                                  ],
                                  berth: [
                                      {
                                          id: berthId[0]?.id
                                      }
                                  ]
                              }
                          ],
                          electricMeterAssignments: [
                              {
                                  insertedBy: req.user.sub,
                                  from: relationsData[i].electricMeterSince,
  
                                  electricMeter: [
                                      {
                                          id: meterId[0]?.id
                                      }
                                  ],
                                  boat: [
                                      {
                                          id: '#ref{boat.id}'
                                      }
                                  ]
                              }
                          ],
                          winterStorageAssignments: [
                              {
                                  insertedBy: req.user.sub,
                                  from: relationsData[i].winterStorageSince,
  
                                  winterStorage: [
                                      {
                                          id: storageId[0]?.id
                                      }
                                  ],
                                  boat: [
                                      {
                                          id: '#ref{boat.id}'
                                      }
                                  ]
                              }
                          ]
  
                      }
                  ],
                  {
  
                      relate: true,
                      allowRefs: true
                  }

I also tried to do it the traditional way with insert and if condition, but this also not working correctly:

  .insert(arrayList[i])
        .then(async insertedBoat => {
            if (relationsData[i].berthHandle) {
                console.log('berthHandle')
                await BerthAssignment
                    .query(req.dbConnection)
                    .insert({
                        bcm_berths_id: berthId[0].id,
                        bcm_boats_id: insertedBoat.id,
                        from: relationsData[i].berthSince,
                        insertedBy: req.user.sub,
                    })
                    .catch((error) => { console.log(error); throw next(error); });
            } else if (relationsData[i].electricMeters) {
                console.log('electricMeter')

                await ElectricMeterBoat
                    .query(req.dbConnection)
                    .insert({
                        bcm_electric_meters_id: meterId[0].id,
                        bcm_boats_id: insertedBoat.id,
                        from: relationsData[i].electricMeterSince,
                        insertedBy: req.user.sub,
                    })
                    .catch((error) => { console.log(error); throw next(error); });
            } else if (relationsData[i].winterStoragehandle) {
                console.log('Hallo')

                await WinterStorageAssignment
                    .query(req.dbConnection)
                    .insert({
                        bcm_winter_storages_id: storageId[0].id,
                        bcm_boats_id: insertedBoat.id,
                        from: relationsData[i].winterStorageSince,
                        insertedBy: req.user.sub,
                    })
                    .catch((error) => { console.log(error); throw next(error); });
            } else if (owner !== null) {
                console.log('asdflksdafsdaf')
                await BoatOwner
                    .query(req.dbConnection)
                    .delete()
                    .where({
                        bcm_boats_id: insertedBoat.id
                    })
                    .catch(() => null);
                await BoatOwner
                    .query(req.dbConnection)
                    .insert({
                        bcm_boats_id: insertedBoat.id,
                        bcm_persons_id: personId[0]?.id,
                        // bcm_companies_id: companyId[0]?.id,
                        isOwnerSince: relationsData[i]?.ownershipSince,
                        insertedBy: req.user.sub
                    })
                    .catch((error) => { console.log(error); throw next(error); });
            }
        })

Someone can help me please? Thanks a lot!

skuettel
  • 13
  • 2

1 Answers1

0

your friend would be upsertGraph

i used it once for for an application that required some deep nested inserts / updates and it was as simple as .upsertGraph(data, {insertMissing: true})

but you can have more specific options on what happens to which relation when using it, from the docs:

const options = {
  // Only enable `unrelate` functionality for these two paths.
  unrelate: ['pets', 'movies.reviews'],
  // Only enable `relate` functionality for 'movies' relation.
  relate: ['movies'],
  // Disable deleting for movies.
  noDelete: ['movies']
};
M-Raw
  • 779
  • 4
  • 10