dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

proposal: separate Object type into JSON and Buffer types #381

Open kapouer opened 10 years ago

kapouer commented 10 years ago

There seems to be a confusion in the definition of types: Object represents both JSON or Buffer. They are really different: JSON can be stringified, or in some cases is even a supported database type (postgres 9.3 and of course mongo) while Buffer calls for a binary representation. I suggest orm provides two distinct types, allowing for better usage of existing types in underlying databases. It also improves readability of data in the database itself.

dresende commented 10 years ago

There is a binary type, you can do this:

var Person = db.define("person", {
    photo: Buffer
});
kapouer commented 10 years ago

Okay, Buffer is for binary. But then i don't understand why object is handled the same as binary.

kapouer commented 10 years ago

Also, "Object" cannot always be serialized. "JSON" might be a better name.

dresende commented 10 years ago

It was initially called "object" because you could use Object to define it (instead of "type": "object") and because actually JSON is a simplified javascript object. :)

They are usually handled the same way (when storing them) because databases don't support it, but as you said mongo does. As for postgresql, if you know a way of detecting if the server has native support we could change it. An alternative is using a custom type (see test/integration/property-custom.js for an example).

kapouer commented 10 years ago

At least it should be impossible to put a Buffer instance inside a column accepting objects; so it will eventually be possible to make the distinction between json and binary blobs.

Note that {mycol: JSON} would have worked instead of {mycol: Object}. That's less important ;)