relationship
relationships table.Each row names a relationship type and a source and target, each given by entity type and id.
const SELECT_COLUMNS
const SELECT_COLUMNS: &str = "id, rel_type, source_id, source_type, target_id, target_type, created_at, updated_at";The columns of relationships in the order that the relationship model’s TryFrom<Row> reads them.
fn create
pub async fn create(
conn: &Connection,
rel_type: RelationshipType,
source_type: EntityType,
source_id: &Id,
target_type: EntityType,
target_id: &Id,
) -> Result<Model, Error>Create a new relationship between two entities.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
rel_type |
RelationshipType | the kind of relationship |
source_type |
EntityType | the kind of the source entity |
source_id |
&Id | the source entity |
target_type |
EntityType | the kind of the target entity |
target_id |
&Id | the target entity |
| returns | Result<Model, Error> | The inserted relationship, with a fresh id and both timestamps set to now, or an error when the insert or the read back fails. |
fn delete
pub async fn delete(conn: &Connection, id: &Id) -> Result<bool, Error>Delete a relationship by its ID. Returns true if deleted.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
id |
&Id | the relationship to delete |
| returns | Result<bool, Error> | false when no relationship has that id, or an error when the delete fails. |
fn find_by_endpoints
pub async fn find_by_endpoints(
conn: &Connection,
source_type: EntityType,
source_id: &Id,
target_type: EntityType,
target_id: &Id,
rel_type: Option<RelationshipType>,
) -> Result<Vec<Model>, Error>Find relationships between a source and target endpoint pair.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
source_type |
EntityType | the kind of the source entity |
source_id |
&Id | the source entity |
target_type |
EntityType | the kind of the target entity |
target_id |
&Id | the target entity |
rel_type |
Option<RelationshipType> | the relationship type to match |
| returns | Result<Vec<Model>, Error> | When rel_type is Some, the unique index guarantees at most one row is returned. When rel_type is None, every relationship between the pair is returned, one per distinct rel_type. Rows are ordered by rel_type ascending for stable output. Only that direction matches, the result may be empty, and it is an error when the query or row decoding fails. |
fn find_by_id
pub async fn find_by_id(conn: &Connection, id: impl Into<Id>) -> Result<Option<Model>, Error>Find a relationship by its Id.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
id |
impl Into<Id> | the full relationship id |
| returns | Result<Option<Model>, Error> | The relationship, None when no row has that id, or an error when the query or row decoding fails. |
fn for_entities
pub async fn for_entities(
conn: &Connection,
entity_type: EntityType,
entity_ids: &[Id],
) -> Result<HashMap<Id, Vec<Model>>, Error>Return all relationships attached to each of the given entities in a single query.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of the entities; an endpoint matches only with this type |
entity_ids |
&[Id] | the entities whose relationships to load |
| returns | Result<HashMap<Id, Vec<Model>>, Error> | Each returned Model is associated with every queried entity it touches: a relationship where both endpoints are in entity_ids appears under both keys. Entities with no relationships are absent from the returned map. Passing an empty slice returns an empty map without issuing a query. It is an error when the query or row decoding fails. |
fn for_entity
pub async fn for_entity(
conn: &Connection,
entity_type: EntityType,
entity_id: &Id,
) -> Result<Vec<Model>, Error>Return all relationships where the entity is either source or target.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity |
| returns | Result<Vec<Model>, Error> | The relationships, in no set order, or an error when the query or row decoding fails. |
fn relationship_audit_payload
pub fn relationship_audit_payload(rel: &Model) -> ValueBuild the JSON payload stored in the audit log for a relationship row.
The object shape mirrors the relationships table column set so
crate::store::repo::transaction::undo can re-insert the row verbatim via
an INSERT whose column list is the object’s key set.
| Parameter | Type | Description |
|---|---|---|
rel |
&Model | the relationship to serialize |
| returns | Value | A JSON object with the relationship’s eight columns as strings, timestamps in RFC 3339. |