-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
types: add VirtualsForModel helper to make it easier to set the corre…
…ct type overrides for lean() Re: Automattic/mongoose#12684
- Loading branch information
Showing
6 changed files
with
89 additions
and
15 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 |
---|---|---|
|
@@ -58,3 +58,5 @@ typings/ | |
.env | ||
|
||
package-lock.json | ||
|
||
test/test.js |
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 |
---|---|---|
@@ -1,17 +1,30 @@ | ||
/// <reference types="../index.d.ts" /> | ||
|
||
import * as mongoose from 'mongoose'; | ||
import * as mongooseLeanVirtuals from "mongoose-lean-virtuals"; | ||
import mongooseLeanVirtuals, { VirtualsForModel } from "mongoose-lean-virtuals"; | ||
|
||
interface Test { | ||
name: string | ||
interface ITest { | ||
name: string | ||
} | ||
/* | ||
const testSchema = new mongoose.Schema({ | ||
name: String | ||
}); | ||
*/ | ||
|
||
const testSchema = new mongoose.Schema<Test>({ | ||
name: String | ||
}); | ||
const testSchema = new mongoose.Schema( | ||
{ name: { type: String, required: true } }, | ||
{ | ||
virtuals: { | ||
nameUpper: { | ||
get() { | ||
return this.name.toUpperCase(); | ||
} | ||
} | ||
} | ||
} | ||
); | ||
|
||
testSchema.plugin(mongooseLeanVirtuals.mongooseLeanVirtuals); | ||
testSchema.plugin(mongooseLeanVirtuals); | ||
|
||
const TestModel = mongoose.model('Test', testSchema); | ||
|
||
TestModel.findOne().lean<ITest & VirtualsForModel<typeof TestModel>>({ virtuals: true }).orFail().then(doc => { | ||
const name: string = doc.name; | ||
const nameUpper: string = doc.nameUpper; | ||
}); |