Adding custom fields
As we have mentioned previously at Entity definition the below data type are supported by Torpedo as part of the code generation:
string
: represents a string data type.integer
: represents the integer numbers and it is mapped toint64
float
: represents the float numbers and it is mapped tofloat64
date
: represents a timestamp number and it is mapped toint64
boolean
: represents a boolean value and it is mapped tobool
However sometimes you will need to add, for instance, a list of string ([]string
) as custom data type. This one is not supported by auto-generation code,
but can be added manually intercepting the "entity points of contact" during the application life cycle. Thanks that we have based our App
on the Hexagonal Architectural pattern we have well identified those entity point of contact:
- DTO: Data Transfer Object are the data entry point, so, the
[]string
should be added as member of the entity DTO. - Entity model: The entity schema is generated from its yaml spec, however there is an Entity class to add the custom
[]string
. - DMO: Data Mapper Object are the data output point and defines how the entity field model should be stored. So,
[]string
must be added as member of the DMO object. - QRO: Query Result Object are the data result object for query (TQL) operations. So,
[]string
must be added as member of the QRO object.
Lets following this with an example!
Imagine that we are writing a software to measure and track values from measurement sensors. Our sensor data schema should be defined like:
.torpedo/entities/sensor.yaml
So, guessing that one of the use cases is: Save the last 5 sensor measurements we can tackle this creating the use case definition and coding it. But there is a faster way to do it, writing less code.
And the answer is yes!, extending the defined sensor entity with a new custom field, like: []float64
.
As we have discussed previously, an entity definition creates automatically its own CRUD actions, and we can extend those entity point of contact:
Entity data model object - Entity
The first step is adding the custom field as part of the entity data model.
./domain/entities/sensor/entity.go
- Measures slice
- Measures getter method
- Measures setter method
Http input object - DTO
Once that we have the entity data model updated with the new field, is time to add it into the application input data flow. In order to achieve this we need to extend the entity DTO object as like:
./domain/entities/sensor/inputs/http/gin/dto.go
- Adding measures as part of custom fields. Note the naming convention, starts with capital letter and ends with underscore
Measures_
. - Getter method needed to populate entity data model from DTO.
Storage mapper (output) object - DMO
So far we can send measures from the input data flow to the entity model, but now we need to save this info in our defined storage. At the beginning we have defined the storage output as a composition of MongoDB and Redis, also for testing purpose we have set Memory adapter as well. So, what we need to do is updating the DMO object on each storage adapter:
Remember
Remember update the DMO object of each storage adapter.
Memory
./domain/entities/sensor/outputs/memory/dmo.go
- Adding measures as part of custom fields. Note the naming convention, starts with capital letter and ends with underscore
Measures_
. - Getter method needed to populate entity data model from DMO when fetch from DB.
MongoDB
./domain/entities/sensor/outputs/mongodb/dmo.go
- Adding measures as part of custom fields. Note the naming convention, starts with capital letter and ends with underscore
Measures_
. - Getter method needed to populate entity data model from DMO when fetch from DB.
What happens with SQL adapter?
By design MongoDB supports arrays as data type, but with SQL engines this is a little different, some ones add it as JSON other ones
implements vectors. So, in order to keep this feature aligned alongside all supported SQL engines Torpedo introduces Array
data type as JSON strings
and following the sensor
example, the sql DMO should look like:
./domain/entities/sensor/outputs/sql/dmo.go
- Supported arrays:
JsonArrayFloat
JsonArrayInteger
JsonArrayString
JsonArrayDate
JsonArrayBoolean
Redis
./domain/entities/sensor/outputs/redis/dmo.go
- Adding measures as part of custom fields. Note the naming convention, starts with capital letter and ends with underscore
Measures_
. - Getter method needed to populate entity data model from DMO when fetch from DB.
Query result object - QRO
And last but no least, each time that we call the entity endpoint query ([POST] /api/v1/sensors/query
) the result object must know
how to map the measures []float64
slice from the extended entity data model.
./domain/entities/sensor/qro.go
- Adding measures as part of custom fields. Note the naming convention, starts with capital letter and ends with underscore
Measures_
.
Try it!
Running the application example, we can create a sensor with the following curl command:
The response should be something like:
And in order to verify that data has been saved, we can call a fetch:
The response would look like: