flow
Search…
Composing with conditionals
Using conditionals statements to fine-tune reductions
Reduction strategies are JSON Schema annotations, and as such their applicability at a given document location can be controlled through the use of conditional keywords within the schema like oneOf or if/then/else. This means Flow’s built-in strategies below can be combined with schema conditionals to construct a wider variety of custom reduction behaviors.
For example, here’s a reset-able counter:
1
collections:
2
- name: example/reductions/sum-reset
3
schema:
4
type: object
5
properties:
6
key: { type: string }
7
value: { type: number }
8
required: [key]
9
# Use oneOf to express a tagged union over "action".
10
oneOf:
11
# When action = reset, reduce by taking this document.
12
- properties: { action: { const: reset } }
13
reduce: { strategy: lastWriteWins }
14
# When action = sum, reduce by summing "value". Keep the LHS "action",
15
# preserving a LHS "reset", so that resets are properly associative.
16
- properties:
17
action:
18
const: sum
19
reduce: { strategy: firstWriteWins }
20
value: { reduce: { strategy: sum } }
21
reduce: { strategy: merge }
22
key: [/key]
23
24
tests:
25
"Expect we can sum or reset numbers":
26
- ingest:
27
collection: example/reductions/sum-reset
28
documents:
29
- { key: "key", action: sum, value: 5 }
30
- { key: "key", action: sum, value: -1.2 }
31
- verify:
32
collection: example/reductions/sum-reset
33
documents:
34
- { key: "key", value: 3.8 }
35
- ingest:
36
collection: example/reductions/sum-reset
37
documents:
38
- { key: "key", action: reset, value: 0 }
39
- { key: "key", action: sum, value: 1.3 }
40
- verify:
41
collection: example/reductions/sum-reset
42
documents:
43
- { key: "key", value: 1.3 }
Copied!
Last modified 3mo ago
Copy link