In the lifecycle of any application, managing data is a central challenge. As projects grow, schemas drift, validation logic gets scattered, and the source of truth for your data's structure becomes a tangled mess spread across ORMs, database migration files, and application code. This chaos leads to bugs, slows down development, and makes maintenance a nightmare. What if you could manage your data models with the same precision and clarity as your application code?
Welcome to the world of "Data as Code." With Resources.do, you can transform your approach to structured data, defining your entire data layer through a simple, declarative API. This guide will walk you through the core concepts, showing you how to model, manage, and scale your data management strategy effectively.
"Data as Code" is a paradigm shift that treats your data models—their schemas, rules, and relationships—as first-class citizens in your codebase. Instead of relying on GUI tools or disparate configuration files, you define your data architecture programmatically. This business-as-code approach, championed by Resources.do, brings the proven benefits of software development to your data layer:
At the heart of Resources.do is the Resource—a data model defined as code. A Resource encapsulates everything about a specific data entity: its structure, validation rules, relationships, and even its business logic.
Let's look at a simple yet powerful example of a Customer resource, defined using the declarative API.
import { Resource } from 'resources.do';
const customerResource = new Resource({
name: 'Customer',
schema: {
id: { type: 'string', required: true },
name: { type: 'string', required: true },
email: { type: 'string', format: 'email', required: true },
company: { type: 'string' },
status: { type: 'string', enum: ['active', 'inactive', 'pending'] },
createdAt: { type: 'date', default: 'now()' }
},
relationships: [
{ type: 'hasMany', resource: 'Order' }
]
});
This compact block of code is now the definitive source of truth for what a "Customer" is in your application. Let's break it down.
The schema object is where you define the anatomy of your data. This is schema definition at its clearest.
By defining validation directly in the model, you guarantee data integrity and eliminate redundant validation code scattered throughout your services and controllers.
Modern applications are built on connected data. The relationships array declaratively defines how your Resources interact.
In our example, { type: 'hasMany', resource: 'Order' } clearly states that a Customer can have many Order records associated with it. This explicit definition allows Resources.do to provide intelligent APIs for querying and manipulating connected data, turning your siloed tables into a cohesive, navigable data graph.
A common question is: "Can I connect Resources to my existing database?" The answer is a definitive yes.
Resources.do is designed to act as a powerful abstraction layer. It provides adapters that connect to your underlying data sources, whether they are SQL databases, NoSQL stores, or even third-party APIs. This means you can leverage a single, unified interface to manage your data, regardless of where it lives. This simplifies your codebase and makes it incredibly easy to swap or add new data sources in the future without rewriting your application's core logic.
Adopting a "Data as Code" workflow with Resources.do pays dividends throughout the entire lifecycle of your project.
By embracing this modern approach, you transform data management from a reactive chore into a proactive, strategic part of your development workflow.
Ready to model your data as code and build more reliable, scalable, and maintainable applications?