Class: PatientHttp::PayloadStore::ActiveRecordStore

Inherits:
Base
  • Object
show all
Defined in:
lib/patient_http/payload_store/active_record_store.rb

Overview

ActiveRecord-based payload store for production deployments.

Stores payloads as JSON in a database table. This store is recommended when you need database-backed storage with transactional guarantees.

Thread-safe: ActiveRecord handles connection pooling and thread safety.

Examples:

Configuration

require "patient_http/payload_store/active_record_store"
config.register_payload_store(:database, adapter: :active_record)

With custom model

config.register_payload_store(:database, adapter: :active_record,
  model: MyApp::PayloadRecord
)

Defined Under Namespace

Classes: Payload

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

create, #generate_key, lookup, register, registered_adapters, #store

Constructor Details

#initialize(model: nil) ⇒ ActiveRecordStore

Initialize a new ActiveRecord store.

Parameters:

  • model (Class) (defaults to: nil)

    ActiveRecord model class to use for storage. Defaults to PatientHttp::PayloadStore::ActiveRecordStore::Payload. Custom models must have: key (string PK), data (text), timestamps



48
49
50
# File 'lib/patient_http/payload_store/active_record_store.rb', line 48

def initialize(model: nil)
  @model = model || Payload
end

Instance Attribute Details

#modelClass (readonly)

Returns The ActiveRecord model class used for storage.

Returns:

  • (Class)

    The ActiveRecord model class used for storage



41
42
43
# File 'lib/patient_http/payload_store/active_record_store.rb', line 41

def model
  @model
end

Instance Method Details

#delete(key) ⇒ Boolean

Delete a payload from the database.

Idempotent - does not raise if record doesn’t exist.

Parameters:

  • key (String)

    The key to delete

Returns:

  • (Boolean)

    true



88
89
90
91
# File 'lib/patient_http/payload_store/active_record_store.rb', line 88

def delete(key)
  @model.where(key: key).delete_all
  true
end

#exists?(key) ⇒ Boolean

Check if a payload exists.

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)

    true if the payload exists



97
98
99
# File 'lib/patient_http/payload_store/active_record_store.rb', line 97

def exists?(key)
  @model.exists?(key: key)
end

#fetch(key) ⇒ Hash?

Fetch data from the database.

Parameters:

  • key (String)

    The key to fetch

Returns:

  • (Hash, nil)

    The stored data or nil if not found



75
76
77
78
79
80
# File 'lib/patient_http/payload_store/active_record_store.rb', line 75

def fetch(key)
  record = @model.find_by(key: key)
  return nil unless record

  JSON.parse(record.data)
end

#store_json(key, json) ⇒ String

Store pre-serialized JSON string directly in the database.

Parameters:

  • key (String)

    Unique key (used as primary key)

  • json (String)

    Pre-serialized JSON string

Returns:

  • (String)

    The key



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/patient_http/payload_store/active_record_store.rb', line 57

def store_json(key, json)
  now = Time.current

  @model.with_connection do
    @model.upsert(
      {key: key, data: json, created_at: now, updated_at: now},
      unique_by: :key,
      update_only: [:data, :updated_at]
    )
  end

  key
end