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 withundefinedids remove every link for that owner (and team).deletePermission/deleteRolemust 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
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
supportsExpiry? | readonly | boolean | true 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()
attachRolePermissions(roleId, permissionIds): Promise<void>;Defined in: packages/core/src/adapter.ts:116
Parameters
| Parameter | Type |
|---|---|
roleId | Id |
permissionIds | readonly Id[] |
Returns
Promise<void>
attachSubjectPermissions()
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
| Parameter | Type |
|---|---|
subject | Subject |
permissionIds | readonly Id[] |
teamId | TeamId |
options? | AttachLinkOptions |
Returns
Promise<void>
attachSubjectRoles()
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
| Parameter | Type |
|---|---|
subject | Subject |
roleIds | readonly Id[] |
teamId | TeamId |
options? | AttachLinkOptions |
Returns
Promise<void>
createPermission()
createPermission(data): Promise<Permission>;Defined in: packages/core/src/adapter.ts:105
Parameters
| Parameter | Type |
|---|---|
data | { guardName: string; name: string; } |
data.guardName | string |
data.name | string |
Returns
Promise<Permission>
createRole()
createRole(data): Promise<Role>;Defined in: packages/core/src/adapter.ts:110
Parameters
| Parameter | Type |
|---|---|
data | { guardName: string; name: string; teamId: TeamId; } |
data.guardName | string |
data.name | string |
data.teamId | TeamId |
Returns
Promise<Role>
deleteExpiredLinks()?
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
| Parameter | Type |
|---|---|
now | Date |
Returns
Promise<DeletedLinkCounts>
deletePermission()
deletePermission(id): Promise<void>;Defined in: packages/core/src/adapter.ts:107
Parameters
| Parameter | Type |
|---|---|
id | Id |
Returns
Promise<void>
deleteRole()
deleteRole(id): Promise<void>;Defined in: packages/core/src/adapter.ts:112
Parameters
| Parameter | Type |
|---|---|
id | Id |
Returns
Promise<void>
detachRolePermissions()
detachRolePermissions(roleId, permissionIds?): Promise<void>;Defined in: packages/core/src/adapter.ts:117
Parameters
| Parameter | Type |
|---|---|
roleId | Id |
permissionIds? | readonly Id[] |
Returns
Promise<void>
detachSubjectPermissions()
detachSubjectPermissions(
subject,
permissionIds,
teamId
): Promise<void>;Defined in: packages/core/src/adapter.ts:139
Parameters
| Parameter | Type |
|---|---|
subject | Subject |
permissionIds | readonly Id[] | undefined |
teamId | TeamId |
Returns
Promise<void>
detachSubjectRoles()
detachSubjectRoles(
subject,
roleIds,
teamId
): Promise<void>;Defined in: packages/core/src/adapter.ts:128
Parameters
| Parameter | Type |
|---|---|
subject | Subject |
roleIds | readonly Id[] | undefined |
teamId | TeamId |
Returns
Promise<void>
detachTeamLinks()?
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
| Parameter | Type |
|---|---|
teamId | Id |
Returns
Promise<void>
findPermissions()
findPermissions(query?): Promise<Permission[]>;Defined in: packages/core/src/adapter.ts:106
Parameters
| Parameter | Type |
|---|---|
query? | FindPermissionsQuery |
Returns
Promise<Permission[]>
findRoles()
findRoles(query?): Promise<Role[]>;Defined in: packages/core/src/adapter.ts:111
Parameters
| Parameter | Type |
|---|---|
query? | FindRolesQuery |
Returns
Promise<Role[]>
getRolePermissionLinks()
getRolePermissionLinks(roleIds): Promise<RolePermissionLink[]>;Defined in: packages/core/src/adapter.ts:115
Parameters
| Parameter | Type |
|---|---|
roleIds | readonly Id[] |
Returns
Promise<RolePermissionLink[]>
getSubjectIdsWithPermissions()
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
| Parameter | Type |
|---|---|
subjectType | string |
permissionIds | readonly Id[] |
teamId | TeamId |
options? | ActiveLinkOptions |
Returns
Promise<Id[]>
getSubjectIdsWithRoles()
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
| Parameter | Type |
|---|---|
subjectType | string |
roleIds | readonly Id[] |
teamId | TeamId |
options? | ActiveLinkOptions |
Returns
Promise<Id[]>
getSubjectLinks()?
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
| Parameter | Type |
|---|---|
subject | Subject |
Returns
Promise<SubjectLinks>
getSubjectPermissionIds()
getSubjectPermissionIds(subject, teamId): Promise<Id[]>;Defined in: packages/core/src/adapter.ts:131
Parameters
| Parameter | Type |
|---|---|
subject | Subject |
teamId | TeamId |
Returns
Promise<Id[]>
getSubjectPermissionLinks()?
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
| Parameter | Type |
|---|---|
subject | Subject |
teamId | TeamId |
Returns
Promise<SubjectPermissionLink[]>
getSubjectRoleIds()
getSubjectRoleIds(subject, teamId): Promise<Id[]>;Defined in: packages/core/src/adapter.ts:120
Parameters
| Parameter | Type |
|---|---|
subject | Subject |
teamId | TeamId |
Returns
Promise<Id[]>
getSubjectRoleLinks()?
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
| Parameter | Type |
|---|---|
subject | Subject |
teamId | TeamId |
Returns
Promise<SubjectRoleLink[]>
getTeamLinks()?
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
| Parameter | Type |
|---|---|
teamId | Id |
Returns
Promise<SubjectLinks>
