Managing organizational charts in a traditional database can be a headache. Complex SQL joins, messy self-referencing tables, and slow queries for finding a complete reporting line are common pain points for developers. What if you could offload that complexity and model your entire org structure with a few simple API calls?
Welcome to tree.service.do. We make building with hierarchies simple. In this guide, we'll walk you through creating a fully functional, dynamic organizational chart in under 10 minutes using our Tree Service API. No more database schema headaches—just clean, intuitive code.
Let's get started.
Hierarchical data is everywhere: file systems, product categories, nested comments, and of course, company org charts. The relationships are parent-child, forming a tree. The problem is that relational databases are built for flat tables, not nested trees. Answering a simple question like "Show me everyone who ultimately reports to the CTO" can require recursive queries that are difficult to write and slow to run.
This is where a dedicated service shines. tree.service.do is a purpose-built API for hierarchical data, handling all the complex traversals, queries, and storage optimizations for you.
First, let's get your environment ready.
npm install @do-sdk/tree
Now, you can initialize the client in your project.
import { TreeClient } from '@do-sdk/tree';
// Initialize the client with your API key
const treeService = new TreeClient({ apiKey: 'YOUR_API_KEY_HERE' });
Think of a "tree" as the top-level container for your data. In this case, it's the org chart itself. Let's create one.
// Create a new tree representing an org chart
const orgChart = await treeService.create({
name: 'Company Org Chart'
});
console.log(`New tree created with ID: ${orgChart.id}`);
This simple call provisions a new, empty tree in our service, ready to be populated. The orgChart object is now your main point of interaction.
Every tree needs a root. For our org chart, that's the CEO. We'll add a "node" to the root of the tree using the path /.
One of the most powerful features of tree.service.do is the flexible data field. You can store any JSON object you need on each node, making it perfect for storing employee details.
// Add a root node for the CEO
const ceo = await orgChart.addNode({
path: '/', // Add to the root
data: {
title: 'Chief Executive Officer',
name: 'Alice',
employeeId: 'E1001'
}
});
console.log(`Added CEO: ${ceo.data.name}`);
Now for the fun part: building out the team. Adding a direct report is as simple as specifying the parent's ID in the path.
Let's add a CTO and a COO who report directly to our CEO, Alice.
// Add a child node for the CTO reporting to the CEO
const cto = await orgChart.addNode({
path: `/${ceo.id}`,
data: {
title: 'Chief Technology Officer',
name: 'Bob',
employeeId: 'E1002'
}
});
// Add a child node for the COO reporting to the CEO
const coo = await orgChart.addNode({
path: `/${ceo.id}`,
data: {
title: 'Chief Operating Officer',
name: 'Charlie',
employeeId: 'E1003'
}
});
console.log(`${cto.data.name} and ${coo.data.name} now report to the CEO.`);
The tree structure is automatically managed. You can go as deep as you need. Let's add an engineer who reports to the CTO.
// Add an engineer reporting to the CTO
const engineer = await orgChart.addNode({
path: `/${ceo.id}/${cto.id}`, // A deeper path
data: {
title: 'Software Engineer',
name: 'Dana',
employeeId: 'E2001'
}
});
console.log(`${engineer.data.name} added under the CTO.`);
Just like that, you have a three-level hierarchy. You've modeled a reporting structure that would have required complex table joins, all with a clear and predictable API.
Storing the data is only half the battle. The real power comes from querying it. Our API makes common hierarchical queries trivial.
Who reports directly to the CEO?
const ceoReports = await orgChart.getChildren(ceo.id);
// Output: An array of nodes for the CTO and COO
console.log('CEO Direct Reports:', ceoReports.map(node => node.data.name));
Who is the Software Engineer's manager?
const engineerManager = await orgChart.getParent(engineer.id);
// Output: The CTO node
console.log(`${engineer.data.name}'s manager is ${engineerManager.data.name}.`);
What is the Software Engineer's full reporting chain?
const reportingChain = await orgChart.getAncestry(engineer.id);
// Output: An array of nodes for the CEO and CTO
const chain = reportingChain.map(node => node.data.name).join(' -> ');
console.log(`Reporting chain: ${chain} -> ${engineer.data.name}`);
In about 10 minutes, you've successfully modeled a dynamic, queryable organizational chart. You can easily add, move, or remove employees, and the API handles all the underlying structural changes and query optimizations.
From here, you could:
Managing hierarchical data shouldn't slow you down. With the right tool, it can be the simplest part of your application.
Ready to start building? Sign up for free at tree.service.do and turn your complex data relationships into powerful applications today.