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: runnpm run site:sync-rulesto 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:Tas the default class being the class itselfIDas the default typenumberAOas the default typeEntityAsObjectOptionsFOas the default typeany
- Extend the class Entity<T, ID>
- The attributes id and updateDate are present in the parent class
Entityand do not need to be added to the derived class.
- The attributes id and updateDate are present in the parent class
- Add a static method:
static fromObject(source: any, options?: any) => MyClass - Add a constructor
- Add the annotation @EntityClass({typename: 'MyClassVO'})
- Add two methods
asObject()andfromObject()defined as followsasObject(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); }