Permix

Setup

Learn how to setup permissions in your project

Overview

After creating Permix instance, you need to define permissions with setup method. You can call setup in any time with any permissions and Permix will replace the previous permissions.

You always should describe all permissions in the setup method that was defined in the Permix generic type.

For role separation, you can use the template method.

Object definition

const permix = createPermix<{
  post: {
    action: 'create'
  }
  comment: {
    action: 'create' | 'update'
  }
}>()
 
permix.setup({
  post: {
    create: true,
  },
  comment: {
    create: true,
    update: true,
  }
})

You can also use enum based permissions. See Enum-based for more information.

Type-Based

When creating a Permix instance, you can define entities that will be used in the setup method for each related entity. This allows you to check permissions for specific data entities. So instead of boolean you can use functions to check permissions.

import {  } from 'permix'
 
interface Post {
  : string
  : string
}
 
const  = <{
  : {
    : Post
    : 'update'
  }
  : {
    : 'update'
  }
}>()
 
.({
  : {
    :  => .
  • authorId
  • id
} : { :
comment: unknown
comment
=> c
} })

unknown

If you cannot define entity types in the Permix instance, the types will be unknown by default, but you can still define them later in the setup method.

This approach is not recommended as it reduces type safety and IDE support.

import {  } from 'permix'
 
const  = <{
  : {
    : 'update'
  }
}>()
 
.({
  : {
    : (: { : string }) => . === user.id
  }
})

Getting Rules

You can get the current rules from an existing Permix instance using the getRules function:

import { getRules } from 'permix'
 
// Get current rules
const rules = getRules(permix)

The getRules function returns the exact rules object that was set using setup, including any permission functions.

On this page