-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongoose): Add VirtualRef decorator
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {PropertyMetadata, PropertyRegistry} from "@tsed/common"; | ||
import {MONGOOSE_SCHEMA} from "../constants"; | ||
|
||
export type VirtualRef<T> = T | null; | ||
export type VirtualRefs<T> = T[]; | ||
|
||
/** | ||
* 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 | ||
* | ||
* ```typescript | ||
* | ||
* @Model() | ||
* class FooModel { | ||
* | ||
* @VirtualRef("Foo2Model", "foo") | ||
* field: VirtualRef<Foo2Model> | ||
* | ||
* @VirtualRef("Foo2Model", "foo") | ||
* list: VirtualRefs<Foo2Model> | ||
* } | ||
* | ||
* @Model() | ||
* class Foo2Model { | ||
* @Ref(FooModel) | ||
* foo: Ref<FooModel>; | ||
* } | ||
* ``` | ||
* | ||
* @param type | ||
* @returns {Function} | ||
* @decorator | ||
* @mongoose | ||
*/ | ||
export function VirtualRef(type: string, foreignField: string) { | ||
return PropertyRegistry.decorate((propertyMetadata: PropertyMetadata) => { | ||
propertyMetadata.store.merge(MONGOOSE_SCHEMA, { | ||
ref: type, | ||
localField: propertyMetadata.name, | ||
foreignField | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {Store} from "@tsed/core"; | ||
import {descriptorOf} from "@tsed/core"; | ||
import {MONGOOSE_SCHEMA} from "../../src/constants"; | ||
import {VirtualRef} from "../../src/decorators"; | ||
import {expect} from "chai"; | ||
|
||
describe("@VirtualRef()", () => { | ||
class Test {} | ||
class RefTest {} | ||
|
||
before(() => { | ||
VirtualRef("RefTest", "foreign")(Test, "test", descriptorOf(Test, "test")); | ||
this.store = Store.from(Test, "test", descriptorOf(Test, "test")); | ||
}); | ||
|
||
it("should set metadata", () => { | ||
expect(this.store.get(MONGOOSE_SCHEMA)).to.deep.eq({ | ||
ref: "RefTest", | ||
foreignField: "foreign", | ||
localField: "test" | ||
}); | ||
}); | ||
}); |