Define Embeddables
- Data
- Datastore
2 min read | Last updated :
Embeddables are data elements which are not entities but are embedded in an entity in jsonb format. The primary purpose of embeddables is to provide the flexiblity of validations on jsonb fields.
Example 1 - Simple Embeddable
Let’s now define an embeddable called address and add the address field to outlets entity.
address.yaml
1foodstore > data > datastores > foodstore > address.yaml2---3embeddables:4- name: address5 fields:6 - name: lane17 type: string8 validations:9 - type: required10 - name: lane211 type: string12 - name: city13 type: string14 validations:15 - type: required16 - type: length17 min: 318 max: 85
outlets.yaml
1foodstore > data > datastores > foodstore > outlets.yaml2
3entities:4- name: outlets5 fields:6 - name: address7 type: address
Example 2 - Embeddable as an Array
Let’s now add a field called past_addresses to outlets entity to track the history of all locations for the outlet.
To achieve this, let’s add address
as an array.
outlets.yaml
1foodstore > data > datastores > foodstore > outlets.yaml2
3entities:4- name: outlets5 fields:6 - name: pastaddress7 type: array(address)
Example 3 - Nested Embeddable
We can recursively call an embeddables to other embeddable.
Let’s define a embeddable called pincode and use it in the address embeddable.
pincode.yaml
1foodstore > data > datastores > pincode.yaml2---3embeddables:4- name: pincode5 fields:6 - name: pincode7 type: string8 validations:9 - type: required10 - type: regex11 expression: ^\d{6}(?:[-\s]\d{4})?$
address.yaml
1foodstore > data > datastores > address.yaml2---3embeddables:4- name: address5 - name: code6 type: pincode