Skip to content

ARRAY Datatype #6197

Closed
Closed

Description

@sandysaders

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.

Activity

  1. added
    docs
    on Jul 1, 2016
  2. kennetpostigo commented on Jul 1, 2016

    @kennetpostigo

    This would be awesome, I've also wanted to use ARRAY in the past but not much docs on how to use. Explaining this would be useful.

  3. felixfbecker commented on Jul 1, 2016

    @felixfbecker
    Contributor

    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)

  4. kennetpostigo commented on Jul 1, 2016

    @kennetpostigo

    Not large 4/5 string in the array use I had in mind. @felixfbecker

  5. felixfbecker commented on Jul 1, 2016

    @felixfbecker
    Contributor

    Then just get the whole array

  6. sandysaders commented on Jul 2, 2016

    @sandysaders
    Author

    The point is how to exactly, query/remove specific indexes from the array with sequelize... @felixfbecker

  7. felixfbecker commented on Jul 3, 2016

    @felixfbecker
    Contributor

    @jamestwiggle Either like this:

    User.findOne().then(user => {
      // get index 3
      console.log(user.roles[3]);
      // find out if admin is in roles
      console.log(user.roles.indexOf('admin') !== -1);
      // remove index 3
      user.roles.splice(3, 1);
      // push new item to it
      user.roles.push('admin');
      // save
      return user.save();
    })

    Or by using a raw query with Postgres' array functions

  8. janmeier commented on Jul 3, 2016

    @janmeier
    Member

    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

  9. felixfbecker commented on Jul 3, 2016

    @felixfbecker
    Contributor

    @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?

  10. janmeier commented on Jul 3, 2016

    @janmeier
    Member

    It all works through setters

  11. sandysaders commented on Jul 3, 2016

    @sandysaders
    Author

    @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.

  12. sandysaders commented on Jul 11, 2016

    @sandysaders
    Author

    @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.

  13. felixfbecker commented on Jul 11, 2016

    @felixfbecker
    Contributor

    @sandysaders see my previous example, just call user.changed('roles', true) before you call save().

  14. kennetpostigo commented on Jul 12, 2016

    @kennetpostigo

    @sandysaders @janmeier ,
    Would this also work?

    user.update({
        imageURI: user.imageURI.push(args.userImage)
    });
  15. janmeier commented on Jul 12, 2016

    @janmeier
    Member

    Yep, that should work as well

  16. janmeier commented on Jul 13, 2016

    @janmeier
    Member

    Maybe push doesn't return the array? I have that case you need to push first, and then pass imageuri: user.imageuri

  17. kennetpostigo commented on Jul 13, 2016

    @kennetpostigo

    yep, that worked.

  18. felixfbecker commented on Jul 13, 2016

    @felixfbecker
    Contributor

    that won't work, push() returns the new array length, not the array itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions