-
-
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.
fix(mongoose): fix dynamicRef and improve deserialization support
Closes: #1767
- Loading branch information
Showing
9 changed files
with
315 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"definitions": { | ||
"ClickedLinkEventModel": { | ||
"properties": { | ||
"id": { | ||
"description": "Mongoose ObjectId", | ||
"examples": ["5ce7ad3028890bd71749d477"], | ||
"pattern": "^[0-9a-fA-F]{24}$", | ||
"type": "string" | ||
}, | ||
"url": { | ||
"minLength": 1, | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["url"], | ||
"type": "object" | ||
}, | ||
"SignedUpEventModel": { | ||
"properties": { | ||
"id": { | ||
"description": "Mongoose ObjectId", | ||
"examples": ["5ce7ad3028890bd71749d477"], | ||
"pattern": "^[0-9a-fA-F]{24}$", | ||
"type": "string" | ||
}, | ||
"user": { | ||
"minLength": 1, | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["user"], | ||
"type": "object" | ||
} | ||
}, | ||
"properties": { | ||
"event": { | ||
"description": "Mongoose Ref ObjectId", | ||
"examples": ["5ce7ad3028890bd71749d477"], | ||
"oneOf": [ | ||
{ | ||
"description": "Mongoose Ref ObjectId", | ||
"examples": ["5ce7ad3028890bd71749d477"], | ||
"type": "string" | ||
}, | ||
{ | ||
"$ref": "#/definitions/ClickedLinkEventModel" | ||
}, | ||
{ | ||
"$ref": "#/definitions/SignedUpEventModel" | ||
} | ||
] | ||
}, | ||
"eventType": { | ||
"enum": ["ClickedLinkEventModel", "SignedUpEventModel"], | ||
"type": "string" | ||
}, | ||
"id": { | ||
"description": "Mongoose ObjectId", | ||
"examples": ["5ce7ad3028890bd71749d477"], | ||
"pattern": "^[0-9a-fA-F]{24}$", | ||
"type": "string" | ||
} | ||
}, | ||
"type": "object" | ||
} |
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 |
---|---|---|
@@ -1,13 +1,32 @@ | ||
import {DynamicRef, Model, Ref} from "@tsed/mongoose"; | ||
import {Enum} from "@tsed/schema"; | ||
import {ModelA} from "./modelA"; | ||
import {ModelB} from "./ModelB"; | ||
import {DynamicRef, Model, ObjectID} from "@tsed/mongoose"; | ||
import {Enum, Required} from "@tsed/schema"; | ||
|
||
@Model() | ||
export class MyModel { | ||
@DynamicRef("type") | ||
dynamicRef: Ref<ModelA | ModelB>; | ||
class ClickedLinkEventModel { | ||
@ObjectID("id") | ||
_id: string; | ||
|
||
@Enum("Mode lA", "ModelB") | ||
type: string; // This field has to match the referenced model's name | ||
@Required() | ||
url: string; | ||
} | ||
|
||
@Model() | ||
class SignedUpEventModel { | ||
@ObjectID("id") | ||
_id: string; | ||
|
||
@Required() | ||
user: string; | ||
} | ||
|
||
@Model() | ||
class EventModel { | ||
@ObjectID("id") | ||
_id: string; | ||
|
||
@DynamicRef("eventType", ClickedLinkEventModel, SignedUpEventModel) | ||
event: DynamicRef<ClickedLinkEventModel | SignedUpEventModel>; | ||
|
||
@Enum("ClickedLinkEventModel", "SignedUpEventModel") | ||
eventType: "ClickedLinkEventModel" | "SignedUpEventModel"; | ||
} |
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
168 changes: 168 additions & 0 deletions
168
packages/orm/mongoose/test/dynamicRef.integration.spec.ts
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,168 @@ | ||
import {Enum, getJsonSchema, Required} from "@tsed/schema"; | ||
import {TestMongooseContext} from "@tsed/testing-mongoose"; | ||
import {deserialize, serialize} from "@tsed/json-mapper"; | ||
import {Server} from "./helpers/Server"; | ||
import {DynamicRef, MongooseModel, ObjectID} from "../src"; | ||
import {Model} from "../src/decorators/model"; | ||
|
||
describe("DynamicRef Integration", () => { | ||
@Model() | ||
class ClickedLinkEventModel { | ||
@ObjectID("id") | ||
_id: string; | ||
|
||
@Required() | ||
url: string; | ||
} | ||
|
||
@Model() | ||
class SignedUpEventModel { | ||
@ObjectID("id") | ||
_id: string; | ||
|
||
@Required() | ||
user: string; | ||
} | ||
|
||
@Model() | ||
class EventModel { | ||
@ObjectID("id") | ||
_id: string; | ||
|
||
@DynamicRef("eventType", ClickedLinkEventModel, SignedUpEventModel) | ||
event: DynamicRef<ClickedLinkEventModel | SignedUpEventModel>; | ||
|
||
@Enum("ClickedLinkEventModel", "SignedUpEventModel") | ||
eventType: "ClickedLinkEventModel" | "SignedUpEventModel"; | ||
} | ||
|
||
beforeEach(TestMongooseContext.bootstrap(Server)); | ||
afterEach(TestMongooseContext.clearDatabase); | ||
afterEach(TestMongooseContext.reset); | ||
|
||
describe("JsonSchema", () => { | ||
it("should return the json schema", () => { | ||
expect(getJsonSchema(EventModel)).toEqual({ | ||
definitions: { | ||
ClickedLinkEventModel: { | ||
properties: { | ||
id: { | ||
description: "Mongoose ObjectId", | ||
examples: ["5ce7ad3028890bd71749d477"], | ||
pattern: "^[0-9a-fA-F]{24}$", | ||
type: "string" | ||
}, | ||
url: { | ||
minLength: 1, | ||
type: "string" | ||
} | ||
}, | ||
required: ["url"], | ||
type: "object" | ||
}, | ||
SignedUpEventModel: { | ||
properties: { | ||
id: { | ||
description: "Mongoose ObjectId", | ||
examples: ["5ce7ad3028890bd71749d477"], | ||
pattern: "^[0-9a-fA-F]{24}$", | ||
type: "string" | ||
}, | ||
user: { | ||
minLength: 1, | ||
type: "string" | ||
} | ||
}, | ||
required: ["user"], | ||
type: "object" | ||
} | ||
}, | ||
properties: { | ||
event: { | ||
description: "Mongoose Ref ObjectId", | ||
examples: ["5ce7ad3028890bd71749d477"], | ||
oneOf: [ | ||
{ | ||
description: "Mongoose Ref ObjectId", | ||
examples: ["5ce7ad3028890bd71749d477"], | ||
type: "string" | ||
}, | ||
{ | ||
$ref: "#/definitions/ClickedLinkEventModel" | ||
}, | ||
{ | ||
$ref: "#/definitions/SignedUpEventModel" | ||
} | ||
] | ||
}, | ||
eventType: { | ||
enum: ["ClickedLinkEventModel", "SignedUpEventModel"], | ||
type: "string" | ||
}, | ||
id: { | ||
description: "Mongoose ObjectId", | ||
examples: ["5ce7ad3028890bd71749d477"], | ||
pattern: "^[0-9a-fA-F]{24}$", | ||
type: "string" | ||
} | ||
}, | ||
type: "object" | ||
}); | ||
}); | ||
}); | ||
|
||
describe("serialize()", () => { | ||
it("should serialize (clickedEvent)", async () => { | ||
const EventRepository = TestMongooseContext.get<MongooseModel<EventModel>>(EventModel); | ||
const ClickedEventRepository = TestMongooseContext.get<MongooseModel<ClickedLinkEventModel>>(ClickedLinkEventModel); | ||
|
||
const clickedEvent = await new ClickedEventRepository({url: "https://www.tsed.io"}).save(); | ||
const event1 = await new EventRepository({eventType: "ClickedLinkEventModel", event: clickedEvent}).save(); | ||
|
||
const result1 = serialize(event1, {type: EventModel}); | ||
|
||
expect(result1).toEqual({ | ||
id: event1.id, | ||
eventType: "ClickedLinkEventModel", | ||
event: {id: clickedEvent.id, url: "https://www.tsed.io"} | ||
}); | ||
}); | ||
it("should serialize (SignedUpEventModel)", async () => { | ||
const EventRepository = TestMongooseContext.get<MongooseModel<EventModel>>(EventModel); | ||
const SignedUpEventRepository = TestMongooseContext.get<MongooseModel<SignedUpEventModel>>(SignedUpEventModel); | ||
|
||
const signedUpEvent = await new SignedUpEventRepository({user: "test"}).save(); | ||
const event = await new EventRepository({eventType: "SignedUpEventModel", event: signedUpEvent}).save(); | ||
|
||
const result = serialize(event, {type: EventModel}); | ||
|
||
expect(result).toStrictEqual({ | ||
id: event.id, | ||
eventType: "SignedUpEventModel", | ||
event: {id: signedUpEvent.id, user: "test"} | ||
}); | ||
}); | ||
}); | ||
describe("deserialize()", () => { | ||
it("should serialize (clickedEvent)", async () => { | ||
const result = deserialize<EventModel>( | ||
{ | ||
id: "6229a38b4e23ac98aad216d6", | ||
eventType: "ClickedLinkEventModel", | ||
event: {id: "6229a38b4e23ac98aad216d5", url: "https://www.tsed.io"} | ||
}, | ||
{type: EventModel} | ||
); | ||
|
||
expect(result).toEqual({ | ||
_id: "6229a38b4e23ac98aad216d6", | ||
event: { | ||
_id: "6229a38b4e23ac98aad216d5", | ||
url: "https://www.tsed.io" | ||
}, | ||
eventType: "ClickedLinkEventModel" | ||
}); | ||
expect(result.event).toBeInstanceOf(ClickedLinkEventModel); | ||
}); | ||
}); | ||
}); |
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