New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@aledavidgueva/nestjs-schemas

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aledavidgueva/nestjs-schemas

Metadata storage for schemas

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

Custom metadata storage for NestJS projects

  • New storage for custom metadata for schemas and props types, with access available for dev by service.
  • Organize decorator annotations in your schemas for better handling and readability.
  • Reduce boilerplate.

Usage

WARNING: Be careful, this documentation is under construction

Schema definition

import { ApiProperty } from '@nestjs/swagger';
import { $Schema, $Prop } from 'nestjs-metadata-storage';

export enum StatusEnum {
  DRAFT = 'DRAFT',
  PUBLISHED = 'PUBLISHED',
}

@$Schema({
  mongoose: {
    timestamps: true,
    collection: 'dummy',
  },
  metadata: {
    customInfo: 'Custom string',
  },
})
export class Dummy {
  @$Prop()
  _id: string;

  @$Prop({
    mongoose: { type: Schema.Types.String, default: null }
    swagger: { type: String, required: false },
  })
  category: string | null;

  @$Prop()
  question: string;

  @$Prop()
  answer: string;

  @$Prop({
    type: 'StatusEnum',
    enum: StatusEnum,
  })
  status: StatusEnum;

  @$Prop()
  position: number;
}

Access to metadata

  • Import MetadataModule in AppModule
import { Module } from '@nestjs/common';
import { MetadataModule } from 'nestjs-metadata-storage';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [MetadataModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  • Inject service and use
import { Controller, Get } from '@nestjs/common';
import { Dummy } from './schemas/faq.dto';
import { MetadataService } from 'nestjs-metadata-storage';
import { ApiExtraModels } from '@nestjs/swagger';

@Controller()
export class AppController {
  constructor(private readonly _metadata: MetadataService) {}

  @Get()
  @ApiExtraModels(Dummy)
  test() {
    console.log(this._metadata.getSchemas());
  }
}

Type helpers for annotations on inheritance

import { IntersectionType, PartialType, $Prop, $Schema } from 'nestjs-metadata-storage';
import { Faq } from './faq.dto';
import { Plan } from './plan.dto';
import { Payment } from './payment.dto';

@$Schema({
  metadata: {
    customInfo: 'Custom string',
  },
})
export class FaqExtended extends IntersectionType(Faq, Plan, PartialType(Payment)) {
  @$Prop({
    swagger: { type: String, required: false },
  })
  extra: string | null;
}

Notes

  • $Schema decorator is optional.
  • @nestjs/swagger is required.
  • MetadataModule is global module.
  • Please, override decorators for improve your code.
  • This project is for provide functionality to other projects.
  • Decorators and type helpers automatically are chained with Swagger decorators for docs annotation.

Keywords

nestjs

FAQs

Package last updated on 24 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts