diff --git a/packages/mongoose/src/decorators/dynamicRef.ts b/packages/mongoose/src/decorators/dynamicRef.ts index 4dc0c490169..2e20f851ab5 100644 --- a/packages/mongoose/src/decorators/dynamicRef.ts +++ b/packages/mongoose/src/decorators/dynamicRef.ts @@ -3,18 +3,18 @@ import {applyDecorators, Store, StoreFn, StoreMerge} from "@tsed/core"; import {Schema as MongooseSchema} from "mongoose"; import {MONGOOSE_SCHEMA} from "../constants"; +export type DynamicRef = T | string; /** * Define a property as mongoose reference to other Model (decorated with @Model). * * ### Example * * ```typescript - * * @Model() * class FooModel { * * @DynamicRef('type') - * field: Ref + * field: DynamicRef * * @Enum(['OtherFooModel', 'OtherModel']) * type: string @@ -29,7 +29,7 @@ import {MONGOOSE_SCHEMA} from "../constants"; * } * ``` * - * @param type + * @param refPath * @returns {Function} * @decorator * @mongoose diff --git a/packages/mongoose/test/decorators/dynamicRef.spec.ts b/packages/mongoose/test/decorators/dynamicRef.spec.ts new file mode 100644 index 00000000000..9519402f183 --- /dev/null +++ b/packages/mongoose/test/decorators/dynamicRef.spec.ts @@ -0,0 +1,30 @@ +import {descriptorOf, Store} from "@tsed/core"; +import {expect} from "chai"; +import {Schema} from "mongoose"; +import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../../src/constants"; +import {DynamicRef} from "../../src/decorators"; + +describe("@Ref()", () => { + it("should set metadata", () => { + // GIVEN + + class RefTest { + } + + Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest"); + + // WHEN + class Test { + @DynamicRef("RefTest") + test: DynamicRef; + } + + // THEN + const store = Store.from(Test, "test", descriptorOf(Test, "test")); + + expect(store.get(MONGOOSE_SCHEMA)).to.deep.eq({ + type: Schema.Types.ObjectId, + refPath: "RefTest" + }); + }); +});