Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

nestjs mongoose schema virtual

// user.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type UserDocument = User & Document;

@Schema({
  toJSON: { // use toObject for objects
    virtuals: true,
  }
})
export class User {
  @Prop({ type: String })
  first_name: string;
  
  @Prop({ type: String })
  last_name: string;
  
  full_name: string;
}

const UserSchema = SchemaFactory.createForClass(User);
UserSchema.virtual('full_name').get(function (this: UserDocument) {
  return `${this.first_name} ${this.last_name}`;
});
export { UserSchema }
Comment

nestjs mongoose schema

// user.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type UserDocument = User & Document;

@Schema()
export class User {
  @Prop({ type: String, unique: true })
  username: string;
  
  @Prop({ type: String })
  password: string;
}

export const UserSchema = SchemaFactory.createForClass(User);
Comment

PREVIOUS NEXT
Code Example
Typescript :: adonis model preload with condition 
Typescript :: angular show other value when is null 
Typescript :: Entity service async requests and how to make them synch 
Typescript :: adonis where or 
Typescript :: get number of objects in enum c++ 
Typescript :: how to set the contents of a div with jquery 
Typescript :: set image on server with changing 
Typescript :: mongo change all documents on field 
Typescript :: ignore typescript error 
Typescript :: rechartjs yaxis label ticks custom 
Typescript :: denoot 
Typescript :: ionic alert controller handler not dimiss 
Typescript :: for of loop in ts with index 
Typescript :: what is the name of belt around the orbits of earth and mars 
Typescript :: how do i set limits in inputs in python 
Typescript :: pub schedule firebase 
Typescript :: ionic modal controller pass parameter 
Typescript :: typescript string contains 
Typescript :: adonis many to many 
Typescript :: multiple scatter plots in python 
Typescript :: html dom typescript 
Typescript :: react query staletime 
Typescript :: typescript type from enum values 
Typescript :: media breakpoints bootstrap 4 
Typescript :: list of environment python 
Typescript :: alert angular 
Typescript :: recharts bar chart 
Typescript :: lua operators 
Typescript :: ts console.log 
Typescript :: typescript import particular class from file 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =