eaco icon indicating copy to clipboard operation
eaco copied to clipboard

JSON Adapter for MySQL >= 5.7.8

Open haveyaseen opened this issue 9 years ago • 1 comments

Hello,

I have been trying to make Eaco use my MySQL database without success.

While rails console development sometimes opens the console, most of the time it's still giving me the error message

Don't know how to look up authorized records on <Document>'s ORM (identified as <ActiveRecord::Base>). To authorize <Document> either use one of the available strategies: pg_jsonb or please define your own Document.accessible_by method. You may at one point want to move this in a new strategy, and send a pull request :-).

So what I did was

  • first I added /usr/local/rvm/gems/ruby-2.3.3/gems/eaco-1.0.0/lib/eaco/adapters/active_record/mysql_json.rb:
module Eaco
  module Adapters
    module ActiveRecord
      module MysqlJSON

        def accessible_by(actor)
          return scoped if actor.is_admin?

          designators = actor.designators.map { |d| sanitize(d) }

          column = "#{connection.quote_table_name(table_name)}.acl"

          where("JSON_CONTAINS_PATH(#{column}, #{designators.join(',')})")
        end

      end
    end
  end
end
  • and then I changed /usr/local/rvm/gems/ruby-2.3.3/gems/eaco-1.0.0/lib/eaco/adapters/active_record.rb to contain
autoload :MysqlJSON,     'eaco/adapters/active_record/mysql_json'

def self.strategies
  {:pg_jsonb => PostgresJSONb, :mysql_json => MysqlJSON}
end

Please tell me if that code would do what it's supposed to do and maybe add it to Eaco? I'd really like to start experimenting with this library to see how flexible it is.

haveyaseen avatar Jan 30 '17 15:01 haveyaseen

@jasonslam thanks for your report. As far as I can tell from MySQL documentation, what you did is mostly correct, except for two details:

  • the designators should be prefixed with $.:
designators = actor.designators.map { |d| sanitize("$.#{d}") }
  • the WHERE should read:
where("JSON_CONTAINS_PATH(#{column}, 'one', #{designators.join(',')})")

I would very much like extending Eaco in supporting MySQL.

vjt avatar Feb 23 '17 20:02 vjt