Skip to content

Commit

Permalink
feat(prisma): Add 'isSet' filter to prisma filters (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0thi authored Sep 29, 2024
1 parent bbb1d70 commit 256a2fe
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/casl-prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ model User {
firstName String
lastName String
age Int
verified Boolean?
posts Post[]
}

Expand Down
16 changes: 16 additions & 0 deletions packages/casl-prisma/spec/prismaQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ describe('PrismaQuery evaluation', () => {
expect(test({ name: 'Jane Doe' })).toBe(false)
})
})
describe('isSet', () => {
it('throws if value is not a boolean', () => {
expect(() => prismaQuery({ items: { isSet: 1 } })).toThrow(/expects to receive a boolean/)
expect(() => prismaQuery({ items: { isSet: {} } })).toThrow(/expects to receive a boolean/)
expect(() => prismaQuery({ items: { isSet: true } })).not.toThrow()
})

it('checks that object value is not defined when using "isSet"', () => {
const test = prismaQuery({ verified: { isSet: true } })
expect(test({ verified: true })).toBe(true)
expect(test({ verified: false })).toBe(true)
expect(test({ verified: null })).toBe(true)
expect(test({ verified: undefined })).toBe(false)
expect(test({})).toBe(false)
})
})

describe('in', () => {
it('throws if passed value is not an array', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/casl-prisma/src/prisma/PrismaQueryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const compound: CompoundInstruction = {
}
};

const isEmpty: FieldInstruction<boolean> = {
const booleanField: FieldInstruction<boolean> = {
type: 'field',
validate(instruction, value) {
if (typeof value !== 'boolean') {
Expand Down Expand Up @@ -183,7 +183,7 @@ const instructions = {
startsWith: compareString,
endsWith: compareString,
contains: compareString,
isEmpty,
isEmpty: booleanField,
has,
hasSome,
hasEvery: hasSome,
Expand All @@ -195,6 +195,7 @@ const instructions = {
none: inverted('some', relation),
is: relation,
isNot: inverted('is', relation),
isSet: booleanField
};

export interface ParseOptions {
Expand Down
6 changes: 6 additions & 0 deletions packages/casl-prisma/src/prisma/interpretPrismaQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ const not: JsInterpreter<CompoundCondition> = (condition, object, { interpret })
return condition.value.every(subCondition => !interpret(subCondition, object));
};

const isSet: JsInterpreter<FieldCondition<Condition>> = (condition, object, { get }) => {
const item = get(object, condition.field);
return item !== undefined;
}

function toComparable(value: unknown) {
return value && typeof value === 'object' ? value.valueOf() : value;
}
Expand Down Expand Up @@ -112,6 +117,7 @@ export const interpretPrismaQuery = createJsInterpreter({
every,
some,
is,
isSet,
}, {
get: (object, field) => object[field],
compare: compareValues,
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 256a2fe

Please sign in to comment.