Skip to content

Commit

Permalink
feat(mongoose): Added one more parameter to @ref for defining the ref…
Browse files Browse the repository at this point in the history
…erence type
  • Loading branch information
Gustavo Mendonça authored and Romakita committed Jun 20, 2019
1 parent 3d18955 commit e22d38d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/mongoose/src/decorators/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Property, Schema} from "@tsed/common";
import {applyDecorators, Store, StoreFn, StoreMerge} from "@tsed/core";
import {Schema as MongooseSchema} from "mongoose";
import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../constants";
import {MongooseSchemaTypes} from "../interfaces/MongooseSchemaTypes";

export type Ref<T> = T | string;

Expand All @@ -27,13 +28,14 @@ export type Ref<T> = T | string;
* }
* ```
*
* @param model
* @param type
* @returns {Function}
* @decorator
* @mongoose
* @property
*/
export function Ref(type: string | any) {
export function Ref(model: string | any, type: MongooseSchemaTypes = MongooseSchemaTypes.OBJECT_ID) {
return applyDecorators(
Property({use: String}),
Schema({
Expand All @@ -45,8 +47,8 @@ export function Ref(type: string | any) {
delete store.get("schema").$ref;
}),
StoreMerge(MONGOOSE_SCHEMA, {
type: MongooseSchema.Types.ObjectId,
ref: typeof type === "string" ? type : Store.from(type).get(MONGOOSE_MODEL_NAME)
type: MongooseSchema.Types[type],
ref: typeof model === "string" ? model : Store.from(model).get(MONGOOSE_MODEL_NAME)
})
);
}
5 changes: 5 additions & 0 deletions packages/mongoose/src/interfaces/MongooseSchemaTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum MongooseSchemaTypes {
OBJECT_ID = "ObjectId",
STRING = "String",
NUMBER = "Number"
}

0 comments on commit e22d38d

Please sign in to comment.