Skip to main content

Referencing and Relationships

This document covers advanced schema features in Drafta, including object references and modular data structures.

Schema References​

Drafta schemas can reference other objects using the $ symbol, allowing for modular and reusable data structures. This reference syntax can also be applied to arrays, enabling you to reference multiple objects in a single field. Cross-referenced objects must be defined within the same schema file.

Field Reference​

A field can reference another object's field using the $ symbol:

UserProfile:
phone_number: $Contact.phone_number

Contact:
phone_number: string

Here, phone_number in UserProfile refers to phone_number in Contact.

Object Reference​

A field can reference an entire object:

UserProfile:
home_address: $Address

Address:
street: string
city: string
zip: string
state: string

This allows home_address to be structured as an Address object.

Array of References​

Fields can reference multiple objects using an array:

UserProfile:
addresses: [$Address]

Here, addresses holds multiple Address objects.

Recursive Object Reference​

Drafta supports recursive object references, where an object can reference itself directly or indirectly. The system automatically detects and handles recursion to prevent infinite loops.

Example:​

Category:
name: string
parent: $Category

In this case, parent allows a category to reference another category, enabling hierarchical structures.

Mutual Object References​

Drafta also supports mutual references, where two objects reference each other. This is useful for modeling relationships such as users following each other or linked entities.

Example:​

User:
name: string
best_friend: $User
company: $Company

Company:
name: string
owner: $User

Here:

  • best_friend in User references another User, allowing bidirectional relationships.
  • company in User references a Company.
  • owner in Company references a User, establishing a mutual reference.