how to append item to an array value with Sequelize + PostgreSQL
append, arrays, postgresql, promise, sequelize.js
Solution
Array Datatype has many methods like append, concat etc. which you can see here. I will give an example with `array_append` function.
`Room` is a sequelize model and `job_ids` is a column in this model with datatype as Array of integer. `sequelize` is an instant of Sequelize class.
Room.update(
{'job_ids': sequelize.fn('array_append', sequelize.col('job_ids'), new_jobId)},
{'where': {'id': roomId}}
);
Assuming default value of this column is an empty array else it may throw an error.
Problem
I have a posgresql database, table has a column which is an array: `Sequelize.ARRAY(Sequelize.BIGINT)`. What is the right way to append a new item to the array? I am new to posgresql, sequelize and nodejs. May be it is a trivial question. From reading around I think I know how to use Promise.all to read all rows, append, and update back. The question, isn't there any useful shortcut. PostreSQL documentation mentions a function `array_append(anyarray, anyelement)`. Sequelize documentation offers a function fn, which `Creates an object representing a database function`, but only seems to be working in `where and order parts` Any way to combine those for an append-like update?