Skip to content
3 changes: 2 additions & 1 deletion lib/couchbase-orm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ class Document
extend Enum

define_model_callbacks :initialize, :only => :after
define_model_callbacks :create, :destroy, :save, :update

Metadata = Struct.new(:cas)

Expand Down Expand Up @@ -212,6 +211,8 @@ class Base < Document
extend Index
extend IgnoredProperties

define_model_callbacks :create, :destroy, :save, :update

class << self
def connect(**options)
@bucket = BucketProxy.new(::MTLibcouchbase::Bucket.new(**options))
Expand Down
12 changes: 10 additions & 2 deletions lib/couchbase-orm/n1ql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module CouchbaseOrm
module N1ql
extend ActiveSupport::Concern
NO_VALUE = :no_value_specified
DEFAULT_SCAN_CONSISTENCY = :request_plus
# sanitize for injection query
def self.sanitize(value)
if value.is_a?(String)
Expand All @@ -19,6 +20,13 @@ def self.sanitize(value)
end
end

def self.config(new_config = nil)
Thread.current['__couchbaseorm_n1ql_config__'] = new_config if new_config
Thread.current['__couchbaseorm_n1ql_config__'] || {
scan_consistency: DEFAULT_SCAN_CONSISTENCY
}
end

module ClassMethods
# Defines a query N1QL for the model
#
Expand Down Expand Up @@ -49,7 +57,7 @@ def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options)
@indexes[name] = method_opts

singleton_class.__send__(:define_method, name) do |key: NO_VALUE, **opts, &result_modifier|
opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus)
opts = options.merge(opts).reverse_merge(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency])
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
Expand Down Expand Up @@ -116,7 +124,7 @@ def run_query(keys, values, query_fn, custom_order: nil, descending: false, limi
limit = build_limit(limit)
n1ql_query = "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}"
result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options))
CouchbaseOrm.logger.debug { "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" }
CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows with scan_consistency : #{options[:scan_consistency]}"
N1qlProxy.new(result)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/couchbase-orm/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def to_n1ql
end

def execute(n1ql_query)
result = @model.cluster.query(n1ql_query, Couchbase::Options::Query.new(scan_consistency: :request_plus))
CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} return #{result.rows.to_a.length} rows" }
result = @model.cluster.query(n1ql_query, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]))
CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} return #{result.rows.to_a.length} rows with scan_consistency : #{CouchbaseOrm::N1ql.config[:scan_consistency]}" }
N1qlProxy.new(result)
end

Expand All @@ -52,13 +52,13 @@ def ids
end

def first
result = @model.cluster.query(self.limit(1).to_n1ql, Couchbase::Options::Query.new(scan_consistency: :request_plus))
result = @model.cluster.query(self.limit(1).to_n1ql, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]))
first_id = result.rows.to_a.first
@model.find(first_id) if first_id
end

def last
result = @model.cluster.query(to_n1ql, Couchbase::Options::Query.new(scan_consistency: :request_plus))
result = @model.cluster.query(to_n1ql, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]))
last_id = result.rows.to_a.last
@model.find(last_id) if last_id
end
Expand Down
2 changes: 1 addition & 1 deletion lib/couchbase-orm/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def view(name, map: nil, emit_key: nil, reduce: nil, **options)
@views[name] = method_opts

singleton_class.__send__(:define_method, name) do |**opts, &result_modifier|
opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus)
opts = options.merge(opts).reverse_merge(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency])
CouchbaseOrm.logger.debug("View [#{@design_document}, #{name.inspect}] options: #{opts.inspect}")
if result_modifier
include_docs(bucket.view_query(@design_document, name.to_s, Couchbase::Options::View.new(**opts.except(:include_docs)))).map(&result_modifier)
Expand Down
9 changes: 9 additions & 0 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base
it_behaves_like "ActiveModel"
end

it 'does not expose callbacks for nested that wont never be called' do
expect{
class InvalidNested < CouchbaseOrm::NestedDocument
before_save {p "this should raise on loading class"}
end

}.to raise_error NoMethodError
end

describe '.ignored_properties' do


Expand Down
20 changes: 20 additions & 0 deletions spec/n1ql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ class N1QLTest < CouchbaseOrm::Base
expect(Set.new(docs)).to eq(Set.new(%w[bob jane mel]))
end

it "should log the default scan_consistency when n1ql query is executed" do
allow(CouchbaseOrm.logger).to receive(:debug)
N1QLTest.by_rating_reverse()
expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once).with("N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=\"n1_ql_test\" order by name DESC return 0 rows with scan_consistency : #{described_class::DEFAULT_SCAN_CONSISTENCY}")
end

it "should log the set scan_consistency when n1ql query is executed with a specific scan_consistency" do
allow(CouchbaseOrm.logger).to receive(:debug)
default_n1ql_config = CouchbaseOrm::N1ql.config
CouchbaseOrm::N1ql.config({ scan_consistency: :not_bounded })
puts "CouchbaseOrm::N1ql.config"
puts CouchbaseOrm::N1ql.config
N1QLTest.by_rating_reverse()
expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once).with("N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=\"n1_ql_test\" order by name DESC return 0 rows with scan_consistency : not_bounded")

CouchbaseOrm::N1ql.config(default_n1ql_config)
N1QLTest.by_rating_reverse()
expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once).with("N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=\"n1_ql_test\" order by name DESC return 0 rows with scan_consistency : #{described_class::DEFAULT_SCAN_CONSISTENCY}")
end

after(:all) do
N1QLTest.delete_all
end
Expand Down