0

I'm trying to create self destructive schema in NestJs(typeScript) project using mangoose and @nestjs/mongoose library, but couldn't able to rectify a way to implement it . I know how to do it in express framework using js but didn't found any documentation for TS with NestJs

Also updated my code like below ,but still isn't working

import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose';
import * as mangoose from 'mongoose';

export type OtpDocument = Otp & mangoose.Document;

// @Schema({ timestamps: { createdAt: { type: Date, expires: 20 } } })
@Schema()
export class Otp {
  @Prop({ type: mangoose.Schema.Types.ObjectId, ref: 'User' })
  user: any;

  @Prop({ expires: 3600 })
  OTP: number;

  @Prop()
  createdAt: Date;

  @Prop({ default: Date.now() + 20000 })
  expireAt: Date;
}

export const OtpSchema = SchemaFactory.createForClass(Otp);
OtpSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
Aniket
  • 127
  • 1
  • 11

1 Answers1

0

It works on Date type ,and can be achieved by adding expires property in @Props decorater ,like

 @Prop({ type: Date, expires: '2m', default: Date.now })
  createdAt: Date;

for more reference here's the full code

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import * as mangoose from 'mongoose';
import { Types } from 'mongoose';

export type OtpDocument = Otp & mangoose.Document;

    @Schema()
    export class Otp {
      @Prop({ type: Date, expires: '2m', default: Date.now })
      createdAt: Date;
    
      @Prop()
      otp: number;
    }
    
    export const OtpSchema = SchemaFactory.createForClass(Otp);
Aniket
  • 127
  • 1
  • 11