Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

allow refType to be defined in @prop (issue #146) #148

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"scripts": {
"start": "npm run build && node ./lib/typegoose.js",
"build": "rimraf lib && tsc",
"build:tests": "rimraf lib && cd src/test && tsc",
"lint": "tslint --project tsconfig.json",
"test": "npm run lint && npm run cover",
"mocha": "npm run build && mocha ./lib --recursive",
"cover": "npm run build && istanbul cover _mocha -- lib --recursive -R spec",
"mocha": "npm run build:tests && mocha ./lib --recursive",
"cover": "npm run build:tests && istanbul cover _mocha -- lib --recursive -R spec",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
"repository": {
Expand All @@ -24,11 +25,11 @@
"lodash": "4.17.10"
},
"peerDependencies": {
"@types/mongoose": "5.0.10",
"mongoose": "^5.0.0",
"reflect-metadata": "^0.1.8"
},
"devDependencies": {
"@types/mongoose": "5.0.10",
"@types/chai": "4.1.3",
"@types/dotenv": "4.0.3",
"@types/lodash": "4.14.108",
Expand Down
13 changes: 11 additions & 2 deletions src/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface BasePropOptions {

export interface PropOptions extends BasePropOptions {
ref?: any;
refType?: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ˋanyˋ really correct here? What about ˋNumber | String | Bufferˋ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're right. It uses a mongoose.Schema.Types object, but I'll find the correct way to type it.

}

export interface ValidateNumberOptions {
Expand Down Expand Up @@ -86,19 +87,27 @@ const baseProp = (rawOptions, Type, target, key, isArray = false) => {

const ref = rawOptions.ref;
if (ref) {
let refType = mongoose.Schema.Types.ObjectId;
if (rawOptions.refType) {
refType = rawOptions.refType;
}
schema[name][key] = {
...schema[name][key],
type: mongoose.Schema.Types.ObjectId,
type: refType,
ref: ref.name,
};
return;
}

const itemsRef = rawOptions.itemsRef;
if (itemsRef) {
let refType = mongoose.Schema.Types.ObjectId;
if (rawOptions.refType) {
refType = rawOptions.refType;
}
schema[name][key][0] = {
...schema[name][key][0],
type: mongoose.Schema.Types.ObjectId,
type: refType,
ref: itemsRef.name,
};
return;
Expand Down
9 changes: 9 additions & 0 deletions src/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"include": [
"../**/*.ts"
],
"exclude": [

]
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
},
"include": [
"src/**/*.ts"
],
"exclude": [
"src/test/**/*.ts"
]
}