Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with new model instance and JSON encoded values #168

Closed
Tape opened this issue May 20, 2013 · 5 comments
Closed

Issue with new model instance and JSON encoded values #168

Tape opened this issue May 20, 2013 · 5 comments

Comments

@Tape
Copy link
Contributor

Tape commented May 20, 2013

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.

@Tape
Copy link
Contributor Author

Tape commented May 20, 2013

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.

@dresende
Copy link
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.

@dresende
Copy link
Owner

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

@dresende
Copy link
Owner

Please test this last commit :)

@dresende
Copy link
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
Projects
None yet
Development

No branches or pull requests

2 participants