Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM - appending new items to array-type column (Postgres)

Postgres supports array type columns, and exposes methods for working with those arrays (like array_append). I'm wondering if TypeORM allows using those methods somehow.

In case it's not supported, what do you think the best way to append items to PG array? Doing something like get-and-set? (a transaction of getting the value, creating the new new array with js, and updating the value in the DB)

like image 441
Shay Yzhakov Avatar asked Feb 22 '26 06:02

Shay Yzhakov


1 Answers

You can use native Postgres methods like array_append if you use the TypeORM Query Builder.

Example below:

await dataSource.createQueryBuilder()
  .update(User)
  .set({
    yourArrayColumn: () => `array_append("yourArrayColumn", 1)`
  })
  .where("id = :id", { id: 1 })
  .execute();

More about using TypeORM Query Builder for updates: https://typeorm.io/update-query-builder

like image 192
Bob Singor Avatar answered Feb 24 '26 19:02

Bob Singor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!