> ## Documentation Index
> Fetch the complete documentation index at: https://docs.serial.okos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Label Formats

> An in-depth look at the structure and purpose of the `label_formats` table in the Serial database

## Introduction to the label\_formats Table

The label\_formats table is designed to store and manage information about label formats for components in the Serial application. This table plays a crucial role in the system by providing a centralized repository for label specifications, including dimensions, metadata, and XML representations of label designs.

## Table Structure

The label\_formats table is structured to capture comprehensive details about each label format. Here's a detailed breakdown of its columns:

| Column Name                | Data Type    | Constraints             | Description                                           |
| -------------------------- | ------------ | ----------------------- | ----------------------------------------------------- |
| id                         | uuid         | primary key             | Unique identifier for the label format                |
| company\_id                | uuid         | not null, foreign key   | Reference to the company owning the label format      |
| component\_id              | uuid         | not null, foreign key   | Reference to the component associated with the format |
| name                       | varchar(255) | not null                | Name of the label format                              |
| width                      | float        | not null                | Width of the label in millimeters                     |
| height                     | float        | not null                | Height of the label in millimeters                    |
| displayed\_size\_unit      | varchar(10)  | check (in ('MM', 'IN')) | Unit for displaying label size (MM or IN)             |
| last\_edited\_at           | timestamp    | not null                | Timestamp of the last edit                            |
| last\_edited\_by\_user\_id | uuid         | not null, foreign key   | Reference to the user who last edited the format      |
| created\_at                | timestamp    | not null                | Timestamp of creation                                 |
| created\_by\_user\_id      | uuid         | not null, foreign key   | Reference to the user who created the format          |
| xml                        | text         |                         | XML representation of the label format                |
| xml\_identifier            | text         |                         | Identifier for the XML format                         |

## Usage and Functionality

The label\_formats table is designed to be a comprehensive storage solution for label specifications. Here are some key points about its usage:

1. **Component-Specific Formats**: Each label format is associated with a specific component, allowing for customized labeling solutions across different parts or products.

2. **Dimensional Flexibility**: The table stores both the width and height of labels in millimeters, with an additional field for displaying sizes in either millimeters or inches, accommodating different measurement preferences.

3. **XML Support**: The inclusion of XML fields allows for storing complex label designs that can be interpreted by label printing software or frontend rendering engines.

4. **Audit Trail**: By tracking creation and last edit information, the table maintains a clear audit trail of changes to label formats.

5. **Security**: Row-level security policies are implemented to ensure that users can only access and modify label formats associated with their company.

## Notes

* The table has foreign key relationships with the companies, components, and users tables, ensuring data integrity and proper associations across the database.

* Row-level security is enabled on this table, which is crucial for maintaining data isolation between different companies using the system.

* In the frontend application, this table is used in conjunction with the label\_elements table to provide a complete label management system. Here's an example of how label formats might be retrieved in the frontend:

  ```typescript
  const getLabels = async (
    componentId: string
  ): Promise<LabelFormatWithContents[]> => {
    const supabase = getSupabase();
    const { data: labelFormats, error: labelFormatsError } = await supabase
      .from("label_formats")
      .select("*")
      .eq("component_id", componentId)
      .returns<LabelFormat[]>();

    // ... additional logic to fetch and associate label elements
  };
  ```

* The XML fields (xml and xml\_identifier) provide flexibility for storing and identifying complex label designs, potentially supporting integration with various label printing systems.

By leveraging the label\_formats table, the Serial application can offer robust label management capabilities, allowing users to create, store, and manage label designs specific to their components while maintaining proper data segregation and security.
