mongo_ql 1.0.1 → 1.0.2
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 +4 -4
- data/README.md +6 -4
- data/lib/mongo_ql/collection_operators.rb +5 -1
- data/lib/mongo_ql/expression/condition.rb +35 -0
- data/lib/mongo_ql/expression.rb +4 -0
- data/lib/mongo_ql/stage_context.rb +5 -0
- data/lib/mongo_ql/version.rb +1 -1
- data/lib/mongo_ql.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2694eccc5339f8c810e6988339bf6faa9325d86518d810eb1f1d43645022e26
|
4
|
+
data.tar.gz: 605cf542eeae93fd97ae4135d5c62dd8b07abf8a6c593c2f415b1676071aa185
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7032e9dc8bce476e20345e0c3702f9563e778612914db1080b4c85022d2de8cc610a26c8cac4a2b4e4fc92dce1bade62498d25b36e9cf60bcab55e00d24be92
|
7
|
+
data.tar.gz: eba833dc7d70fee8958ea30812f01181ce943b9cce00a9d5b7663458ce5547dd874bc3aea907fa19719a72fabc08036899a2c559f9b6718a0490b28bd4612fbb
|
data/README.md
CHANGED
@@ -27,12 +27,14 @@ MongoQL.compose do
|
|
27
27
|
status == :shipped
|
28
28
|
end
|
29
29
|
|
30
|
-
where province == "ON"
|
30
|
+
where province == "ON",
|
31
|
+
discounts.any? { |d| d.price > 6 }
|
31
32
|
|
32
|
-
project
|
33
|
+
project _id,
|
33
34
|
total,
|
34
35
|
customer => customers.name,
|
35
|
-
tax => total * tax_rate
|
36
|
+
tax => total * tax_rate,
|
37
|
+
status => If price <= 10, "Not Bad", "Good"
|
36
38
|
|
37
39
|
group customer,
|
38
40
|
total => total.sum,
|
@@ -42,7 +44,7 @@ MongoQL.compose do
|
|
42
44
|
end
|
43
45
|
```
|
44
46
|
|
45
|
-
## The above aggregation
|
47
|
+
## The above aggregation DSL generates the following MongoDB pipeline
|
46
48
|
```json
|
47
49
|
[{
|
48
50
|
"$lookup": {
|
@@ -10,7 +10,8 @@ module MongoQL
|
|
10
10
|
"sum": "$sum",
|
11
11
|
"avg": "$avg",
|
12
12
|
"size": "$size",
|
13
|
-
"push": "$push"
|
13
|
+
"push": "$push",
|
14
|
+
"reverse": "$reverseArray"
|
14
15
|
}.freeze
|
15
16
|
|
16
17
|
AGGREGATE_OPS.keys.each do |op|
|
@@ -70,5 +71,8 @@ module MongoQL
|
|
70
71
|
alias_method :include?, :contains
|
71
72
|
alias_method :includes?, :contains
|
72
73
|
|
74
|
+
def any?(&block)
|
75
|
+
if_null([]).filter(&block).size > 0
|
76
|
+
end
|
73
77
|
end
|
74
78
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MongoQL
|
4
|
+
class Expression::Condition < Expression
|
5
|
+
attr_accessor :condition, :then_expr, :else_expr
|
6
|
+
|
7
|
+
def initialize(condition, then_val = nil, else_val = nil, &block)
|
8
|
+
@condition = condition
|
9
|
+
@then_expr = then_val
|
10
|
+
@else_expr = else_val
|
11
|
+
|
12
|
+
@then_expr = yield if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def then(then_val = nil, &block)
|
16
|
+
@then_expr = then_val
|
17
|
+
@then_expr = yield if block_given?
|
18
|
+
self
|
19
|
+
end
|
20
|
+
alias_method :Then, :then
|
21
|
+
|
22
|
+
def else(else_val = nil, &block)
|
23
|
+
@else_expr = else_val
|
24
|
+
@else_expr = yield if block_given?
|
25
|
+
self
|
26
|
+
end
|
27
|
+
alias_method :Else, :else
|
28
|
+
|
29
|
+
def to_ast
|
30
|
+
{
|
31
|
+
"$cond" => [condition, then_expr, else_expr]
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/mongo_ql/expression.rb
CHANGED
@@ -52,6 +52,10 @@ module MongoQL
|
|
52
52
|
Expression::DateNode.new(self)
|
53
53
|
end
|
54
54
|
|
55
|
+
def then(then_expr = nil, &block)
|
56
|
+
Expression::Condition.new(self, then_expr, nil, &block)
|
57
|
+
end
|
58
|
+
|
55
59
|
def to_ast
|
56
60
|
raise NotImplementedError, "#{self.class.name} must implement to_ast"
|
57
61
|
end
|
@@ -60,6 +60,11 @@ module MongoQL
|
|
60
60
|
Expression::ValueNode.new(val)
|
61
61
|
end
|
62
62
|
|
63
|
+
def cond(conditon_expr, then_expr = nil, else_expr = nil, &block)
|
64
|
+
Expression::Condition.new(conditon_expr, then_expr, else_expr, &block)
|
65
|
+
end
|
66
|
+
alias_method :If, :cond
|
67
|
+
|
63
68
|
def to_ast
|
64
69
|
pipeline.map(&:to_ast)
|
65
70
|
end
|
data/lib/mongo_ql/version.rb
CHANGED
data/lib/mongo_ql.rb
CHANGED
@@ -54,6 +54,7 @@ require_relative "mongo_ql/expression/value_node"
|
|
54
54
|
require_relative "mongo_ql/expression/method_call"
|
55
55
|
require_relative "mongo_ql/expression/binary"
|
56
56
|
require_relative "mongo_ql/expression/unary"
|
57
|
+
require_relative "mongo_ql/expression/condition"
|
57
58
|
|
58
59
|
require_relative "mongo_ql/expression/descend"
|
59
60
|
require_relative "mongo_ql/expression/ascend"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_ql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xizheng Ding
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/mongo_ql/expression.rb
|
65
65
|
- lib/mongo_ql/expression/ascend.rb
|
66
66
|
- lib/mongo_ql/expression/binary.rb
|
67
|
+
- lib/mongo_ql/expression/condition.rb
|
67
68
|
- lib/mongo_ql/expression/date_note.rb
|
68
69
|
- lib/mongo_ql/expression/descend.rb
|
69
70
|
- lib/mongo_ql/expression/field_node.rb
|