Skip to content

Commit

Permalink
Refact some infer-doc-type import lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Mar 9, 2022
1 parent d5cbee3 commit 11b6bed
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function Schema(obj, options) {
this._indexes = [];
this.methods = {};
this.methodOptions = {};
this.statics = options?.statics || {};
this.statics = (options && options.statics) || {};
this.tree = {};
this.query = {};
this.childSchemas = [];
Expand Down
4 changes: 2 additions & 2 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8117,8 +8117,8 @@ function pick(obj, keys) {
return newObj;
}

describe('Check if statics functions that is supplied in schema option is availabe', function() {
it('should give an array back rather than undefined m0_0aAutoTyped', function M0_0aModelJS() {
describe('Check if statics functions that is supplied in schema option is availabe (m0_0a)', function() {
it('should give an static function back rather than undefined', function M0_0aModelJS() {
const testSchema = new mongoose.Schema({}, { statics: { staticFn() { return 'Returned from staticFn'; } } });
const TestModel = mongoose.model('TestModel', testSchema);
assert.equal(TestModel.staticFn(), 'Returned from staticFn');
Expand Down
3 changes: 1 addition & 2 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Schema, Document, SchemaDefinition, Model } from 'mongoose';
import { Schema, Document, SchemaDefinition, Model, InferSchemaType } from 'mongoose';
import { expectError, expectType } from 'tsd';
import { InferSchemaType } from '../../types/infer-doc-type';

enum Genre {
Action,
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/// <reference path="./error.d.ts" />
/// <reference path="./pipelinestage.d.ts" />
/// <reference path="./schemaoptions.d.ts" />
/// <reference path="./inferschematype.d.ts" />

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

declare module 'mongoose' {

Expand Down
43 changes: 0 additions & 43 deletions types/infer-doc-type.d.ts

This file was deleted.

49 changes: 49 additions & 0 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Schema, InferSchemaType } from 'mongoose';

declare module 'mongoose' {
type ObtainDocumentType<DocDefinition, DocType = any> =
DoesDocTypeExist<DocType> extends true ? DocType : {
[K in keyof (RequiredProperties<DocDefinition> &
OptionalProperties<DocDefinition>)]: ObtainDocumentPropertyType<DocDefinition[K]>;
};

type InferSchemaType<SchemaType> = SchemaType extends Schema<infer DocType, any, any, any, infer DocDefinition>
? DoesDocTypeExist<DocType> extends true ? DocType : DocDefinition
: unknown;

type ObtainSchemaGeneric<TSchema, name extends 'DocType' | 'M' | 'TInstanceMethods' | 'TQueryHelpers' | 'DocDefinition' | 'StaticsMethods'> =
TSchema extends Schema<infer DocType, infer M, infer TInstanceMethods, infer TQueryHelpers, infer DocDefinition, infer StaticsMethods>
? { DocType: DocType, M: M, TInstanceMethods: TInstanceMethods, TQueryHelpers: TQueryHelpers, DocDefinition: DocDefinition, StaticsMethods: StaticsMethods }[name]
: never;
}

type RequiredPropertyKeys<T> = {
[K in keyof T]: T[K] extends { required: true | [true, string | undefined] } ? K : never;
}[keyof T];

type RequiredProperties<T> = {
[K in RequiredPropertyKeys<T>]: T[K];
};

type OptionalPropertyKeys<T> = {
[K in keyof T]: T[K] extends { required: true | [true, string | undefined] } ? never : K;
}[keyof T];

type OptionalProperties<T> = {
[K in OptionalPropertyKeys<T>]?: T[K];
};

type ResolvePropertyType<PropertyValue> = PropertyValue extends (
...args: any
) => any
? ReturnType<PropertyValue>
: PropertyValue;

type ObtainDocumentPropertyType<PropertyValue> = PropertyValue extends Schema<any>
? InferSchemaType<PropertyValue>
: ResolvePropertyType<PropertyValue extends { type: any }
? ResolvePropertyType<PropertyValue['type']>
: PropertyValue
>;

type DoesDocTypeExist<DocType> = keyof DocType extends string ? true : false;

0 comments on commit 11b6bed

Please sign in to comment.