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

@biocomputingup/nest-mongo-acl

v1.0.9

Published

A NestJS module for Access Control List (ACL) management using MongoDB, Mongoose, and Typegoose.

Downloads

465

Readme

Nestjs Mongoose Access Control lists

This is a library that offers Granular Access Control to documents in mongodb collections by maintaining access control lists

Table of Contents

Version Chart

| @biocomputingup/nest-mongo-acl | @nestjs/core | | ------------------------------ | ------------ | | ^0.0.2 | ^10.0.0 | | ^1.0.0 | ^11.0.0 |

Installation

npm install @biocomputingup/nest-mongo-acl

yarn add @biocomputingup/nest-mongo-acl

Setup

This library can be used independently with Mongoose directly and with typegoose through @m8a/nestjs/typegoose or @nestjs/mongoose.

Setup in NestJS

import { Module } from '@nestjs/common';
import { AclModule } from '@biocomputingup/nest-mongo-acl';
import { User } from './user.model'; // Import user type/model where it's defined
@Module({
  imports: [
    // ...
    AclModule.forRoot({
      groupFromUser<User>(user: User): string | string[] {
        // This function should return a "tag" or an Array of "tags"
        // Each tag represents a group that the user belongs to
        // This function allows you to map users to tags
        // Later you'll attach access rights to tags to determine access rights
        return `role=${user.role}`;
      }
    })
    // ...
  ]
})
export class AppModule {}

// Outside of NestJS, using this library requires this call to pass the "groupFromUser" function. This is better done before model initialization

AclModule.setupAclConfiguration({
  groupFromUser<User>(user: User): string | string[] {
    // Return group tag(s)
    return `role=${user.role}`;
  }
})

Setup With Typegoose

Add plugin to model and implement the interface WithAcl Extend the model type to include the methods and query helpers

import { AccessControlLists, IAcl, AclMethodsCls} from '@biocomputingup/nest-mongo-acl';
import { type } from '@typegoose/typegoose';
@plugin(AccessControlLists) // Add Plugin to Model
export class Person implements WithAcl { // Implement interface to get correct type
  @prop({type: () => String})
  surname: string;

  // ensure it's optional, as defining it won't ensure it's setup in all the docs
  acl?: Acl
}

// Update the type of the model to include methods and query helpers
type PersonModel = ReturnModelType<typeof Person & AclMethodsCls, IAclQueryHelpers>;

Setup with @nestjs/mongoose

Add plugin to schema and implement the interface WithAcl

import { Prop, Schema as SchemaDec, SchemaFactory } from '@nestjs/mongoose';
import { AccessControlLists, IAcl, IAclMethods, IAclQueryHelpers} from '@biocomputingup/nest-mongo-acl';
import { Document, Model, HydratedDocument} from 'mongoose';
@Schema()
export class Person implements WithAcl { // Implement interface to get correct type
  @Prop({ type: String }) // For mongoose schema
  surname: string;
  
  acl: Acl
}

const PersonSchema = SchemaFactory.createForClass(Person); // For mongoose schema
PersonSchema.plugin(AccessControlLists); // Add Plugin to Schema
export PersonSchema;

// extends model type to add methods and query helpers
export type PersonModel = Model<Person, IAclQueryHelpers, IAclMethods>;

// extends document type to add methods;
export type PersonDocument = HydratedDocument<Person, IAclMethods>;

// Important note:
// using document.model() to retrieve the model instance requires passing the overrided PersonModel as a generic to gain access to the relevant AclMethods

Usage

Grant/Revoke Access to a single document resource

Through a document instance

const doc: PersonDocument = new PersonModel();

// This will mutate the state of the document to grant admins read access to the document
doc.grantAccess('admins', 'read');
// This will mutate the state of the document to revoke admins read access to the document
doc.revokeAccess('admins', 'read');

Through an update query

import { grantAccessTo } from '@biocomputingup/nest-mongo-acl';

const model: PersonModel;
model.updateOne({ /* Select the relevant Document */}, grantAccessTo('admins', 'read'))
// for many modifications use grantToMany
model.updateOne({ /* Select the relevant Document */}, grantToMany([
    ['admins', 'read'], 
    ['editor', 'write']
  ])
)
// Granting public read access to a document (when user is not passed/undefined)
model.updateOne({ /* Select the relevant Document */}, grantPublicAccess('read'));
model.updateOne({ /* Select the relevant Document */}, revokePublicAccess('read'));
// inversely methods revokeAccessTo and revokeToMany can be used

Filter by Access rights

Through Query helpers

const model: PersonModel;
// Filters documents accessible to the public with read access (user not passed)
model.find({ /* relevant criteria */ }).withAccessFor('read');
// Filters documents accessible to the passed user for read access
model.find({ /* relevant criteria */ }).withAccessFor('read', user);

Through direct filter injection

const model: PersonModel;
const user: UserDocument;
model.find({
  $and:[
    { /* relevant criteria */ },
    accessibleBy('read', 'admins') 
  ]
})
// a user can be passed as well
model.find({
  $and:[
    { /* relevant criteria */ },
    accessibleBy('read', user) 
  ]
})

Check Access Rights on documents

const doc: PersonDocument;
const user: User; // The user model
if(doc.hasAccess('admins', 'read')){ // if the admins group has access to this document
  // Do something
}
if(doc.hasAccess(user, 'read')) { // If user has read access to this document
  // Do something
}