NestJS Casbin module with MongoDB Adapter
  • TypeScript 98%
  • JavaScript 2%
Find a file
Franz Geffke 759a78282f
bump
2025-03-25 16:32:18 +00:00
.circleci Initial commit 2019-10-26 19:29:03 +02:00
.tests add docker-compose to easily run mongodb for tests 2023-05-07 22:40:59 +01:00
.vscode Initial commit 2019-10-26 19:29:03 +02:00
src bump dependencies; NestJS 11 2025-03-25 16:30:25 +00:00
.editorconfig Initial commit 2019-10-26 19:29:03 +02:00
.gitignore Initial commit 2019-10-26 19:29:03 +02:00
.npmignore Initial commit 2019-10-26 19:29:03 +02:00
.prettierignore Initial commit 2019-10-26 19:29:03 +02:00
.travis.yml Initial commit 2019-10-26 19:29:03 +02:00
CHANGELOG.md bump dependencies; Mongo 5.8 2024-02-07 23:49:44 +00:00
docker-compose.yml bump dependencies; NestJS 11 2025-03-25 16:30:25 +00:00
eslint.config.mjs bump dependencies; NestJS 11 2025-03-25 16:30:25 +00:00
LICENSE Initial commit 2019-10-26 19:29:03 +02:00
package.json bump 2025-03-25 16:32:18 +00:00
pnpm-lock.yaml bump dependencies; NestJS 11 2025-03-25 16:30:25 +00:00
README.md corrected readme 2023-05-07 23:33:59 +01:00
tsconfig.json migrated to module 2019-10-26 19:54:34 +02:00
tsconfig.module.json migrated to module 2019-10-26 19:54:34 +02:00

NestJs Casbin Mongodb

NestJS module for Casbin. Supports all adapters

Installation

# https://npm.pantherx.org/-/web/detail/nestjs-casbin
$ pnpm install https://github.com/franzos/nestjs-casbin.git

Setup module

import { Module } from '@nestjs/common';
import { NestCasbinModule } from 'nestjs-casbin';
import { join } from 'path';
import { MongoAdapter } from 'casbin-mongodb-adapter';

const adapter = (async () => await MongoAdapter.newAdapter({
        uri: 'mongodb://localhost:27017',
        collection: 'casbin',
        database: 'casbindb',
}));


@Module({
  imports: [
    NestCasbinModule.register({
      adapter,
      casbinModelPath: join(__dirname, './rbac_model.conf')
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}
import { Module, Injectable } from '@nestjs/common';
import { MongoAdapter } from 'casbin-mongodb-adapter';
import { NestCasbinModuleOptions, NestCasbinOptionsFactory, NestCasbinModule } from 'nestjs-casbin';
import { join } from 'path';

@Injectable()
export class CasbinConfigService implements NestCasbinOptionsFactory {
  async createCasbinOptions(connectionName?: string): Promise<NestCasbinModuleOptions> | NestCasbinModuleOptions {
    
    const adapter = await MongoAdapter.newAdapter({
       uri: 'mongodb://localhost:27017',
       collection: 'casbin',
       database: 'casbindb',
    });

    return {
      adapter,
      casbinModelPath: join(__dirname, './rbac_model.conf')
    };
  }
}

@Module({
  imports: [
    NestCasbinModule.registerAsync({
      useClass: CasbinConfigService
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

Pre 2.0.0 which supports only mongodb adapter

Installation

# https://npm.pantherx.org/-/web/detail/nestjs-casbin
$ pnpm install nestjs-casbin

Setup module

import { Module } from '@nestjs/common';
import { NestCasbinModule } from 'nestjs-casbin-mongodb';
import { join } from 'path';

@Module({
  imports: [
    NestCasbinModule.forRoot({
      uri: 'mongo://localhost:27017',
      casbinModelPath: join(__dirname, './rbac_model.conf'),
      collectionName: 'roles',
      databaseName: 'my-db-name',
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

Tests

Start mongodb with docker-compose and run tests:

docker-compose up
pnpm run test

License

This project is MIT licensed.