Skip to content

Entities

Copy of sumaris-app/.agents/rules/entities.md (rule written for AI coding agents, when creating or modifying TypeScript entity classes). Do not edit here: run npm run site:sync-rules to update it.

Here are the generation constraints for a TypeScript class:

  • TypeScript Class
    • Add the annotation @EntityClass({typename: 'MyClassVO'})
      • Replace 'MyClassVO' with the current class and add the "VO" suffix.
    • Add class parameters <T, ID, AO, FO> with:
      • T as the default class being the class itself
      • ID as the default type number
      • AO as the default type EntityAsObjectOptions
      • FO as the default type any
    • Extend the class Entity<T, ID>
      • The attributes id and updateDate are present in the parent class Entity and do not need to be added to the derived class.
    • Add a static method: static fromObject(source: any, options?: any) => MyClass
    • Add a constructor
  • Add two methods asObject() and fromObject() defined as follows
      asObject(opts?: AO): any {
        const target: any = super.asObject(opts);
        // Can be completed here, if needed
    
        // Dates must explicitly be converted to `string` using the static method `toDateISOString`, for example:
        target.myDate = toDateISOString(this.myDate);
        // Attributes that are entities must also be converted via their `asObject()` function, for example:
        target.myEntity = this.myEntity && this.myEntity.asObject(opts);
        return target;
      }
    
      fromObject(source: any, opts?: FO) {
          super.fromObject(source, opts);
          // List all the class attributes here, copied from the `source` parameter.
          this.myAttribute = source.myAttribute;
          // Dates should use the `Moment` class and be copied in `fromObject()` using the static method `fromDateISOString`, for example:
          this.myDate = fromDateISOString(source.<myDate>)
          // Related entities should be copied via their static function `Entity1.fromObject(entity1, opts)`, for example:
          this.myEntity = Entity1.fromObject(source.myEntity);
      }