0.1.2 • Published 1 year ago

@aledavidgueva/nestjs-schemas v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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.
0.1.2

1 year ago

0.1.0

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago