Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

api.moorages_view

Description

Moorages listing web view

Table Definition
CREATE VIEW moorages_view AS (
 WITH logbook_counts AS (
         SELECT logbook._from_moorage_id AS moorage_id,
            count(*) AS entries
           FROM api.logbook
          WHERE ((logbook.vessel_id = current_setting('vessel.id'::text, true)) AND (logbook._from_moorage_id IS NOT NULL) AND (logbook.active = false))
          GROUP BY logbook._from_moorage_id
        UNION ALL
         SELECT logbook._to_moorage_id AS moorage_id,
            count(*) AS entries
           FROM api.logbook
          WHERE ((logbook.vessel_id = current_setting('vessel.id'::text, true)) AND (logbook._to_moorage_id IS NOT NULL) AND (logbook.active = false))
          GROUP BY logbook._to_moorage_id
        ), logbook_total AS (
         SELECT logbook_counts.moorage_id,
            sum(logbook_counts.entries) AS total_arrivals_departures
           FROM logbook_counts
          GROUP BY logbook_counts.moorage_id
        ), stays_agg AS (
         SELECT stays.moorage_id,
            sum(stays.duration) AS total_duration
           FROM api.stays
          WHERE ((stays.vessel_id = current_setting('vessel.id'::text, true)) AND (stays.active = false))
          GROUP BY stays.moorage_id
        )
 SELECT m.id,
    m.name AS moorage,
    sa.description AS default_stay,
    sa.stay_code AS default_stay_id,
    COALESCE(lt.total_arrivals_departures, (0)::numeric) AS arrivals_departures,
    COALESCE(st.total_duration, 'PT0S'::interval) AS total_duration
   FROM (((api.moorages m
     JOIN api.stays_at sa ON ((m.stay_code = sa.stay_code)))
     LEFT JOIN logbook_total lt ON ((lt.moorage_id = m.id)))
     LEFT JOIN stays_agg st ON ((st.moorage_id = m.id)))
  WHERE ((m.vessel_id = current_setting('vessel.id'::text, true)) AND (m.geog IS NOT NULL))
  ORDER BY COALESCE(st.total_duration, 'PT0S'::interval) DESC
)

Columns

NameTypeDefaultNullable
arrivals_departuresnumerictrue
default_staytexttrue
default_stay_idintegertrue
idintegertrue
mooragetexttrue
total_durationintervaltrue

Referenced Tables

NameColumnsCommentType
api.logbook43The logbook table stores vessel navigation entries with timestamps, locations, and trip metrics. RLS policies filter by vessel_id automatically.
These indexes optimize different query patterns while minimizing storage.
BASE TABLE
api.stays15The stays table records time spent at moorages. Each stay links a moorage with arrival/departure timestamps. RLS policies filter by vessel_id.BASE TABLE
api.moorages14The moorages table stores locations where vessels can stay (marinas, anchorages, etc.). Each moorage has geographic coordinates and metadata.BASE TABLE
api.stays_at2Stay TypeBASE TABLE
logbook_total0
stays_agg0

Relations

erDiagram


"api.moorages_view" {
  numeric arrivals_departures ""
  text default_stay ""
  integer default_stay_id ""
  integer id ""
  text moorage ""
  interval total_duration ""
}

Generated by tbls