Skip to content

Commit

Permalink
fix: move the default select ownership inside the casl generator to b…
Browse files Browse the repository at this point in the history
…e able to configure it (#5)
  • Loading branch information
floross authored May 16, 2023
1 parent 86f5a42 commit 2eba9e8
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
1 change: 0 additions & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ inputs:
description: 'The registry url where the packages are loacated'
default: 'https://registry.npmjs.org'
required: false
type: string

NPM_TOKEN:
description: 'The npm token if needed'
Expand Down
10 changes: 10 additions & 0 deletions libs/hapify/templates/casl/.hapifyrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ module.exports = {
engine: 'hpf',
input: 'all',
},
{
path: 'generated/casl/src/constants/index.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/casl/src/constants/default-select-ownerships.constant.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/casl/src/interfaces/index.ts',
engine: 'hpf',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const OWNERS_DEFAULT_FIELDS = {
export const DEFAULT_SELECT_OWNERSHIPS = {
<<for Models model>>
<<if model.properties.hasOwnership>>
<<model pascal>>: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './default-select-ownerships.constant';
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './config';
export * from './constants';
export * from './interfaces';
5 changes: 0 additions & 5 deletions libs/hapify/templates/nestjs-graphql/.hapifyrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ module.exports = {
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-graphql/src/nestjs-graphql.constants.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-graphql/src/nestjs-models.module.ts',
engine: 'hpf',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
<<ref pascal>>Service,
<<endfor>>
} from '../../../../nestjs-models-common';
import { Actions, AppAbility, Permission } from '../../../../casl';
import { Actions, AppAbility, Permission, DEFAULT_SELECT_OWNERSHIPS } from '../../../../casl';

import {
<<Model pascal>>,
Expand All @@ -71,7 +71,6 @@ import {
import {
FindMany<<Model pascal>>Output,
} from '../dtos';
import { OWNERS_DEFAULT_FIELDS } from '../nestjs-graphql.constants';

@Resolver(() => <<Model pascal>>)
export class <<Model pascal>>Resolver {
Expand Down Expand Up @@ -100,7 +99,7 @@ export class <<Model pascal>>Resolver {
<<endif>>
) {
const select = new PrismaSelect(
info, { defaultFields: OWNERS_DEFAULT_FIELDS }
info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }
).value;
const <<Model camel>> = await this.<<Model camel>>Service.findUnique({ where, ...select });
<<if Fields ownership >>
Expand Down Expand Up @@ -134,7 +133,7 @@ export class <<Model pascal>>Resolver {
<<endif>>
) {
const select = new PrismaSelect(
info, { defaultFields: OWNERS_DEFAULT_FIELDS }
info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }
).valueOf('<<Model camel>>s', '<<Model pascal>>');

const where = {
Expand Down Expand Up @@ -176,7 +175,7 @@ export class <<Model pascal>>Resolver {
@CurrentAbilities() abilities: AppAbility | undefined,
<<endif>>
): Promise<any> {
const select = new PrismaSelect(info, { defaultFields: OWNERS_DEFAULT_FIELDS }).value;
const select = new PrismaSelect(info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }).value;
const data = {
<<if Fields ownership and internal and not primary>>
...(user ? this.<<Model camel>>Service.getDefaultOwnerships(user) : {}),
Expand Down Expand Up @@ -222,7 +221,7 @@ export class <<Model pascal>>Resolver {
@CurrentAbilities() abilities: AppAbility | undefined,
<<endif>>
): Promise<any> {
const select = new PrismaSelect(info, { defaultFields: OWNERS_DEFAULT_FIELDS }).value;
const select = new PrismaSelect(info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }).value;

<<if Fields ownership>>
return this.prisma.$transaction(async (prisma) => {
Expand Down Expand Up @@ -259,7 +258,7 @@ export class <<Model pascal>>Resolver {
@CurrentAbilities() abilities: AppAbility | undefined,
<<endif>>
): Promise<any> {
const select = new PrismaSelect(info, { defaultFields: OWNERS_DEFAULT_FIELDS }).value;
const select = new PrismaSelect(info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }).value;

<<if Fields ownership>>
return this.prisma.$transaction(async (prisma) => {
Expand Down Expand Up @@ -309,7 +308,7 @@ export class <<Model pascal>>Resolver {
const where = {
AND: [abilities ? accessibleBy(abilities).<<=field.model.names.pascal>> : {}, whereInput],
};
const select = new PrismaSelect(info, { defaultFields: OWNERS_DEFAULT_FIELDS }).valueOf('<<Model camel>>s.<<field camel>>', '<<=field.model.names.pascal>>');
const select = new PrismaSelect(info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }).valueOf('<<Model camel>>s.<<field camel>>', '<<=field.model.names.pascal>>');

<<if field oneOne or oneMany>>
<<field camel>> = await this.<<=field.model.names.camel>>Service.findUnique({
Expand Down Expand Up @@ -372,7 +371,7 @@ export class <<Model pascal>>Resolver {
const where = {
AND: [abilities ? accessibleBy(abilities).<<ref pascal>> : {}, whereInput],
};
const select = new PrismaSelect(info, { defaultFields: OWNERS_DEFAULT_FIELDS }).valueOf('<<Model camel>>s.<<-field backRelation camel>>', '<<ref pascal>>');
const select = new PrismaSelect(info, { defaultFields: DEFAULT_SELECT_OWNERSHIPS }).valueOf('<<Model camel>>s.<<-field backRelation camel>>', '<<ref pascal>>');

<<if field oneOne>>
<<-field backRelation camel>> = await this.<<ref camel>>Service.findUnique({
Expand Down

0 comments on commit 2eba9e8

Please sign in to comment.