-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathjob.js
More file actions
169 lines (145 loc) · 4.35 KB
/
Copy pathjob.js
File metadata and controls
169 lines (145 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
printStartPipeline, printEndPipeline,
faulty, faultyAsyncStream, faultify,
splitObject, encryptEvent,
compact,
} from '../utils';
import {
scanSplitDynamoDB, querySplitDynamoDB, queryAllDynamoDB, batchGetDynamoDB,
} from '../queries/dynamodb';
import {
updateDynamoDB,
} from '../sinks/dynamodb';
import { filterOnEventType, filterOnContent } from '../filters';
export const job = (rule) => (s) => s // eslint-disable-line import/prefer-default-export
.filter(onEventType(rule))
.tap(printStartPipeline)
// job level filter
.filter(onContent({
...rule,
filters: rule.jobFilters,
}))
// scan for records of interest
.map(toScanRequest(rule))
.through(scanSplitDynamoDB(rule))
// or query for records of interest
.map(toQuerySplitRequest(rule))
.through(querySplitDynamoDB(rule))
// current uow level filter
.filter(onContent(rule))
// query related data for the current uow
.map(toQueryRelatedRequest(rule))
.through(queryAllDynamoDB(rule))
.through(splitObject({
splitTargetField: rule.queryResponseField || 'queryResponse',
...rule,
}))
// get related data for the current uow
.map(toGetRequest(rule))
.through(batchGetDynamoDB(rule))
// write back to the db
.map(toUpdateRequest(rule))
.through(updateDynamoDB(rule))
// or publish an event
.map(toEvent(rule))
.parallel(rule.parallel || Number(process.env.PARALLEL) || 4)
.through(encryptEvent({
...rule,
sourceField: 'emit',
targetField: 'emit',
}))
.through(rule.publish({
...rule,
parallel: 10,
eventField: 'emit', // so we don't overwrite the incoming event in the uow
}))
.through(flushCursor(rule))
.tap(printEndPipeline);
const onEventType = (rule) => faulty((uow) => filterOnEventType(rule, uow));
const toScanRequest = (rule) => faulty((uow) => ({
...uow,
scanRequest:
rule.toScanRequest
? /* istanbul ignore next */ rule.toScanRequest(uow, rule)
: undefined,
}));
const toQuerySplitRequest = (rule) => faulty((uow) => ({
...uow,
querySplitRequest:
rule.toQuerySplitRequest
? rule.toQuerySplitRequest(uow, rule)
: /* istanbul ignore next */ undefined,
}));
const onContent = (rule) => faulty((uow) => filterOnContent(rule, uow));
const toQueryRelatedRequest = (rule) => faulty((uow) => ({
...uow,
queryRequest:
/* istanbul ignore next */
(rule.toQueryRelatedRequest || rule.toQueryRequest)
? (rule.toQueryRelatedRequest || rule.toQueryRequest)(uow, rule)
: undefined,
}));
const toGetRequest = (rule) => faulty((uow) => ({
...uow,
batchGetRequest:
rule.toGetRequest
? /* istanbul ignore next */ rule.toGetRequest(uow, rule)
: undefined,
}));
const toUpdateRequest = (rule) => faulty((uow) => ({
...uow,
updateRequest:
rule.toUpdateRequest
? /* istanbul ignore next */ rule.toUpdateRequest(uow, rule)
: undefined,
}));
const toEvent = (rule) => faultyAsyncStream(async (uow) => (!rule.toEvent
? /* istanbul ignore next */ uow
: ({
...uow,
emit: {
...await faultify(rule.toEvent)(uow, rule),
},
})));
export const toCursorUpdateRequest = (rule) => faulty((uow) => ({
...uow,
cursorUpdateRequest:
rule.toCursorUpdateRequest
? rule.toCursorUpdateRequest(uow, rule)
: /* istanbul ignore next */ undefined,
}));
export const flushCursor = (rule) => (s) => {
const {
// By default group on a stringified version of the full key. If the key structure
// differs in a users particular implementation or they want to group by something
// else they can simply override this fn in their rule.
cursorKeyFn = (uow) => `pk:${uow.event.raw.new.pk}|sk:${uow.event.raw.new.sk}`,
} = rule;
/* istanbul ignore else */
if (rule.toCursorUpdateRequest) {
return s
.through(compact({
...rule,
compact: {
group: (uow) => cursorKeyFn(uow),
},
}))
.map(toCursorUpdateRequest(rule))
.through(updateDynamoDB({
...rule,
updateRequestField: 'cursorUpdateRequest',
updateResponseField: 'cursorUpdateResponse',
}))
// Maintains backwards compatibility with how this used to manipulate the UOWs,
// duping the last uow.
.flatMap((uow) => {
const { batch, ...lastUow } = uow;
return [
...batch,
lastUow,
];
});
} else {
return s;
}
};