-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
23 deletions.
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
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,11 @@ | ||
import {Model, Ref, DynamicRef} from "@tsed/mongoose"; | ||
import {Enum, Required} from "@tsed/common" | ||
|
||
@Model() | ||
export class DynamicRef { | ||
@DynamicRef('type') | ||
dynamicRef: Ref<ModelA | ModelB> | ||
|
||
@Enum(['Mode lA', 'ModelB']) | ||
type: string // This field has to match the referenced model's name | ||
} |
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,54 @@ | ||
import {Property, Schema} from "@tsed/common"; | ||
import {applyDecorators, Store, StoreFn, StoreMerge} from "@tsed/core"; | ||
import {Schema as MongooseSchema} from "mongoose"; | ||
import {MONGOOSE_SCHEMA} from "../constants"; | ||
|
||
export type DynamicRef<T> = T | string; | ||
/** | ||
* Define a property as mongoose reference to other Model (decorated with @Model). | ||
* | ||
* ### Example | ||
* | ||
* ```typescript | ||
* @Model() | ||
* class FooModel { | ||
* | ||
* @DynamicRef('type') | ||
* field: DynamicRef<OtherFooModel | OtherModel> | ||
* | ||
* @Enum(['OtherFooModel', 'OtherModel']) | ||
* type: string | ||
* } | ||
* | ||
* @Model() | ||
* class OtherFooModel { | ||
* } | ||
* | ||
* @Model() | ||
* class OtherModel { | ||
* } | ||
* ``` | ||
* | ||
* @param refPath | ||
* @returns {Function} | ||
* @decorator | ||
* @mongoose | ||
* @property | ||
*/ | ||
export function DynamicRef(refPath: string) { | ||
return applyDecorators( | ||
Property({use: String}), | ||
Schema({ | ||
type: String, | ||
example: "5ce7ad3028890bd71749d477", | ||
description: "Mongoose Ref ObjectId" | ||
}), | ||
StoreFn((store: Store) => { | ||
delete store.get("schema").$ref; | ||
}), | ||
StoreMerge(MONGOOSE_SCHEMA, { | ||
type: MongooseSchema.Types.ObjectId, | ||
refPath | ||
}) | ||
); | ||
} |
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,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<RefTest>; | ||
} | ||
|
||
// THEN | ||
const store = Store.from(Test, "test", descriptorOf(Test, "test")); | ||
|
||
expect(store.get(MONGOOSE_SCHEMA)).to.deep.eq({ | ||
type: Schema.Types.ObjectId, | ||
refPath: "RefTest" | ||
}); | ||
}); | ||
}); |