Class: Pupa::Processor::Connection::PostgreSQLAdapter

Inherits:
Object
  • Object
show all
Includes:
Concerns::IndifferentAccess
Defined in:
lib/pupa/processor/connection_adapters/postgresql_adapter.rb

Overview

A proxy class to save plain old Ruby objects to PostgreSQL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_url) ⇒ PostgreSQLAdapter

Returns a new instance of PostgreSQLAdapter.

Parameters:

  • database_url (String)

    the database URL



13
14
15
# File 'lib/pupa/processor/connection_adapters/postgresql_adapter.rb', line 13

def initialize(database_url)
  @raw_connection = Sequel.connect(database_url)
end

Instance Attribute Details

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



10
11
12
# File 'lib/pupa/processor/connection_adapters/postgresql_adapter.rb', line 10

def raw_connection
  @raw_connection
end

Instance Method Details

#find(selector) ⇒ Hash?

Finds a document matching the selection criteria.

The selection criteria must set a _type key in order to determine the collection to query.

Parameters:

  • selector (Hash)

    the selection criteria

Returns:

  • (Hash, nil)

    the matched document, or nil



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pupa/processor/connection_adapters/postgresql_adapter.rb', line 25

def find(selector)
  collection_name = collection_name_from_class_name(selector[:_type].camelize)
  if selector.except(:_type).empty?
    raise Errors::EmptySelectorError, "selector is empty during find in collection #{collection_name}"
  end
  collection = raw_connection[collection_name]
  query = collection.filter(symbolize_keys(selector))

  case query.count
  when 0
    nil
  when 1
    stringify_keys(query.first)
  else
    raise Errors::TooManyMatches, "selector matches multiple documents during find in collection #{collection_name}: #{JSON.dump(selector)}"
  end
end

#save(object) ⇒ Array

Inserts or replaces a document in PostgreSQL.

Parameters:

  • object (Object)

    an object

Returns:

  • (Array)

    whether the object was inserted and the object's database ID



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pupa/processor/connection_adapters/postgresql_adapter.rb', line 48

def save(object)
  fingerprint = symbolize_keys(object.fingerprint) # Sequel needs symbols
  selector = if fingerprint.key?(:$or) && fingerprint.size == 1
    Sequel.or(reject_subdocument_criteria(fingerprint[:$or]))
  else
    reject_subdocument_criteria(fingerprint)
  end

  collection_name = collection_name_from_class_name(object.class.to_s)
  if fingerprint.empty?
    raise Errors::EmptySelectorError, "selector is empty during save in collection #{collection_name} for #{object._id}"
  end
  collection = raw_connection[collection_name]
  query = collection.filter(selector)

  # Run query before callbacks to avoid e.g. timestamps in the selector.
  case query.count
  when 0
    object.run_callbacks(:save) do
      object.run_callbacks(:create) do
        collection.insert(object.to_h(persist: true))
        [true, object._id.to_s]
      end
    end
  when 1
    # Make the document available to the callbacks.
    # @see https://github.com/jpmckinney/pupa-ruby/issues/17
    object.document = stringify_keys(query.first)
    object.run_callbacks(:save) do
      query.update(object.to_h(persist: true).except(:_id))
      [false, object.document['_id'].to_s]
    end
  else
    raise Errors::TooManyMatches, "selector matches multiple documents during save in collection #{collection_name} for #{object._id}: #{JSON.dump(selector)}"
  end
end