post_json 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba717a41c7ab9e983103b70305d09b301e8c1130
4
- data.tar.gz: 564e3a2eee9880edde739632a7223f7d571f78a6
3
+ metadata.gz: d5fb855c51537275da0a59cfda2e1b80bc13a257
4
+ data.tar.gz: 4088ddc01020461837ff1b1bfd044c5c1426ed33
5
5
  SHA512:
6
- metadata.gz: 96775c8330a18731ec8ba1b56c7c30a1287a8824c50d9deae8e85f877638ed17c107d26a88f2a22876bcf36e157d499ec9138dccc6f5f3acdc4ca54769bf9f29
7
- data.tar.gz: af55ee745807c8c1246651a319ec9978e4dc266fd84460840f5c79a14d240afc04543c57da37ad97af00d55205e88094b317171d3dca077d2cac155971e34403
6
+ metadata.gz: e5ce9111121f57bd4cbe74ec3bf9ba43704469bbc585abcc004894d47b243749a39cab7744de50d5b389e79c1013116258f418cc53efe5db1054aeb13ca0cad9
7
+ data.tar.gz: 6f27d38837491b0cbb3c5e4acf410c52066bdc6f4dbbed8893aae6886b5c22a401497ed8a7cb2cefb79da705fda3244446e84fc65c7fd2635bbca4d69c902793
data/README.md CHANGED
@@ -71,11 +71,14 @@ Also, __notice you don't have to define model attributes anywhere!__
71
71
 
72
72
  also_me_1 = Person.where(details: {age: 33}).first
73
73
  also_me_2 = Person.where("details.age" => 33).first
74
- also_me_3 = Person.where("json_details.age = ?", 33).first
74
+ also_me_3 = Person.where("function(doc) { return doc.details.age == 33; }").first
75
+ also_me_4 = Person.where("json_details.age = ?", 33).first
75
76
 
76
77
  PostJson support filtering on nested attributes as you can see. The two first queries speak for themself.
77
78
 
78
- The third (and last) query is special and show it is possible to write real SQL queries. We just need to prefix
79
+ The third query is special and show it is possible to use a pure JavaScript function for selecting documents.
80
+
81
+ The last query is also special and show it is possible to write real SQL queries. We just need to prefix
79
82
  the JSON attributes with `json_`.
80
83
 
81
84
  4. Accessing attributes:
@@ -1,7 +1,4 @@
1
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
2
 
6
3
  class CreateProcedures < ActiveRecord::Migration
7
4
  def change
@@ -11,8 +8,7 @@ class CreateProcedures < ActiveRecord::Migration
11
8
  ActiveRecord::Base.connection.execute(json_selector_procedure)
12
9
  ActiveRecord::Base.connection.execute(json_selectors_procedure)
13
10
  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)
11
+ ActiveRecord::Base.connection.execute(show_indexes_procedure)
16
12
  end
17
13
 
18
14
  def json_numeric_procedure
@@ -79,42 +75,17 @@ $$ LANGUAGE plv8 IMMUTABLE STRICT;"
79
75
  $$ LANGUAGE plv8 IMMUTABLE STRICT;"
80
76
  end
81
77
 
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
78
+ def show_indexes_procedure
79
+ "CREATE OR REPLACE FUNCTION show_indexes(table_name text DEFAULT '', index_prefix text DEFAULT '') RETURNS json AS $$
80
+ var show_all_indexes = plv8.find_function('show_all_indexes');
81
+ var indexes = show_all_indexes();
82
+ if (0 < (table_name || '').length) {
83
+ indexes = indexes.filter(function(row) { return row['table'] === table_name; });
84
+ }
85
+ if (0 < (index_prefix || '').length) {
86
+ indexes = indexes.filter(function(row) { return row['index'].lastIndexOf(index_prefix, 0) === 0; });
87
+ }
88
+ return indexes;
89
+ $$ LANGUAGE plv8 IMMUTABLE STRICT;"
90
+ end
120
91
  end
@@ -1,28 +1,7 @@
1
1
  class EnableExtensions < ActiveRecord::Migration
2
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
3
  enable_extension 'uuid-ossp' # generate universally unique identifiers (UUIDs)
24
4
  enable_extension 'hstore' # data type for storing sets of (key, value) pairs
25
5
  enable_extension 'plv8' # PL/JavaScript (v8) trusted procedural language
26
- #enable_extension 'plcoffee' # PL/CoffeeScript (v8) trusted procedural language
27
6
  end
28
7
  end
@@ -1,3 +1,3 @@
1
1
  module PostJson
2
- VERSION = "1.0.4"
2
+ VERSION = "1.0.5"
3
3
  end
@@ -1,5 +1,9 @@
1
- # Collection are identified by their name, so you can change the other attributes and they will be updated at application startup,
2
- # if you keep the same name
3
- #
4
-
5
- # PostJson::Collection.create_or_update([{name: "Customers", use_timestamps: false}, {name: "Orders/", use_version_number: false}])
1
+ # PostJson.setup "people" do |collection|
2
+ # collection.record_timestamps = true # default is 'true'
3
+ # collection.created_at_attribute_name = "created_at" # default is 'created_at'
4
+ # collection.updated_at_attribute_name = "updated_at" # default is 'updated_at'
5
+ # collection.include_version_number = true # default is 'true'
6
+ # collection.version_attribute_name = "version" # default is 'version'
7
+ # collection.use_dynamic_index = true # default is 'true'
8
+ # collection.create_dynamic_index_milliseconds_threshold = 50 # default is '50'
9
+ # end
@@ -0,0 +1,7 @@
1
+ class EnableExtensions < ActiveRecord::Migration
2
+ def change
3
+ enable_extension 'uuid-ossp' # generate universally unique identifiers (UUIDs)
4
+ enable_extension 'hstore' # data type for storing sets of (key, value) pairs
5
+ enable_extension 'plv8' # PL/JavaScript (v8) trusted procedural language
6
+ end
7
+ end
@@ -1,7 +1,4 @@
1
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
2
 
6
3
  class CreateProcedures < ActiveRecord::Migration
7
4
  def change
@@ -11,8 +8,7 @@ class CreateProcedures < ActiveRecord::Migration
11
8
  ActiveRecord::Base.connection.execute(json_selector_procedure)
12
9
  ActiveRecord::Base.connection.execute(json_selectors_procedure)
13
10
  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)
11
+ ActiveRecord::Base.connection.execute(show_indexes_procedure)
16
12
  end
17
13
 
18
14
  def json_numeric_procedure
@@ -79,42 +75,17 @@ $$ LANGUAGE plv8 IMMUTABLE STRICT;"
79
75
  $$ LANGUAGE plv8 IMMUTABLE STRICT;"
80
76
  end
81
77
 
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
78
+ def show_indexes_procedure
79
+ "CREATE OR REPLACE FUNCTION show_indexes(table_name text DEFAULT '', index_prefix text DEFAULT '') RETURNS json AS $$
80
+ var show_all_indexes = plv8.find_function('show_all_indexes');
81
+ var indexes = show_all_indexes();
82
+ if (0 < (table_name || '').length) {
83
+ indexes = indexes.filter(function(row) { return row['table'] === table_name; });
84
+ }
85
+ if (0 < (index_prefix || '').length) {
86
+ indexes = indexes.filter(function(row) { return row['index'].lastIndexOf(index_prefix, 0) === 0; });
87
+ }
88
+ return indexes;
89
+ $$ LANGUAGE plv8 IMMUTABLE STRICT;"
90
+ end
120
91
  end
@@ -161,29 +161,28 @@ CREATE FUNCTION show_all_indexes() RETURNS json
161
161
  $$;
162
162
 
163
163
 
164
- SET default_tablespace = '';
165
-
166
- SET default_with_oids = false;
167
-
168
164
  --
169
- -- Name: post_json_collections; Type: TABLE; Schema: public; Owner: -; Tablespace:
165
+ -- Name: show_indexes(text, text); Type: FUNCTION; Schema: public; Owner: -
170
166
  --
171
167
 
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
- );
168
+ CREATE FUNCTION show_indexes(table_name text DEFAULT ''::text, index_prefix text DEFAULT ''::text) RETURNS json
169
+ LANGUAGE plv8 IMMUTABLE STRICT
170
+ AS $$
171
+ var show_all_indexes = plv8.find_function('show_all_indexes');
172
+ var indexes = show_all_indexes();
173
+ if (0 < (table_name || '').length) {
174
+ indexes = indexes.filter(function(row) { return row['table'] === table_name; });
175
+ }
176
+ if (0 < (index_prefix || '').length) {
177
+ indexes = indexes.filter(function(row) { return row['index'].lastIndexOf(index_prefix, 0) === 0; });
178
+ }
179
+ return indexes;
180
+ $$;
181
+
182
+
183
+ SET default_tablespace = '';
186
184
 
185
+ SET default_with_oids = false;
187
186
 
188
187
  --
189
188
  -- Name: post_json_documents; Type: TABLE; Schema: public; Owner: -; Tablespace:
@@ -239,14 +238,6 @@ CREATE TABLE schema_migrations (
239
238
  );
240
239
 
241
240
 
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
241
  --
251
242
  -- Name: post_json_documents_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
252
243
  --
@@ -298,14 +289,12 @@ CREATE UNIQUE INDEX unique_schema_migrations ON schema_migrations USING btree (v
298
289
 
299
290
  SET search_path TO "$user",public;
300
291
 
301
- INSERT INTO schema_migrations (version) VALUES ('20131015022029');
302
-
303
- INSERT INTO schema_migrations (version) VALUES ('20131015022030');
292
+ INSERT INTO schema_migrations (version) VALUES ('20131018135639');
304
293
 
305
- INSERT INTO schema_migrations (version) VALUES ('20131015022031');
294
+ INSERT INTO schema_migrations (version) VALUES ('20131018135640');
306
295
 
307
- INSERT INTO schema_migrations (version) VALUES ('20131015022032');
296
+ INSERT INTO schema_migrations (version) VALUES ('20131018135641');
308
297
 
309
- INSERT INTO schema_migrations (version) VALUES ('20131015022033');
298
+ INSERT INTO schema_migrations (version) VALUES ('20131018135642');
310
299
 
311
- INSERT INTO schema_migrations (version) VALUES ('20131015022034');
300
+ INSERT INTO schema_migrations (version) VALUES ('20131018135643');
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: post_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Madsen and Martin Thoegersen
@@ -146,12 +146,11 @@ files:
146
146
  - spec/dummy/public/500.html
147
147
  - spec/dummy/public/422.html
148
148
  - spec/dummy/db/structure.sql
149
- - spec/dummy/db/migrate/20131015022029_enable_extensions.rb
150
- - spec/dummy/db/migrate/20131015022031_create_post_json_model_settings.rb
151
- - spec/dummy/db/migrate/20131015022030_create_procedures.rb
152
- - spec/dummy/db/migrate/20131015022034_create_post_json_dynamic_indexes.rb
153
- - spec/dummy/db/migrate/20131015022032_create_post_json_collections.rb
154
- - spec/dummy/db/migrate/20131015022033_create_post_json_documents.rb
149
+ - spec/dummy/db/migrate/20131018135641_create_post_json_model_settings.rb
150
+ - spec/dummy/db/migrate/20131018135643_create_post_json_dynamic_indexes.rb
151
+ - spec/dummy/db/migrate/20131018135639_enable_extensions.rb
152
+ - spec/dummy/db/migrate/20131018135640_create_procedures.rb
153
+ - spec/dummy/db/migrate/20131018135642_create_post_json_documents.rb
155
154
  - spec/modules/query_methods_spec.rb
156
155
  - spec/modules/argument_methods_spec.rb
157
156
  - MIT-LICENSE
@@ -1,28 +0,0 @@
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
@@ -1,16 +0,0 @@
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