2

I want to use UUIDs as primary keys in my database and was recommended to use VARBINARY(16) as column type.

I have the following so far:

module DataMapper
  class Property
      class UUIDKey < String
        key true

        default proc { SecureRandom.uuid.delete('-') }

        writer :private

        def dump(value)
          # add '0x' to save as hex number
          # according to http://kekoav.com/posts/uuid-primary-key-mysql

          '0x' + value unless value.nil?
        end

        def load(value)
          value unless value.nil?
        end

        def typecast(value)
          # how to convert binary value in db?
          load(value)
        end
      end
  end
end

DataMapper will of course save a value as string based on above.

How can I get DataMapper to save this as binary/hex and load as string?

Parnas
  • 87
  • 5

0 Answers0