Class: PatientHttp::PayloadStore::ActiveRecordStore
- 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.
Defined Under Namespace
Classes: Payload
Instance Attribute Summary collapse
-
#model ⇒ Class
readonly
The ActiveRecord model class used for storage.
Instance Method Summary collapse
-
#delete(key) ⇒ Boolean
Delete a payload from the database.
-
#exists?(key) ⇒ Boolean
Check if a payload exists.
-
#fetch(key) ⇒ Hash?
Fetch data from the database.
-
#initialize(model: nil) ⇒ ActiveRecordStore
constructor
Initialize a new ActiveRecord store.
-
#store_json(key, json) ⇒ String
Store pre-serialized JSON string directly in the database.
Methods inherited from Base
create, #generate_key, lookup, register, registered_adapters, #store
Constructor Details
#initialize(model: nil) ⇒ ActiveRecordStore
Initialize a new ActiveRecord store.
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
#model ⇒ Class (readonly)
Returns 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.
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.
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.
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.
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 |