Development prototype · all athletes, IDs and metrics are synthetic · data stays in this browser · role switching below is a demo preview only, not sign-in or access control
training facility

Schema proposal

Draft SQL for review only. It has not been run against any database, and no backend is connected to this prototype.

001_seamworks_schema_proposal.sql

Organizations, memberships, players, player access links, sessions, imports, pitch and hit events — with RLS
-- =====================================================================
-- SEAM WORKS — SCHEMA + RLS PROPOSAL (DRAFT, NOT APPLIED)
-- Status: for review only. Do NOT run against production or any existing
-- database. Intended for a fresh, isolated project after approval.
-- Missing measurements are NULL (never 0). Units are fixed per column.
-- =====================================================================

create type public.org_kind as enum ('training_facility', 'travel_baseball', 'college');
create type public.org_role as enum ('org_admin', 'coach', 'player');
create type public.session_kind as enum ('pitching', 'hitting');
create type public.hand as enum ('R', 'L');

-- ---------- organizations ----------
create table public.organizations (
  id uuid primary key default gen_random_uuid(),
  name text not null,
  kind public.org_kind not null,
  created_at timestamptz not null default now()
);
grant select, insert, update, delete on public.organizations to authenticated;
grant all on public.organizations to service_role;
alter table public.organizations enable row level security;

-- ---------- memberships (roles live here, never on a profile) ----------
create table public.organization_memberships (
  id uuid primary key default gen_random_uuid(),
  org_id uuid not null references public.organizations(id) on delete cascade,
  user_id uuid not null references auth.users(id) on delete cascade,
  role public.org_role not null,
  created_at timestamptz not null default now(),
  unique (org_id, user_id, role)
);
grant select on public.organization_memberships to authenticated;
grant all on public.organization_memberships to service_role;
alter table public.organization_memberships enable row level security;

-- Security-definer helpers avoid recursive RLS on memberships.
create or replace function public.has_org_role(_org uuid, _roles public.org_role[])
returns boolean language sql stable security definer set search_path = public as $$
  select exists (
    select 1 from public.organization_memberships
    where org_id = _org and user_id = auth.uid() and role = any(_roles)
  )
$$;

create or replace function public.is_org_member(_org uuid)
returns boolean language sql stable security definer set search_path = public as $$
  select exists (select 1 from public.organization_memberships where org_id = _org and user_id = auth.uid())
$$;

-- ---------- players ----------
create table public.players (
  id uuid primary key default gen_random_uuid(),
  org_id uuid not null references public.organizations(id) on delete cascade,
  stable_id text not null,               -- e.g. SW-P-0001, used for import matching
  first_name text not null,
  last_name text not null,
  team text,
  throws public.hand,
  bats text check (bats in ('R','L','S')),
  grad_year int,
  position text,
  created_at timestamptz not null default now(),
  unique (org_id, stable_id)
);
create table public.player_external_ids (
  player_id uuid not null references public.players(id) on delete cascade,
  org_id uuid not null references public.organizations(id) on delete cascade,
  external_id text not null,
  source text,
  primary key (org_id, external_id)
);
-- Secure player access: links a login to exactly one player record.
create table public.player_user_links (
  player_id uuid not null references public.players(id) on delete cascade,
  user_id uuid not null references auth.users(id) on delete cascade,
  primary key (player_id, user_id)
);
grant select, insert, update, delete on public.players, public.player_external_ids to authenticated;
grant select on public.player_user_links to authenticated;
grant all on public.players, public.player_external_ids, public.player_user_links to service_role;
alter table public.players enable row level security;
alter table public.player_external_ids enable row level security;
alter table public.player_user_links enable row level security;

create or replace function public.is_linked_player(_player uuid)
returns boolean language sql stable security definer set search_path = public as $$
  select exists (select 1 from public.player_user_links where player_id = _player and user_id = auth.uid())
$$;

-- ---------- imports, sessions, events ----------
create table public.imports (
  id uuid primary key default gen_random_uuid(),
  org_id uuid not null references public.organizations(id) on delete cascade,
  uploaded_by uuid references auth.users(id) on delete set null,
  file_name text not null,
  kind public.session_kind not null,
  rows_total int not null,
  rows_imported int not null,
  rows_skipped int not null,
  duplicates int not null,
  column_mapping jsonb not null,
  created_at timestamptz not null default now()
);
create table public.sessions (
  id uuid primary key default gen_random_uuid(),
  org_id uuid not null references public.organizations(id) on delete cascade,
  import_id uuid references public.imports(id) on delete cascade,
  session_date date not null,
  kind public.session_kind not null,
  label text not null,
  source text
);
create table public.pitch_events (
  id uuid primary key default gen_random_uuid(),
  org_id uuid not null references public.organizations(id) on delete cascade,
  session_id uuid not null references public.sessions(id) on delete cascade,
  player_id uuid not null references public.players(id) on delete cascade,
  fingerprint text not null,
  pitch_type text,
  velo_mph numeric(5,1),
  spin_rpm int,
  hbreak_in numeric(5,1),
  ivbreak_in numeric(5,1),
  release_height_ft numeric(4,2),
  release_side_ft numeric(4,2),
  extension_ft numeric(4,2),
  plate_x_ft numeric(4,2),
  plate_z_ft numeric(4,2),
  batter_side public.hand,
  unique (org_id, fingerprint)          -- server-side duplicate protection on re-import
);
create table public.hit_events (
  id uuid primary key default gen_random_uuid(),
  org_id uuid not null references public.organizations(id) on delete cascade,
  session_id uuid not null references public.sessions(id) on delete cascade,
  player_id uuid not null references public.players(id) on delete cascade,
  fingerprint text not null,
  exit_velo_mph numeric(5,1),
  launch_angle_deg numeric(5,1),
  distance_ft int,
  spray_angle_deg numeric(5,1),
  pitcher_hand public.hand,
  unique (org_id, fingerprint)
);
grant select, insert, update, delete on public.imports, public.sessions, public.pitch_events, public.hit_events to authenticated;
grant all on public.imports, public.sessions, public.pitch_events, public.hit_events to service_role;
alter table public.imports enable row level security;
alter table public.sessions enable row level security;
alter table public.pitch_events enable row level security;
alter table public.hit_events enable row level security;

create index on public.pitch_events (org_id, player_id);
create index on public.pitch_events (session_id);
create index on public.hit_events (org_id, player_id);
create index on public.hit_events (session_id);

-- =====================================================================
-- RLS POLICIES
-- Staff = org_admin or coach. Players see only rows linked to themselves.
-- =====================================================================
create policy "members read org" on public.organizations for select to authenticated
  using (public.is_org_member(id));
create policy "admins update org" on public.organizations for update to authenticated
  using (public.has_org_role(id, array['org_admin']::public.org_role[]));

create policy "read own memberships or admin reads all" on public.organization_memberships for select to authenticated
  using (user_id = auth.uid() or public.has_org_role(org_id, array['org_admin']::public.org_role[]));
-- Membership writes go through a service-role invite flow (no client insert policy).

create policy "staff read players" on public.players for select to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]) or public.is_linked_player(id));
create policy "admins write players" on public.players for all to authenticated
  using (public.has_org_role(org_id, array['org_admin']::public.org_role[]))
  with check (public.has_org_role(org_id, array['org_admin']::public.org_role[]));

create policy "staff manage external ids" on public.player_external_ids for all to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]))
  with check (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]));

create policy "user reads own player link" on public.player_user_links for select to authenticated
  using (user_id = auth.uid());

create policy "staff manage imports" on public.imports for all to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]))
  with check (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]));

create policy "members read sessions" on public.sessions for select to authenticated
  using (public.is_org_member(org_id));
create policy "staff write sessions" on public.sessions for all to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]))
  with check (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]));

create policy "staff or linked player read pitches" on public.pitch_events for select to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]) or public.is_linked_player(player_id));
create policy "staff write pitches" on public.pitch_events for all to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]))
  with check (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]));

create policy "staff or linked player read hits" on public.hit_events for select to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]) or public.is_linked_player(player_id));
create policy "staff write hits" on public.hit_events for all to authenticated
  using (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]))
  with check (public.has_org_role(org_id, array['org_admin','coach']::public.org_role[]));

-- Open review items:
-- * Cross-org integrity: add triggers ensuring player.org_id = session.org_id = event.org_id.
-- * Minor athletes: guardian consent + data retention policy before any real data.
-- * Import commit should be one server-side transaction (RPC) rather than client inserts.