Class: WCC::Contentful::Store::PostgresStore

Inherits:
Base
  • Object
show all
Defined in:
lib/wcc/contentful/store/postgres_store.rb

Defined Under Namespace

Classes: Query

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#find_by, #index

Constructor Details

#initialize(connection_options = nil) ⇒ PostgresStore



8
9
10
11
12
13
# File 'lib/wcc/contentful/store/postgres_store.rb', line 8

def initialize(connection_options = nil)
  super()
  connection_options ||= { dbname: 'postgres' }
  @conn = PG.connect(connection_options)
  PostgresStore.ensure_schema(@conn)
end

Class Method Details

.ensure_schema(conn) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/wcc/contentful/store/postgres_store.rb', line 114

def self.ensure_schema(conn)
  conn.exec("CREATE TABLE IF NOT EXISTS contentful_raw (\n  id varchar PRIMARY KEY,\n  data jsonb\n);\nCREATE INDEX IF NOT EXISTS contentful_raw_value_type ON contentful_raw ((data->'sys'->>'type'));\nCREATE INDEX IF NOT EXISTS contentful_raw_value_content_type ON contentful_raw ((data->'sys'->'contentType'->'sys'->>'id'));\n\nDROP FUNCTION IF EXISTS \"upsert_entry\";\nCREATE FUNCTION \"upsert_entry\"(_id varchar, _data jsonb) RETURNS jsonb AS $$\nDECLARE\n  prev jsonb;\nBEGIN\n  SELECT data FROM contentful_raw WHERE id = _id INTO prev;\n  INSERT INTO contentful_raw (id, data) values (_id, _data)\n    ON CONFLICT (id) DO\n      UPDATE\n      SET data = _data;\n   RETURN prev;\nEND;\n$$ LANGUAGE 'plpgsql';\n"
  )

  conn.prepare('upsert_entry', 'SELECT * FROM upsert_entry($1,$2)')
  conn.prepare('select_entry', 'SELECT * FROM contentful_raw WHERE id = $1')
  conn.prepare('select_ids', 'SELECT id FROM contentful_raw')
  conn.prepare('delete_by_id', 'DELETE FROM contentful_raw WHERE id = $1 RETURNING *')
end

Instance Method Details

#delete(key) ⇒ Object



29
30
31
32
33
# File 'lib/wcc/contentful/store/postgres_store.rb', line 29

def delete(key)
  result = @conn.exec_prepared('delete_by_id', [key])
  return if result.num_tuples == 0
  JSON.parse(result.getvalue(0, 1))
end

#find(key) ⇒ Object



35
36
37
38
39
# File 'lib/wcc/contentful/store/postgres_store.rb', line 35

def find(key)
  result = @conn.exec_prepared('select_entry', [key])
  return if result.num_tuples == 0
  JSON.parse(result.getvalue(0, 1))
end

#find_all(content_type:) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/wcc/contentful/store/postgres_store.rb', line 41

def find_all(content_type:)
  statement = "WHERE data->'sys'->'contentType'->'sys'->>'id' = $1"
  Query.new(
    @conn,
    statement,
    [content_type]
  )
end

#keysObject



22
23
24
25
26
27
# File 'lib/wcc/contentful/store/postgres_store.rb', line 22

def keys
  result = @conn.exec_prepared('select_ids')
  arr = []
  result.each { |r| arr << r['id'].strip }
  arr
end

#set(key, value) ⇒ Object



15
16
17
18
19
20
# File 'lib/wcc/contentful/store/postgres_store.rb', line 15

def set(key, value)
  result = @conn.exec_prepared('upsert_entry', [key, value.to_json])
  return if result.num_tuples == 0
  val = result.getvalue(0, 0)
  JSON.parse(val) if val
end