Skip to content

Issue with new model instance and JSON encoded values #168

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

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