Module: EncryptedJsonb::QueryHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/encrypted_jsonb/query_helpers.rb

Instance Method Summary collapse

Instance Method Details

#where_encrypted_jsonb_array_contains(column, path, value) ⇒ ActiveRecord::Relation



57
58
59
60
61
62
63
# File 'lib/encrypted_jsonb/query_helpers.rb', line 57

def where_encrypted_jsonb_array_contains(column, path, value)
  path = Array(path).join(",")
  path_array = "{#{path}}"
  encrypted_value = @encryptor.encrypt_for_query(value)

  where("? = ANY (#{quote_column(column)} #>> ? ::text[])", encrypted_value, path_array)
end

#where_encrypted_jsonb_contains(column, query_hash) ⇒ ActiveRecord::Relation



24
25
26
27
28
29
# File 'lib/encrypted_jsonb/query_helpers.rb', line 24

def where_encrypted_jsonb_contains(column, query_hash)
  encrypted_hash = deep_transform_values_for_query(query_hash)
  encrypted_json = { "message" => encrypted_hash }.to_json

  where("#{quote_column(column)} @> ?", encrypted_json)
end

#where_encrypted_jsonb_equals(column, path, value) ⇒ ActiveRecord::Relation



13
14
15
16
17
18
19
# File 'lib/encrypted_jsonb/query_helpers.rb', line 13

def where_encrypted_jsonb_equals(column, path, value)
  path = Array(path).join(",")
  path_array = "{#{path}}"
  encrypted_value = @encryptor.encrypt_for_query(value)

  where("#{quote_column(column)} #>> ? = ?", path_array, encrypted_value)
end

#where_encrypted_jsonb_exists(column, path) ⇒ ActiveRecord::Relation



34
35
36
37
38
39
# File 'lib/encrypted_jsonb/query_helpers.rb', line 34

def where_encrypted_jsonb_exists(column, path)
  path = Array(path).join(",")
  path_array = "{#{path}}"

  where("#{quote_column(column)} #> ? IS NOT NULL", path_array)
end

#where_encrypted_jsonb_in(column, path, values) ⇒ ActiveRecord::Relation



45
46
47
48
49
50
51
# File 'lib/encrypted_jsonb/query_helpers.rb', line 45

def where_encrypted_jsonb_in(column, path, values)
  path = Array(path).join(",")
  path_array = "{#{path}}"
  encrypted_values = values.map { |value| @encryptor.encrypt_for_query(value) }

  where("#{quote_column(column)} #>> ? IN (?)", path_array, encrypted_values)
end