post_json 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +194 -0
- data/Rakefile +21 -0
- data/lib/core_ext/abstract_adapter_extend.rb +16 -0
- data/lib/core_ext/active_record_relation_extend.rb +16 -0
- data/lib/core_ext/hash_extend.rb +36 -0
- data/lib/generators/post_json/install/install_generator.rb +32 -0
- data/lib/generators/post_json/install/templates/create_post_json_documents.rb +13 -0
- data/lib/generators/post_json/install/templates/create_post_json_dynamic_indexes.rb +9 -0
- data/lib/generators/post_json/install/templates/create_post_json_model_settings.rb +18 -0
- data/lib/generators/post_json/install/templates/create_procedures.rb +120 -0
- data/lib/generators/post_json/install/templates/enable_extensions.rb +28 -0
- data/lib/generators/post_json/install/templates/initializer.rb +9 -0
- data/lib/post_json.rb +56 -0
- data/lib/post_json/base.rb +278 -0
- data/lib/post_json/concerns/argument_methods.rb +33 -0
- data/lib/post_json/concerns/dynamic_index_methods.rb +34 -0
- data/lib/post_json/concerns/finder_methods.rb +343 -0
- data/lib/post_json/concerns/query_methods.rb +157 -0
- data/lib/post_json/concerns/settings_methods.rb +106 -0
- data/lib/post_json/dynamic_index.rb +99 -0
- data/lib/post_json/model_settings.rb +17 -0
- data/lib/post_json/query_translator.rb +48 -0
- data/lib/post_json/version.rb +3 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +30 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +26 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/post_json.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/db/migrate/20131015022029_enable_extensions.rb +28 -0
- data/spec/dummy/db/migrate/20131015022030_create_procedures.rb +120 -0
- data/spec/dummy/db/migrate/20131015022031_create_post_json_model_settings.rb +18 -0
- data/spec/dummy/db/migrate/20131015022032_create_post_json_collections.rb +16 -0
- data/spec/dummy/db/migrate/20131015022033_create_post_json_documents.rb +13 -0
- data/spec/dummy/db/migrate/20131015022034_create_post_json_dynamic_indexes.rb +9 -0
- data/spec/dummy/db/structure.sql +311 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/models/base_spec.rb +393 -0
- data/spec/models/collection_spec.rb +27 -0
- data/spec/models/queries_spec.rb +164 -0
- data/spec/modules/argument_methods_spec.rb +17 -0
- data/spec/modules/query_methods_spec.rb +69 -0
- data/spec/spec_helper.rb +54 -0
- metadata +184 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
Dummy::Application.routes.draw do
|
2
|
+
# The priority is based upon order of creation: first created -> highest priority.
|
3
|
+
# See how all your routes lay out with "rake routes".
|
4
|
+
|
5
|
+
# You can have the root of your site routed with "root"
|
6
|
+
# root 'welcome#index'
|
7
|
+
|
8
|
+
# Example of regular route:
|
9
|
+
# get 'products/:id' => 'catalog#view'
|
10
|
+
|
11
|
+
# Example of named route that can be invoked with purchase_url(id: product.id)
|
12
|
+
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
|
13
|
+
|
14
|
+
# Example resource route (maps HTTP verbs to controller actions automatically):
|
15
|
+
# resources :products
|
16
|
+
|
17
|
+
# Example resource route with options:
|
18
|
+
# resources :products do
|
19
|
+
# member do
|
20
|
+
# get 'short'
|
21
|
+
# post 'toggle'
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# collection do
|
25
|
+
# get 'sold'
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
|
29
|
+
# Example resource route with sub-resources:
|
30
|
+
# resources :products do
|
31
|
+
# resources :comments, :sales
|
32
|
+
# resource :seller
|
33
|
+
# end
|
34
|
+
|
35
|
+
# Example resource route with more complex sub-resources:
|
36
|
+
# resources :products do
|
37
|
+
# resources :comments
|
38
|
+
# resources :sales do
|
39
|
+
# get 'recent', on: :collection
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
|
43
|
+
# Example resource route with concerns:
|
44
|
+
# concern :toggleable do
|
45
|
+
# post 'toggle'
|
46
|
+
# end
|
47
|
+
# resources :posts, concerns: :toggleable
|
48
|
+
# resources :photos, concerns: :toggleable
|
49
|
+
|
50
|
+
# Example resource route within a namespace:
|
51
|
+
# namespace :admin do
|
52
|
+
# # Directs /admin/products/* to Admin::ProductsController
|
53
|
+
# # (app/controllers/admin/products_controller.rb)
|
54
|
+
# resources :products
|
55
|
+
# end
|
56
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class EnableExtensions < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
# Install Postgresql 9.2 with PLV8 (might require )
|
4
|
+
# 1. sudo add-apt-repository ppa:pitti/postgresql
|
5
|
+
# 2. sudo apt-get update
|
6
|
+
# 3. sudo apt-get install postgresql-9.2 postgresql-contrib-9.2 postgresql-server-dev-9.2 libv8-dev
|
7
|
+
# 4. sudo su - postgres
|
8
|
+
# 5. Enter psql and run "CREATE USER webnuts WITH PASSWORD 'webnuts';" and "ALTER USER webnuts CREATEDB;" and "ALTER USER webnuts SUPERUSER;"
|
9
|
+
# 6. git clone https://code.google.com/p/plv8js/
|
10
|
+
# 7. cd plv8js
|
11
|
+
# 8. make
|
12
|
+
# 9. sudo make install
|
13
|
+
|
14
|
+
# bundle exec rake db:create:all
|
15
|
+
# bundle exec rake db:migrate
|
16
|
+
# bundle exec rake db:test:prepare
|
17
|
+
|
18
|
+
# See all available extensions:
|
19
|
+
# ActiveRecord::Base.connection.execute("select * from pg_available_extensions;").each_row do |row|
|
20
|
+
# puts row.to_s
|
21
|
+
# end
|
22
|
+
|
23
|
+
enable_extension 'uuid-ossp' # generate universally unique identifiers (UUIDs)
|
24
|
+
enable_extension 'hstore' # data type for storing sets of (key, value) pairs
|
25
|
+
enable_extension 'plv8' # PL/JavaScript (v8) trusted procedural language
|
26
|
+
#enable_extension 'plcoffee' # PL/CoffeeScript (v8) trusted procedural language
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# http://pgxn.org/dist/plv8/doc/plv8.html
|
2
|
+
# http://plv8-pgopen.herokuapp.com/
|
3
|
+
# http://www.craigkerstiens.com/2013/06/25/javascript-functions-for-postgres/
|
4
|
+
# http://www.postgresonline.com/journal/archives/272-Using-PLV8-to-build-JSON-selectors.html
|
5
|
+
|
6
|
+
class CreateProcedures < ActiveRecord::Migration
|
7
|
+
def change
|
8
|
+
ActiveRecord::Base.connection.execute(json_numeric_procedure)
|
9
|
+
ActiveRecord::Base.connection.execute(json_text_procedure)
|
10
|
+
ActiveRecord::Base.connection.execute(js_filter_procedure)
|
11
|
+
ActiveRecord::Base.connection.execute(json_selector_procedure)
|
12
|
+
ActiveRecord::Base.connection.execute(json_selectors_procedure)
|
13
|
+
ActiveRecord::Base.connection.execute(show_all_indexes_procedure)
|
14
|
+
# ActiveRecord::Base.connection.execute(show_indexes_procedure)
|
15
|
+
# ActiveRecord::Base.connection.execute(ensure_dynamic_index_procedure)
|
16
|
+
end
|
17
|
+
|
18
|
+
def json_numeric_procedure
|
19
|
+
"CREATE OR REPLACE FUNCTION json_numeric(key text, data json) RETURNS numeric AS $$
|
20
|
+
if (data == null) {
|
21
|
+
return null;
|
22
|
+
}
|
23
|
+
return data[key];
|
24
|
+
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
25
|
+
end
|
26
|
+
|
27
|
+
def json_text_procedure
|
28
|
+
"CREATE OR REPLACE FUNCTION json_text(key text, data json) RETURNS text AS $$
|
29
|
+
if (data == null) {
|
30
|
+
return null;
|
31
|
+
}
|
32
|
+
return data[key];
|
33
|
+
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
34
|
+
end
|
35
|
+
|
36
|
+
def js_filter_procedure
|
37
|
+
"create or replace function js_filter(js_function text, json_arguments text, data json) returns numeric as $$
|
38
|
+
if (data == null) {
|
39
|
+
return null;
|
40
|
+
}
|
41
|
+
eval('var func = ' + js_function);
|
42
|
+
eval('var args = ' + (json_arguments == '' ? 'null' : json_arguments));
|
43
|
+
var final_args = [data].concat(args);
|
44
|
+
var result = func.apply(null, final_args);
|
45
|
+
return result == true || 0 < parseInt(result) ? 1 : 0;
|
46
|
+
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
47
|
+
end
|
48
|
+
|
49
|
+
def json_selector_procedure
|
50
|
+
"CREATE OR REPLACE FUNCTION json_selector(selector text, data json) RETURNS text AS $$
|
51
|
+
if (data == null || selector == null || selector == '') {
|
52
|
+
return null;
|
53
|
+
}
|
54
|
+
var names = selector.split('.');
|
55
|
+
var result = names.reduce(function(previousValue, currentValue, index, array) {
|
56
|
+
if (previousValue == null) {
|
57
|
+
return null;
|
58
|
+
} else {
|
59
|
+
return previousValue[currentValue];
|
60
|
+
}
|
61
|
+
}, data);
|
62
|
+
return result;
|
63
|
+
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
64
|
+
end
|
65
|
+
|
66
|
+
def json_selectors_procedure
|
67
|
+
"CREATE OR REPLACE FUNCTION json_selectors(selectors text, data json) RETURNS json AS $$
|
68
|
+
var json_selector = plv8.find_function('json_selector');
|
69
|
+
var selectorArray = selectors.replace(/\s+/g, '').split(',');
|
70
|
+
var result = selectorArray.map(function(selector) { return json_selector(selector, data); });
|
71
|
+
return result;
|
72
|
+
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
73
|
+
end
|
74
|
+
|
75
|
+
def show_all_indexes_procedure
|
76
|
+
"CREATE OR REPLACE FUNCTION show_all_indexes() RETURNS json AS $$
|
77
|
+
var sql = \"SELECT c3.relname AS table, c2.relname AS index FROM pg_class c2 LEFT JOIN pg_index i ON c2.oid = i.indexrelid LEFT JOIN pg_class c1 ON c1.oid = i.indrelid RIGHT OUTER JOIN pg_class c3 ON c3.oid = c1.oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c3.relnamespace WHERE c3.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c3.oid) ORDER BY c3.relpages DESC;\"
|
78
|
+
return plv8.execute( sql );
|
79
|
+
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
80
|
+
end
|
81
|
+
|
82
|
+
# def show_indexes_procedure
|
83
|
+
# "CREATE OR REPLACE FUNCTION show_indexes(table_name text DEFAULT '', index_prefix text DEFAULT '') RETURNS json AS $$
|
84
|
+
# var show_all_indexes = plv8.find_function('show_all_indexes');
|
85
|
+
# var indexes = show_all_indexes();
|
86
|
+
# if (0 < (table_name || '').length) {
|
87
|
+
# indexes = indexes.filter(function(row) { return row['table'] === table_name; });
|
88
|
+
# }
|
89
|
+
# if (0 < (index_prefix || '').length) {
|
90
|
+
# indexes = indexes.filter(function(row) { return row['index'].lastIndexOf(index_prefix, 0) === 0; });
|
91
|
+
# }
|
92
|
+
# return indexes;
|
93
|
+
# $$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
94
|
+
# end
|
95
|
+
|
96
|
+
# def ensure_dynamic_index_procedure
|
97
|
+
# raise ArgumentError, "index name should be: dyn_col_id_md5_hash_of_selector and truncated to a length of 63"
|
98
|
+
# raise ArgumentError, "it should only create 1 index and not multiple"
|
99
|
+
|
100
|
+
|
101
|
+
# # CREATE INDEX CONCURRENTLY post_json_documents_body_age ON post_json_documents(json_selector('age', body))
|
102
|
+
# "CREATE OR REPLACE FUNCTION ensure_dynamic_index(selectors text, collection_id text) RETURNS json AS $$
|
103
|
+
# var show_indexes = plv8.find_function('show_indexes');
|
104
|
+
# var colId = collection_id.replace('-', '');
|
105
|
+
# var indexPrefix = 'col_' + colId + '_';
|
106
|
+
# var existingIndexes = show_indexes('post_json_documents', indexPrefix).map(function(row) { return row.index; });
|
107
|
+
# var selectorArray = selectors.replace(/\s+/g, '').split(',');
|
108
|
+
|
109
|
+
# var indexes = selectorArray.map(function(selector) { return {'name': indexPrefix + selector.replace('.', '_'), selector: selector}; });
|
110
|
+
# var newIndexes = indexes.filter(function(index) { return existingIndexes.indexOf(index.name) == -1; });
|
111
|
+
|
112
|
+
# newIndexes.forEach(function(index) {
|
113
|
+
# var sql = \"CREATE INDEX \" + index.name + \" ON post_json_documents(json_selector('\" + index.selector + \"', body)) WHERE collection_id = '\" + colId + \"';\"
|
114
|
+
# plv8.execute( sql );
|
115
|
+
# });
|
116
|
+
|
117
|
+
# return newIndexes;
|
118
|
+
# $$ LANGUAGE plv8 IMMUTABLE STRICT;"
|
119
|
+
# end
|
120
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreatePostJsonModelSettings < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :post_json_model_settings, id: :uuid do |t|
|
4
|
+
t.text :collection_name, index: true, unique: true
|
5
|
+
t.json :meta, default: {}, null: false
|
6
|
+
t.boolean :use_timestamps, default: true
|
7
|
+
t.text :created_at_attribute_name, default: 'created_at', null: false
|
8
|
+
t.text :updated_at_attribute_name, default: 'updated_at', null: false
|
9
|
+
t.boolean :include_version_number, default: true
|
10
|
+
t.text :version_attribute_name, default: 'version', null: false
|
11
|
+
t.boolean :use_dynamic_index, default: true
|
12
|
+
t.integer :create_dynamic_index_milliseconds_threshold, default: 50
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
|
16
|
+
execute "CREATE INDEX post_json_model_settings_lower_collection_name ON post_json_model_settings(lower(collection_name));"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreatePostJsonCollections < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :post_json_collections, id: :uuid do |t|
|
4
|
+
t.text :name, index: true, unique: true
|
5
|
+
t.json :meta
|
6
|
+
t.boolean :use_timestamps, default: true
|
7
|
+
t.text :created_at_attribute_name, default: 'created_at', null: false
|
8
|
+
t.text :updated_at_attribute_name, default: 'updated_at', null: false
|
9
|
+
t.boolean :use_version_number, default: true
|
10
|
+
t.text :version_attribute_name, default: 'version', null: false
|
11
|
+
t.boolean :use_dynamic_index, default: true
|
12
|
+
t.integer :create_dynamic_index_milliseconds_threshold, default: 50
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreatePostJsonDocuments < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :post_json_documents, id: false do |t|
|
4
|
+
t.text :id, null: false, index: true
|
5
|
+
t.integer :__doc__version
|
6
|
+
t.json :__doc__body
|
7
|
+
t.uuid :__doc__model_settings_id
|
8
|
+
end
|
9
|
+
|
10
|
+
execute "CREATE UNIQUE INDEX post_json_documents_unique_id ON post_json_documents(id, __doc__model_settings_id);"
|
11
|
+
execute "ALTER TABLE post_json_documents ADD PRIMARY KEY (id, __doc__model_settings_id);"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,311 @@
|
|
1
|
+
--
|
2
|
+
-- PostgreSQL database dump
|
3
|
+
--
|
4
|
+
|
5
|
+
SET statement_timeout = 0;
|
6
|
+
SET lock_timeout = 0;
|
7
|
+
SET client_encoding = 'UTF8';
|
8
|
+
SET standard_conforming_strings = on;
|
9
|
+
SET check_function_bodies = false;
|
10
|
+
SET client_min_messages = warning;
|
11
|
+
|
12
|
+
--
|
13
|
+
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
|
14
|
+
--
|
15
|
+
|
16
|
+
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
|
17
|
+
|
18
|
+
|
19
|
+
--
|
20
|
+
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
|
21
|
+
--
|
22
|
+
|
23
|
+
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
|
24
|
+
|
25
|
+
|
26
|
+
--
|
27
|
+
-- Name: plv8; Type: EXTENSION; Schema: -; Owner: -
|
28
|
+
--
|
29
|
+
|
30
|
+
CREATE EXTENSION IF NOT EXISTS plv8 WITH SCHEMA pg_catalog;
|
31
|
+
|
32
|
+
|
33
|
+
--
|
34
|
+
-- Name: EXTENSION plv8; Type: COMMENT; Schema: -; Owner: -
|
35
|
+
--
|
36
|
+
|
37
|
+
COMMENT ON EXTENSION plv8 IS 'PL/JavaScript (v8) trusted procedural language';
|
38
|
+
|
39
|
+
|
40
|
+
--
|
41
|
+
-- Name: hstore; Type: EXTENSION; Schema: -; Owner: -
|
42
|
+
--
|
43
|
+
|
44
|
+
CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public;
|
45
|
+
|
46
|
+
|
47
|
+
--
|
48
|
+
-- Name: EXTENSION hstore; Type: COMMENT; Schema: -; Owner: -
|
49
|
+
--
|
50
|
+
|
51
|
+
COMMENT ON EXTENSION hstore IS 'data type for storing sets of (key, value) pairs';
|
52
|
+
|
53
|
+
|
54
|
+
--
|
55
|
+
-- Name: uuid-ossp; Type: EXTENSION; Schema: -; Owner: -
|
56
|
+
--
|
57
|
+
|
58
|
+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
|
59
|
+
|
60
|
+
|
61
|
+
--
|
62
|
+
-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner: -
|
63
|
+
--
|
64
|
+
|
65
|
+
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
|
66
|
+
|
67
|
+
|
68
|
+
SET search_path = public, pg_catalog;
|
69
|
+
|
70
|
+
--
|
71
|
+
-- Name: js_filter(text, text, json); Type: FUNCTION; Schema: public; Owner: -
|
72
|
+
--
|
73
|
+
|
74
|
+
CREATE FUNCTION js_filter(js_function text, json_arguments text, data json) RETURNS numeric
|
75
|
+
LANGUAGE plv8 IMMUTABLE STRICT
|
76
|
+
AS $$
|
77
|
+
if (data == null) {
|
78
|
+
return null;
|
79
|
+
}
|
80
|
+
eval('var func = ' + js_function);
|
81
|
+
eval('var args = ' + (json_arguments == '' ? 'null' : json_arguments));
|
82
|
+
var final_args = [data].concat(args);
|
83
|
+
var result = func.apply(null, final_args);
|
84
|
+
return result == true || 0 < parseInt(result) ? 1 : 0;
|
85
|
+
$$;
|
86
|
+
|
87
|
+
|
88
|
+
--
|
89
|
+
-- Name: json_numeric(text, json); Type: FUNCTION; Schema: public; Owner: -
|
90
|
+
--
|
91
|
+
|
92
|
+
CREATE FUNCTION json_numeric(key text, data json) RETURNS numeric
|
93
|
+
LANGUAGE plv8 IMMUTABLE STRICT
|
94
|
+
AS $$
|
95
|
+
if (data == null) {
|
96
|
+
return null;
|
97
|
+
}
|
98
|
+
return data[key];
|
99
|
+
$$;
|
100
|
+
|
101
|
+
|
102
|
+
--
|
103
|
+
-- Name: json_selector(text, json); Type: FUNCTION; Schema: public; Owner: -
|
104
|
+
--
|
105
|
+
|
106
|
+
CREATE FUNCTION json_selector(selector text, data json) RETURNS text
|
107
|
+
LANGUAGE plv8 IMMUTABLE STRICT
|
108
|
+
AS $$
|
109
|
+
if (data == null || selector == null || selector == '') {
|
110
|
+
return null;
|
111
|
+
}
|
112
|
+
var names = selector.split('.');
|
113
|
+
var result = names.reduce(function(previousValue, currentValue, index, array) {
|
114
|
+
if (previousValue == null) {
|
115
|
+
return null;
|
116
|
+
} else {
|
117
|
+
return previousValue[currentValue];
|
118
|
+
}
|
119
|
+
}, data);
|
120
|
+
return result;
|
121
|
+
$$;
|
122
|
+
|
123
|
+
|
124
|
+
--
|
125
|
+
-- Name: json_selectors(text, json); Type: FUNCTION; Schema: public; Owner: -
|
126
|
+
--
|
127
|
+
|
128
|
+
CREATE FUNCTION json_selectors(selectors text, data json) RETURNS json
|
129
|
+
LANGUAGE plv8 IMMUTABLE STRICT
|
130
|
+
AS $$
|
131
|
+
var json_selector = plv8.find_function('json_selector');
|
132
|
+
var selectorArray = selectors.replace(/ +/g, '').split(',');
|
133
|
+
var result = selectorArray.map(function(selector) { return json_selector(selector, data); });
|
134
|
+
return result;
|
135
|
+
$$;
|
136
|
+
|
137
|
+
|
138
|
+
--
|
139
|
+
-- Name: json_text(text, json); Type: FUNCTION; Schema: public; Owner: -
|
140
|
+
--
|
141
|
+
|
142
|
+
CREATE FUNCTION json_text(key text, data json) RETURNS text
|
143
|
+
LANGUAGE plv8 IMMUTABLE STRICT
|
144
|
+
AS $$
|
145
|
+
if (data == null) {
|
146
|
+
return null;
|
147
|
+
}
|
148
|
+
return data[key];
|
149
|
+
$$;
|
150
|
+
|
151
|
+
|
152
|
+
--
|
153
|
+
-- Name: show_all_indexes(); Type: FUNCTION; Schema: public; Owner: -
|
154
|
+
--
|
155
|
+
|
156
|
+
CREATE FUNCTION show_all_indexes() RETURNS json
|
157
|
+
LANGUAGE plv8 IMMUTABLE STRICT
|
158
|
+
AS $$
|
159
|
+
var sql = "SELECT c3.relname AS table, c2.relname AS index FROM pg_class c2 LEFT JOIN pg_index i ON c2.oid = i.indexrelid LEFT JOIN pg_class c1 ON c1.oid = i.indrelid RIGHT OUTER JOIN pg_class c3 ON c3.oid = c1.oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c3.relnamespace WHERE c3.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c3.oid) ORDER BY c3.relpages DESC;"
|
160
|
+
return plv8.execute( sql );
|
161
|
+
$$;
|
162
|
+
|
163
|
+
|
164
|
+
SET default_tablespace = '';
|
165
|
+
|
166
|
+
SET default_with_oids = false;
|
167
|
+
|
168
|
+
--
|
169
|
+
-- Name: post_json_collections; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
170
|
+
--
|
171
|
+
|
172
|
+
CREATE TABLE post_json_collections (
|
173
|
+
id uuid DEFAULT uuid_generate_v4() NOT NULL,
|
174
|
+
name text,
|
175
|
+
meta json,
|
176
|
+
use_timestamps boolean DEFAULT true,
|
177
|
+
created_at_attribute_name text DEFAULT 'created_at'::text NOT NULL,
|
178
|
+
updated_at_attribute_name text DEFAULT 'updated_at'::text NOT NULL,
|
179
|
+
use_version_number boolean DEFAULT true,
|
180
|
+
version_attribute_name text DEFAULT 'version'::text NOT NULL,
|
181
|
+
use_dynamic_index boolean DEFAULT true,
|
182
|
+
create_dynamic_index_milliseconds_threshold integer DEFAULT 50,
|
183
|
+
created_at timestamp without time zone,
|
184
|
+
updated_at timestamp without time zone
|
185
|
+
);
|
186
|
+
|
187
|
+
|
188
|
+
--
|
189
|
+
-- Name: post_json_documents; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
190
|
+
--
|
191
|
+
|
192
|
+
CREATE TABLE post_json_documents (
|
193
|
+
id text NOT NULL,
|
194
|
+
__doc__version integer,
|
195
|
+
__doc__body json,
|
196
|
+
__doc__model_settings_id uuid NOT NULL
|
197
|
+
);
|
198
|
+
|
199
|
+
|
200
|
+
--
|
201
|
+
-- Name: post_json_dynamic_indexes; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
202
|
+
--
|
203
|
+
|
204
|
+
CREATE TABLE post_json_dynamic_indexes (
|
205
|
+
id uuid DEFAULT uuid_generate_v4() NOT NULL,
|
206
|
+
selector text NOT NULL,
|
207
|
+
model_settings_id uuid,
|
208
|
+
created_at timestamp without time zone,
|
209
|
+
updated_at timestamp without time zone
|
210
|
+
);
|
211
|
+
|
212
|
+
|
213
|
+
--
|
214
|
+
-- Name: post_json_model_settings; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
215
|
+
--
|
216
|
+
|
217
|
+
CREATE TABLE post_json_model_settings (
|
218
|
+
id uuid DEFAULT uuid_generate_v4() NOT NULL,
|
219
|
+
collection_name text,
|
220
|
+
meta json DEFAULT '{}'::json NOT NULL,
|
221
|
+
use_timestamps boolean DEFAULT true,
|
222
|
+
created_at_attribute_name text DEFAULT 'created_at'::text NOT NULL,
|
223
|
+
updated_at_attribute_name text DEFAULT 'updated_at'::text NOT NULL,
|
224
|
+
include_version_number boolean DEFAULT true,
|
225
|
+
version_attribute_name text DEFAULT 'version'::text NOT NULL,
|
226
|
+
use_dynamic_index boolean DEFAULT true,
|
227
|
+
create_dynamic_index_milliseconds_threshold integer DEFAULT 50,
|
228
|
+
created_at timestamp without time zone,
|
229
|
+
updated_at timestamp without time zone
|
230
|
+
);
|
231
|
+
|
232
|
+
|
233
|
+
--
|
234
|
+
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -; Tablespace:
|
235
|
+
--
|
236
|
+
|
237
|
+
CREATE TABLE schema_migrations (
|
238
|
+
version character varying(255) NOT NULL
|
239
|
+
);
|
240
|
+
|
241
|
+
|
242
|
+
--
|
243
|
+
-- Name: post_json_collections_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
244
|
+
--
|
245
|
+
|
246
|
+
ALTER TABLE ONLY post_json_collections
|
247
|
+
ADD CONSTRAINT post_json_collections_pkey PRIMARY KEY (id);
|
248
|
+
|
249
|
+
|
250
|
+
--
|
251
|
+
-- Name: post_json_documents_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
252
|
+
--
|
253
|
+
|
254
|
+
ALTER TABLE ONLY post_json_documents
|
255
|
+
ADD CONSTRAINT post_json_documents_pkey PRIMARY KEY (id, __doc__model_settings_id);
|
256
|
+
|
257
|
+
|
258
|
+
--
|
259
|
+
-- Name: post_json_dynamic_indexes_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
260
|
+
--
|
261
|
+
|
262
|
+
ALTER TABLE ONLY post_json_dynamic_indexes
|
263
|
+
ADD CONSTRAINT post_json_dynamic_indexes_pkey PRIMARY KEY (id);
|
264
|
+
|
265
|
+
|
266
|
+
--
|
267
|
+
-- Name: post_json_model_settings_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
|
268
|
+
--
|
269
|
+
|
270
|
+
ALTER TABLE ONLY post_json_model_settings
|
271
|
+
ADD CONSTRAINT post_json_model_settings_pkey PRIMARY KEY (id);
|
272
|
+
|
273
|
+
|
274
|
+
--
|
275
|
+
-- Name: post_json_documents_unique_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
276
|
+
--
|
277
|
+
|
278
|
+
CREATE UNIQUE INDEX post_json_documents_unique_id ON post_json_documents USING btree (id, __doc__model_settings_id);
|
279
|
+
|
280
|
+
|
281
|
+
--
|
282
|
+
-- Name: post_json_model_settings_lower_collection_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
283
|
+
--
|
284
|
+
|
285
|
+
CREATE INDEX post_json_model_settings_lower_collection_name ON post_json_model_settings USING btree (lower(collection_name));
|
286
|
+
|
287
|
+
|
288
|
+
--
|
289
|
+
-- Name: unique_schema_migrations; Type: INDEX; Schema: public; Owner: -; Tablespace:
|
290
|
+
--
|
291
|
+
|
292
|
+
CREATE UNIQUE INDEX unique_schema_migrations ON schema_migrations USING btree (version);
|
293
|
+
|
294
|
+
|
295
|
+
--
|
296
|
+
-- PostgreSQL database dump complete
|
297
|
+
--
|
298
|
+
|
299
|
+
SET search_path TO "$user",public;
|
300
|
+
|
301
|
+
INSERT INTO schema_migrations (version) VALUES ('20131015022029');
|
302
|
+
|
303
|
+
INSERT INTO schema_migrations (version) VALUES ('20131015022030');
|
304
|
+
|
305
|
+
INSERT INTO schema_migrations (version) VALUES ('20131015022031');
|
306
|
+
|
307
|
+
INSERT INTO schema_migrations (version) VALUES ('20131015022032');
|
308
|
+
|
309
|
+
INSERT INTO schema_migrations (version) VALUES ('20131015022033');
|
310
|
+
|
311
|
+
INSERT INTO schema_migrations (version) VALUES ('20131015022034');
|