Sergey  Sharyhin
Sergey Sharyhin

Reputation: 125

TypeORM doesn't return entire entity after calling .save() method

I have a simple entity:

import { BaseEntity, Entity, Column, PrimaryGeneratedColumn, Timestamp } from 'typeorm';

@Entity('organizations')
export class OrganizationEntity extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column('varchar', { length: 10 })
    uid: string;

    @Column('varchar', { length: 100 })
    name: string;

    @Column('varchar', { length: 100 })
    status: string;

    @Column('timestamp')
    createdAt: Timestamp;

    @Column('timestamp')
    updatedAt: Timestamp;

    @Column('timestamp')
    deletedAt: Timestamp;
}

Creating a new entity:

const organization = new OrganizationEntity();
organization.name = 'someName';
organization.status = 'someStatus';
await organization.save();

In Postgres SQL looks like this:

INSERT INTO "organizations"("uid", "name", "status", "createdAt", "updatedAt", "deletedAt") VALUES (DEFAULT, $1, $2, DEFAULT, DEFAULT, DEFAULT) RETURNING "id", "status"

As can be seen it returns only filled fields in RETURNING statement. I know there are some workarounds by using query builder but I am concern is there any semantical way how I can return an entire entity after calling method save so there will be RETURNING * Thanks in advance.

Upvotes: 2

Views: 12604

Answers (2)

Rohan Luthra
Rohan Luthra

Reputation: 51

You can use something like this until TypeORM fixes it:

const user = await this.userRepository.findOne(id);
// handle user not found
const updatedUser = await this.userRepository.save({ id, ...updateDto });
return Object.assign(user, updatedUser);

Upvotes: 5

Sergey  Sharyhin
Sergey Sharyhin

Reputation: 125

Thanks @noam-steiner. It looks like this issue is in development and probably will be fixed soon. There is even open PR: https://github.com/typeorm/typeorm/pull/5680.

Upvotes: 0

Related Questions