npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@trishchuk/nest-casl

v1.1.0

Published

Declarative role-based access control for NestJS with CASL. Supports REST, GraphQL, WebSocket, conditional permissions, field-level restrictions, multi-tenant context, and dynamic DB-driven rules.

Readme

@trishchuk/nest-casl

Declarative, role-based access control for NestJS powered by CASL. Works with REST, GraphQL and WebSocket.

CI Build NPM version

Installation

npm install @trishchuk/nest-casl

Peer dependencies: @nestjs/core, @nestjs/common (>= 7.0.0). Optional: @nestjs/graphql, @nestjs/apollo.

Quick Start

// 1. Define roles
export enum Roles { admin = 'admin', editor = 'editor', viewer = 'viewer' }

// 2. Configure root module
@Module({
  imports: [
    CaslModule.forRoot<Roles>({
      superuserRole: Roles.admin,
      getUserFromRequest: (request) => request.user,
    }),
  ],
})
export class AppModule {}

// 3. Define permissions per feature
const permissions: Permissions<Roles, Post, Actions> = {
  everyone({ can }) {
    can(Actions.read, Post);
  },
  editor({ user, can }) {
    can(Actions.update, Post, { authorId: user.id });
  },
};

@Module({
  imports: [CaslModule.forFeature({ permissions })],
})
export class PostModule {}

// 4. Protect endpoints
@Put(':id')
@UseGuards(AccessGuard)
@UseAbility(Actions.update, Post, PostHook)
async updatePost(@Param('id') id: string, @Body() input: UpdatePostInput) {
  return this.postService.update(id, input);
}

// 5. Use conditions for filtering
@Get()
@UseGuards(AccessGuard)
@UseAbility(Actions.read, Post)
async posts(@CaslFilter() filter: FindOptionsWhere<Post>) {
  return this.postService.findAll({ where: filter });
}

Documentation

| Guide | Description | |-------|-------------| | Configuration | forRoot, forRootAsync, forFeature, async/DB permissions, multi-tenant context | | Permissions | Defining roles, conditions, inheritance, custom actions, multi-tenant | | Conditions | toFilter, toWhere, toSql, toMongo — choosing the right method, ORM integration | | Hooks | Subject hooks, user hooks, paramKey, execution flow | | Decorators | @UseAbility, @CaslConditions, @CaslFilter, @CaslSubject, @CaslUser | | Examples | REST API, multi-tenant SaaS, scope-based, field-level, GraphQL with DB, testing | | Migration | From nest-casl or custom CASL implementation | | API Reference | Complete type signatures for all exports |

Key Features

  • Role-based permissions with can(), cannot(), extend() and everyone/every
  • Conditional access — ownership checks like { userId: user.id } with automatic subject fetching via hooks
  • Multi-transport — HTTP, GraphQL, WebSocket via unified ContextProxy
  • Conditions as queries — convert CASL rules to SQL, MongoDB, or plain filter objects via @CaslConditions() / @CaslFilter()
  • Multi-tenant support — pass tenant context to permission builders via getContextFromRequest
  • Dynamic permissions — load rules from database at runtime via onBuildAbility async hook
  • Custom ConditionsProxyconditionsProxyFactory for role-specific proxy behavior (e.g., admin gets no filter)
  • Field-level restrictionscannot(action, subject, ['field']) with customizable getFieldsFromRequest
  • Full DI integration — root options registered as global NestJS provider, not global mutable state

Comparison with nest-casl

This package is a fork of nest-casl with bug fixes, new features, and architectural improvements. See Migration Guide for a step-by-step upgrade path.

| Feature | nest-casl | @trishchuk/nest-casl | |---------|-----------|---------------------| | Decorators | @UseAbility, @CaslConditions, @CaslSubject, @CaslUser | All of the above + @CaslFilter() | | Conditions output | toSql(), toMongo(), toAst() | All of the above + toFilter(), toWhere(), toQuery(), getRules() | | Permission context | user only | user + custom context via getContextFromRequest | | Multi-tenant support | Manual workaround | Built-in via context in permission builders | | DB-driven permissions | Not supported | onBuildAbility async hook in forFeature() | | Module-scoped metadata | Not supported | moduleName + subjectsMap in forFeature() | | Custom ConditionsProxy | Requires replacing entire guard | conditionsProxyFactory option in forRoot() | | Field extraction | Hardcoded flatten(body) | Customizable via getFieldsFromRequest | | @UseAbility options | (action, subject, hook) | Also accepts { hook, paramKey } object | | Root config storage | Global mutable state (Reflect.defineMetadata) | NestJS DI provider (+ legacy fallback) | | AccessService methods | Synchronous | Async (supports onBuildAbility hooks) | | Internal architecture | Single monolithic AccessService | Decomposed: AbilityResolver, AccessEvaluator, FieldAccessChecker | | CASL type imports | @casl/ability/dist/types/types (private) | Local types.ts (no private path dependency) | | AccessService export from forFeature | Missing (bug #905) | Fixed | | Subject hook with mixed conditions | Broken (bug #923) | Fixed | | ConditionsProxy stale user | Uses pre-hook user | Uses post-hook user |

License

MIT