Procházet zdrojové kódy

Handling entities without schema

Leszek Wiesner před 4 roky
rodič
revize
6f3d263e71

+ 21 - 1
cli/src/base/ContentDirectoryCommandBase.ts

@@ -144,7 +144,12 @@ export default abstract class ContentDirectoryCommandBase extends AccountsComman
     return group
   }
 
-  async getEntity(id: string | number, requiredClass?: string, ownerMemberId?: number): Promise<Entity> {
+  async getEntity(
+    id: string | number,
+    requiredClass?: string,
+    ownerMemberId?: number,
+    requireSchema = true
+  ): Promise<Entity> {
     if (typeof id === 'string') {
       id = parseInt(id)
     }
@@ -172,6 +177,10 @@ export default abstract class ContentDirectoryCommandBase extends AccountsComman
       })
     }
 
+    if (requireSchema && !entity.supported_schemas.toArray().length) {
+      this.error(`${requiredClass || ''}Entity of id ${id} has no schema support added!`)
+    }
+
     return entity
   }
 
@@ -268,10 +277,21 @@ export default abstract class ContentDirectoryCommandBase extends AccountsComman
     ownerMemberId?: number
   ): Promise<Record<string, string>[]> {
     const [classId, entityClass] = await this.classEntryByNameOrId(className)
+    // Create object of default "[not set]" values (prevents breaking the table if entity has no schema support)
+    const defaultValues = entityClass.properties
+      .map((p) => p.name.toString())
+      .reduce((d, propName) => {
+        if (includedProps?.includes(propName)) {
+          d[propName] = chalk.grey('[not set]')
+        }
+        return d
+      }, {} as Record<string, string>)
+
     const entityEntries = await this.entitiesByClassAndOwner(classId.toNumber(), ownerMemberId)
     const parsedEntities = (await Promise.all(
       entityEntries.map(([id, entity]) => ({
         'ID': id.toString(),
+        ...defaultValues,
         ..._.mapValues(this.parseEntityPropertyValues(entity, entityClass, includedProps), (v) =>
           v.value.toJSON() === false && v.type !== 'Single<Bool>' ? chalk.grey('[not set]') : v.value.toString()
         ),

+ 1 - 1
cli/src/commands/content-directory/entity.ts

@@ -15,7 +15,7 @@ export default class EntityCommand extends ContentDirectoryCommandBase {
 
   async run() {
     const { id } = this.parse(EntityCommand).args
-    const entity = await this.getEntity(id)
+    const entity = await this.getEntity(id, undefined, undefined, false)
     const { controller, frozen, referenceable } = entity.entity_permissions
     const [classId, entityClass] = await this.classEntryByNameOrId(entity.class_id.toString())
     const propertyValues = this.parseEntityPropertyValues(entity, entityClass)