1 | initial version |
To modify the value of a pre-existing PostgreSQL column created using Type-ORM Nest JS, you can follow these steps:
Here's an example code snippet that illustrates how you can modify the value of a pre-existing PostgreSQL column using Type-ORM:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ name: 'my_table' })
export class MyEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', length: 50, nullable: false })
my_column: string;
}
async function modifyColumnValue() {
// create a query builder instance
const qb = getRepository(MyEntity).createQueryBuilder('my_table');
// retrieve the entity with id = 1
const entity = await qb.where('id = :id', { id: 1 }).getOne();
// modify the value of my_column
entity.my_column = 'modified value';
// create an EntityManager and save the changes
const entityManager = getManager();
await entityManager.save(entity);
}
In this code snippet, we have defined a new entity class MyEntity
using the Type-ORM decorators. We then use the createQueryBuilder
method to retrieve the data from the table and modify the value of the my_column
column. Finally, we use the entityManager
to save the changes to the database.