Class: WCC::Contentful::Store::PostgresStore
- Inherits:
-
Base
- Object
- Base
- WCC::Contentful::Store::PostgresStore
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
#ensure_hash, #find_by, #index
Constructor Details
#initialize(_config = nil, connection_options = nil) ⇒ PostgresStore
Returns a new instance of PostgresStore.
8
9
10
11
12
13
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 8
def initialize(_config = nil, 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 147
def self.ensure_schema(conn)
conn.exec(" CREATE TABLE IF NOT EXISTS contentful_raw (\n id varchar PRIMARY KEY,\n data jsonb\n );\n CREATE INDEX IF NOT EXISTS contentful_raw_value_type ON contentful_raw ((data->'sys'->>'type'));\n CREATE INDEX IF NOT EXISTS contentful_raw_value_content_type ON contentful_raw ((data->'sys'->'contentType'->'sys'->>'id'));\n\n DROP FUNCTION IF EXISTS \"upsert_entry\"(_id varchar, _data jsonb);\n CREATE FUNCTION \"upsert_entry\"(_id varchar, _data jsonb) RETURNS jsonb AS $$\n DECLARE\n prev jsonb;\n BEGIN\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;\n END;\n $$ LANGUAGE 'plpgsql';\n HEREDOC\n )\n\n conn.prepare('upsert_entry', 'SELECT * FROM upsert_entry($1,$2)')\n conn.prepare('select_entry', 'SELECT * FROM contentful_raw WHERE id = $1')\n conn.prepare('select_ids', 'SELECT id FROM contentful_raw')\n conn.prepare('delete_by_id', 'DELETE FROM contentful_raw WHERE id = $1 RETURNING *')\nend\n"
|
Instance Method Details
#delete(key) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 31
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, **_options) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 38
def find(key, **_options)
result = @conn.exec_prepared('select_entry', [key])
return if result.num_tuples == 0
JSON.parse(result.getvalue(0, 1))
end
|
#find_all(content_type:, options: nil) ⇒ Object
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 45
def find_all(content_type:, options: nil)
statement = "WHERE data->'sys'->'contentType'->'sys'->>'id' = $1"
Query.new(
self,
@conn,
statement,
[content_type],
options
)
end
|
#keys ⇒ Object
24
25
26
27
28
29
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 24
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
21
22
|
# File 'lib/wcc/contentful/store/postgres_store.rb', line 15
def set(key, value)
ensure_hash 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
|