ndb-rails 1.19.0 → 1.20.1

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
  SHA256:
3
- metadata.gz: 25e6a965ea121ec28f1111ab71a468f8a39f181c9576e75f2ef77c4d426eecfb
4
- data.tar.gz: 4a580bf13a810470ed2bcb8a316d9c0c2b839c1a0b11346f9d6791d3b06fc541
3
+ metadata.gz: bdf21a9a55f1aa4d937fa5ec48819a2ebd9e6a045c80d83211b8c1d79b906421
4
+ data.tar.gz: c8085d974bcef8b106d341b56757cfbc8c8c69ffe50433db8d0470e35862fe30
5
5
  SHA512:
6
- metadata.gz: 80b1e74f3e9995ed5e3b445fa1e070bbf4298650f3d49a6b3cc176a3af6387c3cb4d8b6494db240584645993eb0ce3db90ac37b4ed5e113a3e3440c698a1df20
7
- data.tar.gz: 2c021ef1b4032469d5cd8b5b2a0ca301deb3832bca610c0857dccc8593d5747de455da934c9db444d03d9a6fc580bf13dd1a22c5a0583608e3b767e4dafe8026
6
+ metadata.gz: cfd6d52bb2c4a90d5a547e03723fff82a65f61d0b4df47e33c17b464c2f058290b32b80662a59d9f2f6628434c6c60cc1cc1e70062292ed43ea84c60ddc293f8
7
+ data.tar.gz: 87aa236616dc7ff6baad3da31ad91e24386f7fb4a9147072f33c0a544dc26a0d6e5db302c8a6afed16aef8aa856764354386599662504e9c2d8023c6415cf1d1
@@ -26,7 +26,7 @@ module Ndb
26
26
 
27
27
  class << self
28
28
  QUERY_METHODS = %i[
29
- where where_raw order limit offset page after group includes lock find find_by find_by! first last count pluck exists?
29
+ where where_raw order limit offset page after group unwind includes lock find find_by find_by! first last count pluck exists?
30
30
  find_each in_batches sum average minimum maximum to_a each destroy_all delete_all to_ndb
31
31
  ].freeze
32
32
 
@@ -177,6 +177,22 @@ module Ndb
177
177
  @ttl || (superclass.respond_to?(:ttl) ? superclass.ttl : nil)
178
178
  end
179
179
 
180
+ # An index on 1 to 4 attributes or paths. With one it is `index: true`
181
+ # as a call, for a path that is not an attribute ("items.sku"). With
182
+ # more it is a compound index, in this order: it serves a where with
183
+ # equality on the first attributes and a range on the next one. With
184
+ # unique: true it is a unique key instead (one live document per value
185
+ # or tuple of values; Ndb::RecordNotUnique).
186
+ #
187
+ # index :account_id, :created_at
188
+ # index :account_id, :email, unique: true
189
+ # index "items.sku"
190
+ def index(*names, unique: false)
191
+ raise ArgumentError, "index takes 1 to 4 attributes" unless (1..4).cover?(names.size)
192
+
193
+ (unique ? own_unique : own_indexed) << names.join(",")
194
+ end
195
+
180
196
  def indexed_fields
181
197
  inherited_list(:indexed_fields) | own_indexed
182
198
  end
@@ -91,6 +91,16 @@ module Ndb
91
91
  with(group: fields.flatten.map(&:to_s))
92
92
  end
93
93
 
94
+ # Makes the aggregates of the relation (count, sum, average, minimum,
95
+ # maximum, with or without group) read one row per element of an array
96
+ # attribute, as $unwind: `Order.unwind(:items).group("items.sku")
97
+ # .sum("items.qty")`. A document with no element gives no row. It is for
98
+ # the aggregates only: reading the records of such a relation raises
99
+ # ArgumentError.
100
+ def unwind(field)
101
+ with(unwind: field.to_s)
102
+ end
103
+
94
104
  # The JSON body that this relation sends to the daemon, as a String,
95
105
  # with no request (like to_sql of ActiveRecord). It is the body of
96
106
  # POST /{collection}/_search; pretty: true gives it on many lines.
@@ -105,6 +115,8 @@ module Ndb
105
115
 
106
116
  # The find body: JSON only, the daemon decides what it means.
107
117
  def to_query
118
+ raise ArgumentError, "unwind is for count, sum, average, minimum and maximum; it does not change the records of a relation" if values[:unwind]
119
+
108
120
  query = {}
109
121
  where = Conditions.combine(values[:where])
110
122
 
@@ -270,7 +282,7 @@ module Ndb
270
282
  end
271
283
 
272
284
  def count
273
- return calculate(:count) unless values[:group].empty?
285
+ return calculate(:count) unless values[:group].empty? && values[:unwind].nil?
274
286
 
275
287
  klass.ndb_find(to_query.slice("query").merge("count" => true, "limit" => 1, "fields" => [klass.primary_key])).total
276
288
  end
@@ -314,6 +326,7 @@ module Ndb
314
326
  where = Conditions.combine(values[:where])
315
327
 
316
328
  stages << { "$match" => where } unless where.empty?
329
+ stages << { "$unwind" => "$#{values[:unwind]}" } if values[:unwind]
317
330
  stages << { "$group" => { "_id" => group_id, op.to_s => accumulator(op, field) } }
318
331
 
319
332
  { "pipeline" => stages }
@@ -409,7 +422,7 @@ module Ndb
409
422
 
410
423
  return rows.to_h { |row| [group_key(row["_id"]), row[key]] } unless values[:group].empty?
411
424
 
412
- rows.first&.fetch(key) || (op == :sum ? 0 : nil)
425
+ rows.first&.fetch(key) || (%i[sum count].include?(op) ? 0 : nil)
413
426
  end
414
427
 
415
428
  def group_key(id)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ndb
4
4
  module Rails
5
- VERSION = "1.19.0"
5
+ VERSION = "1.20.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndb-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.19.0
4
+ version: 1.20.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thadeu
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 1.19.0
46
+ version: 1.20.1
47
47
  - - "<"
48
48
  - !ruby/object:Gem::Version
49
49
  version: '2'
@@ -53,7 +53,7 @@ dependencies:
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 1.19.0
56
+ version: 1.20.1
57
57
  - - "<"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '2'