Skip to content

Commit daa9a9b

Browse files
klerickAlexander Kharkovey
andauthored
String type for id (#6)
* fix(json-api-nestjs): fix name for type * fix(json-api-nestjs): allow id as string Closes #5 * docs(json-api-nestjs): change readme * feat(json-api-nestjs): add import options Co-authored-by: Alexander Kharkovey <[email protected]>
1 parent fd955b5 commit daa9a9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+431
-248
lines changed

.github/workflows/Release.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ jobs:
3131
- name: Test
3232
run: |
3333
npx nx run-many --target=test --all --code-coverage --coverage --reporters=default --reporters=default --coverageReporters=cobertura,html,json
34-
SUMMARY=$(node ./tools/utils/merge-codecoverage/index.js | grep "All files" | awk '{print $4}')
35-
echo "COVERAGE=$(echo ${SUMMARY})" >> $GITHUB_ENV
36-
- name: Cretae Badge
37-
uses: schneegans/[email protected]
38-
with:
39-
auth: ${{ secrets.GIST_SECRET }}
40-
gistID: ${{ secrets.GIST_ID }}
41-
filename: coverage-json-api.json
42-
label: Test Coverage
43-
message: ${{ env.COVERAGE }}
44-
color: green
45-
namedLogo: jest
34+
# SUMMARY=$(node ./tools/utils/merge-codecoverage/index.js | grep "All files" | awk '{print $4}')
35+
# echo "COVERAGE=$(echo ${SUMMARY})" >> $GITHUB_ENV
36+
# - name: Cretae Badge
37+
# uses: schneegans/[email protected]
38+
# with:
39+
# auth: ${{ secrets.GIST_SECRET }}
40+
# gistID: ${{ secrets.GIST_ID }}
41+
# filename: coverage-json-api.json
42+
# label: Test Coverage
43+
# message: ${{ env.COVERAGE }}
44+
# color: green
45+
# namedLogo: jest
4646
- name: Deploy
4747
run: npx nx affected --target=deploy --base=$BEFORE_SHA
4848

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@
77
</p>
88

99
<p align="center">
10-
Json API plugin for
10+
Json API plugins for
1111
<a href="http://nestjs.com/" target="blank">NestJS</a>
1212
framework
1313
</p>
1414
<p>
1515
Tools to implement JSON API, such as, end point, query params, body params, validation and transformation response.
1616
</p>
1717

18-
## Description
19-
20-
<p>
21-
This plugin works upon TypeOrm library, which is used as the main database abstraction layer tool. The module automatically generates an API according to JSON API specification from the database structure (TypeORM entities). It supports features such as requests validation based on database fields types, request filtering, endpoints extending, data relations control and much more. Our module significantly reduces the development time of REST services by removing the need to negotiate the mechanism of client-server interaction and implementing automatic API generation without the need to write any code.
22-
</p>
23-
18+
- *[json-api-nestjs](https://github.com/klerick/nestjs-json-api/tree/master/libs/json-api-nestjs)* - plugin for create CRUD overs JSON API
19+
- *json-api-nestjs-sdk* - tool for client, call api over *json-api-nestjs*(coming soon...)
20+
- *kson-api-nestjs-acl* - tool for acl over *json-api-nestjs*(coming soon...)
2421
## Installation
2522

2623
```bash
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ParseUUIDPipe } from '@nestjs/common';
2+
import { BookList } from 'database';
3+
import { JsonApi } from 'json-api-nestjs';
4+
5+
@JsonApi(BookList, {
6+
pipeForId: ParseUUIDPipe,
7+
})
8+
export class ExtendBookListController {}

apps/example/src/app/resources/controllers/extend-user/extend-user.controller.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Get, Param, Inject } from '@nestjs/common';
2-
2+
import { HttpService } from '@nestjs/axios';
33
import { Users } from 'database';
44
import {
55
JsonApi,
@@ -10,6 +10,7 @@ import {
1010
QueryParams,
1111
} from 'json-api-nestjs';
1212
import { ExampleService } from '../../service/example/example.service';
13+
import { map } from 'rxjs';
1314

1415
@JsonApi(Users, {
1516
allowMethod: excludeMethod(['deleteRelationship']),
@@ -18,6 +19,7 @@ import { ExampleService } from '../../service/example/example.service';
1819
export class ExtendUserController extends JsonBaseController<Users> {
1920
@InjectService() public service: JsonApiService<Users>;
2021
@Inject(ExampleService) protected exampleService: ExampleService;
22+
@Inject(HttpService) protected httpService: HttpService;
2123

2224
public override getAll(query: QueryParams<Users>) {
2325
return this.service.getAll({ query });
@@ -27,4 +29,11 @@ export class ExtendUserController extends JsonBaseController<Users> {
2729
testOne(@Param('id') id: string): string {
2830
return this.exampleService.testMethode(id);
2931
}
32+
33+
@Get('test-http')
34+
testHttp() {
35+
return this.httpService
36+
.get('http://localhost:3333/api/v1/book-list')
37+
.pipe(map((r) => r.data));
38+
}
3039
}

apps/example/src/app/resources/resources.module.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { Module } from '@nestjs/common';
22
import { JsonApiModule } from 'json-api-nestjs';
3-
import { Users, Addresses, Comments, Roles } from 'database';
3+
import { HttpModule } from '@nestjs/axios';
4+
import { Users, Addresses, Comments, Roles, BookList } from 'database';
45

56
import { ExtendUserController } from './controllers/extend-user/extend-user.controller';
7+
import { ExtendBookListController } from './controllers/extend-book-list/extend-book-list.controller';
68
import { ExampleService } from './service/example/example.service';
79

810
@Module({
911
imports: [
1012
JsonApiModule.forRoot({
11-
entities: [Users, Addresses, Comments, Roles],
12-
controllers: [ExtendUserController],
13+
entities: [Users, Addresses, Comments, Roles, BookList],
14+
controllers: [ExtendUserController, ExtendBookListController],
1315
providers: [ExampleService],
16+
imports: [HttpModule],
1417
options: {
1518
debug: true,
1619
maxExecutionTime: 3000,

libs/database/src/lib/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
22

33
import { config as ormConfig } from './config-cli';
4-
import { Roles, Comments, Users, Addresses } from './entities';
4+
import { Roles, Comments, Users, Addresses, BookList } from './entities';
55

66
export const config: TypeOrmModuleOptions = {
77
...ormConfig,
88
...{
9-
entities: [Roles, Comments, Users, Addresses],
9+
entities: [Roles, Comments, Users, Addresses, BookList],
1010
},
1111
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
Column,
3+
Entity,
4+
PrimaryGeneratedColumn,
5+
UpdateDateColumn,
6+
} from 'typeorm';
7+
import { IsEmpty, IsNotEmpty } from 'class-validator';
8+
9+
@Entity('book_list')
10+
export class BookList {
11+
@PrimaryGeneratedColumn()
12+
public id: string;
13+
14+
@IsNotEmpty()
15+
@Column({
16+
type: 'text',
17+
nullable: false,
18+
})
19+
public text: string;
20+
21+
@IsEmpty()
22+
@Column({
23+
name: 'created_at',
24+
type: 'timestamp',
25+
nullable: true,
26+
default: 'CURRENT_TIMESTAMP',
27+
})
28+
public createdAt: Date;
29+
30+
@IsEmpty()
31+
@UpdateDateColumn({
32+
name: 'updated_at',
33+
type: 'timestamp',
34+
nullable: true,
35+
default: 'CURRENT_TIMESTAMP',
36+
})
37+
public updatedAt: Date;
38+
}

libs/database/src/lib/entities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './roles';
33
export * from './users';
44
export * from './comments';
55
export * from './users-have-roles';
6+
export * from './book-list';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
MigrationInterface,
3+
QueryRunner,
4+
Table,
5+
TableColumn,
6+
TableForeignKey,
7+
} from 'typeorm';
8+
9+
export class CreateCommentsTable1665469071344 implements MigrationInterface {
10+
protected readonly tableName = 'book_list';
11+
public async up(queryRunner: QueryRunner): Promise<void> {
12+
await queryRunner.query(`
13+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
14+
`);
15+
await queryRunner.createTable(
16+
new Table({
17+
name: this.tableName,
18+
columns: [
19+
new TableColumn({
20+
name: 'id',
21+
type: 'uuid',
22+
isGenerated: true,
23+
isPrimary: true,
24+
unsigned: true,
25+
generationStrategy: 'uuid',
26+
}),
27+
new TableColumn({
28+
name: 'text',
29+
type: 'text',
30+
isNullable: false,
31+
}),
32+
new TableColumn({
33+
name: 'created_at',
34+
type: 'timestamp',
35+
isNullable: true,
36+
default: 'CURRENT_TIMESTAMP',
37+
}),
38+
new TableColumn({
39+
name: 'updated_at',
40+
type: 'timestamp',
41+
isNullable: true,
42+
default: 'CURRENT_TIMESTAMP',
43+
}),
44+
],
45+
})
46+
);
47+
}
48+
49+
public async down(queryRunner: QueryRunner): Promise<void> {
50+
await queryRunner.dropTable(this.tableName);
51+
}
52+
}

libs/json-api-nestjs/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
****strong text****
21
# json-api-nestjs
32

3+
This plugin works upon TypeOrm library, which is used as the main database abstraction layer tool. The module automatically generates an API according to JSON API specification from the database structure (TypeORM entities). It supports features such as requests validation based on database fields types, request filtering, endpoints extending, data relations control and much more. Our module significantly reduces the development time of REST services by removing the need to negotiate the mechanism of client-server interaction and implementing automatic API generation without the need to write any code.
4+
45
## Installation
56

67
```bash
@@ -47,9 +48,11 @@ export interface ModuleOptions {
4748
controllers?: NestController[]; // List of controller, if you need extend default present
4849
connectionName?: string; // Type orm connection name: "default" is default name
4950
providers?: NestProvider[]; // List of addition provider for useing in custom controller
51+
imports?: NestImport[]; // List of addition module for useing in custom controller
5052
options?: {
5153
requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default
5254
debug?: boolean; // Debug info in result object
55+
pipeForId?: Type<PipeTransform> // Nestjs pipe for validate id params, by default ParseIntPipe
5356
};
5457
}
5558
```

0 commit comments

Comments
 (0)