nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
521 stars 117 forks source link

Unable to declare TTL index #478

Closed alexy4744 closed 4 years ago

alexy4744 commented 4 years ago

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Using the Prop() decorator, I cannot declare a TTL index using the expires field even if I'm using the raw() function. The issue does not occur if I declare the schema with the traditional way.

// TTL index doesn't get declared

@Schema()
export class Cat extends Document {
  @Prop(
    raw({
      default: () => new Date(Date.now() + 8.64e7),
      expires: 0,
      type: Date,
    }),
  )
  expiresAt: Date;

  @Prop()
  age: number;

  @Prop()
  breed: string;

  @Prop({ unique: true })
  name: string;
}
// Works just fine, TTL is declared correctly

export const CatSchema = new Schema({
  expiresAt: {
    default: (): Date => new Date(Date.now() + 8.64e7),
    expires: 0,
    type: Date,
  },

  age: {
    type: Number,
  },

  breed: {
    type: String,
  },

  name: {
    type: String,
    unique: true,
  },
});

I believe it might have something to do with #413, as CatSchema.obj returns an empty object for the type of expiresAt.

{
  name: { unique: true, type: [Function: String] },
  breed: { type: [Function: String] },
  age: { type: [Function: Number] },
  expiresAt: { default: [Function: default], expires: 0, type: {} }
}

Expected behavior

TTL index should be declared when using the expires field.

Minimal reproduction of the problem with instructions

https://github.com/alexy4744/nestjs-mongoose-reproduction

There are commented sections in src/cats.module.ts and src/cats.service.ts to test between with and without decorators. Start the application and check if the indexes are created using the mongo shell or MongoDB compass.

Environment


Nest version: 7.0.0


For Tooling issues:
- Node version: 14.4.0 
- Platform: Windows 

Others:

kamilmysliwiec commented 4 years ago

Fixed in 7.0.2