Browse Source

query-node: bugfix load entity relations for MediaLocationEntity

metmirr 4 years ago
parent
commit
a5c1266534
1 changed files with 7 additions and 12 deletions
  1. 7 12
      query-node/mappings/content-directory/get-or-create.ts

+ 7 - 12
query-node/mappings/content-directory/get-or-create.ts

@@ -374,24 +374,19 @@ async function mediaLocation(
   location: IReference,
   nextEntityIdBeforeTransaction: number
 ): Promise<MediaLocationEntity> {
-  let loc: MediaLocationEntity | undefined
   const { entityId, existing } = location
+  // Relationships to be loaded
+  const relations = ['httpMediaLocation', 'joystreamMediaLocation']
   if (existing) {
-    loc = await db.get(MediaLocationEntity, { where: { id: entityId.toString() } })
+    const loc = await db.get(MediaLocationEntity, { where: { id: entityId.toString() }, relations })
     if (!loc) throw Error(`MediaLocation entity not found`)
     return loc
   }
+  // Could be created in the same transaction so try to query
   const id = generateEntityIdFromIndex(nextEntityIdBeforeTransaction + entityId)
-
-  // could be created in the transaction
-  loc = await db.get(MediaLocationEntity, {
-    where: { id },
-    relations: ['httpMediaLocation', 'joystreamMediaLocation'],
-  })
-  if (loc) {
-    return loc
-  }
-
+  const loc = await db.get(MediaLocationEntity, { where: { id }, relations })
+  if (loc) return loc
+  // Create entity
   const { properties } = findEntity(entityId, 'MediaLocation', classEntityMap)
   return await createMediaLocation(
     { db, block, id },