Skip to content

Entity

The entity is the core component based on your entity schema. Torpedo generates an object class from your schema. This class will contain the schema fields as class attributes and let you interact with these via getter and setter methods.

Additionally, the next attributes are added to the class:

Field Type Description
id string It is required and is configured as UUID or ULID
created timestamp It is autogenerated and handled by the CRUD operations
updated timestamp It is autogenerated and handled by the CRUD operations

How Torpedo works with the Entity

Once that you have finished your entity schema and after generate your code, Torpedo will create 2 entity classes. The EntityBase and the Entity. The first one is the class that contains the autogenerated code wit all CRUD operations and the Query method.

The Entity class will inherit from the EntityBase letting you add custom logic at Entity level.

Entity and EntityBase code generation

Each time that you run the code generator the EntityBase class will be overwriten, however the Entity class will be keep avoiding overwite your code.

Sample: Author Entity

Having a Blog post website we define the post's author entity schema as:

version: torpedo.darksub.io/v1.0
kind: entity
spec:
  name: "author"
  plural: "authors"
  description: "The blog post author"

  schema:
    reserved:
      # By default, an ID is assigned as ULID format (string).
      # the field name id is reserved, but you can configure it from this section
      id:
        type: ulid

    fields:
      - name: name
        type: string
        description: "The author full name"
        doc: "The author full name"

      - name: email
        type: string
        description: "The author contact email"
        doc: "The author contact email"

Once that we have defined the Entity Schema and executed the torpedo command to generate the code. The outcome will be:

classDiagram
  AuthorBase <|-- Author
  AuthorBase : String id
  AuthorBase : Int created
  AuthorBase : Int updated
  AuthorBase : String name
  AuthorBase : String email
  AuthorBase: +GetId()
  AuthorBase: +GetCreated()
  AuthorBase: +GetUpdated()
  AuthorBase: +SetName(String name)
  AuthorBase: +GetName()
  AuthorBase: +SetEmail(String email)
  AuthorBase: +GetEmail()

As developer

As a developer your own business logic MUST BE written into the Author class in order to avoid that Torpedo code generation tool overwrite your code!

We strongly recommend write your uses cases into the Service class and not as part of the Entity. The Entity object let you expands your data model adding new fields that are not supported by Torpedo.