mongo_ql 1.0.0 → 1.0.1

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
  SHA256:
3
- metadata.gz: e1f6676de369add0c89989cb95013bd170cc5925959f50b74ee1a7824e148b30
4
- data.tar.gz: 0cd46af4aa9fa2b9a98e10e6ba7532c3aef216e3ca2d3af983b739db2fec2237
3
+ metadata.gz: 1b2d85bf237fe01d81f3c4f07053f2f536220bee0142678693be766e9f1f4caf
4
+ data.tar.gz: 783abc9d2dc5ca65fed6064ee10978f7cd8d59faa41dfe58f9fb4d57b847fed7
5
5
  SHA512:
6
- metadata.gz: cc76501d7a5e6e1a13a58fd4164de221f5038afd7006aa7cd8b7b1ec2b4792d278e2797b2d4d0daa117779dd63b4bf34a8c7d5c8b9d110d0ec1c8f3bedde5fd2
7
- data.tar.gz: 2ec8a62fd293bcd9fc8d266424f18a3206db5be9a8e16ebcb4eb2d9e2679bfbab39e54fd64287440f6235463989a5bae220976dad4ee615b89c5b86d8b29ebb7
6
+ metadata.gz: b57f65ea24dcad07a359ae7ed500f5be06679495b1ce9b0c3d67b57d25188fb1a34de044997b55b8a4fbc37b5c9de59ab66cd6da5c73148e2fd3326c370ffd4c
7
+ data.tar.gz: 13e6bd11d96fa929b6ce186ddc21457c570cbc4a958e03b383c1a5f3a97b86df5fd2e7d634ac8553addd01b7f7d3dfc6b279e5e75628abb24c6b7bd19aaed48f
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - name: Set up Ruby 2.6
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+ - name: Build and test with Rake
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+ gemspec
data/README.md CHANGED
@@ -1,33 +1,35 @@
1
+ # MongoQL
2
+ [![Gem](https://img.shields.io/gem/v/mongo_ql.svg?style=flat)](http://rubygems.org/gems/mongo_ql "View this project in Rubygems")
3
+ [![Actions Status](https://github.com/dingxizheng/mongo_ql/workflows/Ruby/badge.svg)](https://github.com/dingxizheng/mongo_ql/actions)
4
+ [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
5
+
6
+ ## Installation
7
+ Install from RubyGems by adding it to your `Gemfile`, then bundling.
1
8
 
2
- # Query DSL
3
9
  ```ruby
4
- Order.where { tax != 0 }
5
- #=> Order.where(tax: { "$ne": 0 })
6
- Order.where { total >= 100 }
7
- #=> Order.where(total: { "$gte": 100 })
8
- Order.where { (total >= 100) & (total_tax < 15) }
9
- #=> Order.where({ "$and": [{ total: { "$gte": 100 }}, { total_tax: { "$lt": 15 }}]})
10
- Order.where { tax > (shipping / 2) }
11
- #=> Order.where(tax: { "$gt": { "$divide": [ "$shipping", 2]}})
12
- Order.where { total >= If(currency == "CAD", 100, 80) }
13
- #=> Order.where(total: { "$cond": { if: { "$eq": ["$currency", "CAD"]}, then: 100, else: 80 }})
10
+ # Gemfile
11
+ gem 'mongo_ql'
12
+ ```
13
+
14
+ ```
15
+ $ bundle install
14
16
  ```
15
17
 
16
- # Aggregation Pipeline DSL
18
+ ## Aggregation Pipeline DSL
17
19
  ```ruby
18
- Order.all.mongo_ql do
19
- join Customer,
20
+ MongoQL.compose do
21
+ lookup customers,
20
22
  on: customer_id == _id.to_id,
21
23
  as: customers
22
24
 
23
- join Shipping, :as => shippings do |doc|
25
+ lookup shippings, :as => shippings do |doc|
24
26
  match order_id == doc._id,
25
27
  status == :shipped
26
28
  end
27
29
 
28
- match province == "ON"
30
+ where province == "ON"
29
31
 
30
- project _id,
32
+ project :_id,
31
33
  total,
32
34
  customer => customers.name,
33
35
  tax => total * tax_rate
@@ -36,47 +38,72 @@ Order.all.mongo_ql do
36
38
  total => total.sum,
37
39
  total_tax => tax.sum * 5
38
40
 
39
- sort_by age.desc
41
+ sort age.dsc
40
42
  end
43
+ ```
41
44
 
42
- # The above aggregation is equivalent to the following mognodb pipeline
43
- Order.collection.pipeline([
44
- { "$lookup": {
45
- from: "customers",
46
- localField: "$customer_id",
47
- foreignField: "$_id",
48
- as: "customers"
49
- }},
50
- { "$unwind": {
51
- path: "customers"
52
- }},
53
- { "$lookup": {
54
- from: "shippings",
55
- as: "shippings",
56
- let: { doc_id: "$_id" },
57
- pipeline: [{
45
+ ## The above aggregation is equivalent to the following mognodb pipeline
46
+ ```json
47
+ [{
48
+ "$lookup": {
49
+ "from": "customers",
50
+ "as": "customers",
51
+ "localField": "customer_id",
52
+ "foreignField": {
53
+ "$toString": {
54
+ "$toObjectId": "$_id"
55
+ }
56
+ }
57
+ }
58
+ }, {
59
+ "$lookup": {
60
+ "from": "shippings",
61
+ "as": "shippings",
62
+ "pipeline": [{
58
63
  "$match": {
59
- order_id: { "$eq": "$$dock_id" },
60
- status: :shipped
64
+ "$expr": {
65
+ "$and": [{
66
+ "$eq": ["$order_id", "$$var__id"]
67
+ }, {
68
+ "$eq": ["$status", "shipped"]
69
+ }]
70
+ }
61
71
  }
62
- }]
63
- }},
64
- { "$unwind": {
65
- path: "customers"
66
- }},
67
- { "$match": {
68
- province: { "$eq": "ON" }
69
- }},
70
- { "$project": {
71
- _id: 1,
72
- total: 1,
73
- customer: "$customers.name",
74
- tax: { "$multiply": ["$total", "$tax_rate"] }
75
- }},
76
- { "$group": {
77
- _id: "$customer",
78
- total: { "$sum": "$total" },
79
- total_tax: { "$multiply": [{ "$sum": "$tax" }, 5] }
80
- }}
81
- ])
72
+ }],
73
+ "let": {
74
+ "var__id": "$_id"
75
+ }
76
+ }
77
+ }, {
78
+ "$match": {
79
+ "$expr": {
80
+ "$eq": ["$province", "ON"]
81
+ }
82
+ }
83
+ }, {
84
+ "$project": {
85
+ "_id": 1,
86
+ "total": 1,
87
+ "customer": "customers",
88
+ "tax": {
89
+ "$multiply": ["$total", "$tax_rate"]
90
+ }
91
+ }
92
+ }, {
93
+ "$group": {
94
+ "_id": "$customer",
95
+ "total": {
96
+ "$sum": "$total"
97
+ },
98
+ "total_tax": {
99
+ "$multiply": [{
100
+ "$sum": "$tax"
101
+ }, 5]
102
+ }
103
+ }
104
+ }, {
105
+ "$sort": {
106
+ "age": -1
107
+ }
108
+ }]
82
109
  ```
@@ -32,6 +32,12 @@ module MongoQL
32
32
  }
33
33
  end
34
34
 
35
+ def concat_arrays(*expressions)
36
+ Expression::MethodCall.new "$concatArrays", self, ast_template: -> (target, **_args) {
37
+ [target, *expressions]
38
+ }
39
+ end
40
+
35
41
  def map(&block)
36
42
  evaled_in = block.call(Expression::FieldNode.new("$item"))
37
43
  Expression::MethodCall.new "$map", self, ast_template: -> (target, **_args) {
@@ -17,7 +17,7 @@ module MongoQL
17
17
  end
18
18
 
19
19
  def to_ast
20
- value
20
+ value.is_a?(MongoQL::Expression::ValueNode) ? value.to_ast : value
21
21
  end
22
22
 
23
23
  def self.valid?(value)
@@ -14,7 +14,7 @@ module MongoQL
14
14
  def to_ast
15
15
  ast = {
16
16
  "$group" => {
17
- "_id" => by.to_ast,
17
+ "_id" => by,
18
18
  }.merge(fields)
19
19
  }
20
20
  MongoQL::Utils.deep_transform_values(ast, &MongoQL::EXPRESSION_TO_AST_MAPPER)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MongoQL
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
data/mongo_ql.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ["Xizheng Ding"]
11
11
  spec.email = ["dingxizheng@gamil.com"]
12
12
 
13
- spec.summary = "A DSL for building mongo query in a more natual way"
13
+ spec.summary = "A DSL for building MongoDB query in a more natual way"
14
14
  spec.homepage = "https://github.com/dingxizheng/mongo_ql"
15
15
  spec.license = "MIT"
16
16
 
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.0
4
+ version: 1.0.1
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-25 00:00:00.000000000 Z
11
+ date: 2019-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -45,13 +45,14 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - ".github/workflows/ruby.yml"
48
49
  - ".gitignore"
49
50
  - ".rubocop.yml"
50
51
  - ".vscode/launch.json"
52
+ - Gemfile
51
53
  - LICENSE.txt
52
54
  - README.md
53
55
  - Rakefile
54
- - Untitled-1
55
56
  - bin/console
56
57
  - bin/setup
57
58
  - debug.sh
@@ -106,5 +107,5 @@ requirements: []
106
107
  rubygems_version: 3.0.3
107
108
  signing_key:
108
109
  specification_version: 4
109
- summary: A DSL for building mongo query in a more natual way
110
+ summary: A DSL for building MongoDB query in a more natual way
110
111
  test_files: []
data/Untitled-1 DELETED
@@ -1,4 +0,0 @@
1
- include MongoQL
2
- define_singleton_method(:method_missing) do |m, *args, &block|
3
- MongoQL::Expression::FieldNode.new(m)
4
- end