NestJS module that provides seamless integration between NestJS and Mongoose ODM for MongoDB database operations
80
Pending
Does it follow best practices?
Impact
80%
1.02xAverage score across 10 eval scenarios
Pending
The risk profile of this skill
Decorators and utilities for defining MongoDB schemas using TypeScript classes with full type safety. The schema definition system allows declarative schema creation using class-based decorators.
Marks a TypeScript class as a Mongoose schema definition.
/**
* Class decorator that marks a TypeScript class as a Mongoose schema
* @param options - Mongoose schema options
* @returns Class decorator function
*/
function Schema(options?: SchemaOptions): ClassDecorator;
type SchemaOptions = mongoose.SchemaOptions;Usage Examples:
import { Schema, Prop } from '@nestjs/mongoose';
// Basic schema
@Schema()
export class User {
@Prop({ required: true })
name: string;
@Prop()
email: string;
}
// Schema with options
@Schema({
timestamps: true,
collection: 'users',
versionKey: false,
})
export class User {
@Prop({ required: true, index: true })
username: string;
@Prop({ select: false })
password: string;
createdAt: Date;
updatedAt: Date;
}Marks a class property as a Mongoose schema property with type and validation options.
/**
* Property decorator that defines a Mongoose schema property
* @param options - Property definition options
* @returns Property decorator function
*/
function Prop(options?: PropOptions): PropertyDecorator;
type PropOptions = Partial<mongoose.SchemaDefinitionProperty> | mongoose.SchemaType;Usage Examples:
import { Schema, Prop } from '@nestjs/mongoose';
import { Types } from 'mongoose';
@Schema()
export class Product {
// Basic property
@Prop()
name: string;
// Required property with validation
@Prop({ required: true, min: 0 })
price: number;
// Property with default value
@Prop({ default: Date.now })
createdAt: Date;
// Array property
@Prop([String])
tags: string[];
// Object reference
@Prop({ type: Types.ObjectId, ref: 'User' })
owner: Types.ObjectId;
// Nested object
@Prop({
type: {
street: String,
city: String,
zipCode: String,
}
})
address: {
street: string;
city: string;
zipCode: string;
};
// Enum property
@Prop({ enum: ['active', 'inactive', 'pending'] })
status: string;
// Property with custom validation
@Prop({
validate: {
validator: (v: string) => v.length >= 3,
message: 'Name must be at least 3 characters long'
}
})
displayName: string;
}Marks a property as a Mongoose virtual property that is computed dynamically.
/**
* Property decorator that defines a Mongoose virtual property
* @param options - Virtual property options
* @returns Property decorator function
*/
function Virtual(options?: VirtualOptions): PropertyDecorator;
interface VirtualOptions {
/** Virtual type options */
options?: any; // VirtualTypeOptions from mongoose
/** Sub-path for nested virtuals */
subPath?: string;
/** Getter function for the virtual property */
get?: (...args: any[]) => any;
/** Setter function for the virtual property */
set?: (...args: any[]) => any;
}Usage Examples:
import { Schema, Prop, Virtual } from '@nestjs/mongoose';
@Schema()
export class User {
@Prop({ required: true })
firstName: string;
@Prop({ required: true })
lastName: string;
@Prop()
email: string;
// Simple virtual property
@Virtual()
get fullName(): string {
return `${this.firstName} ${this.lastName}`;
}
// Virtual with getter and setter
@Virtual({
get: function() {
return this.email?.split('@')[0];
},
set: function(username: string) {
this.email = `${username}@example.com`;
}
})
username: string;
// Virtual populate
@Virtual({
options: {
ref: 'Post',
localField: '_id',
foreignField: 'author',
}
})
posts: any[];
}For complex nested objects that should bypass type transformation:
import { raw } from '@nestjs/mongoose';
@Schema()
export class Analytics {
@Prop(raw({
impressions: { type: Number, default: 0 },
clicks: { type: Number, default: 0 },
conversions: { type: Number, default: 0 },
metadata: mongoose.Schema.Types.Mixed
}))
stats: Record<string, any>;
}For inheritance-based schema patterns:
@Schema()
export class Animal {
@Prop({ required: true })
name: string;
@Prop({ required: true })
species: string;
}
@Schema()
export class Dog extends Animal {
@Prop()
breed: string;
@Prop()
isGoodBoy: boolean;
}
@Schema()
export class Cat extends Animal {
@Prop()
livesRemaining: number;
}@Schema({
indexes: [
{ name: 1 },
{ email: 1, username: 1 },
{ createdAt: -1 },
{ location: '2dsphere' }
]
})
export class User {
@Prop({ required: true, index: true })
name: string;
@Prop({ unique: true })
email: string;
@Prop({ index: true })
username: string;
@Prop({ index: '2dsphere' })
location: {
type: string;
coordinates: number[];
};
}Internal interfaces used by the metadata storage system:
interface PropertyMetadata {
target: Function;
propertyKey: string;
options: PropOptions;
}
interface SchemaMetadata {
target: Function;
options?: SchemaOptions;
}
interface VirtualMetadataInterface {
target: Function;
propertyKey: string;
options?: VirtualOptions;
}docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10