Skip to content

Commit 6202c22

Browse files
committed
feat(json-api-nestjs): Allow set id from request for postOne
1 parent 81fd183 commit 6202c22

File tree

2 files changed

+14
-5
lines changed
  • libs/json-api/json-api-nestjs/src/lib/helper

2 files changed

+14
-5
lines changed

libs/json-api/json-api-nestjs/src/lib/helper/orm/methods/post-one/post-one.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import { DeepPartial, Equal } from 'typeorm';
1+
import { DeepPartial } from 'typeorm';
22
import {
33
Entity,
44
ResourceObject,
55
TypeormServiceObject,
66
} from '../../../../types';
77
import { PostData } from '../../../zod';
8-
import { RelationshipsResult } from '../../../../mixin/service';
9-
import { ObjectTyped } from '../../../utils';
108

119
export async function postOne<E extends Entity>(
1210
this: TypeormServiceObject<E>,
1311
inputData: PostData<E>
1412
): Promise<ResourceObject<E>> {
15-
const { attributes, relationships } = inputData;
13+
const { attributes, relationships, id } = inputData;
14+
15+
const idObject = id
16+
? { [this.typeormUtilsService.currentPrimaryColumn.toString()]: id }
17+
: {};
18+
19+
const attributesObject = {
20+
...attributes,
21+
...idObject,
22+
} as DeepPartial<E>;
1623

1724
const entityTarget = this.repository.manager.create(
1825
this.repository.target,
19-
attributes as DeepPartial<E>
26+
attributesObject
2027
);
2128

2229
const saveData = await this.typeormUtilsService.saveEntityData(

libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
zodRelationshipsSchema,
99
ZodRelationshipsSchema,
1010
} from './relationships';
11+
import { ZodIdSchema } from './id';
1112

1213
export type PostShape<E extends Entity> = {
14+
id: ZodOptional<ZodIdSchema>;
1315
attributes: ZodAttributesSchema<E>;
1416
type: ZodTypeSchema<string>;
1517
relationships: ZodOptional<ZodRelationshipsSchema<E>>;

0 commit comments

Comments
 (0)