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

(casl-mongoose) fix: Proper typing for Mongoose plugins #732

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

qqilihq
Copy link
Contributor

@qqilihq qqilihq commented Feb 12, 2023

Hi @stalniy -

I continue to enjoy using CASL - thank you for your work! After updating Mongoose in one of our projects I got compile errors here:

mySchema.plugin(accessibleRecordsPlugin);
mySchema.plugin(accessibleFieldsPlugin);

Mongoose 6.6.3 made the plugin typing more strict, which currently leads to a TS error which I have to @ts-ignore

See here about the change:

Automattic/mongoose@2b0d429

These changes add the second type parameter to Schema which resolves the issue. The fix should be non-breaking.

* Mongoose made this more strict in 6.6.3 - Automattic/mongoose@2b0d429
@codecov-commenter
Copy link

codecov-commenter commented Feb 12, 2023

Codecov Report

Merging #732 (a1fc7b2) into master (c795e8b) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #732   +/-   ##
=======================================
  Coverage   94.81%   94.81%           
=======================================
  Files          34       34           
  Lines         733      733           
  Branches      174      174           
=======================================
  Hits          695      695           
  Misses         18       18           
  Partials       20       20           
Impacted Files Coverage Δ
packages/casl-mongoose/src/accessible_fields.ts 95.45% <100.00%> (ø)
packages/casl-mongoose/src/accessible_records.ts 85.71% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@stalniy
Copy link
Owner

stalniy commented Feb 15, 2023

hey, thank you for the fix. did you tested this? I'm asking because not really sure that passing AccessibleRecordModel and co to Schema generic type eventually will be correctly mapped.

Mongoose team as usually, do breaking type changes in non-breaking releases...

@qqilihq
Copy link
Contributor Author

qqilihq commented Feb 17, 2023

Hey @stalniy,

hey, thank you for the fix. did you tested this? I'm asking because not really sure that passing AccessibleRecordModel and co to Schema generic type eventually will be correctly mapped.

Yes - I have just tried it again within an isolated test project and the following code:

import mongoose, { Types } from 'mongoose';
import { AccessibleModel, accessibleRecordsPlugin, accessibleFieldsPlugin } from '@casl/mongoose';

interface IPost {
  _id: Types.ObjectId;
  title: string;
  author: string;
}

type PostModel = AccessibleModel<IPost>;

const Post = new mongoose.Schema<IPost, PostModel>({
  title: String,
  author: String
});

Post.plugin(accessibleRecordsPlugin);
Post.plugin(accessibleFieldsPlugin);

export const PostModel = mongoose.model<IPost, PostModel>('Post', Post);

Contrary to what I wrote above, the mentioned compile error happens for me with Mongoose 6.5.1 and upwards. Version 6.5.0 still worked fine.

With the changes in the MR, the code as shown above compiles with 6.5.0, 6.5.1 and the most recent as of today, 6.9.2 for me.

Mongoose team as usually, do breaking type changes in non-breaking releases...

Tell me about it - constant frustration 😠

@@ -77,7 +77,7 @@ export interface AccessibleRecordModel<
>
}

function modelAccessibleBy(this: Model<unknown>, ability: AnyMongoAbility, action?: string) {
function modelAccessibleBy<T>(this: Model<T>, ability: AnyMongoAbility, action?: string) {
Copy link
Owner

Choose a reason for hiding this comment

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

there is no need to make it generic because this types anyway are thrown away

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.

None yet

3 participants