nestjs-seeder
nestjs-seeder copied to clipboard
Generate function static values not being inserted.
Problem:
const data = DataFactory.createForClass(Message).generate(1, {
body: 'Test this overwrites'
});
// Output { "body": "Exercitationem rerum ab quae libero nobis debitis deleniti iusto aut." }
// Expected { "body": "Test this overwrites" }
// message.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Date, Document, Types } from 'mongoose';
import { Factory } from 'nestjs-seeder';
import { Chat } from 'src/chats/chat.schema';
import { User } from 'src/users/user.schema';
@Schema()
export class MessageHistory {
@Prop({ default: '' })
message: string;
@Prop({ type: Date, default: Date.now })
timestamp: Date;
}
// Generate a Mongoose Schema before use as Subdocument
const MessageHistorySchema = SchemaFactory.createForClass(MessageHistory);
@Schema({
timestamps: true,
toJSON: {
virtuals: true
}
})
export class Message {
@Prop({ type: String, ref: 'User' })
user: User;
@Prop({ type: Types.ObjectId, ref: 'Chat', index: true })
chat: Chat;
@Factory((faker) => faker.lorem.sentence())
@Prop({ default: '' })
body: string;
@Prop({ type: Date, default: undefined })
deletedAt: Date;
@Prop({ type: [MessageHistorySchema] })
editHistory: MessageHistory[];
}
export type MessageDocument = Message & Document;
export const MessageSchema = SchemaFactory.createForClass(Message);
MessageSchema.virtual('edited').get(function (this: MessageDocument) {
return this.editHistory.length > 0;
});