Understanding Subjects
A subject in TAPLE Core contains:- Subject ID: Unique identifier derived from governance, schema, and public key
- State: Current properties stored as JSON
- Event Chain: Immutable history of all changes
- Governance: Rules governing the subject’s behavior
- Ownership: Public key of the current owner
Subject struct is defined in core/src/commons/models/state.rs:18.
Subject Data Structure
Creating a Subject
let create_request = EventRequest::Create(StartRequest {
governance_id: governance_id.clone(),
schema_id: "asset".to_string(),
namespace: "company-namespace".to_string(),
name: "Asset-001".to_string(),
public_key: subject_key,
});
Retrieving Subjects
Get a Single Subject
Retrieve a subject by its identifier:core/src/api/api.rs:278. Returns SubjectData with all subject information.
Get All Subjects
Retrieve all subjects with optional pagination:The
from parameter supports negative values for reverse pagination, starting from the end of the collection.Get Subjects by Governance
Retrieve all subjects under a specific governance:core/src/api/api.rs:179 for the implementation.
Get Governance Subjects
Retrieve subjects belonging to a specific governance:core/src/api/api.rs:386.
Subject State Management
Get Subject Context
Obtain the context for evaluating operations:SubjectContext with governance, schema, ownership status, state, and namespace.
Calculate State Hash
Get the hash of the current state:core/src/commons/models/state.rs:199.
Preview State After Patch
Calculate what the state hash would be after applying a patch:core/src/commons/models/state.rs:211.
Updating Subjects
Update Subject State
Apply a JSON patch to update the subject:Transfer Subject Ownership
Transfer a subject to a new owner:core/src/commons/models/state.rs:186.
Subject Lifecycle
Mark Subject as End-of-Life
Deactivate a subject:active flag to false. Defined in core/src/commons/models/state.rs:208.
Check Subject Status
Verify if a subject is still active:Database Operations
Store a Subject
Persist a subject to the database:core/src/database/db.rs:133.
Retrieve from Database
Load a subject from storage:core/src/database/db.rs:129.
Get All Subjects from Database
Retrieve all subjects without pagination:Vec<Subject> of all stored subjects.
Delete a Subject
Remove a subject from the database:Subject Indexing
Index by Governance
Create an index linking subjects to their governance:Get Subjects by Governance Index
Retrieve subject IDs for a governance:core/src/database/db.rs:334.
Preauthorized Subjects
Add Preauthorization
Allow specific providers to access a subject before it’s created:core/src/api/api.rs:318.
Get Preauthorized Subjects
Retrieve all preauthorized subjects and their providers:Subject ID Generation
Subject IDs are deterministically generated:core/src/commons/models/state.rs:230. The ID is derived from:
- Namespace
- Schema ID
- Public key
- Governance ID
- Governance version
Best Practices
- Unique Naming: Use descriptive names and namespaces for subjects
- Key Management: Securely store subject keys, especially for owned subjects
- State Validation: Always validate state against schemas before updates
- Pagination: Use pagination for large subject collections to avoid memory issues
- Monitoring: Track subject sequence numbers to detect missed events
Error Handling
Common errors when working with subjects:NotFound: Subject doesn’t existInvalidParameters: Malformed subject ID or parametersInternalError: Database or internal processing errorNotCreateEvent: Attempted to create subject from wrong event type
