Skip to content

Dynamic key for object in ReQL #272

Closed

Description

@neumino

The only way to create an object or to add a pair (key, value) in an object is to use

r.expr{{ key: "value" })

or

r.expr({}).merge({key: "value" })

In every case, we can not use a dynamic key, something like

r.expr({ r.expr("key"): "value" })

For the JavaScript driver, the problem lies in JavaScript. { r.expr("key"): "value"} is going to throw an error, and doing

var obj = {}
obj[r.expr("key")] = "value"

will create this object

{
  "[object Object]": "value"
}

We will need to add a new method in ReQL that takes pairs of (key, value) and creates an object. Something like that

r.object(["key1", "value1"], ["key2", "value2"] )

The use case is the following one.
I have a table "posts", "tags", "links_posts"tags" and I want to retrieve all the posts with an attribute tags that is an object that look likes that

tags: {
  tag1: true,
  tag2: true,
  tag3: true
}

So I can display all the post and let the users quickly filter them by tags (without having to fire a new request/query) or to go through an array of tags. I would just have to check if post.tags[tag_selected_by_user] is defined.
I think it's something pretty useful for web developers.

Activity

  1. coffeemug commented on Feb 1, 2013

    @coffeemug
    Contributor

    Some time ago we made a conscious decision not to put effort into supporting these types of use cases (dynamic attribute shenanigans) because it complicates the language, has dubious benefits, and potentially significantly complicates performance analysis later on. In this case, instead of an object tags: { tag1: true, tag2: true, ...}, it would be much much better to structure it like this: tags: ['tag1', 'tag2', ...]. You can then query arrays using array operations (which we're working on).

    Closing, due to lack of compellingness.

    Shakespearean tl;dr:

    Compelling use case here I do not see,
    I therefore this bug will promptly close,
    Dynamic attributes in general are,
    Abusive and uncalled for I should think.

  2. mlucy commented on Feb 1, 2013

    @mlucy
    Member

    Version 1.4 supports coercions between arrays and objects. So the following should work:

    r([[r("attr") + "1", r("val") + "1"], ["attr2", "val2"]]).coerce("object").run
      => {"attr1"=>"val1", "attr2"=>"val2"}
  3. neumino commented on Feb 1, 2013

    @neumino
    MemberAuthor

    @coffeemug in the use case I mentioned, I want to filter on the client side (instead of firing a second query, so it has nothing to do with the array operations).
    I want to filter on the client to keep the app fast and responsive.

    @mlucy's solution works for me, even if it's not really nice (not sure if the proposedr.obj() is better though).

    Reopening because I think @coffeemug didn't really understand the use case, and because I would find really irritating not to be able to do it (more than not being able to do operations on arrays/string)

  4. reopened this on Feb 1, 2013
  5. coffeemug commented on Feb 1, 2013

    @coffeemug
    Contributor

    Oh, sorry, I see what you mean. I'd be cool with putting sugar on coerce for this. Moving to post-protobuf.

  6. coffeemug commented on Feb 14, 2013

    @coffeemug
    Contributor

    BTW, it would be nice to have sugar over reduce(merge). Basically a variadic r.merge that also accepts arrays of objects.

  7. neumino commented on Jul 10, 2013

    @neumino
    MemberAuthor

    I just bumped into this.

    Use case: Transform the results of a join.

    I want to do a join, and do something like

    resultJoin.map( function(result) {
        return result("left").merge({
            fieldName: result("right")
        }).default( result("left") )
    })
    

    The thing is I want the users to be able to use whatever name for fieldName (so it's a variable). The only way now to do it is to coerce from array to object.

    While it works, it makes things quite ugly.

  8. coffeemug commented on Jul 10, 2013

    @coffeemug
    Contributor

    The original proposal to fix this issue was r.object(["key1", "value1"], ["key2", "value2"] ). Is this much better than r.expr(["key1", "value1"], ["key2", "value2"]).coerceTo('object')? Considering that the syntaxes are nearly identical, I don't think this is very important right now (unless there is a better proposal for this).

  9. mlucy commented on Jul 10, 2013

    @mlucy
    Member

    We could always do r.obj('key1', 'val1', 'key2', 'val2').

  10. neumino commented on Jul 10, 2013

    @neumino
    MemberAuthor

    I would be cool with

    obj.set( key, value).set( key2, value2)
    
  11. mlucy commented on Jul 10, 2013

    @mlucy
    Member

    set has destructive connotations to me. How about add_field or with_field?

  12. neumino commented on Jul 10, 2013

    @neumino
    MemberAuthor

    Yep, add_field is fine too

    with_field is a little too close to has_field from my point of view.

  13. coffeemug commented on Jul 10, 2013

    @coffeemug
    Contributor

    Ok, I'll move this to optional so it doesn't get lost in the woods.

  14. coffeemug commented on Jan 14, 2014

    @coffeemug
    Contributor

    Closing in favor of #1856.

  15. modified the milestones: duplicate, backlog on Feb 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions