Entities
Entities are foundational blocks of building datastores and APIs. Each entity is mapped to a table in a relation database and to a collection in a Document database. Entities are defined in yaml, and are exposed by Data API block will expose them for CRUD (Create, Update, Retrieve, Delete) operations on REST, GraphQL protocols.
Defining Simple Entity
The simplest entity will have a name and few fields.
Default traits
All entities created on the kis.ai platform have the below fields, injected. into them through the traits kisai.common
and kisai.softdelete
, as they have the attribute applytoall
set to true
.
Trait | Field | Description |
---|---|---|
kisai.common | createdby | this will have the id of the user creating the current row of the entity |
kisai.common | createdon | this will have the timestamp when the user created the current row of the entity |
kisai.common | updatedby | this will have the id of the user updating the current row of the entity |
kisai.common | updatedon | this will have the timestamp when the user updated the current row of the entity |
kisai.softdelete | deletedby | this will have the id of the user soft deleted the current row of the entity |
kisai.softdelete | deletedon | this will have the timestamp when the user soft deleted the current row of the entity |
YAML Definition of common traits
Default Ordering
When querying the data for an entity, it by default ordered by id. However, default ordering by another fields might be needed when fetching multiple rows of an entity. Here default-order
ensures that data retrieved always follows the default order.
Evaluation Lifecycle
Entity has its own internal lifecycle and has an evaluation sequence. Every time a row is created or updated, this lifecycle of evaluation occurs
Default Values
Fields can have either static or dynamic default values. In the below example, ulid() function gives a new ulid every time its called, or a static value New York
Common Default Functions
Function | Description |
---|---|
ulid() | gives new ULIDs |
currentTimestamp() | gives the current timestamp |
contextget(“user”) | returns the current user id from the request context |
Idempotent
Entities can be marked as idempotent by adding idempotent
is true, then the id field is not generated on the server side and it is expected from the client side.
Computed fields
Computed fields allow developers to create dynamic fields, whose values are not provided by the user but are computed from other fields or functions. In the example below takehome
is a computed field.
Transforms
Most times the input values need some transformations before inserting into the datastore, like trimming, capitalizing or replace symbols etc. Listed below are all the supported transforms.
Field Datatype | Payload Datatype | Transform | Examples |
---|---|---|---|
string | Any | tostring - convert any type to string | |
string | string |
| |
int bigint | string float double |
| |
double | string int float |
| |
date datetime timestamp | string |
|
Validations
Validations are a critical part of all API. Validations in kis.ai are isomorphic
Validation | Description | Examples |
---|---|---|
required | marks that the field value should be provided | |
unique | marks that the field values should be unique | |
final | marks that the field value can be set only once and then it becomes readonly | |
readonly | marks that the field value can only be read and cannot be updated. Usually such values also have computed to allow them to be computed from other fields. | |
writeonly | field can be updated but the value stored cannot be read. Usually used to store tokens or PII data or answers to private questions used to authenticate users. This fields still can be used for checking value, but cannot be used to read, and cannot be used as input for computed fields | |
writeonce | field can be updated only once but the value stored cannot be read. | |
length | length of the value is validated against the lower bound or upper bound | |
minmax | Validate that the numerical value is between a lowerbound and upperbound |
Compliance
Compliances are additional constraints that you can add to the field to make it compliant with specific values.
Compliance | Description | Examples |
---|---|---|
nolog | Ensures that field is not logged in log files including audit logs. Eg: Social Security Number, Credit Card | |
hash | The value provided will not be stored directly. A destructive hash is stored. Typically used for passwords | |
mask | when querying this field data is masked and to see the real value, the user should have unmask privilege on this field | |
tokenize | stores the tokenized data in the field. User should have untokenize privilege to see the actual value | |
encrypt | stores the encrypted data in the field. User should have decrypt privilege to see the decrypted value | |
pii | marks the field as Personally Identifiable Information and will be stored encrypted and nolog compliance is applied automatically. The user should have showpii privilege on this field to see the data |
Defining Related Entities
Entities in real world are some times nested, most times related and complex. When modeling the real world in entities, relations are must have. Data API block has the concept of references
to define relations between entities. These relationships dictate how data is connected and ensures data integrity.
1. One-to-One (1:1)
A one-to-one relationship occurs when a single record in one entity is associated with a single record in another entity. This type of relationship is less common but useful for splitting a large entity into smaller ones or for security purposes.
Employee and Salary Breakup
Each employee has one salary breakup, and one salary breakup is linked to one user.
2. One-to-Many (1:M )
A one-to-many relationship is the most common type of relationship between entities. It occurs when a single record of one entity is related to multiple records in another entity.
Customer and Orders:
One customer can place multiple orders, but each order is linked to only one customer.
3. Many-to-Many (M:N )
A many-to-many relationship occurs when multiple records in one table are related to multiple records in another table. This relationship is typically implemented using a junction (or associative) table that breaks down the many-to-many relationship into two one-to-many relationships.
Students and Courses: Students can enroll in multiple courses, and each course can have multiple students.
Customizing the hidden entity
Sometimes you need to store additional details in the hidden entity, then you use the use customentity
4. Self-Referencing (Recursive)
A self-referencing relationship occurs when a entity is related to itself. This is useful for hierarchical data structures such as organizational charts or family trees.
Employees: An employee can be a manager of other employees, creating a recursive relationship within the same table.