“schéma de manongoose Nestjs” Réponses codées

NESTJS SCHEMA MONGOOSE 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 }
Embarrassed Earthworm

schéma de manongoose Nestjs

// 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);
Embarrassed Earthworm

NESTJS SCHEMA MONGOOSE NEST

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

export type UserDocument = User & Document;

@Schema()
class NestedData {
  @Prop({ type: String })
  foo: string;
  
  @Prop({ type: Number })
  bar: number;
}

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

  @Prop({ type: NestedData })
  data: NestedData;
}

export const UserSchema = SchemaFactory.createForClass(User);
Embarrassed Earthworm

Mongoose de schéma JSON imbriqué

var mongoose =require('mongoose');
var Schema = mongoose.Schema;

var standardmessage = new Schema({
  id: Number,
  name: String,
  type: String,
  message: {
    messageType: String,
    timestamp: Number,
    messagestatus: String
  }
});
Alive Alligator

Réponses similaires à “schéma de manongoose Nestjs”

Questions similaires à “schéma de manongoose Nestjs”

Plus de réponses similaires à “schéma de manongoose Nestjs” dans TypeScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code