Thursday, June 19, 2008

CouchDB One more thing

I almost forgot. In DataMapper, it currently stores things like arrays, hashes in couchdb perfectly. It converts them into JSON and stores them, so they're query-able! The bad thing is that when it attempts to read them out (currently) it assumes that because the property types in your model are set to "Object" that it needs to first base64 decode the marshalled/encoded object and then de-serialize it.

Well, I really don't want things that could be incredibly important being fudged in my data. Especially now that I have another dimension I can query on. So I "fixed" the object data type for when I'm working with CouchDB. This will allow you to both store and read values such as arrays and/or hashes without any voodoo. Just drop this override in your init.rb or in another place where it's sure to be loaded AFTER datamapper has been initialized.

module DataMapper
module Types
class Object < DataMapper::Type
primitive String
size 65535
lazy true
track :hash

def self.dump(value, property)
value.to_json unless value.nil?

def self.load(value, property)
value.nil? ? nil : value

No comments: