tag
entity_tags join table.Tags are global, keyed by a unique label, and shared by every project. Detaching a tag removes only the join row, never the tag.
const SELECT_COLUMNS
const SELECT_COLUMNS: &str = "id, label, created_at, updated_at";The columns of tags in the order that the tag model’s TryFrom<Row> reads them.
fn all
pub async fn all(conn: &Connection) -> Result<Vec<Tag>, Error>Return all tags ordered by label.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
| returns | Result<Vec<Tag>, Error> | The tags, attached or not; an error when the query or row decoding fails. |
fn attach
pub async fn attach(
conn: &Connection,
entity_type: EntityType,
entity_id: &Id,
label: &str,
) -> Result<Tag, Error>Attach a tag to an entity. Creates the tag if it doesn’t exist.
Attaching a tag that is already attached is a no-op (INSERT OR IGNORE).
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity to tag |
label |
&str | the tag’s label |
| returns | Result<Tag, Error> | The existing or newly created tag, or an error when a query fails. |
fn attach_recorded
pub async fn attach_recorded(
conn: &Connection,
transaction_id: &Id,
entity_type: EntityType,
entity_id: &Id,
label: &str,
) -> Result<Tag, Error>Attach a tag to an entity as part of a transaction, so that undo removes the link.
The created event on entity_tags is recorded only when the link is new, with the link’s key columns as
its before_data. A link that was already there is left out of the transaction, so undo keeps it.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction the event belongs to |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity to tag |
label |
&str | the tag’s label |
| returns | Result<Tag, Error> | The existing or newly created tag, or an error when a query fails. |
fn insert_link
async fn insert_link(
conn: &Connection,
entity_type: EntityType,
entity_id: &Id,
label: &str,
) -> Result<(Tag, bool), Error>Finds or creates the tag and inserts the link with INSERT OR IGNORE.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | |
entity_type |
EntityType | |
entity_id |
&Id | |
label |
&str | |
| returns | Result<(Tag, bool), Error> | The tag, and whether a new link row was inserted. |
fn by_entity_type
pub async fn by_entity_type(conn: &Connection, entity_type: EntityType) -> Result<Vec<Tag>, Error>Return all distinct tags attached to entities of the given type, ordered by label.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of entity whose tags to collect |
| returns | Result<Vec<Tag>, Error> | The tags; an error when the query or row decoding fails. |
fn create
pub async fn create(conn: &Connection, tag: &Tag) -> Result<Tag, Error>Create a new tag with the given label.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
tag |
&Tag | the tag to insert, with its id, label and timestamps |
| returns | Result<Tag, Error> | The inserted tag as read back from the database, or an error when the insert fails (for example on a duplicate label) or the read fails. |
fn detach_recorded
pub async fn detach_recorded(
conn: &Connection,
transaction_id: &Id,
entity_type: EntityType,
entity_id: &Id,
label: &str,
) -> Result<bool, Error>Detach a tag from an entity as part of a transaction, so that undo puts the link back.
The deleted event on entity_tags is recorded only when the link existed, with the whole row (including
its created_at) as before_data. Does not delete the tag itself.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction the event belongs to |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity to untag |
label |
&str | the tag’s label |
| returns | Result<bool, Error> | true when a link was removed, false when no tag has that label or it was not attached, or an error when a query fails. |
fn detach
pub async fn detach(
conn: &Connection,
entity_type: EntityType,
entity_id: &Id,
label: &str,
) -> Result<bool, Error>Detach a tag from an entity. Does not delete the tag itself.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity to untag |
label |
&str | the tag’s label |
| returns | Result<bool, Error> | true when a join row was removed, false when no tag has that label or it was not attached, or an error when a query fails. |
fn detach_all
pub async fn detach_all(
conn: &Connection,
entity_type: EntityType,
entity_id: &Id,
) -> Result<u64, Error>Detach all tags from an entity. Does not delete the tags themselves.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity to untag |
| returns | Result<u64, Error> | The number of join rows removed, or an error when the delete fails. |
fn detach_all_recorded
pub async fn detach_all_recorded(
conn: &Connection,
transaction_id: &Id,
entity_type: EntityType,
entity_id: &Id,
) -> Result<u64, Error>Detach all tags from an entity as part of a transaction, so that undo puts them back.
Each removed link is recorded as a deleted event on entity_tags, with the whole row as its
before_data. Does not delete the tags themselves.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction the events belong to |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity to untag |
| returns | Result<u64, Error> | The number of join rows removed, or an error when a query fails. |
fn find_by_id
pub async fn find_by_id(conn: &Connection, id: impl Into<Id>) -> Result<Option<Tag>, Error>Find a tag by its Id.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
id |
impl Into<Id> | the full tag id |
| returns | Result<Option<Tag>, Error> | The tag, None when no tag has that id, or an error when the query or row decoding fails. |
fn find_by_label
pub async fn find_by_label(conn: &Connection, label: &str) -> Result<Option<Tag>, Error>Find a tag by its label.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
label |
&str | the exact label to match |
| returns | Result<Option<Tag>, Error> | The tag, None when no tag has that label, or an error when the query or row decoding fails. |
fn find_or_create
pub async fn find_or_create(conn: &Connection, label: &str) -> Result<Tag, Error>Find an existing tag by label or create a new one.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
label |
&str | the tag’s label |
| returns | Result<Tag, Error> | The existing tag, or the newly created one, or an error when a query fails. |
fn for_entities
pub async fn for_entities(
conn: &Connection,
entity_type: EntityType,
entity_ids: &[Id],
) -> Result<HashMap<Id, Vec<Tag>>, Error>Return all tags 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 |
entity_ids |
&[Id] | the entities whose tags to load |
| returns | Result<HashMap<Id, Vec<Tag>>, Error> | Entities with no tags are absent from the returned map. Within each entry the tags are ordered by label. Passing an empty entity_ids slice returns an empty map without issuing a query. It is an error when the query fails or a stored id does not parse. |
fn for_entity
pub async fn for_entity(
conn: &Connection,
entity_type: EntityType,
entity_id: &Id,
) -> Result<Vec<String>, Error>Return all tag labels for a specific entity.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
entity_type |
EntityType | the kind of the entity |
entity_id |
&Id | the entity whose labels to read |
| returns | Result<Vec<String>, Error> | The labels, ordered alphabetically, or an error when the query fails. |