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

@elchinabilov/nestjs-ownership

v1.1.7

Published

A dynamic and recursive access control library for NestJS that enforces branch-level ownership across entities.

Readme

@elchinabilov/nestjs-ownership

A dynamic, recursive branch-based access control module for NestJS.

Installation

npm install @elchinabilov/nestjs-ownership

Nə edir?

Bu modul entitilərdə filial/şöbə (branch) əsaslı sahibliyi məcburi edir. İstifadəçi yalnız özünün aid olduğu branchId-ə sahib olan resurslara daxil ola bilir. Admin rollar üçün bypass var.

Tez başlanğıc

  1. Modulu layihənizə qoşun (asinxron konfiqurasiya ilə):
// app.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule, getDataSourceToken } from "@nestjs/typeorm";
import { OwnershipModule } from "@elchinabilov/nestjs-ownership";
import { DataSource } from "typeorm";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      /* ... */
    }),
    OwnershipModule.forRootAsync({
      imports: [
        TypeOrmModule.forRoot({
          /* ... */
        }),
      ],
      inject: [getDataSourceToken()],
      useFactory: async (dataSource: DataSource) => ({
        dataSource,
        // İstədiyiniz qədər entity üçün xəritə
        map: {
          // Açarı entity adı və ya cədvəl adı ola bilər (bax: OwnershipService.getRepositoryByName)
          menu: {
            // entity-dən branch-a gedən əlaqələr zənciri
            // məsələn: Menu -> Restaurant -> Branch
            path: ["restaurant", "branch"],
            // route param-ın adı (default: "id")
            idParam: "id",
          },
        },
        // İstifadəçinin rolunun saxlandığı sahə (req.user[roleField])
        roleField: "role",
        // Sahibliyin müqayisə olunduğu sahə ( həm user, həm də owner obyektində )
        ownershipField: "branchId",
        // Admin rollar (tam bypass)
        adminRoles: ["admin"],
      }),
    }),
  ],
})
export class AppModule {}
  1. Kontroller və ya metod səviyyəsində dekoratorla entity-ni qeyd edin:
import { Controller, Get, Param, UseGuards } from "@nestjs/common";
import { Ownership, OwnershipGuard } from "@elchinabilov/nestjs-ownership";

@Controller("menus")
@Ownership("menu") // bütün metodlara şamil olunur
@UseGuards(OwnershipGuard)
export class MenuController {
  @Get(":id")
  // İstəsəniz metod səviyyəsində də təyin edə bilərsiniz, idParam-ı override etmək mümkündür
  @Ownership("menu", { idParam: "id" })
  findOne(@Param("id") id: string) {
    // ...
  }
}
  1. İstifadəçinin branchIdrole məlumatı request.user daxilində olmalıdır.

Necə işləyir?

  • Guard request.user içindəki roleField əsasında admin olub-olmadığını yoxlayır. Adminlər üçün bypass edilir.
  • Dekoratordan oxunan entityKeyidParam əsasında resursun id-si params/body/query üzərindən götürülür.
  • OwnershipService.loadOwnerThroughPath TypeORM vasitəsilə xəritədə verilmiş path üzrə LEFT JOIN edərək resursun sahibini (ownerObj) çıxarır.
  • ownerObj[ownershipField] ilə user[ownershipField] müqayisə olunur. Uyğun deyilsə 403 Forbidden atılır.
  • id verilməyən sorğular (məs: siyahı və ya create) guard tərəfindən buraxılır.

Konfiqurasiya parametrləri

  • dataSource: DataSource — TypeORM DataSource obyekti (mütləq)
  • map: OwnershipMap — entity -> { path, idParam } xəritəsi (mütləq)
  • roleField?: string — istifadəçi rol sahəsi; default: "role"
  • ownershipField?: string — müqayisə sahəsi; default: "branchId"
  • adminRoles?: string[] — admin rollar; default: ["admin"]

OwnershipMap tipi:

type OwnershipMap = Record<
  string,
  {
    path?: string[]; // entity-dən branch-a gedən əlaqələr
    idParam?: string; // route param (default: "id")
  }
>;

Qeydlər

  • Modul @Global() olduğu üçün OwnershipGuard qlobal qeydiyyatla gəlir; lakin istəsəniz kontroller səviyyəsində də @UseGuards(OwnershipGuard) ilə tətbiq edə bilərsiniz.
  • Entity açarı olaraq həm entity adı, həm də cədvəl adı istifadə edilə bilər. Daxildə metadata ilə uyğunluq yoxlanılır.
  • src/common/ownership.map.ts faylı geriyə uyğunluq üçün mövcuddur və tipləri src/common/interfaces.ts ilə eyni xəttə gətirilmişdir.

Lisenzya

ISC