Skip to content

Commit e41fd74

Browse files
authored
Merge pull request #45 from ringcentral/refactoring_update_additonal_attr
feat(json-api-sdk): refactoring update and insert attr
2 parents 07cd69f + 0942ba5 commit e41fd74

File tree

2 files changed

+84
-56
lines changed

2 files changed

+84
-56
lines changed

libs/database/src/entities/requests-have-pod-locks.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {
2+
AfterLoad, BeforeInsert, BeforeRemove, BeforeUpdate,
23
Column,
34
CreateDateColumn,
45
Entity,
56
PrimaryGeneratedColumn,
6-
UpdateDateColumn,
7+
UpdateDateColumn
78
} from 'typeorm';
89
import { IsEmpty, IsNotEmpty } from 'class-validator';
910

@@ -14,14 +15,40 @@ export class RequestsHavePodLocks {
1415
@PrimaryGeneratedColumn()
1516
public id: number;
1617

17-
public set requestId(id){
18-
this.request_id = id
18+
@AfterLoad()
19+
protected getRequestId() {
20+
this.requestId = this.request_id;
1921
}
2022

21-
public get requestId(){
22-
return this.request_id;
23+
@BeforeInsert()
24+
@BeforeUpdate()
25+
@BeforeRemove()
26+
protected setRequestId() {
27+
if (this.requestId) {
28+
this.request_id = this.requestId;
29+
}
30+
31+
}
32+
33+
public requestId: number;
34+
35+
@AfterLoad()
36+
protected getPodId() {
37+
this.podId = this.pod_id;
38+
}
39+
40+
@BeforeInsert()
41+
@BeforeUpdate()
42+
@BeforeRemove()
43+
protected setPodId() {
44+
if (this.podId) {
45+
this.pod_id = this.podId;
46+
}
47+
2348
}
2449

50+
public podId: number;
51+
2552
@IsNotEmpty()
2653
@Column({
2754
name: 'request_id',
@@ -38,14 +65,6 @@ export class RequestsHavePodLocks {
3865
})
3966
protected pod_id: number;
4067

41-
public set podId(id){
42-
this.pod_id = id
43-
}
44-
45-
public get podId(): number{
46-
return this.request_id;
47-
}
48-
4968
@IsEmpty()
5069
@CreateDateColumn({
5170
name: 'created_at',

libs/json-api/src/lib/mixins/service/service.mixin.ts

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -470,34 +470,40 @@ export function serviceMixin(entity: Entity, transform: TransformMixin, connecti
470470
.relation(relName)
471471
.of(id);
472472

473+
if (relationObject.relationType === 'many-to-many' && (body as BaseData[]).some(i => i.attributes)) {
474+
const repoTarget = this.repository.manager.getRepository<EntitySchema>(relationObject.junctionEntityMetadata.target);
475+
476+
const entityTarget = repoTarget.target as any
477+
const ownerPropertyName = relationObject.junctionEntityMetadata.ownerColumns[0].propertyName
478+
const inversePropertyName = relationObject.junctionEntityMetadata.inverseColumns[0].propertyName
479+
const bodyParams = body as BaseData[];
480+
481+
const currentData = bodyParams.map((body) => {
482+
const target = new entityTarget();
483+
484+
target[ownerPropertyName] = id
485+
target[inversePropertyName] = body.id
486+
if (body.attributes) {
487+
Object.entries(body.attributes).forEach(([key, val]) => {
488+
target[key] = val;
489+
});
490+
}
491+
return target;
492+
})
493+
const removeData = bodyParams.map(body => ({
494+
[ownerPropertyName]: id,
495+
[inversePropertyName]: body.id
496+
}));
497+
await repoTarget.createQueryBuilder().delete().where(removeData).execute();
498+
await repoTarget.save(currentData);
499+
return;
500+
}
501+
473502
if (['one-to-many', 'many-to-many'].includes(relationObject.relationType)) {
474503
const currentEntities = await builderDeleteRelationships.loadMany();
475504
const idsToDelete = currentEntities.map(entity => entity.id);
476505
const idsToAdd = body !== null ? (body as BaseData[]).map(i => i.id) : [];
477-
478506
await builderDeleteRelationships.addAndRemove(idsToAdd, idsToDelete);
479-
480-
if (relationObject.relationType === 'many-to-many' && (body as BaseData[]).some(i => i.attributes)) {
481-
const repoTarget = this.repository.manager.getRepository<EntitySchema>(relationObject.junctionEntityMetadata.target);
482-
const currentData = await repoTarget.find({
483-
where: {
484-
[relationObject.junctionEntityMetadata.ownColumns[0].databasePath]: id
485-
}
486-
});
487-
const propertyName = relationObject.junctionEntityMetadata.inverseColumns[0].propertyName
488-
const bodyParams = body as BaseData[];
489-
currentData.forEach((item) => {
490-
bodyParams.forEach(body => {
491-
if (body.attributes && parseInt(item[propertyName], 10) === parseInt(body.id, 10)) {
492-
Object.entries(body.attributes).forEach(([key, val]) => {
493-
item[key] = val;
494-
});
495-
}
496-
})
497-
});
498-
await repoTarget.save(currentData);
499-
}
500-
501507
} else if (body !== null) {
502508
const { id } = Array.isArray(body) ? body.shift() : body;
503509
await builderDeleteRelationships.set(id);
@@ -580,6 +586,29 @@ export function serviceMixin(entity: Entity, transform: TransformMixin, connecti
580586
const relations = this.repository.metadata.relations.find(item => {
581587
return item.propertyName === relName;
582588
});
589+
590+
if (relations.relationType === 'many-to-many' && (body as BaseData[]).some(item => item.attributes)) {
591+
const repoTarget = this.repository.manager.getRepository<EntitySchema>(relations.junctionEntityMetadata.target);
592+
const entityTarget = repoTarget.target as any
593+
594+
const ownerPropertyName = relations.junctionEntityMetadata.ownerColumns[0].propertyName
595+
const inversePropertyName = relations.junctionEntityMetadata.inverseColumns[0].propertyName
596+
const bodyParams = body as BaseData[];
597+
const currentData = bodyParams.map((body) => {
598+
const target = new entityTarget(entityTarget);
599+
target[ownerPropertyName] = id
600+
target[inversePropertyName] = body.id
601+
if (body.attributes) {
602+
Object.entries(body.attributes).forEach(([key, val]) => {
603+
target[key] = val;
604+
});
605+
}
606+
return target;
607+
})
608+
await repoTarget.save(currentData);
609+
return;
610+
}
611+
583612
const postBuilder = this.repository.createQueryBuilder()
584613
.relation(relName)
585614
.of(id);
@@ -590,26 +619,6 @@ export function serviceMixin(entity: Entity, transform: TransformMixin, connecti
590619
const { id } = Array.isArray(body) ? body.shift() : body;
591620
await postBuilder.set(id);
592621
}
593-
if (relations.relationType === 'many-to-many' && (body as BaseData[]).some(item => item.attributes)) {
594-
const repoTarget = this.repository.manager.getRepository<EntitySchema>(relations.junctionEntityMetadata.target);
595-
const currentData = await repoTarget.find({
596-
where: {
597-
[relations.junctionEntityMetadata.ownColumns[0].databasePath]: id
598-
}
599-
});
600-
const propertyName = relations.junctionEntityMetadata.inverseColumns[0].propertyName
601-
const bodyParams = body as BaseData[];
602-
currentData.forEach((item) => {
603-
bodyParams.forEach(body => {
604-
if (body.attributes && parseInt(item[propertyName], 10) === parseInt(body.id, 10)) {
605-
Object.entries(body.attributes).forEach(([key, val]) => {
606-
item[key] = val;
607-
});
608-
}
609-
})
610-
});
611-
await repoTarget.save(currentData);
612-
}
613622

614623
}
615624

0 commit comments

Comments
 (0)