Skip to content

Interface: RbacAdapter ​

Defined in: packages/core/src/adapter.ts:97

Storage layer. Implement this to plug rbac-evolution into any database / ORM (Prisma, Drizzle, TypeORM, Mongoose, ...).

Conceptually it maps to the same tables as spatie/laravel-permission: permissions, roles, role_has_permissions, model_has_roles, model_has_permissions.

Rules for implementers:

  • All attach* methods must be idempotent (attaching an existing link is a no-op).
  • detach* methods with undefined ids remove every link for that owner (and team).
  • deletePermission / deleteRole must also remove their links.
  • Uniqueness of names is checked by the engine; adapters may additionally enforce it.

Expiry (optional): an adapter that stores an expiry per subject link sets supportsExpiry: true and implements getSubjectRoleLinks, getSubjectPermissionLinks and deleteExpiredLinks. The engine then passes AttachLinkOptions to the attach methods and ActiveLinkOptions to the reverse lookups, and filters expired links itself everywhere else. Without it, the engine never passes those arguments and refuses an expiresAt with EXPIRY_NOT_SUPPORTED.

Properties ​

PropertyModifierTypeDescriptionDefined in
supportsExpiry?readonlybooleantrue when the adapter stores an expiry per subject link (see above). Default: off. First-party adapters only set it when created with { expiry: true }, because it needs an expires_at column.packages/core/src/adapter.ts:102

Methods ​

attachRolePermissions() ​

ts
attachRolePermissions(roleId, permissionIds): Promise<void>;

Defined in: packages/core/src/adapter.ts:116

Parameters ​

ParameterType
roleIdId
permissionIdsreadonly Id[]

Returns ​

Promise<void>


attachSubjectPermissions() ​

ts
attachSubjectPermissions(
   subject, 
   permissionIds, 
   teamId, 
   options?
): Promise<void>;

Defined in: packages/core/src/adapter.ts:133

options is only passed with supportsExpiry: true; store its expiresAt on every given link.

Parameters ​

ParameterType
subjectSubject
permissionIdsreadonly Id[]
teamIdTeamId
options?AttachLinkOptions

Returns ​

Promise<void>


attachSubjectRoles() ​

ts
attachSubjectRoles(
   subject, 
   roleIds, 
   teamId, 
   options?
): Promise<void>;

Defined in: packages/core/src/adapter.ts:122

options is only passed with supportsExpiry: true; store its expiresAt on every given link.

Parameters ​

ParameterType
subjectSubject
roleIdsreadonly Id[]
teamIdTeamId
options?AttachLinkOptions

Returns ​

Promise<void>


createPermission() ​

ts
createPermission(data): Promise<Permission>;

Defined in: packages/core/src/adapter.ts:105

Parameters ​

ParameterType
data{ guardName: string; name: string; }
data.guardNamestring
data.namestring

Returns ​

Promise<Permission>


createRole() ​

ts
createRole(data): Promise<Role>;

Defined in: packages/core/src/adapter.ts:110

Parameters ​

ParameterType
data{ guardName: string; name: string; teamId: TeamId; }
data.guardNamestring
data.namestring
data.teamIdTeamId

Returns ​

Promise<Role>


ts
deleteExpiredLinks?(now): Promise<DeletedLinkCounts>;

Defined in: packages/core/src/adapter.ts:213

Deletes every role and direct-permission link (any subject, team and guard) whose expiresAt is at or before now. Links without an expiry stay. Used by rbac.pruneExpired().

Parameters ​

ParameterType
nowDate

Returns ​

Promise<DeletedLinkCounts>


deletePermission() ​

ts
deletePermission(id): Promise<void>;

Defined in: packages/core/src/adapter.ts:107

Parameters ​

ParameterType
idId

Returns ​

Promise<void>


deleteRole() ​

ts
deleteRole(id): Promise<void>;

Defined in: packages/core/src/adapter.ts:112

Parameters ​

ParameterType
idId

Returns ​

Promise<void>


detachRolePermissions() ​

ts
detachRolePermissions(roleId, permissionIds?): Promise<void>;

Defined in: packages/core/src/adapter.ts:117

Parameters ​

ParameterType
roleIdId
permissionIds?readonly Id[]

Returns ​

Promise<void>


detachSubjectPermissions() ​

ts
detachSubjectPermissions(
   subject, 
   permissionIds, 
   teamId
): Promise<void>;

Defined in: packages/core/src/adapter.ts:139

Parameters ​

ParameterType
subjectSubject
permissionIdsreadonly Id[] | undefined
teamIdTeamId

Returns ​

Promise<void>


detachSubjectRoles() ​

ts
detachSubjectRoles(
   subject, 
   roleIds, 
   teamId
): Promise<void>;

Defined in: packages/core/src/adapter.ts:128

Parameters ​

ParameterType
subjectSubject
roleIdsreadonly Id[] | undefined
teamIdTeamId

Returns ​

Promise<void>


ts
detachTeamLinks?(teamId): Promise<void>;

Defined in: packages/core/src/adapter.ts:194

Removes every role and direct-permission link held in the team (model_has_* rows only; roles stay). Match the team id exactly. Never called with null (the global scope).

Used by rbac.deleteTeam(). Fallback: one detachSubjectRoles and one detachSubjectPermissions call (with undefined ids) per subject returned by getTeamLinks.

Parameters ​

ParameterType
teamIdId

Returns ​

Promise<void>


findPermissions() ​

ts
findPermissions(query?): Promise<Permission[]>;

Defined in: packages/core/src/adapter.ts:106

Parameters ​

ParameterType
query?FindPermissionsQuery

Returns ​

Promise<Permission[]>


findRoles() ​

ts
findRoles(query?): Promise<Role[]>;

Defined in: packages/core/src/adapter.ts:111

Parameters ​

ParameterType
query?FindRolesQuery

Returns ​

Promise<Role[]>


ts
getRolePermissionLinks(roleIds): Promise<RolePermissionLink[]>;

Defined in: packages/core/src/adapter.ts:115

Parameters ​

ParameterType
roleIdsreadonly Id[]

Returns ​

Promise<RolePermissionLink[]>


getSubjectIdsWithPermissions() ​

ts
getSubjectIdsWithPermissions(
   subjectType, 
   permissionIds, 
   teamId, 
   options?
): Promise<Id[]>;

Defined in: packages/core/src/adapter.ts:160

Ids of subjects of subjectType that have ANY of the given DIRECT permissions in the given team. Unique. With options (only passed with supportsExpiry: true), links expired at options.now don't count.

Parameters ​

ParameterType
subjectTypestring
permissionIdsreadonly Id[]
teamIdTeamId
options?ActiveLinkOptions

Returns ​

Promise<Id[]>


getSubjectIdsWithRoles() ​

ts
getSubjectIdsWithRoles(
   subjectType, 
   roleIds, 
   teamId, 
   options?
): Promise<Id[]>;

Defined in: packages/core/src/adapter.ts:150

Ids of subjects of subjectType that have ANY of the given roles in the given team. Unique. With options (only passed with supportsExpiry: true), links expired at options.now don't count.

Parameters ​

ParameterType
subjectTypestring
roleIdsreadonly Id[]
teamIdTeamId
options?ActiveLinkOptions

Returns ​

Promise<Id[]>


ts
getSubjectLinks?(subject): Promise<SubjectLinks>;

Defined in: packages/core/src/adapter.ts:176

Every role and direct-permission link of the subject, in EVERY team (global included), whatever the guard. Match the subject exactly, like getSubjectRoleIds.

Used by rbac.for(subject).teams() and revokeAll({ allTeams: true }). No fallback: the required methods can only read one team at a time, so without it those two throw ADAPTER_METHOD_MISSING.

Parameters ​

ParameterType
subjectSubject

Returns ​

Promise<SubjectLinks>


getSubjectPermissionIds() ​

ts
getSubjectPermissionIds(subject, teamId): Promise<Id[]>;

Defined in: packages/core/src/adapter.ts:131

Parameters ​

ParameterType
subjectSubject
teamIdTeamId

Returns ​

Promise<Id[]>


ts
getSubjectPermissionLinks?(subject, teamId): Promise<SubjectPermissionLink[]>;

Defined in: packages/core/src/adapter.ts:207

The subject's direct-permission links in the team, like getSubjectRoleLinks.

Parameters ​

ParameterType
subjectSubject
teamIdTeamId

Returns ​

Promise<SubjectPermissionLink[]>


getSubjectRoleIds() ​

ts
getSubjectRoleIds(subject, teamId): Promise<Id[]>;

Defined in: packages/core/src/adapter.ts:120

Parameters ​

ParameterType
subjectSubject
teamIdTeamId

Returns ​

Promise<Id[]>


ts
getSubjectRoleLinks?(subject, teamId): Promise<SubjectRoleLink[]>;

Defined in: packages/core/src/adapter.ts:204

The subject's role links in the team, expired ones included, each with its expiresAt (null = never). Match the subject and team exactly, like getSubjectRoleIds.

Parameters ​

ParameterType
subjectSubject
teamIdTeamId

Returns ​

Promise<SubjectRoleLink[]>


ts
getTeamLinks?(teamId): Promise<SubjectLinks>;

Defined in: packages/core/src/adapter.ts:185

Every role and direct-permission link held in the team, by any subject, whatever the guard. Match the team id exactly. Never called with null (the global scope).

Used by rbac.deleteTeam(). No fallback: the required methods need each subject's type and id, so without it deleteTeam() throws ADAPTER_METHOD_MISSING before writing anything.

Parameters ​

ParameterType
teamIdId

Returns ​

Promise<SubjectLinks>

Released under the MIT License. Inspired by spatie/laravel-permission.