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

Add generic support to Collection and it's methods #16315

Merged
merged 9 commits into from
Jun 11, 2017
Merged

Add generic support to Collection and it's methods #16315

merged 9 commits into from
Jun 11, 2017

Conversation

beary
Copy link

@beary beary commented May 4, 2017

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If changing an existing definition:

TypeScript support default generic since version 2.3, So I add generic to collection. When create a collection by DB.collection()orDB.createCollection(), developer could specify a scheme, and when do some query, the result can be static analysd.

These changes don't break the old cold.

eg:

import { MongoClient } from 'mongodb'

interface User {
  name: string
  gender: number
}

interface SomeType {
  // ...
}


MongoClient.connect('...').then(DB => {
  // It's also support old code
  DB
    .collection('User')
    .find()
    .toArray()
    .then(arr => {
      arr.forEach(user => {
        // user: any
      })
    })

  DB
    .collection<User>('User')
    .find()
    .toArray()
    .then(arr => {
      arr.forEach(user => {
        console.log(user.gender) // user: User
      })
    })

  DB
    .collection<User>('User')
    .aggregate<SomeType>([])
    .toArray()
    .then(arr => {
      arr.forEach(item => {
        // item: SomeType
      })
    })

  DB
    .collection<User>('User')
    .findOne({})
    .then(user => {
      // user: User
    })

  DB
    .collection<User>('User')
    .findOne<SomeType>({}, { fields: {/* ... */ } }) // If pass fields option, could specify generic to `findOne()`
    .then(user => {
      // user: SomeType
    })
})

beary added 3 commits May 4, 2017 18:13
TypeScript support default generic since version 2.3, so add generic to Collection.
@dt-bot
Copy link
Member

dt-bot commented May 4, 2017

types/mongodb/index.d.ts

to author (@CaselIT). Could you review this PR?
👍 or 👎?

Checklist

  • pass the Travis CI test?

@beary
Copy link
Author

beary commented May 4, 2017

I checked logs, it says there are some syntax error. But there is no problem on my code, so strange... 😢
My versions:
typescript: 2.3.2
vscode: code-insiders_1.12.0-1493823801_amd64.deb

# test logs
../mongodb/index.d.ts(184,24): error TS1005: ',' expected.
../mongodb/index.d.ts(185,24): error TS1005: ',' expected.
../mongodb/index.d.ts(186,24): error TS1005: ',' expected.
../mongodb/index.d.ts(195,30): error TS1005: ',' expected.
../mongodb/index.d.ts(196,30): error TS1005: ',' expected.
../mongodb/index.d.ts(197,30): error TS1005: ',' expected.
../mongodb/index.d.ts(239,30): error TS1005: ',' expected.
../mongodb/index.d.ts(240,30): error TS1005: ',' expected.
../mongodb/index.d.ts(241,30): error TS1005: ',' expected.
../mongodb/index.d.ts(424,17): error TS1005: ',' expected.
../mongodb/index.d.ts(425,17): error TS1005: ',' expected.
../mongodb/index.d.ts(466,12): error TS1005: ',' expected.
../mongodb/index.d.ts(469,15): error TS1005: ',' expected.
../mongodb/index.d.ts(470,15): error TS1005: ',' expected.

@CaselIT
Copy link
Contributor

CaselIT commented May 4, 2017

@beary the build fails because of microsoft/dtslint#31. Shortly DT will start supporting features that require the latest version of TS about a month after its release.

Otherwise LGTM 👍

@mhegazy mhegazy added the Revision needed This PR needs code changes before it can be merged. label May 4, 2017
@beary
Copy link
Author

beary commented May 5, 2017

Thanks for your response @CaselIT

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a // TypeScript Version: 2.3 comment on line 5.

/** @deprecated */
find(query: Object, fields?: Object, skip?: number, limit?: number, timeout?: number): Cursor<any>;
find<T = TSchema>(query: Object, fields?: Object, skip?: number, limit?: number, timeout?: number): Cursor<T>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is T ever not TSchema?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, When fields parameter was passed in, the results would not have some properties which TSchema has. This is an old api http://mongodb.github.io/node-mongodb-native/1.4/api-generated/collection.html#find

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my expression was puzzled. Actually I means T always same as TSchema, but if developer pass fields parameter, they can specify T according their fields

@yuit
Copy link
Contributor

yuit commented Jun 6, 2017

@beary if you could add the // TypeScript Version: 2.3 it should resolve your build failure and I can promptly merge the PR. Thanks for your contribution!


// Documentation : http://mongodb.github.io/node-mongodb-native/2.2/api/

// TypeScript Version: 2.3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old location was correct.
Packages that depend on mongo will also have to be updated to have this in their header. #16198 is doing this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@hinell
Copy link
Contributor

hinell commented Jun 7, 2017

@beary I had the same problem when have tried to migrate mongodb module to 2.3 ts version.
Seems like if you want to solve this error you have to add ts 2.3 header to all these modules 😐 :

The following packages had errors: acl, agenda, connect-mongo, easy-jsend, express-brute-mongo,
gridfs-stream, mongoose, mongoose-auto-increment, mongoose-deep-populate, mongoose-mock,
mongoose-paginate, mongoose-promise, mongoose-sequence, passport-local-mongoose

@CaselIT
Copy link
Contributor

CaselIT commented Jun 7, 2017

@beary @hinell @andy-ms I'll be doing the same thing for this other PR #16198 where it's still missing a couple of definitions. Maybe its best to merge this after the other one, to avoid duplicating the work?

@hinell
Copy link
Contributor

hinell commented Jun 7, 2017

@CaselIT Are you going to do it manually? If so It is pretty a lot of job there. I think it would be more better or quicker if somebody would write script that automatically resets all the headers in specified modules.

Maybe its best to merge this after the other one, to avoid duplicating the work?

Yep, it is.

@beary
Copy link
Author

beary commented Jun 7, 2017

@hinell Thanks, @CaselIT add // TypeScript Version 2.3 to all those modules on #16198 , I'll waiting for his PR was merged.

@CaselIT No problem. I can do my work after your PR was merged.

@hinell
Copy link
Contributor

hinell commented Jun 7, 2017

@CaselIT At first I didn't understand you. See my post above again pls. 😄

@CaselIT
Copy link
Contributor

CaselIT commented Jun 7, 2017

@hinell Yes I did it manually. It's not too much work

@beary
Copy link
Author

beary commented Jun 8, 2017

@CaselIT I merged #16198 and resolved the conflicts, mind taking one more look 😉

Copy link
Contributor

@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the note I've left it looks good. 👍

@@ -743,9 +741,9 @@ export interface DeleteWriteOpResultObject {
}

//http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#~findAndModifyWriteOpResult
export interface FindAndModifyWriteOpResultObject {
export interface FindAndModifyWriteOpResultObject<TSchema> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to set <TSchema=Default>


// Documentation : http://mongodb.github.io/node-mongodb-native/2.2/api/

// TypeScript Version: 2.3
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -443,7 +443,7 @@ export interface FSyncOptions {
}

// Documentation : http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html
export interface Collection {
export interface Collection<TSchema> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collection was created by DB.collection() and other methods of DB, So I add default type argument to those methods but Collection interface. But the test was failed, I'll and a Default type argument to Collection interface.

@beary
Copy link
Author

beary commented Jun 9, 2017

@CaselIT Thanks for remind, default type argument for FindAndModifyWriteOpResultObject was added. I realize sometimes people would use the interface as a type annotations, the default type argument is necessary.

@yuit yuit merged commit 15ef67b into DefinitelyTyped:master Jun 11, 2017
@beary
Copy link
Author

beary commented Jun 11, 2017

@yuit Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants