Skip to content

Issue with new model instance and JSON encoded values #168

Closed

Description

@Tape

Suppose I have a model like this:

var GraphNode = db.define("GraphNode", {
    node_id: Number,
    working_dir: String,
    config: Object
});

Upon creation unless the created object's field contains a JSON string it will be null. It will always be a string from the database, but in a situation where I am creating the object it does not always make sense. I think that both cases below should work.

// Currently not possible, config will be null
new GraphNode({
    node_id: 0
    working_dir: "/some/directory",
    config: {
        a_field: "some value"
    }
});
// As opposed to
new GraphNode({
    node_id: 0
    working_dir: "/some/directory",
    config: '{"a_field": "some value"}'
});
// ...

I have a workaround for the Postgres driver at least, by editing lib/Drivers/DML/postgres.js and adding the following code right below case "object" (https://github.com/dresende/node-orm2/blob/master/lib/Drivers/DML/postgres.js#L252)

if (value === Object(value)) {
    return value;
}

It may be as simple as adding that to all of the drivers.

Activity

  1. Tape commented on May 20, 2013

    @Tape
    ContributorAuthor

    The instance is stored in object notation when it is initialized in the first place. In my case the objects I am parsing are already JSON strings that are parsed to create the model instances. It seems a bit silly to parse the JSON, re-stringify parts of the model instance, and decode them again when the instances are created. This solution provides a means by which you can supply a raw object (or array) which is still valid JSON.

  2. dresende commented on May 28, 2013

    @dresende
    Owner

    The Object property type should handle this correctly. If it's not it's a bug and it has to be fixed. I'll look at it.

  3. dresende commented on May 28, 2013

    @dresende
    Owner

    I found the bug, at least in mysql this happens too. I'm going to change in the other drivers.

  4. dresende commented on May 28, 2013

    @dresende
    Owner

    Please test this last commit :)

  5. dresende commented on May 31, 2013

    @dresende
    Owner

    This should be ok now, reopen if the problem persists.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions