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

@akuma225/viewmodel

v1.0.3

Published

Package NestJS pour gérer les viewmodels avec support d'audit personnalisable

Readme

@akuma225/viewmodel

Package NestJS pour gérer les viewmodels avec support d'audit personnalisable.

License: MIT

🚀 Fonctionnalités

  • ✅ Classe de base BaseVm extensible pour tous vos viewmodels
  • ✅ Méthodes statiques : create(), createArray(), createPaginated()
  • ✅ Configuration flexible avec forRoot() dans AppModule
  • ✅ Support des propriétés d'audit personnalisables
  • ✅ Transformation personnalisée des propriétés d'audit
  • ✅ Messages d'erreur personnalisables
  • ✅ Support TypeScript complet
  • ✅ Compatible avec Swagger/OpenAPI
  • ✅ Optimisé pour Prisma, TypeORM, Mongoose, etc.

Installation

Depuis GitHub Packages

npm install @akuma225/viewmodel

Si c'est la première fois que vous installez depuis GitHub Packages, vous devrez configurer l'authentification :

  1. Créez un Personal Access Token (PAT) avec les permissions read:packages sur GitHub
  2. Créez un fichier .npmrc à la racine de votre projet avec :
//npm.pkg.github.com/:_authToken=VOTRE_TOKEN_ICI
@akuma225:registry=https://npm.pkg.github.com

Note : Ne commitez jamais votre .npmrc contenant votre token !

Configuration

Dans votre app.module.ts :

import { Module } from '@nestjs/common';
import { ViewmodelModule } from '@akuma225/viewmodel';

@Module({
  imports: [
    ViewmodelModule.forRoot({
      // Fonction personnalisée pour récupérer un utilisateur
      getUserById: async (userId: string | number) => {
        // Votre logique personnalisée
        return await prisma.user.findUnique({ where: { id: userId } });
      },
      
      // Fonction personnalisée pour transformer les propriétés d'audit
      transformAuditProperty: async (property, userId) => {
        // Exemple: transformer l'utilisateur en viewmodel minimal
        const user = await prisma.user.findUnique({ where: { id: userId } });
        return { [property]: user ? new UserMinVm(user) : null };
      },
      
      // Activer les propriétés d'audit par défaut (created_by, updated_by, deleted_by)
      enableDefaultAudit: true,
      
      // Messages d'erreur personnalisés
      notFoundMessage: 'Ressource introuvable',
      notFoundArrayMessage: 'Aucune ressource trouvée',
    }),
  ],
})
export class AppModule {}

Utilisation

Créer un viewmodel

import { BaseVm } from '@akuma225/viewmodel';

export class UserVm extends BaseVm {
  id: string;
  email: string;
  name: string;
  
  created_by: any;
  updated_by: any;
  deleted_by: any;

  constructor(data: any) {
    super(data);
    this.id = data.id;
    this.email = data.email;
    this.name = data.name;
  }
}

Dans vos controllers

import { Controller, Get, Param } from '@nestjs/common';
import { BaseVm, UserVm } from '@akuma225/viewmodel';

@Controller('users')
export class UsersController {
  
  @Get(':id')
  async findOne(@Param('id') id: string) {
    const userData = await this.userService.findOne(id);
    return await UserVm.create(userData);
  }
  
  @Get()
  async findAll() {
    const usersData = await this.userService.findAll();
    return await UserVm.createArray(usersData);
  }
  
  @Get('paginated')
  async findPaginated() {
    const paginatedData = await this.userService.findPaginated();
    return await UserVm.createPaginated(paginatedData);
  }
}

Contrôler les propriétés d'audit

Vous pouvez spécifier quelles propriétés d'audit inclure :

import { AuditProperties } from '@akuma225/viewmodel';

// Inclure uniquement created_by et updated_by
return await UserVm.create(userData, [
  AuditProperties.CREATED_BY,
  AuditProperties.UPDATED_BY,
]);

// Sans propriétés d'audit
return await UserVm.create(userData, []);

API

BaseVm

Méthodes statiques

  • create(data, properties?) - Crée une instance unique
  • createArray(dataArray, properties?) - Crée un tableau d'instances
  • createPaginated(data, properties?) - Crée une réponse paginée

ViewmodelConfig

Configuration disponible pour ViewmodelModule.forRoot() :

  • getUserById?: (userId) => Promise<any> - Fonction pour récupérer un utilisateur
  • transformAuditProperty?: (property, userId) => Promise<any> - Fonction pour transformer les propriétés d'audit
  • enableDefaultAudit?: boolean - Activer/désactiver les propriétés d'audit (défaut: true)
  • notFoundMessage?: string - Message d'erreur personnalisé (défaut: 'Donnée introuvable')
  • notFoundArrayMessage?: string - Message d'erreur pour les tableaux (défaut: 'Données introuvables')

Exemples avancés

Avec Swagger

import { ApiResponseProperty, ApiProperty } from '@nestjs/swagger';
import { BaseVm } from '@akuma225/viewmodel';

export class UserVm extends BaseVm {
  @ApiResponseProperty()
  id: string;
  
  @ApiResponseProperty()
  email: string;
  
  @ApiProperty({ type: UserMinVm })
  created_by: UserMinVm;
  
  constructor(data: any) {
    super(data);
    this.id = data.id;
    this.email = data.email;
  }
}

Avec Prisma

// Configuration dans AppModule
ViewmodelModule.forRoot({
  getUserById: async (userId: string | number) => {
    return await prisma.user.findUnique({ 
      where: { id: String(userId) },
      select: { id: true, email: true, name: true }
    });
  },
  
  transformAuditProperty: async (property, userId) => {
    const user = await prisma.user.findUnique({ 
      where: { id: String(userId) } 
    });
    return { [property]: user ? new UserMinVm(user) : null };
  },
})

📚 Documentation

🤝 Contribution

Les contributions sont les bienvenues ! N'hésitez pas à ouvrir une issue ou soumettre une pull request.

📝 Licence

MIT

⭐ Support

Si ce package vous a été utile, n'hésitez pas à lui donner une étoile sur GitHub !