Module: MongoClient

Defined in:
lib/schemaless_rest_api/mongo_client.rb

Overview

Client to talk to Mongo database

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

Returns Client to work with MongoDb.

Returns:

  • Client to work with MongoDb



11
12
13
# File 'lib/schemaless_rest_api/mongo_client.rb', line 11

def client
  @client
end

Class Method Details

.delete(model, id) ⇒ Object



80
81
82
83
# File 'lib/schemaless_rest_api/mongo_client.rb', line 80

def delete(model, id)
  collection = MongoClient.client[model]
  collection.delete_one({ id: id })
end

.find(model, id) ⇒ Object



18
19
20
# File 'lib/schemaless_rest_api/mongo_client.rb', line 18

def find(model, id)
  find_all(model, { id: id }, false)[:items]
end

.find_all(model, params, paginate: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/schemaless_rest_api/mongo_client.rb', line 41

def find_all(model, params, paginate: true)
  collection = MongoClient.client[model]
  pagination = page_data(model, params, collection)
  skip = (pagination[:current_page] - 1) * pagination[:page_size]
  query = query_from_params params

  items = get_items collection, query, skip, pagination[:page_size]
  results = { _embedded: {} }
  results[:_embedded][model.to_sym] = items
  results[:pagination] = pagination if paginate
  results
end

.get_items(collection, query, skip, page_size) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/schemaless_rest_api/mongo_client.rb', line 54

def get_items(collection, query, skip, page_size)
  if query == {}
    return collection.find.skip(skip).limit(page_size).collect do |match|
      match.delete("_id")
      match
    end
  end

  collection.find(query).skip(skip).limit(page_size).collect do |match|
    match.delete("_id")
    match
  end
end

.insert(model, data, id) ⇒ Object



13
14
15
16
# File 'lib/schemaless_rest_api/mongo_client.rb', line 13

def insert(model, data, id)
  collection = MongoClient.client[model]
  collection.insert_one({ id: id, **data })
end

.page_data(model, params, collection) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/schemaless_rest_api/mongo_client.rb', line 22

def page_data(model, params, collection)
  page = params[:page].to_i.positive? ? params[:page].to_i : 1
  page_size = params[:page_size].to_i.positive? ? params[:page_size].to_i : 10
  total_count = collection.count_documents({})
  total_pages = (total_count / page_size.to_f).ceil
  page_res = {
    current_page: page,
    page_size: page_size,
    total_count: total_count,
    total_pages: total_pages,
    _links: {
      self: { href: "/#{model}?page=#{page}&page_size=#{page_size}" }
    }
  }
  page_res[:_links][:next] = { href: "/#{model}?page=#{page + 1}&page_size=#{page_size}" } if page < total_pages
  page_res[:_links][:prev] = { href: "/#{model}?page=#{page - 1}&page_size=#{page_size}" } if page > 1
  page_res
end

.query_from_params(params) ⇒ Object



68
69
70
71
72
73
# File 'lib/schemaless_rest_api/mongo_client.rb', line 68

def query_from_params(params)
  query = params.dup
  query.delete(:page)
  query.delete(:page_size)
  query
end

.update(model, id, data) ⇒ Object



75
76
77
78
# File 'lib/schemaless_rest_api/mongo_client.rb', line 75

def update(model, id, data)
  collection = MongoClient.client[model]
  collection.update_one({ id: id }, { id: id, **data })
end