In the real world, data is never isolated. A customer has orders, a blog post has comments, and an invoice contains line items. These connections are what give data meaning and context. But modeling and managing these relationships in traditional software development can be a complex, error-prone process involving database migrations, ORM configurations, and custom API logic.
Enter Resources.do. Our "Business-as-Code" approach simplifies this entire process. By defining your core business objects as code, you can also define the relationships between them. The platform then does the heavy lifting, instantly generating powerful, interconnected APIs that reflect your business logic.
This guide will walk you through why data relationships are crucial and how you can model them effortlessly within Resources.do.
At its core, a data relationship is a defined link between two or more data models. Think of it as the connective tissue of your application's data structure. Establishing these links from the start offers several powerful advantages:
Let's model a common e-commerce scenario: A Customer can have many Orders.
First, we need to define the individual blueprints for our data. In Resources.do, these blueprints are called Resources. A Resource defines the schema, validation rules, and properties for a core business entity.
Here are our two basic Resources, Customer and Order:
Customer Resource:
{
"name": "string",
"email": "email",
"status": "string"
}
Order Resource:
{
"amount": "number",
"status": "string",
"createdAt": "datetime"
}
Now, let's create the link between them. Resources.do supports intuitive relationship types like hasMany and belongsTo.
We'll update our Resource definitions to include these relationships:
Updated Customer Resource:
{
"name": "string",
"email": "email",
"status": "string",
"orders": {
"type": "hasMany",
"resource": "Order"
}
}
Updated Order Resource:
{
"amount": "number",
"status": "string",
"createdAt": "datetime",
"customer": {
"type": "belongsTo",
"resource": "Customer"
}
}
That's it. With these simple, declarative definitions, you have fully described a core piece of your business logic.
The moment you save these relationship definitions, Resources.do gets to work. It automatically provisions and updates your versioned RESTful APIs. You don't need to write a single line of backend code or configure a database join.
Now, when you make a request to fetch a specific customer, you can easily include their related orders:
GET /api/customers/cus_1a2b3c4d5e?include=orders
The API response will be a perfectly structured JSON object, with the customer's data and a nested array of all their associated orders.
{
"id": "cus_1a2b3c4d5e",
"name": "ACME Corporation",
"email": "contact@acme.com",
"status": "active",
"createdAt": "2023-10-27T10:00:00Z",
"orders": [
{
"id": "ord_6f7g8h9i0j",
"amount": 499.99,
"status": "shipped",
"createdAt": "2023-10-28T14:30:00Z"
},
{
"id": "ord_k1l2m3n4p5",
"amount": 1250.00,
"status": "processing",
"createdAt": "2023-11-05T09:15:00Z"
}
]
}
This instant feedback loop between data modeling and a live API allows you to build and iterate faster than ever before.
What about more complex scenarios? Consider Orders and Products. An order can contain many products, and a single product can appear in many different orders. This is a classic "many-to-many" relationship.
You can model this in Resources.do by introducing a linking Resource, often called a LineItem.
This structure allows you to model sophisticated business logic with the same declarative simplicity, creating a robust and scalable data model that truly reflects how your business operates.
When you define your data models and their relationships as code in Resources.do, you create a centralized, version-controlled source of truth.
Stop wrestling with database schemas and custom API endpoints. Start defining your business as code.
Ready to unify your structured data? Sign up for Resources.do and transform your data models into live, interconnected APIs in minutes.