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 and official mongoose types #436

Closed
p-fruck opened this issue Jan 10, 2021 · 25 comments
Closed

Casl/mongoose and official mongoose types #436

p-fruck opened this issue Jan 10, 2021 · 25 comments

Comments

@p-fruck
Copy link

p-fruck commented Jan 10, 2021

Hey @stalniy, thank you for your awesome work on this project. I just got started using the casl library for permission management and I really love it so far, but there's one small issue I wasn't able to fix:

Describe the bug
I am using the @casl/ability and the @casl/mongoose package to manage database permissions within nodejs. My code runs just fine but as soon as I want to compile my typescript project using tsc, it just errors out.

To Reproduce
Steps to reproduce the behavior:
I've created src/test.ts within my current project with the following content:

import { accessibleRecordsPlugin } from '@casl/mongoose';
import * as mongoose from 'mongoose';

mongoose.plugin(accessibleRecordsPlugin);

Expected behavior
Running npx tsc src/test.ts should just compile the ts file. Instead, I get the following error message:

npx tsc src/test.ts 
node_modules/@casl/mongoose/dist/types/accessible_records.d.ts:3:18 - error TS2305: Module '"mongoose"' has no exported member 'DocumentQuery'.

3 import { Schema, DocumentQuery, Model, Document } from 'mongoose';
                   ~~~~~~~~~~~~~

node_modules/@casl/mongoose/dist/types/accessible_records.d.ts:6:76 - error TS2314: Generic type 'Model<T>' requires 1 type argument(s).

6 export interface AccessibleRecordModel<T extends Document, K = {}> extends Model<T, K & {
                                                                             ~~~~~~~~~~~~~~
7     accessibleBy: GetAccessibleRecords<T>;
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 }> {

CASL Version

@casl/ability - v 5.1.2
@casl/mongoose - v 3.2.2

Environment:

Fedora 33, Node 14.15.1
mongoose - v 5.11.11
typescript -v 4.1.3

@p-fruck p-fruck added the bug label Jan 10, 2021
@stalniy
Copy link
Owner

stalniy commented Jan 10, 2021

Do you esMofuleInterop enabled? As in this tsconfig - https://github.com/stalniy/casl/blob/master/tsconfig.json#L10

@p-fruck
Copy link
Author

p-fruck commented Jan 10, 2021

Thank you for the quick answer! Yes, I do have esModuleInterop enabled. I did even replace my tsconfig.json with your example, but I still get the same error.

@p-fruck
Copy link
Author

p-fruck commented Jan 10, 2021

It seems like the issue is connected to mongoose v 5.11.11. When downgrading to v 5.10.3 tsc works just fine, but on v 5.11.11 I get the above error. So I will stay on 5.10.3 for the time being. Will there be support for the latest mongoose version soon? 😄

@stalniy
Copy link
Owner

stalniy commented Jan 10, 2021

Strange. Maybe in the latest version of @types/mongoose they removed some exports. I recently made a release of casl mongoose and tsc didn’t complain.

Let me check it with the latest version

@stalniy
Copy link
Owner

stalniy commented Jan 10, 2021

They added official ts support in 5.11 and in 5.10 you need to use @types/mongoose - https://github.com/Automattic/mongoose/blob/master/History.md#5110--2020-11-30

thats why they are incompatible

@stalniy stalniy changed the title @casl/mongoose typescript compile errors Casl/mongoose and official mongoose types Jan 10, 2021
@p-fruck
Copy link
Author

p-fruck commented Jan 10, 2021

Ok thank you, but the official types included in mongoose v 5.11.11 seem to differ from the types provided by @types/mongoose v 5.10.3, because even without having @types/mongoose installed (those two libraries conflict for obvious reasons) tsc still wont compile. So the question whether mongoose 5.11 will be supported soon still remains

@stalniy
Copy link
Owner

stalniy commented Jan 10, 2021

This is a breaking change for casl/mongoose, so very likely I'll ship it with #404 together . Also, currently I'm on holidays, so it won't be fast. I think I'll have time to fix this in the next couple weeks.

@stalniy stalniy added enhancement and removed bug labels Jan 13, 2021
@stalniy
Copy link
Owner

stalniy commented Jan 13, 2021

@p-fruck did you check if npm i mongoose @types/mongoose still works for mongoose@5.11?

@p-fruck
Copy link
Author

p-fruck commented Jan 13, 2021

it didn't work for me since the types provided by @types/mongoose (v 5.10.3) were conflicting with those provided by mongoose v 5.11

@stalniy
Copy link
Owner

stalniy commented Jan 24, 2021

I do not recommend to use official types as there are a bunch of TypeScript related issues introduced in mongoose@5.11.x:

That makes it impossible to have the same level of TS support in the package.

So, suggested workaround is to use @types/mongoose and remove official types through postinstall npm hook:

{
  "scripts": {
    "postinstall": "rm -f node_modules/mongoose/index.d.ts"
  }
}

There is nothing better I can do with this until official mongoose types are improved

@stalniy
Copy link
Owner

stalniy commented Mar 12, 2021

looks like that ts issues where fixed by mongoose and I can try to implement it one more time with official types

@mdwekat
Copy link

mdwekat commented Mar 26, 2021

I work around this by setting skipLibCheck to true in tsconfig.json

"compilerOptions": {
    "skipLibCheck": true
  }
}

@stalniy
Copy link
Owner

stalniy commented May 10, 2021

fixed in @casl/mongoose@5.0.0

@qqilihq
Copy link
Contributor

qqilihq commented May 10, 2021

Thank you!

@leonardoviada
Copy link

Hi everyone, I'm getting this error and reading the comments above didn't help. What am I doing wrong?
I have @casl/mongoose@5.0.0 and I'm using NestJs

src/restaurant/restaurant.service.ts:25:40 - error TS2339: Property 'accessibleBy' does not exist on type 'Query<(Restaurant & Document<any, any, Restaurant>)[], Restaurant & Document<an
y, any, Restaurant>, {}, Restaurant>'.

25     return this.restaurantModel.find().accessibleBy().exec();
                                          ~~~~~~~~~~~~

[5:04:27 PM] Found 1 error. Watching for file changes.

@0soltys
Copy link

0soltys commented Jul 30, 2021

Hi everyone, I'm getting this error and reading the comments above didn't help. What am I doing wrong?
I have @casl/mongoose@5.0.0 and I'm using NestJs

src/restaurant/restaurant.service.ts:25:40 - error TS2339: Property 'accessibleBy' does not exist on type 'Query<(Restaurant & Document<any, any, Restaurant>)[], Restaurant & Document<an
y, any, Restaurant>, {}, Restaurant>'.

25     return this.restaurantModel.find().accessibleBy().exec();
                                          ~~~~~~~~~~~~

[5:04:27 PM] Found 1 error. Watching for file changes.

Same problem

@stalniy
Copy link
Owner

stalniy commented Jul 30, 2021

Looks like types on Query doesn't work properly

@anuraagy
Copy link

Any update on this?

@stalniy
Copy link
Owner

stalniy commented Sep 11, 2021

Nothing new, I have one project were I have older mongoose version. As soon as I finish some refactoring and will be able to update to newer version I’ll check it.

Very low on free time right now. Also, I fed up with mongoose types so very likely will create an alternative fully typesafe mongo db DAL lib based on json schema in the next year.

@anuraagy
Copy link

I totally understand, do you have advice on how we can hide these type errors created by accessibleBy?

@anuraagy
Copy link

We were able to get types working with the following setup! Posting here for others who are running into this issue.

import { AnyMongoAbility, Generics, Normalize } from "@casl/ability";
import { DocumentQuery, Model, Types, Document } from "mongoose";

type Schema = { _id: Types.ObjectId };


type ExtractQueryHelpersFromDocument<D> = D extends Document<any, infer QueryHelpers, any>
  ? QueryHelpers
  : never;

export type AccessibleBy<T extends Schema, D extends Document> = <U extends AnyMongoAbility>(
  ability: U,
  action?: Normalize<Generics<U>["abilities"]>[0]
) => DocumentQuery<T & D, D, ExtractQueryHelpersFromDocument<D>>;

export interface AccessibleRecordModel<
  T extends Schema,
  D extends Document,
  QueryHelpers = {},
  Methods = {}
> extends Model<
    T,
    QueryHelpers & {
      accessibleBy: AccessibleBy<T, D>;
    },
    Methods
  > {
  accessibleBy: AccessibleBy<T, D>;
}`

@kamalkech
Copy link

still the same issue with "mongoose": "~6.6.0" and "@types/mongoose": "^5.11.97" mean should downground the version in 2022 ???

@stalniy
Copy link
Owner

stalniy commented Nov 2, 2022

You don’t need types/mongoose anymore. Mongoose provides built-in types

@stalniy stalniy reopened this Nov 2, 2022
@stalniy stalniy closed this as completed Nov 2, 2022
@kamalkech
Copy link

@stalniy but not working without types/mongoose

@stalniy
Copy link
Owner

stalniy commented Nov 2, 2022

tests for casl/mongoose are written in ts and they work. If smth doesnt work for you, please make a minimal repo/example where I can take a look.

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

No branches or pull requests

8 participants