Skip to content

Commit

Permalink
Merge pull request #333 from conceptadev/featue/cfg-crud-builder
Browse files Browse the repository at this point in the history
feat: configurable crud builder
  • Loading branch information
MrMaz authored Dec 20, 2024
2 parents 567819a + 977d35d commit 001c74c
Show file tree
Hide file tree
Showing 37 changed files with 1,314 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/nestjs-crud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@concepta/nestjs-common": "^5.1.0",
"@concepta/nestjs-exception": "^5.1.0",
"@concepta/nestjs-typeorm-ext": "^5.1.0",
"@concepta/typeorm-common": "^5.1.0",
"@nestjs/common": "^10.4.1",
"@nestjs/config": "^3.2.3",
Expand All @@ -24,7 +25,6 @@
"@nestjsx/crud-typeorm": "^5.0.0-alpha.3"
},
"devDependencies": {
"@concepta/nestjs-typeorm-ext": "^5.1.0",
"@concepta/typeorm-seeding": "^4.0.0",
"@faker-js/faker": "^8.4.1",
"@nestjs/testing": "^10.4.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { default as ormConfig } from './ormconfig.fixture';
import { CrudModule } from '../crud.module';
import { PhotoCcbCustomModuleFixture } from './photo-ccb-custom/photo-ccb-custom.module.fixture';

@Module({
imports: [
TypeOrmModule.forRoot(ormConfig),
CrudModule.forRoot({}),
PhotoCcbCustomModuleFixture,
],
})
export class AppCcbCustomModuleFixture {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmExtModule } from '@concepta/nestjs-typeorm-ext';
import { default as ormConfig } from './ormconfig.fixture';
import { CrudModule } from '../crud.module';
import { PhotoCcbExtModuleFixture } from './photo-ccb-ext/photo-ccb-ext.module.fixture';

@Module({
imports: [
TypeOrmExtModule.forRoot(ormConfig),
CrudModule.forRoot({}),
PhotoCcbExtModuleFixture,
],
})
export class AppCcbExtModuleFixture {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { default as ormConfig } from './ormconfig.fixture';
import { CrudModule } from '../crud.module';
import { PhotoCcbSubModuleFixture } from './photo-ccb-sub/photo-ccb-sub.module.fixture';

@Module({
imports: [
TypeOrmModule.forRoot(ormConfig),
CrudModule.forRoot({}),
PhotoCcbSubModuleFixture,
],
})
export class AppCcbSubModuleFixture {}
14 changes: 14 additions & 0 deletions packages/nestjs-crud/src/__fixtures__/app-ccb.module.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { default as ormConfig } from './ormconfig.fixture';
import { CrudModule } from '../crud.module';
import { PhotoCcbModuleFixture } from './photo-ccb/photo-ccb.module.fixture';

@Module({
imports: [
TypeOrmModule.forRoot(ormConfig),
CrudModule.forRoot({}),
PhotoCcbModuleFixture,
],
})
export class AppCcbModuleFixture {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { Inject } from '@nestjs/common';
import { PhotoEntityInterfaceFixture } from '../photo/interfaces/photo-entity.interface.fixture';
import { PhotoCreatableInterfaceFixture } from '../photo/interfaces/photo-creatable.interface.fixture';
import { PhotoUpdatableInterfaceFixture } from '../photo/interfaces/photo-updatable.interface.fixture';
import { PhotoFixture } from '../photo/photo.entity.fixture';
import { PhotoDtoFixture } from '../photo/dto/photo.dto.fixture';
import { PhotoPaginatedDtoFixture } from '../photo/dto/photo-paginated.dto.fixture';
import { PhotoCreateDtoFixture } from '../photo/dto/photo-create.dto.fixture';
import { PhotoCreateManyDtoFixture } from '../photo/dto/photo-create-many.dto.fixture';
import { PhotoUpdateDtoFixture } from '../photo/dto/photo-update.dto.fixture';
import { ConfigurableCrudBuilder } from '../../util/configurable-crud.builder';
import { CrudSoftDelete } from '../../decorators/routes/crud-soft-delete.decorator';
import { AbstractCrudController } from '../../controllers/abstract-crud.controller';
import { CrudRequest } from '../../decorators/params/crud-request.decorator';
import { CrudRequestInterface } from '../../interfaces/crud-request.interface';
import { TypeOrmCrudService } from '../../services/typeorm-crud.service';
import { CrudBody } from '../../decorators/params/crud-body.decorator';

export const PHOTO_CRUD_SERVICE_TOKEN = Symbol('__PHOTO_CRUD_SERVICE_TOKEN__');

const crudBuilder = new ConfigurableCrudBuilder<
PhotoEntityInterfaceFixture,
PhotoCreatableInterfaceFixture,
PhotoUpdatableInterfaceFixture
>();

const {
ConfigurableServiceClass,
CrudController,
CrudGetMany,
CrudGetOne,
CrudCreateMany,
CrudCreateOne,
CrudUpdateOne,
CrudReplaceOne,
CrudDeleteOne,
CrudRecoverOne,
} = crudBuilder.build({
service: {
entity: PhotoFixture,
injectionToken: PHOTO_CRUD_SERVICE_TOKEN,
},
controller: {
path: 'photo',
model: {
type: PhotoDtoFixture,
paginatedType: PhotoPaginatedDtoFixture,
},
},
getMany: {},
getOne: {},
createMany: {
dto: PhotoCreateManyDtoFixture,
},
createOne: {
dto: PhotoCreateDtoFixture,
},
updateOne: {
dto: PhotoUpdateDtoFixture,
},
replaceOne: {
dto: PhotoUpdateDtoFixture,
},
deleteOne: {
extraDecorators: [CrudSoftDelete(true)],
},
recoverOne: { path: 'recover/:id' },
});

export class PhotoCcbCustomCrudServiceFixture extends ConfigurableServiceClass {}

@CrudController
export class PhotoCcbCustomControllerFixture extends AbstractCrudController<
PhotoEntityInterfaceFixture,
PhotoCreatableInterfaceFixture,
PhotoUpdatableInterfaceFixture
> {
constructor(
@Inject(PHOTO_CRUD_SERVICE_TOKEN)
protected crudService: TypeOrmCrudService<PhotoEntityInterfaceFixture>,
) {
super(crudService);
}

@CrudGetMany
async getMany(@CrudRequest() crudRequest: CrudRequestInterface) {
return this.crudService.getMany(crudRequest);
}

@CrudGetOne
async getOne(@CrudRequest() crudRequest: CrudRequestInterface) {
return this.crudService.getOne(crudRequest);
}

@CrudCreateMany
async createMany(
@CrudRequest() crudRequest: CrudRequestInterface,
@CrudBody() dto: PhotoCreateManyDtoFixture,
) {
return this.crudService.createMany(crudRequest, dto);
}

@CrudCreateOne
async createOne(
@CrudRequest() crudRequest: CrudRequestInterface,
@CrudBody() dto: PhotoCreateDtoFixture,
) {
return this.crudService.createOne(crudRequest, dto);
}

@CrudUpdateOne
async updateOne(
@CrudRequest() crudRequest: CrudRequestInterface,
@CrudBody() dto: PhotoUpdateDtoFixture,
) {
return this.crudService.createOne(crudRequest, dto);
}

@CrudReplaceOne
async replaceOne(
@CrudRequest() crudRequest: CrudRequestInterface,
@CrudBody() dto: PhotoUpdateDtoFixture,
) {
return this.crudService.replaceOne(crudRequest, dto);
}

@CrudDeleteOne
async deleteOne(@CrudRequest() crudRequest: CrudRequestInterface) {
return this.crudService.deleteOne(crudRequest);
}

@CrudRecoverOne
async recoverOne(@CrudRequest() crudRequest: CrudRequestInterface) {
return this.crudService.recoverOne(crudRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { PhotoFixture } from '../photo/photo.entity.fixture';

import {
PhotoCcbCustomControllerFixture,
PhotoCcbCustomCrudServiceFixture,
PHOTO_CRUD_SERVICE_TOKEN,
} from './photo-ccb-custom.controller.fixture';

@Module({
imports: [TypeOrmModule.forFeature([PhotoFixture])],
providers: [
{
provide: PHOTO_CRUD_SERVICE_TOKEN,
useClass: PhotoCcbCustomCrudServiceFixture,
},
],
controllers: [PhotoCcbCustomControllerFixture],
})
export class PhotoCcbCustomModuleFixture {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { PhotoEntityInterfaceFixture } from '../photo/interfaces/photo-entity.interface.fixture';
import { PhotoCreatableInterfaceFixture } from '../photo/interfaces/photo-creatable.interface.fixture';
import { PhotoUpdatableInterfaceFixture } from '../photo/interfaces/photo-updatable.interface.fixture';
import { PhotoDtoFixture } from '../photo/dto/photo.dto.fixture';
import { PhotoPaginatedDtoFixture } from '../photo/dto/photo-paginated.dto.fixture';
import { PhotoCreateDtoFixture } from '../photo/dto/photo-create.dto.fixture';
import { PhotoCreateManyDtoFixture } from '../photo/dto/photo-create-many.dto.fixture';
import { PhotoUpdateDtoFixture } from '../photo/dto/photo-update.dto.fixture';
import { ConfigurableCrudBuilder } from '../../util/configurable-crud.builder';
import { CrudSoftDelete } from '../../decorators/routes/crud-soft-delete.decorator';

export const PHOTO_CRUD_SERVICE_TOKEN = Symbol('__PHOTO_CRUD_SERVICE_TOKEN__');

const crudBuilder = new ConfigurableCrudBuilder<
PhotoEntityInterfaceFixture,
PhotoCreatableInterfaceFixture,
PhotoUpdatableInterfaceFixture
>();

const { ConfigurableControllerClass, ConfigurableServiceClass } =
crudBuilder.build({
service: {
entityKey: 'photo',
injectionToken: PHOTO_CRUD_SERVICE_TOKEN,
},
controller: {
path: 'photo',
model: {
type: PhotoDtoFixture,
paginatedType: PhotoPaginatedDtoFixture,
},
},
getMany: {},
getOne: {},
createMany: {
dto: PhotoCreateManyDtoFixture,
},
createOne: {
dto: PhotoCreateDtoFixture,
},
updateOne: {
dto: PhotoUpdateDtoFixture,
},
replaceOne: {
dto: PhotoUpdateDtoFixture,
},
deleteOne: {
extraDecorators: [CrudSoftDelete(true)],
},
recoverOne: { path: 'recover/:id' },
});

export class PhotoCcbExtCrudServiceFixture extends ConfigurableServiceClass {}
export class PhotoCcbExtControllerFixture extends ConfigurableControllerClass {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TypeOrmExtModule } from '@concepta/nestjs-typeorm-ext';
import { Module } from '@nestjs/common';

import { PhotoFixture } from '../photo/photo.entity.fixture';

import {
PhotoCcbExtControllerFixture,
PhotoCcbExtCrudServiceFixture,
PHOTO_CRUD_SERVICE_TOKEN,
} from './photo-ccb-ext.controller.fixture';

@Module({
imports: [
TypeOrmExtModule.forFeature({
photo: {
entity: PhotoFixture,
},
}),
],
providers: [
{
provide: PHOTO_CRUD_SERVICE_TOKEN,
useClass: PhotoCcbExtCrudServiceFixture,
},
],
controllers: [PhotoCcbExtControllerFixture],
})
export class PhotoCcbExtModuleFixture {}
Loading

0 comments on commit 001c74c

Please sign in to comment.