Skip to content

Commit 11b6bed

Browse files
Refact some infer-doc-type import lines
1 parent d5cbee3 commit 11b6bed

File tree

6 files changed

+54
-49
lines changed

6 files changed

+54
-49
lines changed

lib/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function Schema(obj, options) {
108108
this._indexes = [];
109109
this.methods = {};
110110
this.methodOptions = {};
111-
this.statics = options?.statics || {};
111+
this.statics = (options && options.statics) || {};
112112
this.tree = {};
113113
this.query = {};
114114
this.childSchemas = [];

test/model.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8117,8 +8117,8 @@ function pick(obj, keys) {
81178117
return newObj;
81188118
}
81198119

8120-
describe('Check if statics functions that is supplied in schema option is availabe', function() {
8121-
it('should give an array back rather than undefined m0_0aAutoTyped', function M0_0aModelJS() {
8120+
describe('Check if statics functions that is supplied in schema option is availabe (m0_0a)', function() {
8121+
it('should give an static function back rather than undefined', function M0_0aModelJS() {
81228122
const testSchema = new mongoose.Schema({}, { statics: { staticFn() { return 'Returned from staticFn'; } } });
81238123
const TestModel = mongoose.model('TestModel', testSchema);
81248124
assert.equal(TestModel.staticFn(), 'Returned from staticFn');

test/types/schema.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Schema, Document, SchemaDefinition, Model } from 'mongoose';
1+
import { Schema, Document, SchemaDefinition, Model, InferSchemaType } from 'mongoose';
22
import { expectError, expectType } from 'tsd';
3-
import { InferSchemaType } from '../../types/infer-doc-type';
43

54
enum Genre {
65
Action,

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
/// <reference path="./error.d.ts" />
33
/// <reference path="./pipelinestage.d.ts" />
44
/// <reference path="./schemaoptions.d.ts" />
5+
/// <reference path="./inferschematype.d.ts" />
56

67
import events = require('events');
78
import mongodb = require('mongodb');
89
import mongoose = require('mongoose');
910
import stream = require('stream');
10-
import { InferSchemaType, ObtainDocumentType, ObtainSchemaGeneric } from './infer-doc-type';
1111

1212
declare module 'mongoose' {
1313

types/infer-doc-type.d.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

types/inferschematype.d.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Schema, InferSchemaType } from 'mongoose';
2+
3+
declare module 'mongoose' {
4+
type ObtainDocumentType<DocDefinition, DocType = any> =
5+
DoesDocTypeExist<DocType> extends true ? DocType : {
6+
[K in keyof (RequiredProperties<DocDefinition> &
7+
OptionalProperties<DocDefinition>)]: ObtainDocumentPropertyType<DocDefinition[K]>;
8+
};
9+
10+
type InferSchemaType<SchemaType> = SchemaType extends Schema<infer DocType, any, any, any, infer DocDefinition>
11+
? DoesDocTypeExist<DocType> extends true ? DocType : DocDefinition
12+
: unknown;
13+
14+
type ObtainSchemaGeneric<TSchema, name extends 'DocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'DocDefinition' | 'StaticsMethods'> =
15+
TSchema extends Schema<infer DocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer DocDefinition, infer StaticsMethods>
16+
? { DocType: DocType, M: M, TInstanceMethods: TInstanceMethods, TQueryHelpers: TQueryHelpers, DocDefinition: DocDefinition, StaticsMethods: StaticsMethods }[name]
17+
: never;
18+
}
19+
20+
type RequiredPropertyKeys<T> = {
21+
[K in keyof T]: T[K] extends { required: true | [true, string | undefined] } ? K : never;
22+
}[keyof T];
23+
24+
type RequiredProperties<T> = {
25+
[K in RequiredPropertyKeys<T>]: T[K];
26+
};
27+
28+
type OptionalPropertyKeys<T> = {
29+
[K in keyof T]: T[K] extends { required: true | [true, string | undefined] } ? never : K;
30+
}[keyof T];
31+
32+
type OptionalProperties<T> = {
33+
[K in OptionalPropertyKeys<T>]?: T[K];
34+
};
35+
36+
type ResolvePropertyType<PropertyValue> = PropertyValue extends (
37+
...args: any
38+
) => any
39+
? ReturnType<PropertyValue>
40+
: PropertyValue;
41+
42+
type ObtainDocumentPropertyType<PropertyValue> = PropertyValue extends Schema<any>
43+
? InferSchemaType<PropertyValue>
44+
: ResolvePropertyType<PropertyValue extends { type: any }
45+
? ResolvePropertyType<PropertyValue['type']>
46+
: PropertyValue
47+
>;
48+
49+
type DoesDocTypeExist<DocType> = keyof DocType extends string ? true : false;

0 commit comments

Comments
 (0)