Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 58 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
PATH
remote: .
specs:
dolly (3.0.0)
oj

GEM
remote: https://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
crack (0.4.3)
safe_yaml (~> 1.0.0)
hashdiff (0.4.0)
metaclass (0.0.4)
mocha (1.9.0)
metaclass (~> 0.0.1)
oj (3.7.12)
power_assert (1.1.4)
public_suffix (3.1.0)
rake (10.5.0)
rr (1.2.1)
safe_yaml (1.0.5)
test-unit (3.3.3)
power_assert
test-unit-context (0.5.1)
test-unit (>= 2.4.0)
test-unit-full (0.0.5)
test-unit
test-unit-context
test-unit-notify
test-unit-rr
test-unit-runner-tap
test-unit-notify (1.0.4)
test-unit (>= 2.4.9)
test-unit-rr (1.0.5)
rr (>= 1.1.1)
test-unit (>= 2.5.2)
test-unit-runner-tap (1.1.2)
test-unit
webmock (3.6.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
dolly!
mocha
rake (~> 10.0)
test-unit-full
webmock

BUNDLED WITH
2.0.2
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,46 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dolly3.


## Migrating from couch 1.x to 2.x

[Official docs](https://docs.couchdb.org/en/2.3.1/install/index.html)


You will need to uninstall the couchdb service with brew

`brew services stop couchdb`
`brew services uninstall couchdb`

Download the application from the following source

http://couchdb.apache.org/#download

launch fauxton and check your installation

Copy [this file](https://github.com/apache/couchdb/blob/master/rel/overlay/bin/couchup) into your filesystem


make it executable

`chmod +x couchup.py`

and run it

`./couchup.py -h`

You might need to install python 3 and pip3 and the following libs

`pip3 install requests progressbar2`

move your .couch files into the specified `database_dir` in your [fauxton config](http://127.0.0.1:5984/_utils/#_config/couchdb@localhost)


```
$ ./couchup list # Shows your unmigrated 1.x databases
$ ./couchup replicate -a # Replicates your 1.x DBs to 2.x
$ ./couchup rebuild -a # Optional; starts rebuilding your views
$ ./couchup delete -a # Deletes your 1.x DBs (careful!)
$ ./couchup list # Should show no remaining databases!
```
2 changes: 1 addition & 1 deletion lib/dolly/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def request(method, resource, data = {})

def start_request(req)
Net::HTTP.start(req.uri.hostname, req.uri.port) do |http|
req.basic_auth env['username'], env['password'] if env['username'].present?
req.basic_auth env['username'], env['password'] if env['username']&.present?
http.request(req)
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/dolly/document.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'dolly/mango'
require 'dolly/query'
require 'dolly/connection'
require 'dolly/request'
Expand All @@ -15,11 +16,13 @@

module Dolly
class Document
extend Mango
extend Query
extend Request
extend DepracatedDatabase
extend Properties
extend DocumentCreation

include PropertyManager
include Timestamp
include DocumentState
Expand Down
10 changes: 10 additions & 0 deletions lib/dolly/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def to_s
end
end

class InvalidMangoOperatorError < RuntimeError
def initialize msg
@msg = msg
end

def to_s
"Invalid Mango operator: #{@msg.inspect}"
end
end

class InvalidConfigFileError < RuntimeError; end
class InvalidProperty < RuntimeError; end
class DocumentInvalidError < RuntimeError; end
Expand Down
91 changes: 91 additions & 0 deletions lib/dolly/mango.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

require 'refinements/hash_refinements'

module Dolly
module Mango
using HashRefinements

SELECTOR_SYMBOL = '$'

COMBINATION_OPERATORS = %I[
and
or
not
nor
all
elemMatch
allMath
].freeze

CONDITION_OPERATORS = %I[
lt
lte
eq
ne
gte
gt
exists
type
in
nin
size
mod
regex
].freeze

ALL_OPERATORS = COMBINATION_OPERATORS + CONDITION_OPERATORS

DESIGN = '_find'

def find_by(query, opts = {})
build_model_from_doc(find_doc_by(query, opts))
end

def find_doc_by(query, opts = {})
opts.merge!(limit: 1)
perform_query(build_query(query, opts))[:docs].first
Comment thread
ErickFabian marked this conversation as resolved.
end

def where(query, opts = {})
docs_where(query, opts).map do |doc|
build_model_from_doc(doc)
end
end

def docs_where(query, opts = {})
perform_query(build_query(query, opts))[:docs]
Comment thread
javierg marked this conversation as resolved.
end

private

def build_model_from_doc(doc)
return nil if doc.nil?
new(doc.slice(*all_property_keys))
end

def perform_query(structured_query)
connection.post(DESIGN, structured_query)
end

def build_query(query, opts)
{ 'selector' => build_selectors(query) }.merge(opts)
end

def build_selectors(query)
query.deep_transform_keys do |key|
next build_key(key) if is_operator?(key)
raise InvalidMangoOperatorError.new(key) unless self.all_property_keys.include?(key)
key
end
end

def build_key(key)
"#{SELECTOR_SYMBOL}#{key}"
end

def is_operator?(key)
ALL_OPERATORS.include?(key)
end
end
end
6 changes: 5 additions & 1 deletion lib/dolly/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ def properties
@properties ||= PropertySet.new
end

def all_property_keys
properties.map(&:key) + SPECIAL_KEYS
end

def property_keys
properties.map(&:key) - SPECIAL_KEYS
all_property_keys - SPECIAL_KEYS
end

def property_clean_doc(doc)
Expand Down
27 changes: 27 additions & 0 deletions lib/refinements/hash_refinements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module HashRefinements
refine Hash do
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 82
def deep_transform_keys(&block)
_deep_transform_keys_in_object(self, &block)
end

def slice(*keys)
keys.each_with_object(Hash.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
end

private

def _deep_transform_keys_in_object(object, &block)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
end
when Array
object.map { |e| _deep_transform_keys_in_object(e, &block) }
else
object
end
end
end
end
2 changes: 0 additions & 2 deletions test/document_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'test_helper'

class BaseDolly < Dolly::Document; end

class BarFoo < BaseDolly
property :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :persist
end
Expand Down
Loading