Browse Source

query-node: throw error if entity id is not in ClassEntity table

metmirr 4 years ago
parent
commit
8d77114c41

+ 2 - 2
query-node/mappings/content-directory/content-dir-consts.ts

@@ -1,4 +1,4 @@
-import { IPropertyWithId } from '../types'
+import { IKnownClass, IPropertyWithId } from '../types'
 
 // Content directory predefined class names
 export enum ContentDirectoryKnownClasses {
@@ -18,7 +18,7 @@ export enum ContentDirectoryKnownClasses {
 }
 
 // Predefined content-directory classes, classId may change after the runtime seeding
-export const contentDirectoryClassNamesWithId: { classId: number; name: string }[] = [
+export const contentDirectoryClassNamesWithId: IKnownClass[] = [
   { name: ContentDirectoryKnownClasses.CHANNEL, classId: 1 },
   { name: ContentDirectoryKnownClasses.CATEGORY, classId: 2 },
   { name: ContentDirectoryKnownClasses.HTTPMEDIALOCATION, classId: 3 },

+ 4 - 5
query-node/mappings/content-directory/entity/index.ts

@@ -91,7 +91,7 @@ async function contentDirectory_EntitySchemaSupportAdded(db: DB, event: Substrat
   const { blockNumber: block } = event
   const entityId = decode.stringIfyEntityId(event)
 
-  const knownClass = await getKnownClass(db, { where: { id: entityId } })
+  const [knownClass] = await getKnownClass(db, { where: { id: entityId } })
   if (!knownClass) return
 
   const arg: IDBBlockId = { db, block, id: entityId }
@@ -184,7 +184,7 @@ async function contentDirectory_EntityRemoved(db: DB, event: SubstrateEvent): Pr
   const entityId = decode.stringIfyEntityId(event)
   const where: IWhereCond = { where: { id: entityId } }
 
-  const knownClass = await getKnownClass(db, where)
+  const [knownClass, classEntity] = await getKnownClass(db, where)
   if (!knownClass) return
 
   switch (knownClass.name) {
@@ -242,8 +242,7 @@ async function contentDirectory_EntityRemoved(db: DB, event: SubstrateEvent): Pr
     default:
       throw new Error(`Unknown class name: ${knownClass.name}`)
   }
-  const ce = await db.get(ClassEntity, where)
-  if (ce) await db.remove<ClassEntity>(ce)
+  await db.remove<ClassEntity>(classEntity)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -275,7 +274,7 @@ async function contentDirectory_EntityPropertyValuesUpdated(db: DB, event: Subst
   const entityId = decode.stringIfyEntityId(event)
   const where: IWhereCond = { where: { id: entityId } }
 
-  const knownClass = await getKnownClass(db, where)
+  const [knownClass] = await getKnownClass(db, where)
   if (!knownClass) return
 
   // TODO: change setProperties method signature to accecpt SubstrateExtrinsic, then remove the following

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

@@ -36,6 +36,7 @@ import {
   IEntity,
   IHttpMediaLocation,
   IJoystreamMediaLocation,
+  IKnownClass,
   IKnownLicense,
   ILanguage,
   ILicense,
@@ -426,16 +427,15 @@ async function video(
   )
 }
 
-export async function getKnownClass(db: DB, where: IWhereCond): Promise<{ classId: number; name: string } | undefined> {
+export async function getKnownClass(db: DB, where: IWhereCond): Promise<[IKnownClass | undefined, ClassEntity]> {
   const ce = await db.get(ClassEntity, where)
   if (!ce) {
-    console.log(`Class not found for the EntityId: ${where.where.id} or the entity has not been created.`)
-    return
+    throw Error(`Class not found for the EntityId: ${where.where.id} or the entity has not been created.`)
   }
 
   const knownClass = contentDirectoryClassNamesWithId.find((c) => c.classId === ce.classId)
   if (!knownClass) console.log('Unknown class')
-  return knownClass
+  return [knownClass, ce]
 }
 
 export const getOrCreate = {

+ 5 - 0
query-node/mappings/types.ts

@@ -202,3 +202,8 @@ export type ClassEntityMap = Map<string, IEntity[]>
 export interface IFeaturedVideo {
   video?: IReference
 }
+
+export interface IKnownClass {
+  name: string
+  classId: number
+}