Skip to content

definePermissions

definePermissions<TRoles>(config): Permissions<TRoles>

Defined in: packages/permissions/src/define-permissions.ts:57

Creates a permission configuration that can be shared between server-side enforcement (@cfast/db) and client-side introspection (@cfast/actions).

Supports two calling styles:

  • Direct: definePermissions(config) when no custom user type is needed.
  • Curried: definePermissions<MyUser>()(config) to get typed where clause user parameters.

TRoles extends readonly string[]

PermissionsConfig<TRoles>

The permissions configuration with roles, grants, and optional hierarchy.

Permissions<TRoles>

A Permissions object containing roles, raw grants, and hierarchy-expanded resolvedGrants.

import { definePermissions, grant } from "@cfast/permissions";
import { eq } from "drizzle-orm";
import { posts, comments } from "./schema";
const permissions = definePermissions({
roles: ["anonymous", "user", "admin"] as const,
grants: {
anonymous: [
grant("read", posts, { where: (p) => eq(p.published, true) }),
],
user: [
grant("read", posts),
grant("create", posts),
grant("update", posts, { where: (p, u) => eq(p.authorId, u.id) }),
],
admin: [grant("manage", "all")],
},
});

definePermissions<TUser>(): <TRoles>(config) => Permissions<TRoles>

Defined in: packages/permissions/src/define-permissions.ts:60

Creates a permission configuration that can be shared between server-side enforcement (@cfast/db) and client-side introspection (@cfast/actions).

Supports two calling styles:

  • Direct: definePermissions(config) when no custom user type is needed.
  • Curried: definePermissions<MyUser>()(config) to get typed where clause user parameters.

TUser

A Permissions object containing roles, raw grants, and hierarchy-expanded resolvedGrants.

<TRoles>(config): Permissions<TRoles>

TRoles extends readonly string[]

PermissionsConfig<TRoles, TUser>

Permissions<TRoles>

import { definePermissions, grant } from "@cfast/permissions";
import { eq } from "drizzle-orm";
import { posts, comments } from "./schema";
const permissions = definePermissions({
roles: ["anonymous", "user", "admin"] as const,
grants: {
anonymous: [
grant("read", posts, { where: (p) => eq(p.published, true) }),
],
user: [
grant("read", posts),
grant("create", posts),
grant("update", posts, { where: (p, u) => eq(p.authorId, u.id) }),
],
admin: [grant("manage", "all")],
},
});