Skip to content

Commit

Permalink
feat: update mongoose to v6 and use mongoose-v6 QueryWithHelpers inte…
Browse files Browse the repository at this point in the history
…rface (#561)

Co-authored-by: soy <sylvain.oyer@hygia.fr>

BREAKING CHANGE: updates mongoose and related ts types to v6
  • Loading branch information
eSRoye authored Nov 4, 2021
1 parent 825121b commit a5f1036
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 110 deletions.
4 changes: 2 additions & 2 deletions packages/casl-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
"license": "MIT",
"peerDependencies": {
"@casl/ability": "^3.0.0 || ^4.0.0 || ^5.1.0",
"mongoose": "^5.12.0"
"mongoose": "^6.0.0"
},
"devDependencies": {
"@casl/ability": "^5.1.0",
"@casl/dx": "workspace:^1.0.0",
"chai": "^4.1.0",
"chai-spies": "^1.0.0",
"mongoose": "^5.12.0"
"mongoose": "^6.0.0"
},
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/casl-mongoose/spec/accessible_records.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('Accessible Records Plugin', () => {
})

describe('when ability disallow to perform an action', () => {
let query: mongoose.DocumentQuery<Post, Post>
let query: mongoose.QueryWithHelpers<Post, Post>

beforeEach(() => {
query = Post.find().accessibleBy(ability, 'notAllowedAction')
Expand Down
8 changes: 4 additions & 4 deletions packages/casl-mongoose/src/accessible_records.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Normalize, AnyMongoAbility, Generics, ForbiddenError, getDefaultErrorMessage } from '@casl/ability';
import type { Schema, DocumentQuery, Model, Document } from 'mongoose';
import type { Schema, QueryWithHelpers, Model, Document } from 'mongoose';
import mongoose from 'mongoose';
import { toMongoQuery } from './mongo';

function failedQuery(
ability: AnyMongoAbility,
action: string,
modelName: string,
query: DocumentQuery<Document, Document>
query: QueryWithHelpers<Document, Document>
) {
query.where({ __forbiddenByCasl__: 1 }); // eslint-disable-line
const anyQuery: any = query;
Expand All @@ -28,13 +28,13 @@ function failedQuery(
type GetAccessibleRecords<T extends Document> = <U extends AnyMongoAbility>(
ability: U,
action?: Normalize<Generics<U>['abilities']>[0]
) => DocumentQuery<T, T>;
) => QueryWithHelpers<T, T>;

function accessibleBy<T extends AnyMongoAbility>(
this: any,
ability: T,
action?: Normalize<Generics<T>['abilities']>[0]
): DocumentQuery<Document, Document> {
): QueryWithHelpers<Document, Document> {
let modelName: string | undefined = this.modelName;

if (!modelName) {
Expand Down
Loading

0 comments on commit a5f1036

Please sign in to comment.