#Instead of a Schema from Mongoose we need a interface?

6 messages · Page 1 of 1 (latest)

sly hearth
#

I have a question did u make both? schema / interface for user?

import { HydratedDocument } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

export type UserDocument = HydratedDocument<User>;

@Schema({
  timestamps: true,
  versionKey: false,
  _id: false,
  id: true,
  autoIndex: true,
})
export class User {
  @Prop({ required: true })
  firstName: string;

  @Prop({ required: true })
  lastName: string;

  @Prop({ required: true })
  username: string;

  @Prop({ required: true })
  email: string;

  @Prop({ required: true })
  password: string;
}

export const UserSchema = SchemaFactory.createForClass(User);
``` did it is better when we make both? like user.interface.ts and user.schema.ts? while in the schema is not the createdAt and the updatedAt etc. to use typing later what other say here?
#

Or add optional Like createdAt? To the document?

short jetty
#

If I understand you correctly, yes, you need to add the timestamp fields to the entity class (it's only a document, when the model is initiated either via "new" model.create or via retrieving data from the database). This question also belongs in the #1025201603843719200 forum.

sly hearth
#

Also the Schema is enought? @short jetty

short jetty
#

I have no idea what you are asking.

sly hearth
#

@short jetty see #1149302888930226186