You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/news/posts/2025-09-05-orm.md
+65-17Lines changed: 65 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
blogpost: true
3
3
category: Blog
4
-
tags: usability
4
+
tags: architecture
5
5
author: Julian Geiger
6
6
date: 2025-09-05
7
7
---
@@ -11,11 +11,10 @@ date: 2025-09-05
11
11
AiiDA provides an Object-Relational Mapping (ORM) system that abstracts database operations while supporting multiple database backends.
12
12
In this post, we'll explore how AiiDA leverages SQLAlchemy to create a flexible, multi-backend ORM to separate concerns between Python objects and database persistence.
13
13
14
-
## Architecture Overview: The Three-Layer Design
14
+
## Architecture overview: The multi-layer design
15
15
16
-
AiiDA's ORM follows a three-layer architecture that provides clean separation between the user interface, business logic, and data persistence:
16
+
AiiDA's ORM follows a four-layer architecture that provides clean separation between the user interface, business logic, and data persistence:
17
17
18
-
<!-- TODO: check again, we have 4 top-level sections, but only 3 here -->
19
18
```
20
19
┌─────────────────────┐
21
20
│ User Interface │ ← Node (Python ORM class)
@@ -24,8 +23,11 @@ AiiDA's ORM follows a three-layer architecture that provides clean separation be
24
23
│ Backend Interface │ ← BackendNode (Abstract base class)
This approach ensures data consistency while supporting nested transactions for complex operations.
252
254
255
+
!!! info ""
253
256
254
257
## The user interface: `Node`
255
258
@@ -324,7 +327,7 @@ It also uses @cached_property to ensure these namespace objects are created only
324
327
325
328
## Honorable mentions
326
329
327
-
### Link Management: Modeling Relationships
330
+
### Link management: modeling relationships
328
331
329
332
AiiDA models relationships between nodes through a separate `DbLink` table:
330
333
@@ -350,7 +353,32 @@ The `label` field describes the specific role of the link (e.g., "structure", "p
350
353
This metadata enables complex provenance queries like "find all structures that were relaxed using PBE exchange-correlation functional."
351
354
The `ondelete='CASCADE'` on the output relationship ensures that when a node is deleted, all its incoming links are also removed, maintaining referential integrity.
352
355
353
-
### Pydantic Model Integration: Modern Serialization
356
+
### The QueryBuilder
357
+
358
+
The `QueryBuilder` is AiiDA's main API provided to retrieve data from the database.
359
+
It provides a uniform, backend-agnostic interface:
"""Delete a Node from the collection with the given id"""
442
+
node =self.get(id=pk)
443
+
444
+
if node.base.links.get_incoming().all():
445
+
raise exceptions.InvalidOperation(f'cannot delete Node<{node.pk}> because it has incoming links')
446
+
447
+
if node.base.links.get_outgoing().all():
448
+
raise exceptions.InvalidOperation(f'cannot delete Node<{node.pk}> because it has outgoing links')
449
+
450
+
self._backend.nodes.delete(pk)
451
+
```
452
+
This collection pattern provides several benefits:
453
+
454
+
*__Type safety__: Generic typing ensures you get the correct node type back
455
+
*__Validation__: Prevents deletion of nodes with existing links to maintain provenance integrity
456
+
*__Backend abstraction__: Hides database-specific operations behind a clean interface
407
457
408
458
## Key Takeaways
409
459
410
-
AiiDA's ORM architecture demonstrates several important design principles:
460
+
AiiDA's ORM architecture makes use of several important design principles:
411
461
412
-
1.**Multi-Layer Abstraction**: The three-layer design (User Interface → Backend Interface → Database Implementation) provides clean separation while allowing backend-specific optimizations.
462
+
1.**Multi-Layer Abstraction**: The multi-layer design (User Interface → Backend Interface → Database Implementation) provides clean separation while allowing backend-specific optimizations.
413
463
414
464
2.**Automatic Backend Adaptation**: The PostgreSQL-to-SQLite conversion system shows how to maintain feature parity across different database engines without code duplication.
415
465
@@ -425,9 +475,7 @@ AiiDA's ORM architecture demonstrates several important design principles:
425
475
426
476
8.**Collection Patterns**: The collection system provides a consistent, intuitive interface for data access that scales from simple lookups to complex queries.
427
477
428
-
This architecture allows AiiDA to provide a powerful, flexible ORM that adapts to different database backends while maintaining a consistent user experience. The careful balance between abstraction and performance, combined with modern Python practices, makes it an excellent example of how to build scalable scientific software that can evolve with changing requirements and technologies.
429
-
430
-
The system's ability to automatically convert PostgreSQL models to SQLite equivalents, while maintaining query compatibility through sophisticated backend-specific implementations, demonstrates how thoughtful architecture can provide both flexibility and performance without compromising on either.
478
+
This architecture allows AiiDA to provide a powerful, flexible ORM that adapts to different database backends while maintaining a consistent user experience.
431
479
432
480
<!-- TODO: -->
433
481
<!-- Add statement that the whole infrastructure also had to be implemented for the other, specialized data types of AiiDA -->
0 commit comments