8

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?

1 Answer 1

20

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.