ndb-rails 0.4.0 → 1.1.0
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 +28 -7
- data/lib/ndb/rails/persistence.rb +2 -2
- data/lib/ndb/rails/record.rb +44 -10
- data/lib/ndb/rails/relation.rb +4 -4
- data/lib/ndb/rails/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c0f0729033c1ef26134978cde25930469278ab40902c140807e619e89fefb9a
|
|
4
|
+
data.tar.gz: '0408d4c1b4416a615fa6e64ca225f14fee91c91c7db8341f227d55fdc1268ea7'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85db63acec6ef42aaef8988edf08f20df0b2f3c3703b154490bc5528566015fe98e1ee8dc68d0898d973bb74c46358840f45bd7f7c44497967e16717cb2a2852
|
|
7
|
+
data.tar.gz: c3f1faf960f387bb93f40eadcac39a5ddf088a9ac674e8055c88db8c45960346aab4af4d52811781171b7b1601f31c9331ceaa3822d6fd386029163463e45db0
|
data/README.md
CHANGED
|
@@ -6,7 +6,7 @@ ActiveModel layer for [ndb](https://github.com/thadeu/ndb), an NDJSON query daem
|
|
|
6
6
|
|
|
7
7
|
No Rails, or no models? Use the gem `ndb` alone: it has no runtime dependency.
|
|
8
8
|
|
|
9
|
-
Docs: [client guide](https://github.com/thadeu/ndb/blob/main/docs/dev/clients.md), [query language](https://github.com/thadeu/ndb/blob/main/docs/dev/query.md), [HTTP API](https://github.com/thadeu/ndb/blob/main/docs/dev/http
|
|
9
|
+
Docs: [client guide](https://github.com/thadeu/ndb/blob/main/docs/dev/clients.md), [query language](https://github.com/thadeu/ndb/blob/main/docs/dev/query.md), [HTTP API](https://github.com/thadeu/ndb/blob/main/docs/dev/http.md).
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -102,6 +102,9 @@ end
|
|
|
102
102
|
call = Call.create!(_id: "c1", status: "answered", caller: "a", at: Time.now.utc)
|
|
103
103
|
Call.create!([{ _id: "c2", status: "failed", caller: "b" }, { _id: "c3", status: "answered", caller: "c" }])
|
|
104
104
|
|
|
105
|
+
Call.upsert(id: "c2", status: "answered") # create by id, or merge the given attributes; no callbacks
|
|
106
|
+
Call.upsert_all([{ id: "c2", status: "done" }, { id: "c5", status: "new" }])
|
|
107
|
+
|
|
105
108
|
call = Call.new(_id: "c4", status: "answered", caller: "d")
|
|
106
109
|
call.save # false when invalid
|
|
107
110
|
call.save! # raises Ndb::RecordInvalid
|
|
@@ -213,7 +216,25 @@ Call.where(status: "failed").exists?
|
|
|
213
216
|
- The order of the `order` keys is the order of the sort. The id is always the last key.
|
|
214
217
|
- `page(n).per(size)` sets offset paging. The default page size is 25.
|
|
215
218
|
- `pluck` asks the daemon for only those fields.
|
|
216
|
-
- There is no `explain` on a relation. Use the client: `Ndb::Rails.client.
|
|
219
|
+
- There is no `explain` on a relation. Use the client: `Ndb::Rails.client.search(Call.collection_name, "query" => {...}, "explain" => true)` returns a `Page` with `plan` (the `X-Ndb-Plan` header: `id`, `index:<field>`, `scan`, `snapshot`) and `examined`.
|
|
220
|
+
|
|
221
|
+
### A query built elsewhere
|
|
222
|
+
|
|
223
|
+
A query from a search form or a saved search goes to `where_raw`. Its Hash is sent as it is, with no Rails conventions (an Array stays an Array, not `$in`), ANDed with the other clauses:
|
|
224
|
+
|
|
225
|
+
```ruby
|
|
226
|
+
Call.where_raw("status" => "answered", "at" => { "$gte" => "now-7d" }).order(at: :desc).limit(50).to_a
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
For a whole search body, use `Model.search`. The filter goes in `"query"`:
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
result = Call.search("query" => { "status" => "answered" }, "sort" => { "at" => -1 }, "limit" => 50, "count" => true)
|
|
233
|
+
|
|
234
|
+
result.records # Call instances
|
|
235
|
+
result.next_cursor # nil on the last page
|
|
236
|
+
result.total # with "count" => true
|
|
237
|
+
```
|
|
217
238
|
|
|
218
239
|
### Regular expressions
|
|
219
240
|
|
|
@@ -289,7 +310,7 @@ page = Ndb::Rails.client.aggregate(Call.collection_name,
|
|
|
289
310
|
|
|
290
311
|
page.rows # array of Hash
|
|
291
312
|
page.total # with count: true
|
|
292
|
-
page.
|
|
313
|
+
page.next # the cursor of the next page, nil on the last one
|
|
293
314
|
```
|
|
294
315
|
|
|
295
316
|
`$dateTrunc` units are `minute`, `hour`, `day`, `week` and `month`. Optional `"timezone"`: a name such as `"Europe/Rome"`.
|
|
@@ -302,8 +323,8 @@ A watch streams the changes of a collection to a named consumer: first the event
|
|
|
302
323
|
client = Ndb::Rails.client
|
|
303
324
|
|
|
304
325
|
begin
|
|
305
|
-
client.watch(Call.collection_name, "relay",
|
|
306
|
-
# event has "pos", "op" ("put", "del" or "truncate"), "_id", "
|
|
326
|
+
client.watch(Call.collection_name, "relay", query: { status: "answered" }) do |event|
|
|
327
|
+
# event has "pos", "op" ("put", "del" or "truncate"), "_id", "_v" and "doc"
|
|
307
328
|
handle(event)
|
|
308
329
|
|
|
309
330
|
client.ack(Call.collection_name, "relay", event["pos"])
|
|
@@ -315,7 +336,7 @@ rescue Ndb::HistoryTruncated
|
|
|
315
336
|
end
|
|
316
337
|
```
|
|
317
338
|
|
|
318
|
-
- `
|
|
339
|
+
- `query:` takes the same conditions as `where` on a model (Hash and Range). Only matching events come. A delete event has no `"doc"`; it is sent only if this stream saw an earlier matching put of that id.
|
|
319
340
|
- `from: "now"` starts a new consumer at the end. `limit: n` ends the stream after n events.
|
|
320
341
|
- Without a block, `watch` returns an Enumerator.
|
|
321
342
|
- Delivery is at least once. After a restart an event can come again, so make the work safe to repeat.
|
|
@@ -376,7 +397,7 @@ rescue Ndb::RecordNotUnique => e
|
|
|
376
397
|
|
|
377
398
|
## Testing
|
|
378
399
|
|
|
379
|
-
The tests of this gem build the daemon from the repository (`go build ./cmd/ndb`), start it on a short temp unix socket (macOS allows 104 bytes), wait for `GET /
|
|
400
|
+
The tests of this gem build the daemon from the repository (`go build ./cmd/ndb`), start it on a short temp unix socket (macOS allows 104 bytes), wait for `GET /_health` to return 200, and stop it at exit. To do the same by hand:
|
|
380
401
|
|
|
381
402
|
```sh
|
|
382
403
|
ndb -config /nonexistent.yml -data-dir "$(mktemp -d)" -unix /tmp/ndb-test.sock
|
|
@@ -205,7 +205,7 @@ module Ndb
|
|
|
205
205
|
end
|
|
206
206
|
|
|
207
207
|
def refresh_expiry(result, expires_at)
|
|
208
|
-
@version = result["
|
|
208
|
+
@version = result["_v"]
|
|
209
209
|
|
|
210
210
|
field = self.class.expires_field
|
|
211
211
|
|
|
@@ -282,7 +282,7 @@ module Ndb
|
|
|
282
282
|
result = self.class.ndb_insert(insert_document, expire_in: @expire_in)
|
|
283
283
|
|
|
284
284
|
self.id = result["_id"] if id.nil?
|
|
285
|
-
loaded!(result["
|
|
285
|
+
loaded!(result["_v"])
|
|
286
286
|
|
|
287
287
|
true
|
|
288
288
|
end
|
data/lib/ndb/rails/record.rb
CHANGED
|
@@ -19,7 +19,7 @@ module Ndb
|
|
|
19
19
|
attribute :_id, :string
|
|
20
20
|
|
|
21
21
|
# The answer of Model.search.
|
|
22
|
-
SearchResult =
|
|
22
|
+
SearchResult = Data.define(:records, :next_cursor, :total)
|
|
23
23
|
|
|
24
24
|
class << self
|
|
25
25
|
QUERY_METHODS = %i[
|
|
@@ -39,8 +39,30 @@ module Ndb
|
|
|
39
39
|
Relation.new(self)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
# Writes a document by its _id, like ActiveRecord upsert: it creates
|
|
43
|
+
# the document when the _id is free, and merges the given attributes
|
|
44
|
+
# into the stored document when it exists (the others stay). No
|
|
45
|
+
# validations or callbacks run. id is an alias of _id. Returns
|
|
46
|
+
# {"_id", "_v"}.
|
|
47
|
+
#
|
|
48
|
+
# User.upsert(id: "u1", plan: "pro")
|
|
49
|
+
def upsert(attributes = nil, expire_in: nil, **attrs)
|
|
50
|
+
upsert_all([attributes || attrs], expire_in: expire_in).first
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# upsert for many documents in one transaction, all or nothing.
|
|
54
|
+
def upsert_all(attributes, expire_in: nil)
|
|
55
|
+
ensure_collection!
|
|
56
|
+
|
|
57
|
+
docs = attributes.map { |attrs| upsert_document(attrs) }
|
|
58
|
+
|
|
59
|
+
expire_in = expire_in.to_i if expire_in.is_a?(ActiveSupport::Duration)
|
|
60
|
+
|
|
61
|
+
Rails.client.bulk(collection_name, docs, expire_in: expire_in, upsert: true)
|
|
62
|
+
end
|
|
63
|
+
|
|
42
64
|
# Runs a find with a whole body built elsewhere, such as from a search
|
|
43
|
-
# form: { "
|
|
65
|
+
# form: { "query" => {...}, "sort" => {...}, "limit" => 50, "after" =>
|
|
44
66
|
# token, "count" => true }. The body is sent as is (the daemon
|
|
45
67
|
# validates it). Returns the records, the next cursor (nil on the last
|
|
46
68
|
# page) and the total (with "count" => true).
|
|
@@ -49,7 +71,7 @@ module Ndb
|
|
|
49
71
|
|
|
50
72
|
page = ndb_find(body.transform_keys(&:to_s))
|
|
51
73
|
|
|
52
|
-
SearchResult.new(page.rows.map { |row| instantiate(row) }, page.
|
|
74
|
+
SearchResult.new(page.rows.map { |row| instantiate(row) }, page.next, page.total)
|
|
53
75
|
end
|
|
54
76
|
|
|
55
77
|
# Finds the record with these attributes, or builds an unsaved one.
|
|
@@ -200,7 +222,7 @@ module Ndb
|
|
|
200
222
|
def ndb_find(body)
|
|
201
223
|
ensure_collection!
|
|
202
224
|
|
|
203
|
-
Rails.client.
|
|
225
|
+
Rails.client.search(collection_name, body.merge("versions" => true))
|
|
204
226
|
end
|
|
205
227
|
|
|
206
228
|
def ndb_aggregate(body)
|
|
@@ -209,23 +231,35 @@ module Ndb
|
|
|
209
231
|
Rails.client.aggregate(collection_name, body)
|
|
210
232
|
end
|
|
211
233
|
|
|
234
|
+
# The given attributes, cast by their type, with id as _id.
|
|
235
|
+
def upsert_document(attrs)
|
|
236
|
+
attrs.to_h.each_with_object({}) do |(name, value), doc|
|
|
237
|
+
key = name.to_s == "id" ? primary_key : name.to_s
|
|
238
|
+
type = attribute_types[key]
|
|
239
|
+
|
|
240
|
+
doc[key] = Conditions.json_value(type ? type.serialize(type.cast(value)) : value)
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
212
244
|
def ndb_insert(doc, expire_in: nil)
|
|
213
245
|
ensure_collection!
|
|
214
246
|
|
|
215
|
-
Rails.client.insert(collection_name,
|
|
247
|
+
return Rails.client.insert(collection_name, doc) unless expire_in
|
|
248
|
+
|
|
249
|
+
Rails.client.bulk(collection_name, [doc], expire_in: expire_in).first
|
|
216
250
|
end
|
|
217
251
|
|
|
218
|
-
# Atomic update by
|
|
219
|
-
def ndb_update(
|
|
252
|
+
# Atomic update by query, see Ndb::Client#update_by.
|
|
253
|
+
def ndb_update(query, ops, **options)
|
|
220
254
|
ensure_collection!
|
|
221
255
|
|
|
222
|
-
Rails.client.
|
|
256
|
+
Rails.client.update_by(collection_name, query, ops, **options)
|
|
223
257
|
end
|
|
224
258
|
|
|
225
|
-
def ndb_delete(
|
|
259
|
+
def ndb_delete(query, **options)
|
|
226
260
|
ensure_collection!
|
|
227
261
|
|
|
228
|
-
Rails.client.
|
|
262
|
+
Rails.client.delete_by(collection_name, query, **options)
|
|
229
263
|
end
|
|
230
264
|
|
|
231
265
|
private
|
data/lib/ndb/rails/relation.rb
CHANGED
|
@@ -80,7 +80,7 @@ module Ndb
|
|
|
80
80
|
query = {}
|
|
81
81
|
where = Conditions.combine(values[:where])
|
|
82
82
|
|
|
83
|
-
query["
|
|
83
|
+
query["query"] = where unless where.empty?
|
|
84
84
|
query["sort"] = values[:order] unless values[:order].empty?
|
|
85
85
|
query.merge!(window)
|
|
86
86
|
query["after"] = values[:after] if values[:after]
|
|
@@ -148,7 +148,7 @@ module Ndb
|
|
|
148
148
|
def next_cursor
|
|
149
149
|
load
|
|
150
150
|
|
|
151
|
-
@result.
|
|
151
|
+
@result.next
|
|
152
152
|
end
|
|
153
153
|
|
|
154
154
|
def first(count = nil)
|
|
@@ -179,7 +179,7 @@ module Ndb
|
|
|
179
179
|
end
|
|
180
180
|
|
|
181
181
|
def exists?
|
|
182
|
-
klass.ndb_find(to_query.slice("
|
|
182
|
+
klass.ndb_find(to_query.slice("query").merge("limit" => 1, "fields" => [klass.primary_key])).rows.any?
|
|
183
183
|
end
|
|
184
184
|
|
|
185
185
|
def pluck(*fields)
|
|
@@ -198,7 +198,7 @@ module Ndb
|
|
|
198
198
|
def count
|
|
199
199
|
return calculate(:count) unless values[:group].empty?
|
|
200
200
|
|
|
201
|
-
klass.ndb_find(to_query.slice("
|
|
201
|
+
klass.ndb_find(to_query.slice("query").merge("count" => true, "limit" => 1, "fields" => [klass.primary_key])).total
|
|
202
202
|
end
|
|
203
203
|
|
|
204
204
|
# Pagination
|
data/lib/ndb/rails/version.rb
CHANGED
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:
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thadeu
|
|
@@ -43,14 +43,14 @@ dependencies:
|
|
|
43
43
|
requirements:
|
|
44
44
|
- - "~>"
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version: '0
|
|
46
|
+
version: '1.0'
|
|
47
47
|
type: :runtime
|
|
48
48
|
prerelease: false
|
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
50
50
|
requirements:
|
|
51
51
|
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '0
|
|
53
|
+
version: '1.0'
|
|
54
54
|
description: Ndb::Record, a lazy Relation, validations, blobs and a Rails railtie
|
|
55
55
|
on top of the ndb client gem. The gem only builds JSON; the daemon decides every
|
|
56
56
|
query semantic.
|