-
-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crud-objection): update objection-crud related code to the lates…
…t upstream
- Loading branch information
Showing
70 changed files
with
3,636 additions
and
1,291 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
|
||
import { UsersService } from './users'; | ||
import { USER_REQUEST_KEY } from './constants'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor(private usersService: UsersService) {} | ||
|
||
async canActivate(ctx: ExecutionContext): Promise<boolean> { | ||
const req = ctx.switchToHttp().getRequest(); | ||
req[USER_REQUEST_KEY] = await this.usersService.modelClass.query().findById(1); | ||
|
||
return true; | ||
} | ||
} |
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
33 changes: 29 additions & 4 deletions
33
integration/crud-objection/companies/companies.controller.ts
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
19 changes: 19 additions & 0 deletions
19
integration/crud-objection/companies/requests/create-company.dto.ts
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,19 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsString, MaxLength } from 'class-validator'; | ||
|
||
export class CreateCompanyDto { | ||
@ApiProperty({ type: 'string' }) | ||
@IsString() | ||
@MaxLength(100) | ||
name: string; | ||
|
||
@ApiProperty({ type: 'string' }) | ||
@IsString() | ||
@MaxLength(100) | ||
domain: string; | ||
|
||
@ApiProperty({ type: 'string' }) | ||
@IsString() | ||
@MaxLength(100) | ||
description: string; | ||
} |
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,5 @@ | ||
import { CreateCompanyDto } from './create-company.dto'; | ||
|
||
export const dto = { | ||
create: CreateCompanyDto, | ||
}; |
22 changes: 22 additions & 0 deletions
22
integration/crud-objection/companies/responses/get-company-response.dto.ts
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,22 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Exclude } from 'class-transformer'; | ||
|
||
export class GetCompanyResponseDto { | ||
@ApiProperty({ type: 'number' }) | ||
id: string; | ||
|
||
@ApiProperty({ type: 'string' }) | ||
name: string; | ||
|
||
@ApiProperty({ type: 'string' }) | ||
domain: string; | ||
|
||
@ApiProperty({ type: 'string' }) | ||
description: string; | ||
|
||
@Exclude() | ||
createdAt: any; | ||
|
||
@Exclude() | ||
updatedAt: any; | ||
} |
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,6 @@ | ||
import { SerializeOptions } from '@nestjsx/crud'; | ||
import { GetCompanyResponseDto } from './get-company-response.dto'; | ||
|
||
export const serialize: SerializeOptions = { | ||
get: GetCompanyResponseDto, | ||
}; |
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 const USER_REQUEST_KEY = 'user'; |
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 |
---|---|---|
|
@@ -2,18 +2,30 @@ import { Global, Module } from '@nestjs/common'; | |
import * as Knex from 'knex'; | ||
import { knexSnakeCaseMappers, Model } from 'objection'; | ||
import { UserProfile } from './users-profiles'; | ||
import { Project } from './projects'; | ||
import { Project, UserProject } from './projects'; | ||
import { Company } from './companies'; | ||
import { User } from './users'; | ||
import { KNEX_CONNECTION } from './injection-tokens'; | ||
import { UserProject } from './users-projects'; | ||
import { Device } from './devices'; | ||
import { License, UserLicense } from './users-licenses'; | ||
import { Note } from './notes'; | ||
|
||
const models = [User, Company, Project, UserProfile, UserProject]; | ||
const models = [ | ||
User, | ||
Company, | ||
Project, | ||
UserProfile, | ||
UserProject, | ||
Device, | ||
License, | ||
UserLicense, | ||
Note, | ||
]; | ||
|
||
const modelProviders = models.map(model => { | ||
const modelProviders = models.map((model) => { | ||
return { | ||
provide: model.name, | ||
useValue: model | ||
useValue: model, | ||
}; | ||
}); | ||
|
||
|
@@ -26,18 +38,18 @@ const providers = [ | |
client: 'pg', | ||
connection: 'postgres://root:[email protected]:5455/nestjsx_crud_objection', | ||
debug: process.env.KNEX_DEBUG === 'true', | ||
...knexSnakeCaseMappers() | ||
...knexSnakeCaseMappers(), | ||
}); | ||
|
||
Model.knex(knex); | ||
return knex; | ||
} | ||
} | ||
}, | ||
}, | ||
]; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [...providers], | ||
exports: [...providers] | ||
exports: [...providers], | ||
}) | ||
export class DatabaseModule {} |
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,18 @@ | ||
import { IsOptional, IsString, IsUUID } from 'class-validator'; | ||
import { Model } from 'objection'; | ||
|
||
export class Device extends Model { | ||
static readonly tableName = 'devices'; | ||
|
||
static get idColumn() { | ||
return ['deviceKey']; | ||
} | ||
|
||
@IsOptional({ always: true }) | ||
@IsUUID('4', { always: true }) | ||
deviceKey: string; | ||
|
||
@IsOptional({ always: true }) | ||
@IsString({ always: true }) | ||
description?: string; | ||
} |
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 { Controller } from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { Crud } from '@nestjsx/crud'; | ||
|
||
import { DevicesService } from './devices.service'; | ||
import { serialize } from './response'; | ||
import { Device } from './device.model'; | ||
|
||
@Crud({ | ||
model: { type: Device }, | ||
serialize, | ||
params: { | ||
deviceKey: { | ||
field: 'deviceKey', | ||
type: 'uuid', | ||
primary: true, | ||
}, | ||
}, | ||
routes: { | ||
deleteOneBase: { | ||
returnDeleted: true, | ||
}, | ||
}, | ||
}) | ||
@ApiTags('devices') | ||
@Controller('/devices') | ||
export class DevicesController { | ||
constructor(public service: DevicesService) {} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { DevicesService } from './devices.service'; | ||
import { DevicesController } from './devices.controller'; | ||
|
||
@Module({ | ||
providers: [DevicesService], | ||
exports: [DevicesService], | ||
controllers: [DevicesController], | ||
}) | ||
export class DevicesModule {} |
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,12 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
|
||
import { Device } from './device.model'; | ||
import { ObjectionCrudService } from '@nestjsx/crud-objection'; | ||
import { ModelClass } from 'objection'; | ||
|
||
@Injectable() | ||
export class DevicesService extends ObjectionCrudService<Device> { | ||
constructor(@Inject('Device') modelClass: ModelClass<Device>) { | ||
super(modelClass); | ||
} | ||
} |
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,2 @@ | ||
export * from './device.model'; | ||
export * from './devices.service'; |
10 changes: 10 additions & 0 deletions
10
integration/crud-objection/devices/response/delete-device-response.dto.ts
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,10 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Exclude } from 'class-transformer'; | ||
|
||
export class DeleteDeviceResponseDto { | ||
@ApiProperty({ type: 'string' }) | ||
deviceKey: string; | ||
|
||
@Exclude() | ||
description?: string; | ||
} |
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,6 @@ | ||
import { SerializeOptions } from '@nestjsx/crud'; | ||
import { DeleteDeviceResponseDto } from './delete-device-response.dto'; | ||
|
||
export const serialize: SerializeOptions = { | ||
delete: DeleteDeviceResponseDto, | ||
}; |
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
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
Oops, something went wrong.