> ## 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.

# Companies

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

## Introduction to the Companies Table

The Companies table is designed to store and manage essential information about organizations using the Serial application. This table serves as a cornerstone for the entire database structure, forming the basis for user associations, data ownership, and access control across the system.

## Table Structure

The Companies table is structured to efficiently capture key company details while allowing for flexible configuration. Here's a detailed breakdown of its columns:

| Column Name | Data Type                | Constraints                                         | Description                                                     |
| ----------- | ------------------------ | --------------------------------------------------- | --------------------------------------------------------------- |
| id          | uuid                     | primary key, not null, default uuid\_generate\_v4() | Unique identifier for each company                              |
| created\_at | timestamp with time zone | not null, default now()                             | Timestamp of when the company record was created                |
| name        | text                     | not null                                            | The name of the company                                         |
| is\_active  | boolean                  | not null, default false                             | Indicates whether the company is currently active in the system |
| config      | jsonb                    | not null, default '{}'                              | JSON object storing company-specific configuration settings     |

## Usage and Functionality

The Companies table is designed to be the central reference point for company-related data and permissions. Here are some key points about its usage:

1. **Data Ownership and Access Control**: The company\_id is used extensively throughout other tables (such as users, components, processes) to associate data with specific companies. This forms the basis for row-level security policies that ensure users can only access data belonging to their company.

2. **Flexible Configuration**: The `config` column, using JSONB type, allows for storing a wide range of company-specific settings without needing to alter the table structure. This can include flags for features like `use_operator_pins`, `allow_wip_linking`, or `enable_location_tracking`.

3. **User Management**: New users are initially associated with a "NULL COMPANY" before being properly assigned, facilitating a smooth onboarding process.

## Notes

* The Companies table implements row-level security, ensuring that even at the database level, data access is restricted based on user permissions and company associations.

* There's a utility function to delete test companies, which can be useful in development and testing environments:

  ```sql
  CREATE OR REPLACE FUNCTION delete_test_companies()
  RETURNS void AS $$
  BEGIN
    DELETE FROM public.companies WHERE name LIKE 'test-company%';
  END;
  $$ LANGUAGE plpgsql;
  ```

* The `is_active` flag allows for soft-deactivation of companies without deleting their data, which can be crucial for maintaining historical records or temporarily suspending access.

By leveraging the Companies table, the Serial application establishes a robust foundation for multi-tenancy, ensuring data isolation between different organizations while providing flexible configuration options to cater to diverse company needs.
