Skip to content

Define Embeddables

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

1
foodstore > data > datastores > foodstore > address.yaml
2
---
3
embeddables:
4
- name: address
5
fields:
6
- name: lane1
7
type: string
8
validations:
9
- type: required
10
- name: lane2
11
type: string
12
- name: city
13
type: string
14
validations:
15
- type: required
16
- type: length
17
min: 3
18
max: 85

outlets.yaml

1
foodstore > data > datastores > foodstore > outlets.yaml
2
3
entities:
4
- name: outlets
5
fields:
6
- name: address
7
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

1
foodstore > data > datastores > foodstore > outlets.yaml
2
3
entities:
4
- name: outlets
5
fields:
6
- name: pastaddress
7
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

1
foodstore > data > datastores > pincode.yaml
2
---
3
embeddables:
4
- name: pincode
5
fields:
6
- name: pincode
7
type: string
8
validations:
9
- type: required
10
- type: regex
11
expression: ^\d{6}(?:[-\s]\d{4})?$

address.yaml

1
foodstore > data > datastores > address.yaml
2
---
3
embeddables:
4
- name: address
5
- name: code
6
type: pincode