Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds support for custom generate prisma client
- Loading branch information
Showing
16 changed files
with
235 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './dist/types/runtime'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/es6c/runtime'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { AbilityClass } from '@casl/ability' | ||
import { AbilityOptionsOf, PureAbility, RawRuleOf } from '@casl/ability' | ||
import { User, Post } from '@prisma/client' | ||
import { PrismaAbility, Subjects } from '../src' | ||
import { createPrismaAbility, PrismaQuery, Subjects } from '../src' | ||
|
||
export type AppAbility = PrismaAbility<[string, Subjects<{ | ||
export type AppAbility = PureAbility<[string, 'all' | Subjects<{ | ||
User: User, | ||
Post: Post | ||
}>]> | ||
// eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export const AppAbility = PrismaAbility as AbilityClass<AppAbility> | ||
}>], PrismaQuery> | ||
|
||
type AppAbilityFactory = ( | ||
rules?: RawRuleOf<AppAbility>[], | ||
options?: AbilityOptionsOf<AppAbility> | ||
) => AppAbility | ||
export const createAppAbility = createPrismaAbility as AppAbilityFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AbilityOptions, AbilityTuple, fieldPatternMatcher, PureAbility, RawRuleFrom } from '@casl/ability'; | ||
import { prismaQuery } from './prisma/prismaQuery'; | ||
|
||
export function createAbilityFactory< | ||
TModelName extends string, | ||
TPrismaQuery extends Record<string, any> | ||
>() { | ||
return function createAbility< | ||
A extends AbilityTuple = [string, TModelName], | ||
C extends TPrismaQuery = TPrismaQuery | ||
>( | ||
rules?: RawRuleFrom<A, C>[], | ||
options?: AbilityOptions<A, C> | ||
) { | ||
return new PureAbility<A, C>(rules, { | ||
...options, | ||
conditionsMatcher: prismaQuery, | ||
fieldMatcher: fieldPatternMatcher, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
export { prismaQuery } from './prisma/PrismaQuery'; | ||
export type { PrismaQuery, Model, Subjects } from './prisma/PrismaQuery'; | ||
export { accessibleBy } from './accessibleBy'; | ||
export { PrismaAbility } from './PrismaAbility'; | ||
export { ParsingQueryError } from './errors/ParsingQueryError'; | ||
import { AbilityOptions, AbilityTuple, fieldPatternMatcher, PureAbility, RawRuleFrom } from '@casl/ability'; | ||
import { createAbilityFactory, createAccessibleByFactory, prismaQuery } from './runtime'; | ||
import { WhereInputPerModel, ModelName, PrismaQuery } from './prismaClientBoundTypes'; | ||
|
||
export type { PrismaQuery } from './prismaClientBoundTypes'; | ||
export { prismaQuery, Model, Subjects, ParsingQueryError } from './runtime'; | ||
|
||
const createPrismaAbility = createAbilityFactory<ModelName, PrismaQuery>(); | ||
const accessibleBy = createAccessibleByFactory<WhereInputPerModel, PrismaQuery>(); | ||
|
||
export { | ||
createPrismaAbility, | ||
accessibleBy, | ||
}; | ||
|
||
type ExtendedAbilityTuple<T extends AbilityTuple> = [T[0], 'all' | T[1]]; | ||
|
||
/** | ||
* @deprecated use createPrismaAbility instead | ||
*/ | ||
export class PrismaAbility< | ||
A extends AbilityTuple = [string, ModelName], | ||
C extends PrismaQuery = PrismaQuery | ||
> extends PureAbility<ExtendedAbilityTuple<A>, C> { | ||
constructor( | ||
rules?: RawRuleFrom<ExtendedAbilityTuple<A>, C>[], | ||
options?: AbilityOptions<ExtendedAbilityTuple<A>, C> | ||
) { | ||
super(rules, { | ||
conditionsMatcher: prismaQuery, | ||
fieldMatcher: fieldPatternMatcher, | ||
...options, | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { AnyInterpreter, createTranslatorFactory } from '@ucast/core'; | ||
import { ForcedSubject } from '@casl/ability'; | ||
import { PrismaQueryParser } from './PrismaQueryParser'; | ||
import { interpretPrismaQuery } from './interpretPrismaQuery'; | ||
|
||
const parser = new PrismaQueryParser(); | ||
export const prismaQuery = createTranslatorFactory( | ||
parser.parse, | ||
interpretPrismaQuery as AnyInterpreter | ||
); | ||
|
||
export type Model<T, TName extends string> = T & ForcedSubject<TName>; | ||
export type Subjects<T extends Partial<Record<string, Record<string, unknown>>>> = | ||
| keyof T | ||
| { [K in keyof T]: Model<T[K], K & string> }[keyof T]; | ||
|
||
/** | ||
* Extracts Prisma model name from given object and possible list of all subjects | ||
*/ | ||
export type ExtractModelName< | ||
TObject, | ||
TModelName extends string | ||
> = TObject extends { kind: TModelName } | ||
? TObject['kind'] | ||
: TObject extends ForcedSubject<TModelName> | ||
? TObject['__caslSubjectType__'] | ||
: TObject extends { __typename: TModelName } | ||
? TObject['__typename'] | ||
: TModelName; |
Oops, something went wrong.