Skip to main content
zerotal

Documentation


Documentation / @zerotal/tenancy / Tenancy

Class: Tenancy

Defined in: tenancy/src/Tenancy.ts:36

Tenancy service — the engine behind the Tenant facade. Implements the current- tenant lifecycle, tenant CRUD against the internal TenantModel, and membership (the tenant_members pivot) with admin-gated mutations.

Resolved from the container as "tenancy"; use it via the Tenant facade.

Constructors

Constructor

new Tenancy(): Tenancy

Returns

Tenancy

Methods

current()

current(): Tenant | null

Defined in: tenancy/src/Tenancy.ts:42

The active tenant, or null outside a tenant boundary.

Returns

Tenant | null


check()

check(): boolean

Defined in: tenancy/src/Tenancy.ts:47

True when there is an active, enabled tenant.

Returns

boolean


id()

id(): number | null

Defined in: tenancy/src/Tenancy.ts:53

The active tenant's id, or null.

Returns

number | null


slug()

slug(): string | null

Defined in: tenancy/src/Tenancy.ts:58

The active tenant's slug, or null.

Returns

string | null


run()

run<T>(tenant, cb): T

Defined in: tenancy/src/Tenancy.ts:72

Run cb with tenant as the active tenant (jobs, CLI, cross-tenant loops).

Type Parameters

T

T

Parameters

tenant

Tenant

cb

() => T

Returns

T


forId()

forId<T>(id, cb): Promise<T>

Defined in: tenancy/src/Tenancy.ts:77

Load the tenant with id and run cb inside its context.

Type Parameters

T

T

Parameters

id

number

cb

() => T | Promise<T>

Returns

Promise<T>


find()

find(slug): Promise<TenantModel | null>

Defined in: tenancy/src/Tenancy.ts:84

Parameters

slug

string

Returns

Promise<TenantModel | null>


findById()

findById(id): Promise<TenantModel | null>

Defined in: tenancy/src/Tenancy.ts:88

Parameters

id

number

Returns

Promise<TenantModel | null>


all()

all(): Promise<TenantModel[]>

Defined in: tenancy/src/Tenancy.ts:92

Returns

Promise<TenantModel[]>


exists()

exists(slug): Promise<boolean>

Defined in: tenancy/src/Tenancy.ts:96

Parameters

slug

string

Returns

Promise<boolean>


create()

create(data): Promise<TenantModel>

Defined in: tenancy/src/Tenancy.ts:106

Create a new tenant. If a user is authenticated on the current request, they become the tenant's first admin member.

Parameters

data

Partial<Tenant> & object

Returns

Promise<TenantModel>


update()

update(data): Promise<TenantModel>

Defined in: tenancy/src/Tenancy.ts:116

Update the current tenant. Throws unless the current user is an admin member.

Parameters

data

Partial<Tenant>

Returns

Promise<TenantModel>


forceUpdate()

forceUpdate(data): Promise<TenantModel>

Defined in: tenancy/src/Tenancy.ts:122

Update the current tenant, bypassing the admin check. Use from trusted code only.

Parameters

data

Partial<Tenant>

Returns

Promise<TenantModel>


delete()

delete(): Promise<void>

Defined in: tenancy/src/Tenancy.ts:129

Delete the current tenant and its memberships. Throws unless current user is admin.

Returns

Promise<void>


forceDelete()

forceDelete(): Promise<void>

Defined in: tenancy/src/Tenancy.ts:135

Delete the current tenant and its memberships, bypassing the admin check.

Returns

Promise<void>


onTenantDeleted()

onTenantDeleted(hook): void

Defined in: tenancy/src/Tenancy.ts:144

Register a cleanup hook fired after a tenant is deleted (drop tenant-owned data).

Parameters

hook

TenantDeletedHook

Returns

void


member()

member<T>(): Promise<T | null>

Defined in: tenancy/src/Tenancy.ts:151

The authenticated user as a member of the current tenant, or null if not a member.

Type Parameters

T

T = BaseModel

Returns

Promise<T | null>


members()

members<T>(): Promise<T[]>

Defined in: tenancy/src/Tenancy.ts:158

Every member of the current tenant, hydrated as User models.

Type Parameters

T

T = BaseModel

Returns

Promise<T[]>


memberCount()

memberCount(): Promise<number>

Defined in: tenancy/src/Tenancy.ts:167

Number of members in the current tenant.

Returns

Promise<number>


isMember()

isMember(userId?): Promise<boolean>

Defined in: tenancy/src/Tenancy.ts:172

True when the user (default: current authenticated user) belongs to the current tenant.

Parameters

userId?

number | null

Returns

Promise<boolean>


isMemberAdmin()

isMemberAdmin(userId?): Promise<boolean>

Defined in: tenancy/src/Tenancy.ts:183

True when the user (default: current authenticated user) is an admin of the current tenant.

Parameters

userId?

number | null

Returns

Promise<boolean>


addMember()

addMember(userId, opts?): Promise<void>

Defined in: tenancy/src/Tenancy.ts:194

Add userId to the current tenant (idempotent). Pass { admin: true } for an admin.

Parameters

userId

number

opts?
admin?

boolean

Returns

Promise<void>


removeMember()

removeMember(userId): Promise<void>

Defined in: tenancy/src/Tenancy.ts:214

Remove userId from the current tenant.

Parameters

userId

number

Returns

Promise<void>


promote()

promote(userId): Promise<void>

Defined in: tenancy/src/Tenancy.ts:222

Grant the member admin rights in the current tenant.

Parameters

userId

number

Returns

Promise<void>


demote()

demote(userId): Promise<void>

Defined in: tenancy/src/Tenancy.ts:227

Revoke the member's admin rights in the current tenant.

Parameters

userId

number

Returns

Promise<void>