Datenbankschema
Tabellen-Übersicht
Auth (Auth.js)
| Tabelle | Beschreibung | RLS |
|---|---|---|
users | Auth.js User-Tabelle | Nein |
accounts | OAuth-Provider-Accounts | Nein |
sessions | Auth.js Sessions (nicht aktiv bei JWT) | Nein |
verification_tokens | E-Mail-Verifikation | Nein |
Foundation
| Tabelle | Beschreibung | RLS |
|---|---|---|
tenants | Multi-Tenant-Wurzel | Ja |
profiles | User-Tenant-Zuordnung mit Rolle | Ja |
tenant_settings | 1:1 Konfiguration pro Tenant | Ja |
order_sequences | Atomare Nummernvergabe | Ja |
Produktion
| Tabelle | Beschreibung | RLS |
|---|---|---|
suppliers | Rohkaffee-Lieferanten | Ja |
green_coffee_lots | Rohkaffee-Lots | Ja |
machines | Röstmaschinen | Ja |
roast_profiles | Röstrezepte | Ja |
roast_log_entries | Einzelne Röstungen | Ja |
roasted_barrels | Tonnen-Sessions | Ja |
product_variants | Produkt-Varianten (SKUs) | Ja |
packing_positions | Abpackpositionen | Ja |
maintenance_tasks | Wartungsaufgaben | Ja |
Commerce
| Tabelle | Beschreibung | RLS |
|---|---|---|
b2b_customers | Geschäftskunden | Ja |
b2b_orders | Bestellungen | Ja |
b2b_order_positions | Bestellpositionen | Ja |
b2b_invoices | Rechnungen | Ja |
b2b_invoice_positions | Rechnungspositionen | Ja |
pricing_agreements | Preisvereinbarungen | Ja |
forecast_positions | Nachfrage-Overrides | Ja |
subscriptions | Kaffee-Abonnements | Ja |
fixed_costs | Fixkosten für GuV | Ja |
Versand
| Tabelle | Beschreibung | RLS |
|---|---|---|
shipments | DHL-Sendungen | Ja |
shipment_labels | PDF-Labels | Ja |
dhl_tracking_events | Tracking-Historie | Ja |
Dealer-Portal
| Tabelle | Beschreibung | RLS |
|---|---|---|
order_form_customers | Portal-Kunden | Ja |
order_form_customer_variants | Sichtbare Varianten pro Kunde | Ja |
Platform Admin (PROJ-31)
| Tabelle | Beschreibung | RLS |
|---|---|---|
admin_impersonation_sessions | Admin-Impersonation | Nein |
admin_audit_logs | Admin-Aktionsprotokoll | Nein |
Shopify (Mirror)
| Tabelle | Beschreibung | RLS |
|---|---|---|
shopify_products | Produkt-Mirror | Ja |
shopify_product_variants | Varianten-Mirror | Ja |
shopify_orders | Bestellungs-Mirror | Ja |
shopify_fulfillments | Fulfillment-Mirror | Ja |
shopify_customers | Kunden-Mirror | Ja |
shopify_variant_mapping | Roestify ↔ Shopify Zuordnung | Ja |
System
| Tabelle | Beschreibung | RLS |
|---|---|---|
team_invitations | Team-Einladungen | Ja |
notifications | In-App Benachrichtigungen | Ja |
usage_metrics | Nutzungszähler pro Monat | Ja |
Computed Fields (Views / Query-Time)
| Feld | Berechnung | Quelle |
|---|---|---|
lot_remaining_kg | raw_weight_kg − SUM(roast_raw_weight_g)/1000 | VIEW |
lot_remaining_pct | remaining_kg / raw_weight_kg × 100 | VIEW |
machine_kg_counter | kg_counter_offset + SUM(roasted_weight_g)/1000 | VIEW |
roast_loss_pct | (raw − roasted) / raw × 100 | RPC (auto) |
barrel_status | Open/Resting/Ready/Empty Logic | Query-Time |
barrel_available_g | total_roasted_g − packed_g | Query-Time |
forecast_interval | Median(delivery intervals) | Query-Time |
RPC Functions (SECURITY DEFINER)
| Funktion | Beschreibung |
|---|---|
get_tenant_id() | Tenant-ID des aktuellen Users |
next_order_number() | Lückenlose Bestellnummer (B2B-YYYY-NNNN) |
next_invoice_number() | Lückenlose Rechnungsnummer (RE-YYYY-NNN) |
create_roast_log_entry() | Atomare Röstanlage (Session + Barrel + Warnings) |
update_roast_log_entry() | Atomares Röst-Update |
soft_delete_roast_log_entry() | Soft-Delete mit Restbestands-Wiederherstellung |
resolve_or_create_barrel_session() | Session finden oder erstellen |
close_barrel_session() | Session schließen (Open → Resting) |
create_packing_position() | Packposition mit Gewichtsberechnung |
increment_usage() | Atomarer Usage-Counter |
Trigger
| Trigger | Tabelle | Aktion |
|---|---|---|
set_updated_at | Alle | Auto-Update von updated_at |
prevent_tenant_id_change | Alle | Immutable tenant_id |
validate_*_fk_tenants | Diverse | Cross-Tenant FK-Prüfung |
auto_barrel_status | roasted_barrels | Status-Transitions |
Indizes
Alle Tabellen haben mindestens:
- PK auf
id - Index auf
tenant_id - Partial Index
WHERE deleted_at IS NULL(für Soft-Delete-Tabellen) - Unique Constraint auf natürliche Schlüssel (z.B.
lot_numberpro Tenant)