Sha256: 0f6d6c907781a9b2758df4c63a11e30004a8c271a71449c365536138b458d394

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

require "mongo"

# typed: false - MongoClient new

module MongoClient
  class << self
    # @return Client to work with MongoDb
    attr_accessor :client

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

    def find(model, id)
      find_all(model, { id: id })
    end

    def get_all(model)
      collection = MongoClient.client[model]
      collection.find.collect do |match|
        match.delete("_id")
        match
      end
    end

    def find_all(model, query)
      collection = MongoClient.client[model]
      collection.find(query).collect do |match|
        match.delete("_id")
        match
      end
    end

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

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

MongoClient.client = if ENV["mongo_root_password"]
                       Mongo::Client.new(
                         ["#{ENV["mongodb"]}:27017"],
                         database: "api", password: ENV["mongo_root_password"],
                         user: "root"
                       )
                     else
                       Mongo::Client.new(
                         ["#{ENV["mongodb"]}:27017"],
                         database: "api"
                       )
                     end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
schemaless_rest_api-0.5.1 lib/schemaless_rest_api/mongo_client.rb
schemaless_rest_api-0.5.0 lib/schemaless_rest_api/mongo_client.rb
schemaless_rest_api-0.4.2 lib/schemaless_rest_api/mongo_client.rb
schemaless_rest_api-0.4.1 lib/schemaless_rest_api/mongo_client.rb
schemaless_rest_api-0.4.0 lib/schemaless_rest_api/mongo_client.rb