Module: CouchbaseOrm::N1ql::ClassMethods
- Defined in:
- lib/couchbase-orm/n1ql.rb
Constant Summary collapse
- N1QL_DEFAULTS =
{ include_docs: true }.freeze
Instance Method Summary collapse
-
#index_n1ql(attr, validate: true, find_method: nil, n1ql_method: nil) ⇒ void
Sets up a Couchbase N1QL query and a corresponding finder method for the given attribute.
-
#n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options) ⇒ void
Defines a N1QL query method with dynamic method creation.
Instance Method Details
#index_n1ql(attr, validate: true, find_method: nil, n1ql_method: nil) ⇒ void
This method returns an undefined value.
Sets up a Couchbase N1QL query and a corresponding finder method for the given attribute.
N1QL (Non-first Normal Form Query Language) is a powerful query language for Couchbase that allows you to perform complex queries on your data. For more details, see the Couchbase N1QL Documentation.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/couchbase-orm/n1ql.rb', line 131 def index_n1ql(attr, validate: true, find_method: nil, n1ql_method: nil) n1ql_method ||= "by_#{attr}" find_method ||= "find_#{n1ql_method}" validates(attr, presence: true) if validate n1ql n1ql_method, emit_key: attr define_singleton_method find_method do |value| send n1ql_method, key: [value] end end |
#n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options) ⇒ void
This method returns an undefined value.
Defines a N1QL query method with dynamic method creation.
N1QL (Non-first Normal Form Query Language) is a powerful query language for Couchbase that allows you to perform complex queries on your data. For more details, see the Couchbase N1QL Documentation.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/couchbase-orm/n1ql.rb', line 63 def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **) raise ArgumentError.new("#{self} already respond_to? #{name}") if self.respond_to?(name) emit_key = Array.wrap(emit_key) emit_key.each do |key| raise "unknown emit_key attribute for n1ql :#{name}, emit_key: :#{key}" if key && !attribute_names.include?(key.to_s) end = N1QL_DEFAULTS.merge() method_opts = {} method_opts[:emit_key] = emit_key @indexes ||= {} @indexes[name] = method_opts singleton_class.__send__(:define_method, name) do |key: NO_VALUE, **opts, &result_modifier| opts = .merge(opts).reverse_merge(scan_consistency: :request_plus) values = key == NO_VALUE ? NO_VALUE : convert_values(method_opts[:emit_key], key) current_query = run_query(method_opts[:emit_key], values, query_fn, custom_order: custom_order, **opts.except(:include_docs, :key)) if result_modifier current_query.results(&result_modifier) elsif opts[:include_docs] results = current_query.results.to_a results = if results.empty? results else find(results, **opts.slice(:quiet, :chunck)) end ResultsProxy.new(Array.wrap(results)) else current_query.results end end end |