You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'm trying to use the ARRAY datatype for sequelize with PostgreSQL. However I'm not exactly sure how to utilize it. I know how to set it on the schema but how do you push new items onto the array with a write and retrieve a specific index from the array in a query, etc.
I tried going to the docs to figure this out however theres not much documentation on this as opposed to other datatypes. If this comes up a lot I would be glad to add docs to it if you can explain it to me.
I don't know if this is possible without raw queries. In the end, sequelizes job is to map objects to table rows. In that respect, you can get a whole object, where one attribute will be the array. Then you can access any index you want or push items on it, then save the instance. If your arrays are very large I would recommend you put it in a relation table anyway, as it will be bad for performance (postgres will save the array externally after a certain threshold)
User.findOne().then(user=>{// get index 3console.log(user.roles[3]);// find out if admin is in rolesconsole.log(user.roles.indexOf('admin')!==-1);// remove index 3user.roles.splice(3,1);// push new item to ituser.roles.push('admin');// savereturnuser.save();})
Note:
Inline modifications of the array are not picked up by sequelize automatically - For example, even if you push a new value onto the array, it will not be updated. You need to either mark it as such user.changed('roles', true) or user.roles = user.roles
@janmeier didn't know that - why? I thought Sequelize had an internal copy of the unmodified state to dirty-check the properties? Or does it all work through setters?
@janmeier documentation on this would be vary helpful, to get this straight I'm started working with sequelize about a month ago and its great, but docs on these type of things are really important otherwise I find myself spending lots of time on SoF trying to figure things out and end up not finding anything.
@janmeier can you demonstrate how to push/pop/removeIndex from the Array Datatype in sequelize? I'm still having trouble trying to figure this out. I tried Stack Overflow and no one answered.
Activity
kennetpostigo commentedon Jul 1, 2016
This would be awesome, I've also wanted to use
ARRAYin the past but not much docs on how to use. Explaining this would be useful.felixfbecker commentedon Jul 1, 2016
I don't know if this is possible without raw queries. In the end, sequelizes job is to map objects to table rows. In that respect, you can get a whole object, where one attribute will be the array. Then you can access any index you want or push items on it, then save the instance. If your arrays are very large I would recommend you put it in a relation table anyway, as it will be bad for performance (postgres will save the array externally after a certain threshold)
kennetpostigo commentedon Jul 1, 2016
Not large 4/5 string in the array use I had in mind. @felixfbecker
felixfbecker commentedon Jul 1, 2016
Then just get the whole array
sandysaders commentedon Jul 2, 2016
The point is how to exactly, query/remove specific indexes from the array with sequelize... @felixfbecker
felixfbecker commentedon Jul 3, 2016
@jamestwiggle Either like this:
Or by using a raw query with Postgres' array functions
janmeier commentedon Jul 3, 2016
Note:
Inline modifications of the array are not picked up by sequelize automatically - For example, even if you push a new value onto the array, it will not be updated. You need to either mark it as such
user.changed('roles', true)oruser.roles = user.rolesfelixfbecker commentedon Jul 3, 2016
@janmeier didn't know that - why? I thought Sequelize had an internal copy of the unmodified state to dirty-check the properties? Or does it all work through setters?
janmeier commentedon Jul 3, 2016
It all works through setters
sandysaders commentedon Jul 3, 2016
@janmeier documentation on this would be vary helpful, to get this straight I'm started working with sequelize about a month ago and its great, but docs on these type of things are really important otherwise I find myself spending lots of time on SoF trying to figure things out and end up not finding anything.
sandysaders commentedon Jul 11, 2016
@janmeier can you demonstrate how to push/pop/removeIndex from the Array Datatype in sequelize? I'm still having trouble trying to figure this out. I tried Stack Overflow and no one answered.
felixfbecker commentedon Jul 11, 2016
@sandysaders see my previous example, just call
user.changed('roles', true)before you callsave().kennetpostigo commentedon Jul 12, 2016
@sandysaders @janmeier ,
Would this also work?
janmeier commentedon Jul 12, 2016
Yep, that should work as well
janmeier commentedon Jul 13, 2016
Maybe push doesn't return the array? I have that case you need to push first, and then pass
imageuri: user.imageurikennetpostigo commentedon Jul 13, 2016
yep, that worked.
felixfbecker commentedon Jul 13, 2016
that won't work,
push()returns the new array length, not the array itself.