Skip to content

Commit

Permalink
fix(crud): primary param fix and swagger
Browse files Browse the repository at this point in the history
fix #283
  • Loading branch information
michaelyali committed Oct 4, 2019
1 parent 2eb90fa commit bdfcc55
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 11 deletions.
2 changes: 2 additions & 0 deletions integration/crud-typeorm/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { withCache } from './orm.config';
import { CompaniesModule } from './companies/companies.module';
import { ProjectsModule } from './projects/projects.module';
import { UsersModule } from './users/users.module';
import { DevicesModule } from './devices/devices.module';

@Module({
imports: [
TypeOrmModule.forRoot(withCache),
CompaniesModule,
ProjectsModule,
UsersModule,
DevicesModule,
],
})
export class AppModule {}
18 changes: 18 additions & 0 deletions integration/crud-typeorm/devices/device.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
import { IsOptional, IsString, IsUUID } from 'class-validator';
import { CrudValidationGroups } from '@nestjsx/crud';

const { CREATE, UPDATE } = CrudValidationGroups;

@Entity('devices')
export class Device {
@IsOptional({ always: true })
@IsUUID('4', { always: true })
@PrimaryGeneratedColumn('uuid')
deviceKey: string;

@IsOptional({ always: true })
@IsString({ always: true })
@Column({ type: 'text', nullable: true })
description?: string;
}
22 changes: 22 additions & 0 deletions integration/crud-typeorm/devices/devices.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller } from '@nestjs/common';
import { ApiUseTags } from '@nestjs/swagger';
import { Crud } from '@nestjsx/crud';

import { Device } from './device.entity';
import { DevicesService } from './devices.service';

@Crud({
model: { type: Device },
params: {
deviceKey: {
field: 'deviceKey',
type: 'uuid',
primary: true,
},
},
})
@ApiUseTags('devices')
@Controller('/devices')
export class DevicesController {
constructor(public service: DevicesService) {}
}
14 changes: 14 additions & 0 deletions integration/crud-typeorm/devices/devices.module.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 { Device } from './device.entity';
import { DevicesService } from './devices.service';
import { DevicesController } from './devices.controller';

@Module({
imports: [TypeOrmModule.forFeature([Device])],
providers: [DevicesService],
exports: [DevicesService],
controllers: [DevicesController],
})
export class DevicesModule {}
12 changes: 12 additions & 0 deletions integration/crud-typeorm/devices/devices.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';

import { Device } from './device.entity';

@Injectable()
export class DevicesService extends TypeOrmCrudService<Device> {
constructor(@InjectRepository(Device) repo) {
super(repo);
}
}
2 changes: 2 additions & 0 deletions integration/crud-typeorm/devices/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './device.entity';
export * from './devices.service';
19 changes: 15 additions & 4 deletions packages/crud/src/crud/crud-routes.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,20 @@ export class CrudRoutesFactory {
arrayMerge: (a, b, c) => b,
});

// merge params
const params = isObjectFull(this.options.params) ? this.options.params : {};
this.options.params = { ...CrudConfigService.config.params, ...params };
// set params
this.options.params = isObjectFull(this.options.params)
? this.options.params
: isObjectFull(CrudConfigService.config.params)
? CrudConfigService.config.params
: {};
const hasPrimary = this.getPrimaryParam();
if (!hasPrimary) {
this.options.params['id'] = {
field: 'id',
type: 'number',
primary: true,
};
}

R.setCrudOptions(this.options, this.target);
}
Expand Down Expand Up @@ -292,7 +303,7 @@ export class CrudRoutesFactory {

private getPrimaryParam(): string {
return objKeys(this.options.params).find(
(param) => this.options.params[param].primary,
(param) => this.options.params[param] && this.options.params[param].primary,
);
}

Expand Down
8 changes: 1 addition & 7 deletions packages/crud/src/module/crud-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ export class CrudConfigService {
},
deleteOneBase: { interceptors: [], decorators: [], returnDeleted: false },
},
params: {
id: {
field: 'id',
type: 'number',
primary: true,
},
},
params: {},
};

static load(config: CrudGlobalConfig = {}) {
Expand Down

0 comments on commit bdfcc55

Please sign in to comment.