Skip to content

BaseEntity should have a constructor  #4464

Closed as not planned
Closed as not planned

Description

@dpacmittal

Issue type:

[ ] question
[ ] bug report
[ x ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ x ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Consider the following:

@Entity()
export class Attribute extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  value: string;
}

I should be able to do:

const attr = new Attribute({
  name: name,
  value: 'xyz'
});
await attr.save();

This doesn't work right now. It's a pain to copy all the object properties if an object is already passed as an argument, and it has already been validated, sanitized, I should be able to directly pass in the object when creating a new instance.

Activity

  1. imomin commented on Jul 30, 2019

    @imomin

    This is how I do it,
    this.attributeRepository.create(attributeObj).save();

    optionally, you can create your own constructor, in your entity class.

        constructor(name: string, value?:string) {
            super();
            if (name !== undefined) {
                this.name = name;
            }
            else {
                throw new Error("Name cannot be empty or null.");
            }
            if(this.value) {
                this.value = value;
            }
        }
    
  2. mak1986 commented on Dec 29, 2020

    @mak1986

    This is how I do it with lodash.

      async createBot(
        createBotDto: CreateBotDto
      ) {
    
        const bot = new Bot()
    
        _.assignIn(bot, createBotDto)
    
        await bot.save()
    
        return bot
      }
    
  3. nebkat commented on Dec 29, 2020

    @nebkat
    Contributor

    Useful solution for this pattern:

    class ConstructableBaseEntity extends BaseEntity {
        static construct(this: new () => T, params: Partial<T>): T {
            return Object.assign(new this(), params);
        }
    }
    @Entity()
    export class Attribute extends ConstructableBaseEntity {
      @PrimaryGeneratedColumn()
      id: number;
    
      @Column()
      name: string;
    
      @Column()
      value: string;
    }
    
    Attribute.construct({id: 1, name: "test", value: "test"});
  4. mishabruml commented on Jun 16, 2022

    @mishabruml

    Useful solution for this pattern:

    class ConstructableBaseEntity extends BaseEntity {
        static construct(this: new () => T, params: Partial<T>): T {
            return Object.assign(new this(), params);
        }
    }
    @Entity()
    export class Attribute extends ConstructableBaseEntity {
      @PrimaryGeneratedColumn()
      id: number;
    
      @Column()
      name: string;
    
      @Column()
      value: string;
    }
    
    Attribute.construct({id: 1, name: "test", value: "test"});

    Lovely, thank you! Your first snippet is missing a Type parameter <T> :

    export class ConstructableBaseEntity extends BaseEntity {
      static construct<T>(this: new () => T, params: Partial<T>): T {
        return Object.assign(new this(), params);
      }
    }
  5. dlahaye commented on Apr 24, 2024

    @dlahaye

    Because BaseEntity owns a static method create, maybe this pattern could help too :)

    @Entity()
    export class WeeklyBudgetDao extends BaseEntity {
        @PrimaryGeneratedColumn()
        id: number;
    
        @Column()
        name: string;
    
        @Column()
        currentBalance: number;
    
        // static constructor method
        static fromDomain(model: WeeklyBudget) {
            return WeeklyBudgetDao.create({
                id: model.id,
                name: model.name,
                currentBalance: model.currentBalance,
            });
        }
    
        // bonus: method for DAO -> domain model mapping
        toDomain(): WeeklyBudget {
            return new WeeklyBudget({
                id: this.id,
                name: this.name,
                currentBalance: this.currentBalance,
            });
        }
    }

    What do you think about?

  6. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions