question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Casl/mongoose and official mongoose types

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:25 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
stalniycommented, May 10, 2021

fixed in @casl/mongoose@5.0.0

3reactions
stalniycommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

CASL Mongoose
This package integrates CASL and MongoDB. In other words, it allows to fetch records based on CASL rules from MongoDB and answer questions...
Read more >
@casl/mongoose - npm
Allows to query accessible records from MongoDB based on CASL rules. Latest version: 7.1.2, last published: 2 months ago.
Read more >
casl/ability NPM - npm.io
CASL has a complementary package @casl/mongoose which provides easy integration with MongoDB and mongoose. import { accessibleRecordsPlugin } from '@casl/ ...
Read more >
NestJS + CASL + Mongoose: CASL cannot infer subject type ...
My guess is that CASL cannot infer the subject type of my Mongoose model using the Cat class in InferSubject interface of the...
Read more >
What is CASL or how can you build a castle around your ...
They determine object's type based on constructor.name or custom logic ... Also CASL provides mongoose plugin which adds accessibleBy method to models.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found