Skip to content

Commit

Permalink
fix(): return a typed schema from create for class method
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jan 7, 2021
1 parent 3725961 commit d93ace5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
6 changes: 4 additions & 2 deletions lib/factories/schema.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { TypeMetadataStorage } from '../storages/type-metadata.storage';
import { DefinitionsFactory } from './definitions.factory';

export class SchemaFactory {
static createForClass<T = any>(target: Type<unknown>) {
static createForClass<T extends Type<unknown> = any>(
target: T,
): mongoose.Schema<mongoose.Document<InstanceType<T>>> {
const schemaDefinition = DefinitionsFactory.createForClass(target);
const schemaMetadata = TypeMetadataStorage.getSchemaMetadataByTarget(
target,
);
return new mongoose.Schema(
return new mongoose.Schema<mongoose.Document<InstanceType<T>>>(
schemaDefinition,
schemaMetadata && schemaMetadata.options,
);
Expand Down
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"husky": "4.3.5",
"jest": "26.6.3",
"lint-staged": "10.5.3",
"mongoose": "5.11.5",
"mongoose": "^5.11.10",
"prettier": "2.2.1",
"reflect-metadata": "0.1.13",
"release-it": "14.2.2",
Expand Down

0 comments on commit d93ace5

Please sign in to comment.