workspace

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

workspace

The shape of a library that owns tables and imports nothing. Of the four packages in this story it is the one worth copying furthest.

It owns ws_member, it refers to a user by id, and it has no import of authlite or of any other identity library. It cannot: nothing in it names one.

Taking the column as an argument

// This package has no import of the auth library; the application passes
// auth's UserID here.
func MemberJoin(userID sqlb.Expr[string], workspaceID string) sqlb.Join {
	return sqlb.Join{
		Type:  sqlb.JoinLeft,
		Table: TableMember,
		On: []sqlb.Predicate{
			sqlb.EqExpr(MemberUserID, userID),
			sqlb.Eq(MemberWorkspaceID, workspaceID),
		},
	}
}

That signature is the entire design. The join condition is written by the package that owns the table, so it cannot be got wrong by a caller; the other side of it is supplied by whoever is doing the joining. Any identity source works — this one, a different auth library, or a users table an application wrote by hand.

And it is still type-checked. MemberJoin takes an sqlb.Expr[string], so a caller passing a time.Time column, or an Expr[int64] from a schema whose ids are integers, does not compile. Two packages that share no type and no import, and the boundary between them is checked by the compiler.

The rest

Role is a named string type over a closed set, so a role cannot be an arbitrary string variable at a call site. MemberBindings binds through COALESCE because the join is LEFT, and Membership.Exists() is what tells a non-member from a member with empty values — the same pattern, and the same reason, as in phone.

workspaceID is scoped per call rather than per package: the same user in two workspaces is two different memberships, which TestTask_TheSameUserInADifferentWorkspace in contrib/driver-sqlite executes against a real database.

Documentation

Overview

Package workspace is the second-library shape: it owns tables that refer to a user by id, and it does not import the auth library. The caller supplies the user id column, so any identity source works.

Index

Constants

View Source
const (
	TableNameMember  = "ws_member"
	TableAliasMember = "wm"

	ColWorkspaceID = "workspace_id"
	ColUserID      = "user_id"
	ColRole        = "role"
)

The table and column names this plugin owns. Exported so another package can build its own alias over the same table without copying strings.

Variables

View Source
var (
	MemberWorkspaceID = sqlb.Col[string](TableMember, ColWorkspaceID)
	MemberUserID      = sqlb.Col[string](TableMember, ColUserID)
	MemberRole        = sqlb.Col[Role](TableMember, ColRole)
)

The typed columns of TableMember. These are the plugin's schema contract: another package can join on them and scan them, and can do nothing else.

View Source
var TableMember = sqlb.Table{Name: TableNameMember, Alias: TableAliasMember}

TableMember is the membership table under this plugin's alias.

Functions

func MemberBindings

func MemberBindings(dst *Membership) []sqlb.Binding

MemberBindings binds the membership columns to dst.

func MemberJoin

func MemberJoin(userID sqlb.Expr[string], workspaceID string) sqlb.Join

MemberJoin joins one workspace's membership to any string user id column. This package has no import of the auth library; the application passes auth's UserID here.

Types

type Membership

type Membership struct {
	WorkspaceID string
	Role        Role
}

Membership is the user's row in one workspace.

func (*Membership) Exists

func (m *Membership) Exists() bool

Exists reports whether the LEFT join found a row.

type Role

type Role string

Role is a named string type, so a role cannot be an arbitrary string variable at a call site.

const (
	RoleOwner  Role = "owner"
	RoleMember Role = "member"
)

The roles a membership may hold.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL