Skip to content
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

STEP 2 Activemodel7 couchbase ruby client #40

Merged
merged 48 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
20991b9
add query_fn
CedricCouton Jul 18, 2022
8716f50
can have custom_order
CedricCouton Jul 18, 2022
1c8cae6
don't remove nil values but add them in request
DamienVoreiter Jul 19, 2022
19523c7
oupsi move =
DamienVoreiter Jul 19, 2022
9c7cc56
try to add is missing
DamienVoreiter Jul 19, 2022
4bfd2dd
re add select (wanted ?) to fix test
DamienVoreiter Jul 20, 2022
30534a7
try to remove values empty but keep nil values
DamienVoreiter Jul 20, 2022
8d885da
have a read_fn
CedricCouton Jul 22, 2022
808d91e
Merge branch 'activemodel7-couchbase-ruby-client' of github.com:docto…
CedricCouton Jul 22, 2022
87c8331
add example with include_docs false
CedricCouton Jul 25, 2022
9806edf
add column_names & attribute_names to orm
DamienVoreiter Jul 25, 2022
04cfdea
add options in params of query_fn
CedricCouton Jul 25, 2022
373c022
Merge branch 'activemodel7-couchbase-ruby-client' of github.com:docto…
CedricCouton Jul 25, 2022
6914cb9
clean
CedricCouton Jul 28, 2022
99a2ca8
fix n1ql
CedricCouton Jul 28, 2022
203f469
fix n1ql
CedricCouton Jul 28, 2022
3bbd3e6
fix n1ql
CedricCouton Jul 28, 2022
6337bd1
clean
CedricCouton Jul 28, 2022
95227c1
fix test
CedricCouton Jul 28, 2022
08a139a
fix test
CedricCouton Jul 28, 2022
db72a7d
fix test
CedricCouton Jul 28, 2022
b2e1113
try fix ci
CedricCouton Jul 28, 2022
fb59dae
try fix ci
CedricCouton Jul 28, 2022
a0d184c
Merge branch 'Mapotempo:master' into activemodel7-couchbase-ruby-client
DamienVoreiter Sep 8, 2022
d480a6b
update .gitignore to ignore .rbenv-vars
simkim Sep 9, 2022
f5a05ed
Merge remote-tracking branch 'origin/master' into activemodel7-couchb…
DamienVoreiter Sep 12, 2022
3aa996b
Update lib/couchbase-orm/connection.rb
DamienVoreiter Sep 13, 2022
093ac90
Encryption for CBLite3
xdm67x Aug 29, 2022
84d5785
Rename fn
adrien-jeser-doctolib Sep 12, 2022
7595d2c
try fix ci starts_with -> start_with
DamienVoreiter Sep 14, 2022
8a3ad53
Merge pull request #6 from doctolib/new_tanker_encryption_cblite3
DamienVoreiter Sep 14, 2022
4166029
Merge remote-tracking branch 'mapotempo/master' into activemodel7-cou…
DamienVoreiter Sep 15, 2022
ab479ab
remove select
DamienVoreiter Sep 15, 2022
bdcd207
revert useless
DamienVoreiter Sep 15, 2022
66e3289
Merge remote-tracking branch 'mapotempo/master'
DamienVoreiter Sep 15, 2022
3f9c24e
Update .gitignore
DamienVoreiter Sep 16, 2022
b5bc324
Merge branch 'master' into activemodel7-couchbase-ruby-client
DamienVoreiter Sep 16, 2022
67f81fb
revert a modify test
DamienVoreiter Sep 16, 2022
77a1280
Merge branch 'activemodel7-couchbase-ruby-client' of github.com:docto…
DamienVoreiter Sep 16, 2022
cbabc75
2 to 4 spaces
DamienVoreiter Sep 16, 2022
d876005
split tests
DamienVoreiter Sep 16, 2022
3905c57
remove useless code
DamienVoreiter Sep 16, 2022
39567d8
fix master
DamienVoreiter Sep 19, 2022
2907b63
Merge branch 'Mapotempo:master' into master
DamienVoreiter Sep 19, 2022
bbaafdb
Merge remote-tracking branch 'origin/master' into activemodel7-couchb…
DamienVoreiter Sep 19, 2022
3e41628
oups revert encrypt part
DamienVoreiter Sep 19, 2022
149dc68
fix some tests
DamienVoreiter Sep 19, 2022
43862c6
fix all tests
DamienVoreiter Sep 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/run_couchbase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ sleep 5
sleep 1
/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username $USER --rbac-password $PASSWORD --rbac-name "Auto Tester" --roles admin --auth-domain local
curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX \`default_type\` ON \`$BUCKET\`(\`type\`)"
curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX \`default_rating\` ON \`$BUCKET\`(\`rating\`)"
curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX \`default_name\` ON \`$BUCKET\`(\`name\`)"
13 changes: 7 additions & 6 deletions lib/couchbase-orm/n1ql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module ClassMethods
# # ...
# end
# TODO: add range keys [:startkey, :endkey]
def n1ql(name, query_fn: nil, emit_key: [], **options)
def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options)
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)
Expand All @@ -50,7 +50,8 @@ def n1ql(name, query_fn: nil, emit_key: [], **options)
singleton_class.__send__(:define_method, name) do |**opts, &result_modifier|
opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus)
values = convert_values(method_opts[:emit_key], opts.delete(:key)) if opts[:key]
current_query = run_query(method_opts[:emit_key], values, query_fn, **opts.except(:include_docs, :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
Expand Down Expand Up @@ -81,7 +82,7 @@ def index_n1ql(attr, validate: true, find_method: nil, n1ql_method: nil)

def convert_values(keys, values)
raise ArgumentError, "Empty keys but values are present, can't type cast" if keys.empty? && Array.wrap(values).any?
keys.zip(Array.wrap(values)).map do |key, value_before_type_cast|
keys.zip(Array.wrap(values)).map do |key, value_before_type_cast|
# cast value to type
value = if value_before_type_cast.is_a?(Array)
value_before_type_cast.map do |v|
Expand Down Expand Up @@ -121,13 +122,13 @@ def build_limit(limit)
limit ? "limit #{limit}" : ""
end

def run_query(keys, values, query_fn, descending: false, limit: nil, **options)
def run_query(keys, values, query_fn, custom_order: nil, descending: false, limit: nil, **options)
if query_fn
N1qlProxy.new(query_fn.call(bucket, values, Couchbase::Options::Query.new(**options)))
N1qlProxy.new(query_fn.call(bucket, values, cluster, Couchbase::Options::Query.new(**options)))
else
bucket_name = bucket.name
where = build_where(keys, values)
order = build_order(keys, descending)
order = custom_order || build_order(keys, descending)
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))
Expand Down
1 change: 0 additions & 1 deletion lib/couchbase-orm/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
module Rails #:nodoc:
module Couchbase #:nodoc:
class Railtie < Rails::Railtie #:nodoc:

config.couchbase_orm = ActiveSupport::OrderedOptions.new
config.couchbase_orm.ensure_design_documents = true

Expand Down
13 changes: 6 additions & 7 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require File.expand_path("../support", __FILE__)


class BaseTest < CouchbaseOrm::Base
attribute :name, :string
attribute :job, :string
Expand All @@ -18,7 +17,7 @@ class TimestampTest < CouchbaseOrm::Base

describe CouchbaseOrm::Base do
it "should be comparable to other objects" do
base = BaseTest.create!(name: 'joe')
base = BaseTest.create!(name: 'joe')
base2 = BaseTest.create!(name: 'joe')
base3 = BaseTest.create!(ActiveSupport::HashWithIndifferentAccess.new(name: 'joe'))

Expand Down Expand Up @@ -48,7 +47,7 @@ class TimestampTest < CouchbaseOrm::Base
base_loaded = BaseTest.new(resp, id: base.id)

expect(base_loaded.id).to eq(base.id)
expect(base_loaded).to eq(base)
expect(base_loaded).to eq(base)
expect(base_loaded).not_to be(base)

base.destroy
Expand All @@ -66,8 +65,8 @@ class TimestampTest < CouchbaseOrm::Base
base = BaseTest.create!(name: 'joe')

base_id = base.id
expect(base.to_json).to eq({id: base_id, name: 'joe', job: nil}.to_json)
expect(base.to_json(only: :name)).to eq({name: 'joe'}.to_json)
expect(base.to_json).to eq({ id: base_id, name: 'joe', job: nil }.to_json)
expect(base.to_json(only: :name)).to eq({ name: 'joe' }.to_json)

base.destroy
end
Expand All @@ -87,19 +86,19 @@ class TimestampTest < CouchbaseOrm::Base
expect(base.changes.empty?).to be(false)

# Attributes are set by initializer from hash
base = BaseTest.new({name: 'bob'})
base = BaseTest.new({ name: 'bob' })
expect(base.changes.empty?).to be(false)
expect(base.previous_changes.empty?).to be(true)

# A saved model should have no changes
base = BaseTest.create!(name: 'joe')
expect(base.changes.empty?).to be(true)
expect(base.previous_changes.empty?).to be(false)

# Attributes are copied from the existing model
base = BaseTest.new(base)
expect(base.changes.empty?).to be(false)
expect(base.previous_changes.empty?).to be(true)

ensure
base.destroy if base.id
end
Expand Down
49 changes: 48 additions & 1 deletion spec/n1ql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

class N1QLTest < CouchbaseOrm::Base
attribute :name, type: String
attribute :lastname, type: String
enum rating: [:awesome, :good, :okay, :bad], default: :okay

n1ql :all
n1ql :by_name, emit_key: :name
n1ql :by_custom_rating, emit_key: [:name, :rating], query_fn: proc { |bucket, _values, cluster|
cluster.query("SELECT raw meta().id FROM `#{bucket.name}` WHERE rating IN [1, 2] ORDER BY name ASC")
}
n1ql :by_name, emit_key: [:name, :rating]
n1ql :by_lastname, emit_key: [:lastname]
n1ql :by_rating, emit_key: :rating
n1ql :by_custom_rating, query_fn: proc { |bucket, _values, options|
cluster.query("SELECT raw meta().id FROM `#{bucket.name}` where type = 'n1_ql_test' AND rating IN [1,2] ORDER BY name ASC", options)
}
n1ql :by_custom_rating_values, emit_key: [:rating], query_fn: proc { |bucket, values, options|
cluster.query("SELECT raw meta().id FROM `#{bucket.name}` where type = 'n1_ql_test' AND rating IN #{values[0]} ORDER BY name ASC", options)
}
n1ql :by_rating_reverse, emit_key: :rating, custom_order: "name DESC"
n1ql :by_rating_without_docs, emit_key: :rating, include_docs: false

# This generates both:
# view :by_rating, emit_key: :rating # same as above
Expand Down Expand Up @@ -62,6 +69,46 @@ class N1QLTest < CouchbaseOrm::Base
}

expect(Set.new(docs)).to eq(Set.new(%w[bob jane]))

DamienVoreiter marked this conversation as resolved.
Show resolved Hide resolved
docs = N1QLTest.by_custom_rating().collect { |ob|
ob.name
}

expect(Set.new(docs)).to eq(Set.new(%w[bob jane mel]))
end

it "should return matching results with reverse order" do
N1QLTest.create! name: :bob, rating: :awesome
N1QLTest.create! name: :jane, rating: :awesome
N1QLTest.create! name: :greg, rating: :bad
N1QLTest.create! name: :mel, rating: :good

docs = N1QLTest.by_rating_reverse(key: 1).collect { |ob|
ob.name
}

expect(docs).to eq(%w[jane bob])
end

it "should return matching results without full documents" do
inst_bob = N1QLTest.create! name: :bob, rating: :awesome
inst_jane = N1QLTest.create! name: :jane, rating: :awesome
N1QLTest.create! name: :greg, rating: :bad
N1QLTest.create! name: :mel, rating: :good

docs = N1QLTest.by_rating_without_docs(key: 1)

expect(Set.new(docs)).to eq(Set.new([inst_bob.id, inst_jane.id]))
end

it "should return matching results with nil usage" do
N1QLTest.create! name: :bob, lastname: nil
N1QLTest.create! name: :jane, lastname: "dupond"

docs = N1QLTest.by_lastname(key: [nil]).collect { |ob|
ob.name
}
expect(docs).to eq(%w[bob])
end

it "should return matching results with custom n1ql query" do
Expand Down