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

[Deps] Update to Mongoose 6 #170

Merged
merged 10 commits into from
Jun 15, 2024
Merged

Conversation

dchege711
Copy link
Owner

No description provided.

The goal is to get to v8, the current version. But [1] advises to go
major version by major version instead of making an abrupt jump.

mongoose@6 needs mongodb@4, but this requirement is already satisfied.

[1]: https://mongoosejs.com/docs/migrating_to_6.html
Resolves this build error:

```log
src/models/CardsMongoDB.ts:86:23 - error TS2769: No overload matches this call.
  Overload 1 of 3, '(filter?: FilterQuery<ICardRaw> | undefined, projection?: ProjectionType<ICardRaw> | null | undefined, options?: QueryOptions<ICardRaw> | null | undefined, callback?: Callback<...> | undefined): Query<...>', gave the following error.
    Argument of type 'Partial<ICard>' is not assignable to parameter of type 'FilterQuery<ICardRaw>'.
      Type 'Partial<ICard>' is not assignable to type 'RootQuerySelector<ICardRaw>'.
        Types of property '$where' are incompatible.
          Type 'Record<string, unknown> | undefined' is not assignable to type 'string | Function | undefined'.
            Type 'Record<string, unknown>' is not assignable to type 'string | Function | undefined'.
              Type 'Record<string, unknown>' is missing the following properties from type 'Function': apply, call, bind, prototype, and 5 more.
  Overload 2 of 3, '(filter?: FilterQuery<ICardRaw> | undefined, projection?: ProjectionType<ICardRaw> | null | undefined, callback?: Callback<(Document<unknown, any, ICardRaw> & Omit<...>) | null> | undefined): Query<...>', gave the following error.
    Argument of type 'Partial<ICard>' is not assignable to parameter of type 'FilterQuery<ICardRaw>'.
  Overload 3 of 3, '(filter?: FilterQuery<ICardRaw> | undefined, callback?: Callback<(Document<unknown, any, ICardRaw> & Omit<ICardRaw & Required<{ _id: string; }>, never>) | null> | undefined): Query<...>', gave the following error.
    Argument of type 'Partial<ICard>' is not assignable to parameter of type 'FilterQuery<ICardRaw>'.

86   return Card.findOne(query).select(projection).exec();
                         ~~~~~
```
Fixes this build error:

```log
src/models/CardsMongoDB.ts:260:11 - error TS2345: Argument of type 'object' is not assignable to parameter of type 'string | { [key: string]: SortOrder | { $meta: "textScore"; }; } | [string, SortOrder][] | null | undefined'.

260     .sort(queryObject.sortCriteria)
              ~~~~~~~~~~~~~~~~~~~~~~~~
```
…unique"]`

Fixes these errors:

```log
src/models/mongoose_models/Token.ts:19:5 - error TS2322: Type '{ type: StringConstructor; required: true; unique: (string | true)[]; }' is not assignable to type 'SchemaDefinitionProperty<string> | undefined'.
  Types of property 'unique' are incompatible.
    Type '(string | true)[]' is not assignable to type 'number | boolean | undefined'.

19     token_id: {
       ~~~~~~~~

src/models/mongoose_models/UserSchema.ts:28:5 - error TS2322: Type '{ type: StringConstructor; required: true; unique: (string | true)[]; immutable: true; match: RegExp; }' is not assignable to type 'SchemaDefinitionProperty<string> | undefined'.
  Types of property 'unique' are incompatible.
    Type '(string | true)[]' is not assignable to type 'number | boolean | undefined'.

28     username: {
       ~~~~~~~~
```

AFAICT, the custom string doesn't seem like it was ever supported in v5 [1].

[1]: https://mongoosejs.com/docs/5.x/docs/validation.html#the-unique-option-is-not-a-validator
```log
src/models/MetadataMongoDB.ts:117:46 - error TS2769: No overload matches this call.
  Overload 1 of 3, '(filter?: FilterQuery<IMetadataRaw> | undefined, projection?: ProjectionType<IMetadataRaw> | null | undefined, options?: QueryOptions<...> | ... 1 more ... | undefined, callback?: Callback<...> | undefined): Query<...>', gave the following error.
    Argument of type 'Partial<IMetadata>' is not assignable to parameter of type 'FilterQuery<IMetadataRaw>'.
      Type 'Partial<IMetadata>' is not assignable to type 'RootQuerySelector<IMetadataRaw>'.
        Types of property '$where' are incompatible.
          Type 'Record<string, unknown> | undefined' is not assignable to type 'string | Function | undefined'.
  Overload 2 of 3, '(filter?: FilterQuery<IMetadataRaw> | undefined, projection?: ProjectionType<IMetadataRaw> | null | undefined, callback?: Callback<...> | undefined): Query<...>', gave the following error.
    Argument of type 'Partial<IMetadata>' is not assignable to parameter of type 'FilterQuery<IMetadataRaw>'.
  Overload 3 of 3, '(filter?: FilterQuery<IMetadataRaw> | undefined, callback?: Callback<(Document<unknown, any, IMetadataRaw> & Omit<IMetadataRaw & { ...; }, never>) | null> | undefined): Query<...>', gave the following error.
    Argument of type 'Partial<IMetadata>' is not assignable to parameter of type 'FilterQuery<IMetadataRaw>'.

117   const metadataDoc = await Metadata.findOne(metadataQuery).exec();
                                                 ~~~~~~~~~~~~~
```
This package already existed but as a dependency of Mongoose. This
commit brings it to the top-level dependencies so that we can take
advantage of MongoDB's TS types. [1]

We had not used @types/mongodb, so removing that package isn't
applicable to us.

[1]: https://github.com/mongodb/node-mongodb-native/blob/4.0/docs/CHANGES_4.0.0.md#typescript
```log
src/models/MetadataMongoDB.ts:204:3 - error TS2322: Type 'Promise<DeleteResult>' is not assignable to type 'Promise<void>'.
  Type 'DeleteResult' is not assignable to type 'void'.

204   return Metadata.deleteMany({ createdById: payload.userIDInApp }).exec();
      ~~~~~~
```

[1]: https://mongoosejs.com/docs/6.x/docs/migrating_to_6.html#mongodb-driver-40
```log
src/models/mongoose_models/MetadataCardSchema.ts:47:5 - error TS2322: Type '{ type: MapConstructor; default: { cardIDs: never[]; length: number; dailyTarget: number; timeStamp: () => number; }; }' is not assignable to type 'SchemaDefinitionProperty<IStreak> | undefined'.
  Type '{ type: MapConstructor; default: { cardIDs: never[]; length: number; dailyTarget: number; timeStamp: () => number; }; }' is not assignable to type 'undefined'.

47     streak: {
       ~~~~~~
```
```log
src/models/DailyTasks.ts:57:29 - error TS2345: Argument of type '{ updateOne: { filter?: FilterQuery<IMetadata> | undefined; update?: UpdateQuery<IMetadata> | undefined; }; }[]' is not assignable to parameter of type 'AnyBulkWriteOperation<IMetadataRaw>[]'.
  Type '{ updateOne: { filter?: FilterQuery<IMetadata> | undefined; update?: UpdateQuery<IMetadata> | undefined; }; }' is not assignable to type 'AnyBulkWriteOperation<IMetadataRaw>'.
    Type '{ updateOne: { filter?: FilterQuery<IMetadata> | undefined; update?: UpdateQuery<IMetadata> | undefined; }; }' is not assignable to type '{ updateOne: UpdateOneModel<IMetadataRaw>; }'.
      The types of 'updateOne.filter' are incompatible between these types.
        Type 'FilterQuery<IMetadata> | undefined' is not assignable to type 'Filter<IMetadataRaw>'.
          Type 'undefined' is not assignable to type 'Filter<IMetadataRaw>'.

57   return Metadata.bulkWrite(bulkWriteOps);
                               ~~~~~~~~~~~~
```
`useNewUrlParser` is `true` by default and should not be specified in client code. [1]

`poolSize` has been replaced with `maxPoolSize` which defaults to `100`. [2]

[1]: https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options
[2]: https://mongoosejs.com/docs/migrating_to_6.html#mongodb-driver-40
Copy link

render bot commented Jun 15, 2024

@dchege711 dchege711 merged commit 96e1220 into main Jun 15, 2024
4 checks passed
@dchege711 dchege711 deleted the user/dchege711/package-updates/mongoose-6 branch June 15, 2024 17:27
@dchege711
Copy link
Owner Author

Contributes to #174

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant