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](docs.couchbase.com/server/current/n1ql/n1ql-intro/index.html).
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/couchbase-orm/n1ql.rb', line 126 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](docs.couchbase.com/server/current/n1ql/n1ql-intro/index.html).
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 |
# 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 opts[:include_docs] = true current_query.results(&result_modifier) elsif opts[:include_docs] current_query.results { |res| find(res) } else current_query.results end end end |