mongery 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9aba484357e3a8ce7540342ebffe714395965f9e
4
- data.tar.gz: af67b49c495afd48ed3fa88484b4b8910789d313
3
+ metadata.gz: 1c2ea2dd29e75e89c48aae9e0aa1614a8896625e
4
+ data.tar.gz: eeb65b09d38eb4ace06422f046caa6657324ec6e
5
5
  SHA512:
6
- metadata.gz: 59a7ca1ac35849be2d4fd0ca2a980b0178c2a0f1f94bc5c9df5a0674f979dc465ecc39df058e04047377cf1d99d3ea9dc00b10bcf403fc39debf88bf2f9d3f24
7
- data.tar.gz: 5f770ea95067208df3072d4b4b87dbe3bb72d4e78afea96b961f9c1a885e7422272f764622cacb16160f67e0f915ddba9a7db9bd1c20c28832419a3cf46cf94a
6
+ metadata.gz: a2f38853f1ffd8fe79824dc01efa935dfd4e253399e9f8324b403a93c6a0fa446bf2e0133f93cf0d7f04dd4fa74bf5338301401dfdb1906d4e2aa57cb84716f1
7
+ data.tar.gz: e56c8c81a04e7c55bc6e21dcdb5343719e760e43e3843f51e73f555a9e8be921ce351cd599c8b818dd32a12668fea3fa05e93f70a88bf9ea7e31c6c67bae3b33
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.0.4 (2014/10/23)
2
+ - Support insert(), update() and delete() for more compatibilities
3
+
1
4
  ## 0.0.3 (2014/10/22)
2
5
  - Fix a quotation bug
3
6
 
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Mongery
2
2
 
3
- Mongery helps you to migrate off of MongoDB object storage to PostgreSQL with JSON column by translating Mongo query into Arel object, ready to use with ActiveRecord connection.
3
+ Mongery translates MongoDB query to Arel AST for Postgres with JSON columns, ready to use with ActiveRecord connection.
4
+
5
+ This could be useful in situations where you migrate off of MongoDB backend to Postgres without touching the application code, or to provide MongoDB query syntax as a JSON API to your Postgres backend.
4
6
 
5
7
  ## Limitation
6
8
 
@@ -22,6 +24,8 @@ CREATE TABLE objects (
22
24
 
23
25
  Currently Mongery assumes the `id` values are stored as `_id` key duplicated in the json data as well. This can be customized in the future updates.
24
26
 
27
+ ### Selects
28
+
25
29
  ```ruby
26
30
  builder = Mongery::Builder.new(:objects)
27
31
 
@@ -32,6 +36,25 @@ builder.find({ age: {"$gte" => 21 } }).sort({ name: -1 }).to_sql
32
36
  # => SELECT data FROM objects WHERE (data->>'age')::integer >= 21 ORDER BY data->>'name' DESC
33
37
  ```
34
38
 
39
+ ### Inserts, Updates and Deletes
40
+
41
+ ```ruby
42
+ builder = Mongery::Builder.new(:objects)
43
+
44
+ builder.insert({ _id: 'foobar' }).to_sql
45
+ # => INSERT INTO "objects" ("id", "data") VALUES ('foobar', '{"_id":"foobar"}')
46
+
47
+ builder.find({ age: {"$gte" => 21 } }).update({ name: "John" }).to_sql
48
+ # => UPDATE "objects" SET "data" = '{"name":"John"}' WHERE (data->>'age')::integer >= 21
49
+
50
+ builder.find({ age: {"$eq" => 55 } }).delete.to_sql
51
+ # => DELETE FROM "objects" WHERE (data->>'age')::integer = 21
52
+
53
+ ## See Also
54
+
55
+ * [mosql](https://github.com/stripe/mosql)
56
+ * [mongres](https://github.com/umitanuki/mongres)
57
+
35
58
  ## License
36
59
 
37
60
  MIT
data/lib/mongery.rb CHANGED
@@ -12,8 +12,12 @@ module Mongery
12
12
  @table = Arel::Table.new(model, engine)
13
13
  end
14
14
 
15
- def find(args)
16
- Query.new(table).where(args)
15
+ def find(*args)
16
+ Query.new(table).where(*args)
17
+ end
18
+
19
+ def insert(*args)
20
+ Query.new(table).insert(*args)
17
21
  end
18
22
  end
19
23
 
@@ -22,16 +26,16 @@ module Mongery
22
26
 
23
27
  def initialize(table)
24
28
  @table = table
29
+ @condition = nil
25
30
  end
26
31
 
27
32
  def where(args)
28
- condition = translate(args)
29
- arel.where(condition) if condition
33
+ @where = args
30
34
  self
31
35
  end
32
36
 
33
37
  def arel
34
- @arel ||= table.project(table[:data])
38
+ @arel ||= build_arel
35
39
  end
36
40
 
37
41
  def to_arel
@@ -65,8 +69,40 @@ module Mongery
65
69
  self
66
70
  end
67
71
 
72
+ def insert(args)
73
+ Arel::InsertManager.new(table.engine).tap do |manager|
74
+ manager.into(table)
75
+ manager.insert([[table[:id], args[:_id]], [table[:data], args.to_json]])
76
+ end
77
+ end
78
+
79
+ def update(args)
80
+ Arel::UpdateManager.new(table.engine).tap do |manager|
81
+ manager.table(table)
82
+ manager.set([[table[:data], args.to_json]])
83
+ manager.where(condition) if condition
84
+ end
85
+ end
86
+
87
+ def delete
88
+ Arel::DeleteManager.new(table.engine).tap do |manager|
89
+ manager.from(table)
90
+ manager.where(condition) if condition
91
+ end
92
+ end
93
+
68
94
  private
69
95
 
96
+ def condition
97
+ @condition ||= translate(@where)
98
+ end
99
+
100
+ def build_arel
101
+ table.project(table[:data]).tap do |t|
102
+ t.where(condition) if condition
103
+ end
104
+ end
105
+
70
106
  def translate(query)
71
107
  chain(:and, query.map { |col, value| translate_cv(col, value) })
72
108
  end
@@ -1,3 +1,3 @@
1
1
  module Mongery
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -60,10 +60,12 @@ describe Mongery::Builder do
60
60
  /WHERE \(data->>'ids' ILIKE '%"foo"%' OR data->>'ids' ILIKE '%"bar"%'\)$/ ],
61
61
  ]
62
62
 
63
+ builder = Mongery::Builder.new(:test)
64
+
63
65
  tests.each do |query, condition, sql|
64
66
  context "with query #{query}" do
65
67
  subject do
66
- Mongery::Builder.new(:test).find(query).tap { |q|
68
+ builder.find(query).tap { |q|
67
69
  condition.each do |method, value|
68
70
  q.send(method, value)
69
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuhiko Miyagawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-22 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: arel