Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): apply changes from #11563 to "connection.model" #12240

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions test/types/connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model } from 'mongoose';
import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection } from 'mongoose';
import * as mongodb from 'mongodb';
import { expectError, expectType } from 'tsd';
import { expectAssignable, expectError, expectType } from 'tsd';
import { AutoTypedSchemaType, autoTypedSchema } from './schema.test';

expectType<Connection>(createConnection());
expectType<Connection>(createConnection('mongodb://localhost:27017/test'));
Expand All @@ -9,7 +10,7 @@ expectType<void>(createConnection('mongodb://localhost:27017/test', { appName: '

const conn = createConnection();

expectType<Model<{ name: string }, any, any, any>>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } })));
expectAssignable<Model<{ name: string }, any, any, any>>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } })));
expectType<Model<{ name: string }>>(conn.model<{ name: string }>('Test', new Schema({ name: { type: String } })));

expectType<Promise<Connection>>(conn.openUri('mongodb://localhost:27017/test'));
Expand Down Expand Up @@ -112,3 +113,32 @@ expectType<Connection>(conn.useDb('test'));
expectType<Connection>(conn.useDb('test', {}));
expectType<Connection>(conn.useDb('test', { noListener: true }));
expectType<Connection>(conn.useDb('test', { useCache: true }));

export function autoTypedModelConnection() {
const AutoTypedSchema = autoTypedSchema();
const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema);

(async() => {
// Model-functions-test
// Create should works with arbitrary objects.
const randomObject = await AutoTypedModel.create({ unExistKey: 'unExistKey', description: 'st' });
expectType<AutoTypedSchemaType['schema']['userName']>(randomObject.userName);

const testDoc1 = await AutoTypedModel.create({ userName: 'M0_0a' });
expectType<AutoTypedSchemaType['schema']['userName']>(testDoc1.userName);
expectType<AutoTypedSchemaType['schema']['description']>(testDoc1.description);

const testDoc2 = await AutoTypedModel.insertMany([{ userName: 'M0_0a' }]);
expectType<AutoTypedSchemaType['schema']['userName']>(testDoc2[0].userName);
expectType<AutoTypedSchemaType['schema']['description'] | undefined>(testDoc2[0]?.description);

const testDoc3 = await AutoTypedModel.findOne({ userName: 'M0_0a' });
expectType<AutoTypedSchemaType['schema']['userName'] | undefined>(testDoc3?.userName);
expectType<AutoTypedSchemaType['schema']['description'] | undefined>(testDoc3?.description);

// Model-statics-functions-test
expectType<ReturnType<AutoTypedSchemaType['statics']['staticFn']>>(AutoTypedModel.staticFn());

})();
return AutoTypedModel;
}
14 changes: 14 additions & 0 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Schema, model, Model, Document, Types } from 'mongoose';
import { expectAssignable, expectError, expectType } from 'tsd';
import { autoTypedModel } from './models.test';
import { autoTypedModelConnection } from './connection.test';
import { AutoTypedSchemaType } from './schema.test';

const Drink = model('Drink', new Schema({
Expand Down Expand Up @@ -198,6 +199,19 @@ function autoTypedDocument() {

}

function autoTypedDocumentConnection() {
const AutoTypedModel = autoTypedModelConnection();
const AutoTypeModelInstance = new AutoTypedModel({ unExistProperty: 1, description: 2 });

expectType<AutoTypedSchemaType['schema']['userName']>(AutoTypeModelInstance.userName);
expectType<AutoTypedSchemaType['schema']['favoritDrink']>(AutoTypeModelInstance.favoritDrink);
expectType<AutoTypedSchemaType['schema']['favoritColorMode']>(AutoTypeModelInstance.favoritColorMode);

// Document-Methods-tests
expectType<ReturnType<AutoTypedSchemaType['methods']['instanceFn']>>(new AutoTypedModel().instanceFn());

}

async function gh11960() {
type DocumentType<T> = Document<any> & T;
type SubDocumentType<T> = DocumentType<T> & Types.Subdocument;
Expand Down
6 changes: 6 additions & 0 deletions types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ declare module 'mongoose' {
readonly models: Readonly<{ [index: string]: Model<any> }>;

/** Defines or retrieves a model. */
model<TSchema extends Schema = any>(
name: string,
schema?: TSchema,
collection?: string,
options?: CompileModelOptions
): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
model<T, U, TQueryHelpers = {}>(
name: string,
schema?: Schema<T, any, any, TQueryHelpers, any, any>,
Expand Down