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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@traitjs/nestjs

v1.0.29

Published

TraitJS/NestJS is a library that provides a foundation for a unique architectural idea in NestJS applications. It aims to unify all CRUD operations into a single codebase, eliminating redundancy and the need for copy/pasting. It introduces the concept of

Downloads

14

Readme

TraitJS/NestJS

TraitJS/NestJS is a library that provides a foundation for a unique architectural idea in NestJS applications. It aims to unify all CRUD operations into a single codebase, eliminating redundancy and the need for copy/pasting. It introduces the concept of features/traits, where a trait is a list of dependencies and a function that unites all dependency functionalities into a class, creating a new functionality on top of that.

Features

TraitJS/NestJS provides the following key features:

  • Feature/Trait System: This system allows you to define a set of functionalities (traits) that can be applied to a class (feature). This promotes code reuse and reduces redundancy.

  • CRUD Module Factory: This helper function allows you to create a NestJS module with CRUD operations. It takes a set of default options and a collection of feature options, and returns a NestJS module with the specified features.

  • Compile Features: This helper function compiles a set of features into a single object, which can be used to provide the features as dependencies in a NestJS module.

  • Autoload Types: This helper function allows you to automatically load all types from a specified path. This can be useful for automatically loading all features from a directory.

  • Provide Feature As: This helper function allows you to define how a feature should be provided as a dependency in a NestJS module. This can be useful for customizing the way features are provided in a module.

Usage Examples

Example: Creating a CRUD Module

import { crudModuleFactory } from 'traitjs/nestjs';

interface ICrudFeature
  extends IFeature<ICrudOptions<Type<any>>, string> {
  controller?: ITrait<ICrudOptions<Type<any>>>;
  service?: ITrait<ICrudOptions<Type<any>>>;
  repository?: ITrait<ICrudOptions<Type<any>>>;
}

const crudFeature: ICrudFeature = {
  controller: makeTrait(
    (_, options: ICrudOptions<Type<any>>) => {
      return class BaseController {
      };
    },
  ),
  service: makeTrait(
    (_, options: ICrudOptions<Type<any>>) => {
      return class BaseService {
      };
    },
  ),
  repository: makeTrait(
    (_, options: ICrudOptions<Type<any>>) => {
      return class BaseRepository {
      };
    },
  ),
};

// Define the default options for the module
const defaultOptions = {
  features: [crudFeature],
  options: {anything: 'default'},
  provideFeatureAs: {
    controller: () => '',
    repository: ({ entity }: ICrudOptions<Type<any>>) =>
      crudRepositoryNameFor(entity),
    service: ({ entity }: ICrudOptions<Type<any>>) =>
      crudServiceNameFor(entity),
  },
  controllers: ['controller'],
  providers: ['service', 'repository'],
  exports: ['service', 'repository'],
};

// Create the module
const CrudModule = crudModuleFactory(defaultOptions);

In this example, crudModuleFactory is used to create a NestJS module with CRUD operations. The defaultOptions object defines the default options for the module, and the featureOptions object defines the options for the feature. The resulting CrudModule can be used as a NestJS module.

Example: Using a CRUD Module

import { Module } from '@nestjs/common';
import { CrudModule } from './crud.module';

class TestEntity {}

@CrudModule({
  featureOptions: {
    entity: TestEntity,
    anyOption: 'value',
    anything: 'override',
  },
})
export class AppModule {}

In this example, the CrudModule is used as a NestJS module in the AppModule. The featureOptions object specifies the options for the feature, including the entity type and other options.

Conclusion

TraitJS/NestJS provides a unique approach to structuring your NestJS applications. By using features and traits, you can reduce redundancy and promote code reuse in your application. This library is a proof of concept and serves as a starting point for further exploration. Your feedback, additional functionalities, and different perspectives are highly encouraged and welcomed. Feel free to contribute and enhance the TraitJS/NestJS library according to your specific needs and requirements.