Skip to content

Commit

Permalink
feat: breaking change on CrudField relations
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiocro committed Nov 3, 2024
1 parent 881f7ad commit 4f10ad2
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-flies-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@panter/crud": patch
---

Feat: freaking change in CrudFiel, all relation show\* properties are default to false
5 changes: 5 additions & 0 deletions .changeset/mighty-apricots-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@panter/nestjs-utils": patch
---

Fix ModuleLogger typings
13 changes: 9 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ variables:
DOCKER_TLS_CERTDIR: ''
DOCKER_DRIVER: overlay2
DOCKER_BUILDKIT: '1'
DOCKER_MEMORY: '4g'
DOCKER_CPUS: '2'
TESTCONTAINERS_HOST_OVERRIDE: 'host.docker.internal'
KUBERNETES_MEMORY_REQUEST: 3Gi
KUBERNETES_MEMORY_LIMIT: 6Gi
KUBERNETES_CPU_REQUEST: '0.5'
KUBERNETES_CPU_LIMIT: '1'
KUBERNETES_MEMORY_REQUEST: 2Gi
KUBERNETES_MEMORY_LIMIT: 4Gi

cache:
- key: yarn-workspace
Expand Down Expand Up @@ -59,7 +62,8 @@ build-samples:
# - yarn changesets-gitlab comment

test-lib:
stage: test
stage: build
needs: ['build-lib']
only: ['merge_requests', 'main']
script:
- yarn --immutable
Expand All @@ -73,7 +77,8 @@ test-lib:
- .yarn

test-samples:
stage: test
stage: build
needs: ['build-samples']
only: ['merge_requests', 'main']
allow_failure: true
script:
Expand Down
14 changes: 9 additions & 5 deletions libs/crud/src/crud.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ export interface CrudModuleOptions<T> {

export type CrudModuleAsyncOptions<T> = ModuleAsyncOptions<
CrudModuleOptions<T>
> & { defaultRelationModifier?: boolean };
>;

let relationModifier: boolean | undefined = true;
const relationModifier: boolean | undefined = true;
const connectRelationModifier: boolean | undefined = true;

export const defaultRelationModifier = () => relationModifier;
export const defaultRelationModifier = () => {
return relationModifier;
};
export const defaultConnectRelationModifier = () => {
return connectRelationModifier;
};

@Module({})
export class CrudModule implements OnModuleInit {
static async forRootAsync<T>({
defaultRelationModifier,
inject,
imports,
useFactory,
}: CrudModuleAsyncOptions<T>): Promise<DynamicModule> {
relationModifier = defaultRelationModifier;
return {
module: CrudModule,
imports: [...(imports || []), ConfigModule, DiscoveryModule],
Expand Down
8 changes: 4 additions & 4 deletions libs/crud/src/graphql/crud-field.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export type InputFieldResolver<Entity = any, GqlInput = any> = (

export type CrudRelationFieldOptions = {
/**
* If set to `true`, the relation cannot be connected by primary key
* If set to `true`, the relation can be disconnected with all it's fields.
*/
hideConnect?: boolean;
showConnect?: boolean;
/**
* If set to `true`, the relation can be created with all it's fields.
*/
Expand All @@ -65,9 +65,9 @@ export type CrudRelationFieldOptions = {
*/
showUpdate?: boolean;
/**
* If set to `true`, the relation cannot be disconnected by primary key.
* If set to `true`, the relation can be disconnected with all it's fields.
*/
hideDisconnect?: boolean;
showDisconnect?: boolean;
};

export type CrudFieldProps<Entity = any, GqlInput = any, GqlWhere = any> = {
Expand Down
60 changes: 35 additions & 25 deletions libs/crud/src/graphql/upsert-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Field, Float, InputType, TypeMetadataStorage } from '@nestjs/graphql';
import { GraphQLJSON } from 'graphql-scalars';
import GraphQLUpload from 'graphql-upload/GraphQLUpload.js';
import { isArray, uniqBy } from 'lodash';
import { CrudGqlType, defaultRelationModifier } from '../'; // CrudGqlType needs to be imported from root index.ts
import {
CrudGqlType,
defaultConnectRelationModifier,
defaultRelationModifier,
} from '../'; // CrudGqlType needs to be imported from root index.ts
import { CrudEntityType } from './crud-types';
import { manyRelationInput } from './many-relation-input';
import { typesCache } from './types-cache';
Expand Down Expand Up @@ -98,9 +102,9 @@ const getCreateDesignType = (p: CrudInfo, parentRef: CrudEntityType) => {
? !p.crudOptions?.relation?.showCreate
: defaultRelationModifier(),
hideConnect:
p.crudOptions?.relation?.hideConnect !== undefined
? p.crudOptions?.relation?.hideConnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showConnect !== undefined
? !p.crudOptions?.relation?.showConnect
: defaultConnectRelationModifier(),
parentProperty: p.name,
},
);
Expand Down Expand Up @@ -136,9 +140,9 @@ const getCreateDesignType = (p: CrudInfo, parentRef: CrudEntityType) => {
? !p.crudOptions?.relation?.showCreate
: defaultRelationModifier(),
hideConnect:
p.crudOptions?.relation?.hideConnect !== undefined
? p.crudOptions?.relation?.hideConnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showConnect !== undefined
? !p.crudOptions?.relation?.showConnect
: defaultConnectRelationModifier(),
parentProperty: p.name,
});
}
Expand Down Expand Up @@ -169,13 +173,15 @@ const getUpdateDesignType = (p: CrudInfo, parentRef: CrudEntityType) => {
? !p.crudOptions?.relation?.showUpdate
: defaultRelationModifier(),
hideDisconnect:
p.crudOptions?.relation?.hideDisconnect !== undefined
? p.crudOptions?.relation?.hideDisconnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showDisconnect !== undefined
? !p.crudOptions?.relation?.showDisconnect
: !p.crudOptions?.fieldOptions?.nullable
? true
: defaultConnectRelationModifier(),
hideConnect:
p.crudOptions?.relation?.hideConnect !== undefined
? p.crudOptions?.relation?.hideConnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showConnect !== undefined
? !p.crudOptions?.relation?.showConnect
: defaultConnectRelationModifier(),
parentProperty: p.name,
},
);
Expand Down Expand Up @@ -216,13 +222,15 @@ const getUpdateDesignType = (p: CrudInfo, parentRef: CrudEntityType) => {
? !p.crudOptions?.relation?.showUpdate
: defaultRelationModifier(),
hideConnect:
p.crudOptions?.relation?.hideConnect !== undefined
? p.crudOptions?.relation?.hideConnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showConnect !== undefined
? !p.crudOptions?.relation?.showConnect
: defaultConnectRelationModifier(),
hideDisconnect:
p.crudOptions?.relation?.hideDisconnect !== undefined
? p.crudOptions?.relation?.hideDisconnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showDisconnect !== undefined
? !p.crudOptions?.relation?.showDisconnect
: !p.crudOptions?.fieldOptions?.nullable
? true
: defaultConnectRelationModifier(),
parentProperty: p.name,
},
);
Expand All @@ -241,13 +249,15 @@ const getUpdateDesignType = (p: CrudInfo, parentRef: CrudEntityType) => {
? !p.crudOptions?.relation?.showUpdate
: defaultRelationModifier(),
hideConnect:
p.crudOptions?.relation?.hideConnect !== undefined
? p.crudOptions?.relation?.hideConnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showConnect !== undefined
? !p.crudOptions?.relation?.showConnect
: defaultConnectRelationModifier(),
hideDisconnect:
p.crudOptions?.relation?.hideDisconnect !== undefined
? p.crudOptions?.relation?.hideDisconnect
: defaultRelationModifier(),
p.crudOptions?.relation?.showDisconnect !== undefined
? !p.crudOptions?.relation?.showDisconnect
: !p.crudOptions?.fieldOptions?.nullable
? true
: defaultConnectRelationModifier(),
parentProperty: p.name,
},
);
Expand Down
13 changes: 11 additions & 2 deletions libs/crud/src/service/crud-entity-service.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ import { CRUD_SERVICE } from './crud-service.decorator';
* Factory to get the correct CRUD service for a given entity.
*
* To provide a default CRUD service, the factory will return the provided CRUD service if no specific service is found.
*
* @example
* @CrudServiceResource({ entity: PersonEntity })
* @Injectable()
* export class PersonEntityService implements ICrudEntityService<PersonEntity> {}
*
*/
@Injectable()
export class CrudEntityServiceFactory {
constructor(
private readonly discovery: DiscoveryService,
private readonly reflector: Reflector,
private readonly crudEntityService: CrudEntityService<any>,
private readonly defaultCrudEntityService: CrudEntityService<any>,
) {}

/**
* Get the CRUD service for the given entity.
* If no service is found, the default service will be returned.
* The decorator CrudServiceResource will be used to find the service a entity.
*
*/
getCrudService<T extends object>(
entity: CrudEntityType<T>,
Expand All @@ -34,7 +43,7 @@ export class CrudEntityServiceFactory {

if (!provider || !provider.instance) {
// TODO: make sure i can reuse the instance if this is on how to solve it
return this.crudEntityService;
return this.defaultCrudEntityService;
}

return provider.instance;
Expand Down
3 changes: 3 additions & 0 deletions libs/crud/src/service/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { DiscoveryService } from '@nestjs/core';
import { CrudAuditCallback, CrudAuthorizeCallback } from '../';
import { CrudModuleOptions } from '../crud.module';

/**
* This service is used to register CRUD resources and to provide default authorization and audit callbacks.
*/
@Injectable()
export class CrudService {
private logger = new Logger(CrudService.name);
Expand Down
6 changes: 3 additions & 3 deletions libs/crud/test/e2e/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export default async () => {
console.log('Starting PostgreSQL (pgContainer) container');
console.time('pgContainer');
if (!global.pgContainer) {
console.log('Creating new PostgreSQL (pgContainer) container');
global.pgContainer = await new PostgreSqlContainer('postgres:13.3-alpine')
console.log('Creating new PostgreSQL (pgContainer) testcontainer');
global.pgContainer = await new PostgreSqlContainer()
.withExposedPorts(5432)
.withStartupTimeout(60000)
.start();
} else {
console.log('Reusing existing PostgreSQL (pgContainer) container');
console.log('Reusing existing PostgreSQL (pgContainer) testcontainer');
}
console.timeEnd('pgContainer');
};
4 changes: 2 additions & 2 deletions libs/crud/test/fixtures/company.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class Company {
@Property({ nullable: true })
description?: string;

@CrudField({ hideUpdate: true, relation: { hideConnect: false } })
@CrudField({ hideUpdate: true, relation: { showConnect: true } })
@Field()
@ManyToOne(() => User)
founder!: User;

@CrudField({ hideCreate: true, relation: { hideConnect: false } })
@CrudField({ hideCreate: true, relation: { showConnect: true } })
@Field({ nullable: true })
@ManyToOne(() => User, { nullable: true })
ceo?: User;
Expand Down
8 changes: 4 additions & 4 deletions libs/crud/test/fixtures/group.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export class Group {
@CrudField({
hideCreate: false,
hideUpdate: true,
relation: { hideConnect: false },
relation: { showConnect: true },
})
@Field(() => [User])
@ManyToMany({ entity: () => User })
founders = new Collection<User>(this);

@CrudField({
hideCreate: true,
relation: { hideConnect: false, hideDisconnect: false },
relation: { showConnect: true, showDisconnect: true },
})
@Field(() => [User], { nullable: true })
@ManyToMany({ entity: () => User, nullable: true })
Expand All @@ -52,8 +52,8 @@ export class Group {
relation: {
showCreate: true,
showUpdate: true,
hideConnect: false,
hideDisconnect: false,
showConnect: true,
showDisconnect: true,
},
})
@Field(() => [User], { nullable: true })
Expand Down
2 changes: 1 addition & 1 deletion libs/crud/test/unit/fixtures/all-types-shema.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Dummy {

@CrudField({
typeFn: () => Dummy2,
relation: { showCreate: true, showUpdate: true, hideConnect: false },
relation: { showCreate: true, showUpdate: true, showConnect: true },
})
@Field(() => [Dummy2])
@ManyToMany(() => Dummy2, (dummy2) => dummy2.dummyProp)
Expand Down

0 comments on commit 4f10ad2

Please sign in to comment.