Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function processFile(props) {
if ('constructor' in ctx && ctx.constructor === undefined) {
ctx.constructorWasUndefined = true;
}

if (!prop.tags) continue;
for (const __tag of prop.tags) {
// the following has been done, because otherwise no type could be applied for intellisense
/** @type {TagObject} */
Expand Down
160 changes: 84 additions & 76 deletions test/types/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
expectError(conn.readyState = 0);

expectType<Promise<Record<string, Error | mongodb.Collection<any>>>>(
conn.createCollections()
conn.createCollections()

Check failure on line 24 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
);

expectType<Connection>(new Connection());
Expand All @@ -45,18 +45,18 @@
expectType<mongodb.MongoClient>(conn.getClient());
expectType<Connection>(conn.setClient(new mongodb.MongoClient('mongodb://127.0.0.1:27017/test')));

expectType<Promise<string>>(conn.transaction(async(res) => {
expectType<mongodb.ClientSession>(res);
return 'a';
expectType<Promise<string>>(conn.transaction(async (res) => {

Check failure on line 48 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Unexpected space before function parentheses
expectType<mongodb.ClientSession>(res);

Check failure on line 49 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
return 'a';

Check failure on line 50 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
}));
expectType<Promise<string>>(conn.transaction(async(res) => {
expectType<mongodb.ClientSession>(res);
return 'a';
expectType<Promise<string>>(conn.transaction(async (res) => {

Check failure on line 52 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Unexpected space before function parentheses
expectType<mongodb.ClientSession>(res);

Check failure on line 53 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
return 'a';

Check failure on line 54 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
}, { readConcern: 'majority' }));

expectType<Promise<string>>(conn.withSession(async(res) => {
expectType<mongodb.ClientSession>(res);
return 'a';
expectType<Promise<string>>(conn.withSession(async (res) => {

Check failure on line 57 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Unexpected space before function parentheses
expectType<mongodb.ClientSession>(res);

Check failure on line 58 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
return 'a';

Check failure on line 59 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 2 spaces but found 4
}));

expectError(conn.user = 'invalid');
Expand All @@ -78,94 +78,102 @@
expectType<Connection>(conn.useDb('test', { useCache: true }));

expectType<Promise<string[]>>(
conn.listCollections().then(collections => collections.map(coll => coll.name))
conn.listCollections().then(collections => collections.map(coll => coll.name))
);

expectType<Promise<string[]>>(
conn.listDatabases().then(dbs => dbs.databases.map(db => db.name))
conn.listDatabases().then(dbs => dbs.databases.map(db => db.name))
);

export function autoTypedModelConnection() {
const AutoTypedSchema = autoTypedSchema();
const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema);
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' } as Partial<InferSchemaType<typeof AutoTypedSchema>>);
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 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 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);
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());
// Model-statics-functions-test
expectType<ReturnType<AutoTypedSchemaType['statics']['staticFn']>>(AutoTypedModel.staticFn());
})();

})();
return AutoTypedModel;
return AutoTypedModel;
}

function schemaInstanceMethodsAndQueryHelpersOnConnection() {
type UserModelQuery = Query<any, HydratedDocument<User>, UserQueryHelpers> & UserQueryHelpers;
interface UserQueryHelpers {
byName(this: UserModelQuery, name: string): this
}
interface User {
name: string;
}
interface UserInstanceMethods {
doSomething(this: HydratedDocument<User>): string;
}
interface UserStaticMethods {
findByName(name: string): Promise<HydratedDocument<User>>;
}
type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods;

const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
name: String
}, {
statics: {
findByName(name: string) {
return connection.model('User').findOne({ name }).orFail();
}
},
methods: {
doSomething() {
return 'test';
}
},
query: {
byName(this: UserModelQuery, name: string) {
return this.where({ name });
}
type UserModelQuery = Query<any, HydratedDocument<User>, UserQueryHelpers> & UserQueryHelpers;
interface UserQueryHelpers {
byName(this: UserModelQuery, name: string): this;
}
});

const TestModel = connection.model<User, UserModel, UserQueryHelpers>('User', userSchema);
interface User {
name: string;
}
interface UserInstanceMethods {
doSomething(this: HydratedDocument<User>): string;
}
interface UserStaticMethods {
findByName(name: string): Promise<HydratedDocument<User>>;
}
type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods;

const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
name: String
}, {
statics: {
findByName(name: string) {
return connection.model('User').findOne({ name }).orFail();
}
},
methods: {
doSomething() {
return 'test';
}
},
query: {
byName(this: UserModelQuery, name: string) {
return this.where({ name });
}
}
});

const TestModel = connection.model<User, UserModel, UserQueryHelpers>('User', userSchema);
}

async function gh15359() {
const res = await conn.bulkWrite([{ model: 'Test', name: 'insertOne', document: { name: 'test1' } }]);
expectType<number>(res.insertedCount);
expectError(res.mongoose.validationErrors);

const res2 = await conn.bulkWrite([{ model: 'Test', name: 'insertOne', document: { name: 'test2' } }], { ordered: false });
expectType<number>(res2.insertedCount);
expectType<Error[] | undefined>(res2.mongoose?.validationErrors);

const res3 = await conn.bulkWrite([
{ model: 'Test', name: 'updateOne', filter: { name: 'test5' }, update: { $set: { num: 42 } } },
{ model: 'Test', name: 'updateOne', filter: { name: 'test4' }, update: { $set: { num: 'not a number' } } }
], { ordered: false });
expectType<number>(res3.insertedCount);
expectType<Error[] | undefined>(res3.mongoose?.validationErrors);
const res = await conn.bulkWrite([{ model: 'Test', name: 'insertOne', document: { name: 'test1' } }]);
expectType<number>(res.insertedCount);
expectError(res.mongoose.validationErrors);

const res2 = await conn.bulkWrite(
[{ model: 'Test', name: 'insertOne', document: { name: 'test2' } }],
{ ordered: false }
);
expectType<number>(res2.insertedCount);
expectType<Error[] | undefined>(res2.mongoose?.validationErrors);

const res3 = await conn.bulkWrite(
[
{ model: 'Test', name: 'updateOne', filter: { name: 'test5' }, update: { $set: { num: 42 } } },
{ model: 'Test', name: 'updateOne', filter: { name: 'test4' }, update: { $set: { num: 'not a number' } } }
],
{ ordered: false }
);
expectType<number>(res3.insertedCount);
expectError((res3 as typeof res3 & { validationErrors?: unknown }).validationErrors);
expectType<Error[] | undefined>(res3.mongoose?.validationErrors);
}
5 changes: 3 additions & 2 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ function gh11085(): void {

const newUser = new UserModel();

let _id: number;
expectError(_id = newUser._id);
expectError(() => {
const _id: number = newUser._id;
});
const _id2: Types.ObjectId = newUser._id;
}

Expand Down