Skip to content

Commit

Permalink
feat(json-api-nestjs): Allow set id from request for postOne
Browse files Browse the repository at this point in the history
  • Loading branch information
klerick committed Dec 5, 2024
1 parent 81fd183 commit 6202c22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { DeepPartial, Equal } from 'typeorm';
import { DeepPartial } from 'typeorm';
import {
Entity,
ResourceObject,
TypeormServiceObject,
} from '../../../../types';
import { PostData } from '../../../zod';
import { RelationshipsResult } from '../../../../mixin/service';
import { ObjectTyped } from '../../../utils';

export async function postOne<E extends Entity>(
this: TypeormServiceObject<E>,
inputData: PostData<E>
): Promise<ResourceObject<E>> {
const { attributes, relationships } = inputData;
const { attributes, relationships, id } = inputData;

const idObject = id
? { [this.typeormUtilsService.currentPrimaryColumn.toString()]: id }
: {};

const attributesObject = {
...attributes,
...idObject,
} as DeepPartial<E>;

const entityTarget = this.repository.manager.create(
this.repository.target,
attributes as DeepPartial<E>
attributesObject
);

const saveData = await this.typeormUtilsService.saveEntityData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
zodRelationshipsSchema,
ZodRelationshipsSchema,
} from './relationships';
import { ZodIdSchema } from './id';

export type PostShape<E extends Entity> = {
id: ZodOptional<ZodIdSchema>;
attributes: ZodAttributesSchema<E>;
type: ZodTypeSchema<string>;
relationships: ZodOptional<ZodRelationshipsSchema<E>>;
Expand Down

0 comments on commit 6202c22

Please sign in to comment.