Permix

Instance

Learn how to create a new Permix instance

Overview

Instance is the main entry point for Permix that will check permissions in every returned method. To create an instance, you need to use the createPermix function.

TypeScript

Permix is built with TypeScript, providing type safety and validation. Using TypeScript enables autocompletion and compile-time checks for your permission definitions.

/lib/permix.ts
import {  } from 'permix'
 
const  = <{
  : {
    : 'create' | 'edit'
  }
}>()

Generic type

Permix instance accepts a generic type to define permissions.

action

Union type of all actions you want to check on the entity.

/lib/permix.ts
import {  } from 'permix'
 
const  = <{
  : {
    : 'create' | 'edit'
  }
}>()

dataType

Not required, but recommended.

To define a type of your entities, you can pass the dataType property to a generic type. This is useful if you want to check permissions on a specific entity otherwise the type will be unknown.

/lib/permix.ts
import {  } from 'permix'
 
interface Post {
  : string
  : string
  : string
}
 
const  = <{
  : {
    : Post
    : 'create' | 'edit'
  }
}>()
 
.({
  : {
    : true,
    :  => . === 'John Doe'
  }
})
 
const : Post = {
  : '1',
  : 'John Doe',
  : 'Hello World'
}
 
const  = .('post', 'edit', )

PermixDefinition

You can use PermixDefinition type to define your permissions separately from the instance.

/lib/permix.ts
import type { ,  } from 'permix'
import {  } from 'permix'
 
type  = <{
  : {
    : 'create' | 'edit'
  }
}>
 
async function (): <<>> {
  // get user or something like that
  return {
    : {
      : true,
      : false
    }
  }
}
 
const  = <>()
 
.(await ())

Return type

Each Permix instance provides a list of methods to manage and check permissions. These methods are documented in detail on their separate pages.

/lib/permix.ts
import {  } from 'permix'
 
const  = ()
 
.
  • check
  • checkAsync
  • hook
  • hookOnce
  • isReady
  • isReadyAsync
  • setup
  • template

JavaScript

Not using TypeScript? Permix works perfectly fine even with plain JavaScript.

/lib/permix.js
const permix = createPermix()

Initial Rules

You can provide initial rules when creating a Permix instance. This allows you to set up permissions immediately without calling setup separately.

/lib/permix.ts
import {  } from 'permix'
 
const  = <{
  : {
    : 'create' | 'edit'
  }
}>({
  : {
    : true,
    : false
  }
})
 
// Permissions are immediately available
.(.('post', 'create')) // true
.(.()) // true

This is equivalent to:

/lib/permix.ts
import {  } from 'permix'
 
const  = <{
  : {
    : 'create' | 'edit'
  }
}>({
  : {
    : true,
    : false
  }
})

Initial rules are useful when you have permissions that are known at initialization time and don't need to be loaded asynchronously.

You still should pass generic type to createPermix even if you provide initial rules. Otherwise, Permix will not be able to validate your permissions.

On this page