Skip to content

Commit c8da9f8

Browse files
author
Alexander Kharkovey
committed
feat(json-api-nestjs-sdk): new version sdk
BREAKING CHANGE: change param for getOne method
1 parent daa9a9b commit c8da9f8

Some content is hidden

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

41 files changed

+3216
-827
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
</p>
1717

1818
- *[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...)
19+
- *[json-api-nestjs-sdk](https://github.com/klerick/nestjs-json-api/tree/master/libs/json-api-nestjs-sdk)* - tool for client, call api over *json-api-nestjs*
20+
- *json-api-nestjs-acl* - tool for acl over *json-api-nestjs*(coming soon...)
2121
## Installation
2222

2323
```bash

apps/example-angular-client/project.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
"prefix": "nestjs-json-api",
77
"targets": {
88
"build": {
9-
"executor": "@angular-devkit/build-angular:browser",
9+
"executor": "@angular-builders/custom-webpack:browser",
1010
"outputs": ["{options.outputPath}"],
1111
"options": {
12+
"customWebpackConfig": {
13+
"path": "apps/example-angular-client/webpack.config.ts",
14+
"mergeRules": {
15+
"externals": "replace"
16+
}
17+
},
1218
"outputPath": "dist/apps/example-angular-client",
1319
"index": "apps/example-angular-client/src/index.html",
1420
"main": "apps/example-angular-client/src/main.ts",
@@ -55,7 +61,7 @@
5561
"defaultConfiguration": "production"
5662
},
5763
"serve": {
58-
"executor": "@angular-devkit/build-angular:dev-server",
64+
"executor": "@angular-builders/custom-webpack:dev-server",
5965
"configurations": {
6066
"production": {
6167
"browserTarget": "example-angular-client:build:production"
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
<nestjs-json-api-nx-welcome></nestjs-json-api-nx-welcome>
1+
<div>
2+
<pre>{{dataItem | async | json}}</pre>
3+
</div>
4+
<div>
5+
<pre>{{dataList | async | json}}</pre>
6+
</div>
Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,113 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
2+
import { JsonApiSdkService, EmptyArrayRelation } from 'json-api-nestjs-sdk';
3+
import { Users, BookList, Addresses } from 'database/entity';
4+
import { map, switchMap } from 'rxjs';
5+
6+
const user = new Users();
7+
user.id = 17;
28

39
@Component({
410
selector: 'nestjs-json-api-root',
511
templateUrl: './app.component.html',
612
styleUrls: ['./app.component.css'],
13+
changeDetection: ChangeDetectionStrategy.OnPush,
714
})
8-
export class AppComponent {
15+
export class AppComponent implements OnInit {
916
title = 'example-angular-client';
17+
dataList = this.jsonApiSdkService.getAll<Users>(
18+
Users,
19+
{
20+
filter: {
21+
target: {
22+
isActive: {
23+
eq: 'true',
24+
},
25+
},
26+
},
27+
include: ['comments', 'addresses'],
28+
page: {
29+
size: 5,
30+
number: 1,
31+
},
32+
},
33+
true
34+
);
35+
dataItem = this.jsonApiSdkService.getOne<Users>(
36+
Users,
37+
17,
38+
{
39+
fields: {
40+
target: ['login'],
41+
addresses: ['city', 'state'],
42+
comments: ['text', 'kind'],
43+
},
44+
include: ['comments', 'addresses'],
45+
},
46+
true
47+
);
48+
49+
constructor(private jsonApiSdkService: JsonApiSdkService) {}
50+
51+
ngOnInit(): void {
52+
this.jsonApiSdkService
53+
.getOne<Users>(
54+
Users,
55+
17,
56+
{
57+
fields: {
58+
target: ['login'],
59+
addresses: ['city', 'state'],
60+
comments: ['text', 'kind'],
61+
},
62+
include: ['comments', 'addresses'],
63+
},
64+
true
65+
)
66+
.pipe(
67+
switchMap(({ entity, meta }) => {
68+
const bookList = new BookList();
69+
bookList.users = [entity];
70+
bookList.text = 'Book list';
71+
return this.jsonApiSdkService.postOne<BookList>(bookList, true);
72+
}),
73+
switchMap(({ entity, meta }) => {
74+
const bookList = new BookList();
75+
bookList.id = entity.id;
76+
bookList.users = new EmptyArrayRelation();
77+
bookList.text = 'Change Text';
78+
return this.jsonApiSdkService.patchOne(bookList, true);
79+
}),
80+
switchMap(({ entity, meta }) => {
81+
return this.jsonApiSdkService.deleteOne(entity);
82+
})
83+
)
84+
.subscribe((r) => console.log(r));
85+
86+
this.jsonApiSdkService
87+
.getRelationships(user, 'addresses')
88+
.subscribe((r) => console.log(r));
89+
this.jsonApiSdkService
90+
.getRelationships(user, 'comments')
91+
.subscribe((r) => console.log(r));
92+
93+
this.jsonApiSdkService
94+
.getAll<Addresses>(Addresses)
95+
.pipe(
96+
map((r) => r[0]),
97+
switchMap((addresses) => {
98+
return this.jsonApiSdkService
99+
.getOne<Users>(Users, 18, { include: ['comments', 'addresses'] })
100+
.pipe(
101+
switchMap((user) => {
102+
user.addresses = addresses;
103+
return this.jsonApiSdkService.patchRelationships<Users>(
104+
user,
105+
'addresses'
106+
);
107+
})
108+
);
109+
})
110+
)
111+
.subscribe((r) => console.log(r));
112+
}
10113
}

apps/example-angular-client/src/app/app.module.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
23
import { BrowserModule } from '@angular/platform-browser';
4+
import { JsonApiNestjsSdkModule } from 'json-api-nestjs-sdk';
5+
import { BookList, Users, Roles, Comments, Addresses } from 'database/entity';
36

47
import { AppComponent } from './app.component';
58
import { NxWelcomeComponent } from './nx-welcome.component';
69

710
@NgModule({
811
declarations: [AppComponent, NxWelcomeComponent],
9-
imports: [BrowserModule],
12+
imports: [
13+
CommonModule,
14+
BrowserModule,
15+
JsonApiNestjsSdkModule.forRoot(
16+
{
17+
apiPrefix: '/api/v1',
18+
apiHost: window.location.origin,
19+
},
20+
{ BookList, Users, Roles, Comments, Addresses }
21+
),
22+
],
1023
providers: [],
1124
bootstrap: [AppComponent],
1225
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Configuration } from 'webpack';
2+
import { CustomWebpackBrowserSchema, TargetOptions } from '@angular-builders/custom-webpack';
3+
import {resolve} from 'path';
4+
5+
export default function webPackConfig(
6+
config: Configuration,
7+
options: CustomWebpackBrowserSchema,
8+
targetOptions: TargetOptions): Configuration {
9+
10+
(config.resolve || {}).alias = {
11+
typeorm: resolve(__dirname, "../../node_modules/typeorm/typeorm-model-shim"),
12+
validator: resolve(__dirname, "../../node_modules/validator/es")
13+
}
14+
15+
return config;
16+
}
17+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { map } from 'rxjs';
1414

1515
@JsonApi(Users, {
1616
allowMethod: excludeMethod(['deleteRelationship']),
17-
requiredSelectField: true,
17+
requiredSelectField: false,
1818
})
1919
export class ExtendUserController extends JsonBaseController<Users> {
2020
@InjectService() public service: JsonApiService<Users>;

libs/database/src/entity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/entities';

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type IAddresses = Addresses;
1414
@Entity('addresses')
1515
export class Addresses {
1616
@PrimaryGeneratedColumn()
17-
public id: number;
17+
public id!: number;
1818

1919
@IsOptional()
2020
@Length(3, 70)
@@ -24,7 +24,7 @@ export class Addresses {
2424
nullable: true,
2525
default: 'NULL',
2626
})
27-
public city: string;
27+
public city!: string;
2828

2929
@IsOptional()
3030
@Length(3, 70)
@@ -34,7 +34,7 @@ export class Addresses {
3434
nullable: true,
3535
default: 'NULL',
3636
})
37-
public state: string;
37+
public state!: string;
3838

3939
@IsOptional()
4040
@Length(3, 68)
@@ -44,7 +44,7 @@ export class Addresses {
4444
nullable: true,
4545
default: 'NULL',
4646
})
47-
public country: string;
47+
public country!: string;
4848

4949
@IsEmpty()
5050
@Column({
@@ -53,7 +53,7 @@ export class Addresses {
5353
nullable: true,
5454
default: 'CURRENT_TIMESTAMP',
5555
})
56-
public createdAt: Date;
56+
public createdAt!: Date;
5757

5858
@IsEmpty()
5959
@UpdateDateColumn({
@@ -62,8 +62,8 @@ export class Addresses {
6262
nullable: true,
6363
default: 'CURRENT_TIMESTAMP',
6464
})
65-
public updatedAt: Date;
65+
public updatedAt!: Date;
6666

6767
@OneToOne(() => Users, (item) => item.addresses)
68-
public user: IUsers;
68+
public user!: IUsers;
6969
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import {
22
Column,
33
Entity,
4+
ManyToMany,
45
PrimaryGeneratedColumn,
56
UpdateDateColumn,
67
} from 'typeorm';
78
import { IsEmpty, IsNotEmpty } from 'class-validator';
9+
import { IUsers, Users } from './users';
10+
11+
export type IBookList = BookList;
812

913
@Entity('book_list')
1014
export class BookList {
1115
@PrimaryGeneratedColumn()
12-
public id: string;
16+
public id!: string;
1317

1418
@IsNotEmpty()
1519
@Column({
1620
type: 'text',
1721
nullable: false,
1822
})
19-
public text: string;
23+
public text!: string;
2024

2125
@IsEmpty()
2226
@Column({
@@ -25,7 +29,7 @@ export class BookList {
2529
nullable: true,
2630
default: 'CURRENT_TIMESTAMP',
2731
})
28-
public createdAt: Date;
32+
public createdAt!: Date;
2933

3034
@IsEmpty()
3135
@UpdateDateColumn({
@@ -34,5 +38,8 @@ export class BookList {
3438
nullable: true,
3539
default: 'CURRENT_TIMESTAMP',
3640
})
37-
public updatedAt: Date;
41+
public updatedAt!: Date;
42+
43+
@ManyToMany(() => Users, (item) => item.books)
44+
public users!: IUsers[];
3845
}

0 commit comments

Comments
 (0)