# VirtualRef Decorator

Module
import { VirtualRef } from "@tsed/mongoose"
Source/packages/mongoose/src/decorators/virtualRef.ts

# Overview

type VirtualRef<T> = T | null;
function VirtualRef(type: string, foreignField: string): Function;
function VirtualRef(options: MongooseVirtualRefOptions): Function;
function VirtualRef(options: string | MongooseVirtualRefOptions, foreignField?: string): Function;

# Description

Define a property as mongoose virtual reference to other Model (decorated with @Model).

WARNING

To avoid circular dependencies, do not use the virtual reference model in anything except a type declaration. Using the virtual reference model will prevent typescript transpiler from stripping away the import statement and cause a circular import in node.

# Example


@Model()
class FooModel {

   @VirtualRef("Foo2Model", "foo")
   field: VirtualRef<Foo2Model>

   @VirtualRef("Foo2Model", "foo")
   list: VirtualRefs<Foo2Model>
}

@Model()
class Foo2Model {
   @Ref(FooModel)
   foo: Ref<FooModel>;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

:::