Teams
Tenant teams and their members, shared with the inbox.
teams stores a tenant's teams and who is on them. Creating, changing, archiving, restoring and purging a team, and changing its members, needs teams.manage. blocks.teams.permissions can split it: create checks create_team, update checks update_team, archive_team and restore_team, purge checks purge_team and purge_teams (the hard delete; archive keeps the row; purgeMany(ids) purges up to 1000 teams in one call, all or nothing), and members checks create_team_member and delete_team_member. An action you leave unset checks manage, so a config without them behaves as before. team_ids_of(user) lists the teams a user belongs to. A signed-in caller may only pass their own id (TEAM_FORBIDDEN otherwise); the service role and direct database sessions may pass any user's.
better-supabase add teamsimport { createTeams } from "better-supabase/blocks/teams";| Table | Holds |
|---|---|
teams | Name, slug, description and status |
team_members | A user on a team, with a foreign key to the tenant membership |
teams has a unique (tenant_id, id) index, and team_members references it
with (tenant_id, team_id), so a member row can't point at another
tenant's team. An app table can use the same composite foreign key.
options.teamIdType is uuid (the default) or bigint. The inbox needs uuid.
With blocks.teams.options.patch: true, create(organizationId, name, description?, fields?, { slug? }) takes the slug the app chose (lowercase
letters and digits joined by single hyphens; TEAM_SLUG_INVALID otherwise)
or derives it from the name, and update(id, name, description?, fields?, { slug?, description? }) changes the slug or description it names;
description: null clears it, which the plain description argument can't.
In SQL they are the slug argument of create_team and the patch jsonb
argument of update_team. archive(id) sets a team's status to archived;
with options.restore: true, restore(id) sets it back to active through
restore_team and records team.restored.
The Next.js example builds its Teams
settings on these: a list from teams.list() with a create dialog, and a
team page that edits the slug, archives, restores and purges the team and
adds or removes members (apps/examples/nextjs/src/features/teams).
Inbox
The inbox assigns a conversation to a teams row. Installing both drops inbox_teams and inbox_team_members after copying their rows. create_inbox_team and set_inbox_team_member call the teams functions.
Over your own tables
Map tables.teams and tables.members onto the app's tables. Team ids are uuid by default. When your team ids are bigint, set options.teamIdType to bigint; the inbox assigns teams by uuid, so it can't be installed next to bigint team ids.
Your own columns
Add columns to the team table with alter table ... add column in your own schema files
(column name to SQL type), then pass a Standard Schema for them as fields.
create and update (the last argument) validates and writes them, and create, update and list return them parsed under
fields. A column whose type is not null without a default is written in
the insert itself, so a required column of your own teams table can be set
on create. The block also takes hooks around its methods; see
extending blocks.
Server code calls the teams instance on db, in the caller's organization. With gen.hooks set, better-supabase gen writes the hooks:
export const { useList: useTeams } = defineDataHooks(
"teams",
(db: AppDb) => db.teams,
);const rows = await db.teams.list(organizationId).orThrow();list returns the teams sorted by name. Each row has its id, name,
slug, description, status (active or archived) and memberIds, the
user ids on the team, so a page can show who is on a team or hide archived
teams without reading team_members. update returns the stored row.
list is deprecated in favor of teamPage (ADR 1077). teamPage pages teams
by name (the default) or createdAt, takes the status filter as a list, and
q searches the name, the slug and the description. Pass organizationId on
each call. teamFacets and teamCount read under the same options, and
getTeam(id) returns one team or a not_found error. The SQL functions are
list_teams (a page) and get_team.
const page = await db.teams
.teamPage({ organizationId, q: "sales", filters: { status: ["active"] } })
.orThrow();"use client";
import { useTeams } from "@/lib/supabase/hooks";
const rows = useTeams({ limit: 50 });Without the client
db.teams is this client. Code that does not go through bsb.db() builds it with a transport and an organization id:
import { createTeams } from "better-supabase/blocks/teams";
import { z } from "zod";
const client = createTeams({
transport,
fields: z.object({ priority: z.enum(["low", "high"]) }),
});Replacing members and archiving
setMembers({ organizationId, teamId, userIds }) makes userIds the team's members. It reads list, adds the missing users with addMember, removes the others with removeMember, and returns { added, removed }. A failure stops the loop with that error, and running it again finishes the change. A team outside the organization fails with not_found. With the employees block it also takes employeeIds: after the users it reads list again, syncs the employees the same way, and returns them as addedEmployees and removedEmployees. A list you leave out stays as it is.
setArchived({ teamId, archived }) calls archive when archived is true and restore otherwise.
Employees as members
With the employees block installed, a team holds
employees next to users. team_members gets an employee_id column
(columns.members.employee), user_id becomes nullable, and each row has a user,
an employee or both. A team can mix them: the owner or an external accountant as
a user, a technician without an account as an employee, and a colleague with an
account as one row with both. Each user and each employee is on a team once.
| Call | SQL |
|---|---|
addMember({ teamId, employeeId }) | create_team_member(team, employee => id) |
removeMember({ teamId, employeeId }) | delete_team_member(team, employee => id) |
setMembers({ ..., employeeIds }) | Both, after the user members |
list() returns memberEmployeeIds | list_teams returns member_employee_ids |
The functions take a user (member) or an employee (employee), never both
(TEAM_MEMBER_INVALID). An employee must belong to the team's organization.
Removing a user whose row has an employee removes the row. memberIds keeps
listing user ids only, and team_ids_of(user) and the policies read user_id,
so access checks don't change. team_member.added and team_member.deleted
carry userId, null for an employee without an account, and employeeId.
user_id follows the employee's account. Linking an account fills it on the
employee's rows and merges a row the user already had on the same team.
Unlinking the account, deleting the auth user or removing the user from the
organization sets it to null and keeps the employee on the team. A row
without an employee still goes with its membership. Deleting the employee
removes its rows.
employees.team_id is the employee's primary team. Setting or changing it adds
a row for the employee on that team. The row on the previous team stays; remove
it with removeMember when the employee leaves that team.
Without the employees block, or with columns.members.employee: null, the
table and functions are the user-only ones above. For an app that adds the
employees block later, better-supabase add --adopt writes the teams data file
as a migration: it fills employee_id for members with an employee record in
the same organization and adds each employee to their primary team. Over your
own team_members table, drop its (team_id, user_id) primary key and the
not null on its user column yourself.
Last updated on
SSO
Verified email domains with auto-join, SAML providers per organization, SSO enforcement in the access token hook, and a SCIM 2.0 endpoint that provisions memberships.
Metrics
Declare metrics with defineMetric, roll them up into metric_points or read small tables directly, and read series and KPIs with a previous-range comparison.