How to use the typeorm.BeforeUpdate function in typeorm

To help you get started, we’ve selected a few typeorm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github danielwii / asuna-node-server / src / modules / core / auth / auth.entities.ts View on Github external
@Column({ nullable: true })
  description: string;

  @MetaInfo({ name: '权限', type: 'Authorities' })
  @Column(jsonType(), { nullable: true })
  authorities: JsonMap;

  @ManyToMany(
    // eslint-disable-next-line @typescript-eslint/no-use-before-define
    type => AdminUser,
    user => user.roles,
  )
  users: AdminUser[];

  @BeforeInsert()
  @BeforeUpdate()
  preSave(): void {
    safeReloadObject(this, 'authorities');
  }
}

@EntityMetaInfo({ name: 'auth__users' })
@Entity('auth__t_users')
export class AdminUser extends AbstractTimeBasedAuthUser {
  constructor() {
    super('sa');
  }

  @MetaInfo({ name: 'Tenant' })
  @ManyToOne(
    type => Tenant,
    tenant => tenant.users,
github danielwii / asuna-node-server / src / modules / app / app.entities.ts View on Github external
@MetaInfo({ name: 'File', type: 'File' })
  @Column(jsonType(), { nullable: false, name: 'paths' })
  paths: JsonArray;

  @MetaInfo({ name: '所属应用' })
  @ManyToOne(
    type => AppInfo,
    info => info.releases,
    { onDelete: 'CASCADE' },
  )
  @JoinColumn({ name: 'app_info__id' })
  appInfo: AppInfo;

  // TODO try reload in entity subscribers
  @BeforeInsert()
  @BeforeUpdate()
  preSave(): void {
    safeReloadArray(this, 'paths');
  }
}
github chnirt / nestjs-graphql-best-practice / src / models / file.entity.ts View on Github external
createdAt: number
	@Column()
	updatedAt: number

	constructor(file: Partial) {
		Object.assign(this, file)
	}

	@BeforeInsert()
	save() {
		this._id = uuid.v1()
		this.createdAt = +new Date()
		this.updatedAt = +new Date()
	}

	@BeforeUpdate()
	update() {
		this.updatedAt = +new Date()
	}
}
github EndyKaufman / nest-permissions-seed / src / libs / core / entities / permission.entity.ts View on Github external
inverseJoinColumn: {
            name: 'group_id',
            referencedColumnName: 'id'
        }
    })
    groups: Group[];

    @BeforeInsert()
    doBeforeInsertion() {
        const errors = validateSync(this, { validationError: { target: false } });
        if (errors.length > 0) {
            throw new CustomValidationError(errors)
        }
    }

    @BeforeUpdate()
    doBeforeUpdate() {
        const errors = validateSync(this, { validationError: { target: false } });
        if (errors.length > 0) {
            throw new CustomValidationError(errors)
        }
    }
}
github rucken / core-nestjs / libs / rucken / auth-nestjs / src / entities / oauth-tokens-accesstoken.entity.ts View on Github external
scope: string = undefined;

  @ManyToOne(type => User, { eager: true })
  @IsNotEmpty()
  @JoinColumn({ name: 'user_id' })
  user: User = undefined;

  @BeforeInsert()
  doBeforeInsertion() {
    const errors = validateSync(this, { validationError: { target: false } });
    if (errors.length > 0) {
      throw new CustomValidationError(errors);
    }
  }

  @BeforeUpdate()
  doBeforeUpdate() {
    const errors = validateSync(this, { validationError: { target: false } });
    if (errors.length > 0) {
      throw new CustomValidationError(errors);
    }
  }
}
github hackoregon / openelections / api / models / entity / Contribution.ts View on Github external
@IsDefined()
    status: ContributionStatus;

    @ManyToOne(type => Government, government => government.contributions)
    government: Government;

    @ManyToOne(type => Campaign, campaign => campaign.contributions)
    campaign: Campaign;

    @OneToMany(type => Activity, activity => activity.contribution)
    activities: Activity[];

    public errors: ValidationError[] = [];

    @BeforeInsert()
    @BeforeUpdate()
    async validate() {
        await this.validateAsync();
        if (this.errors.length > 0) {
            throw new Error('contribution has one or more validation problems');
        }
    }

    async isValidAsync(): Promise {
        await this.validateAsync();
        return this.errors.length === 0;
    }

    async validateAsync(): Promise {
        const errors = await validate(this);
        this.errors = errors;
        await this.validateCampaignAsync();
github wix / quix / quix-frontend / service / src / entities / dbnotebook.entity.ts View on Github external
@Column({...dbConf.json, name: 'json_content'})
  jsonContent: any = {};

  isLiked!: boolean;

  @OneToMany(type => DbNote, n => n.notebook, {onDelete: 'CASCADE'})
  notes!: INote[];

  @OneToOne(type => DbFileTreeNode, node => node.notebook, {
    onDelete: 'CASCADE',
  })
  fileNode!: DbFileTreeNode;

  path: IFilePathItem[] = [];

  @BeforeUpdate()
  @BeforeInsert()
  updateContentOnSave() {
    this.jsonContent = this.jsonContent || {};
  }

  constructor(base?: INotebook) {
    if (base) {
      const {id, dateCreated, dateUpdated, name, notes, owner, path} = base;
      this.id = id;
      this.dateCreated = dateCreated;
      this.dateUpdated = dateUpdated;
      this.name = name;
      this.notes = notes;
      this.owner = owner;
      this.path = path;
    }
github lonelyhentai / minellius / server / src / role / entities / group.entity.ts View on Github external
inverseJoinColumn: {
      name: 'user_id',
      referencedColumnName: 'id'
    }
  })
  users: User[];

  @BeforeInsert()
  doBeforeInsertion() {
    const errors = validateSync(this, { validationError: { target: false } });
    if (errors.length > 0) {
      throw new CustomValidationError(errors);
    }
  }

  @BeforeUpdate()
  doBeforeUpdate() {
    const errors = validateSync(this, { validationError: { target: false } });
    if (errors.length > 0) {
      throw new CustomValidationError(errors);
    }
  }
}
github lonelyhentai / minellius / server / src / role / entities / content-type.entity.ts View on Github external
@IsNotEmpty()
  @MaxLength(255)
  title: string = undefined;

  @OneToMany(type => Permission, permission => permission.contentType)
  permissions: Permission[];

  @BeforeInsert()
  doBeforeInsertion() {
    const errors = validateSync(this, { validationError: { target: false } });
    if (errors.length > 0) {
      throw new CustomValidationError(errors);
    }
  }

  @BeforeUpdate()
  doBeforeUpdate() {
    const errors = validateSync(this, { validationError: { target: false } });
    if (errors.length > 0) {
      throw new CustomValidationError(errors);
    }
  }
}
github wix / quix / quix-frontend / service / src / entities / dbnote.entity.ts View on Github external
@Column()
  notebookId!: string;

  @Column({type: 'integer'})
  rank!: number;

  content: any;

  @AfterLoad()
  updateContentOnLoad() {
    this.content = this.textContent;
  }

  @BeforeInsert()
  @BeforeUpdate()
  updateContent() {
    this.jsonContent = this.jsonContent || {};
    this.textContent = this.content;
  }

  constructor(base?: INote) {
    if (base) {
      const {
        id,
        dateCreated,
        dateUpdated,
        name,
        owner,
        content,
        notebookId,
        type,