Skip to content

JSON Adapter for MySQL >= 5.7.8 #8

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

Open
haveyaseen opened this issue Jan 30, 2017 · 1 comment
Open

JSON Adapter for MySQL >= 5.7.8 #8

haveyaseen opened this issue Jan 30, 2017 · 1 comment

Comments

@haveyaseen
Copy link

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.

@pzac pzac assigned lleirborras and unassigned pzac Feb 9, 2017
@vjt
Copy link
Contributor

vjt commented Feb 23, 2017

@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 vjt self-assigned this Feb 23, 2017
@vjt vjt mentioned this issue Feb 23, 2017
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

4 participants