@openfn/language-collections 0.4.0 → 0.5.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.
- package/README.md +92 -4
- package/dist/index.cjs +117 -58
- package/dist/index.js +117 -58
- package/package.json +1 -1
- package/types/collections.d.ts +5 -4
- package/types/mock.d.ts +3 -1
package/README.md
CHANGED
|
@@ -1,16 +1,104 @@
|
|
|
1
1
|
# language-collections <img src='./assets/square.png' width="30" height="30"/>
|
|
2
2
|
|
|
3
|
-
An
|
|
4
|
-
collections API.
|
|
3
|
+
An **_adaptor_** for using the OpenFn Collections API.
|
|
5
4
|
|
|
6
|
-
The
|
|
7
|
-
other adaptors.
|
|
5
|
+
The Collections adaptor is not designed to be used alone, but in conjunction
|
|
6
|
+
with other adaptors. As a result, it's a bit different to other adaptors.
|
|
7
|
+
|
|
8
|
+
The Collections API is a key/value store, which can act as a high performance
|
|
9
|
+
cache, buffer or storage solution. All values are strings (but can contain
|
|
10
|
+
JSON).
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
All collections functions are exposed through the `collections` namespace:
|
|
15
|
+
|
|
16
|
+
To iterate over all values with `2024` somewhere in the key
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
collections.each($.collectionName, '*2024*', (state, value, key) => {
|
|
20
|
+
console.log(value)
|
|
21
|
+
})
|
|
22
|
+
```
|
|
8
23
|
|
|
9
24
|
## Documentation
|
|
10
25
|
|
|
11
26
|
View the [docs site](https://docs.openfn.org/adaptors/packages/collections-docs)
|
|
12
27
|
for full technical documentation.
|
|
13
28
|
|
|
29
|
+
## CLI
|
|
30
|
+
|
|
31
|
+
Collections can be used from the CLI since v1.8.7, but you will need to:
|
|
32
|
+
|
|
33
|
+
- Set the job to use two adaptors
|
|
34
|
+
- Pass a Personal Access Token
|
|
35
|
+
- Set the Collections endpoint
|
|
36
|
+
|
|
37
|
+
You can get a Personal Access Token from any v2 deployment.
|
|
38
|
+
|
|
39
|
+
### For a single job
|
|
40
|
+
|
|
41
|
+
You can pass multiple adaptors from the CLI:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
openfn job.js -a collections -a http -s state.json
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
You'll need to set configuration on the state.json:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
{
|
|
51
|
+
"configuration": {
|
|
52
|
+
"collections_endpoint": "http://localhost:4000/collections",
|
|
53
|
+
"collections_token": "...paste the token from the app..."
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### For a workflow
|
|
60
|
+
|
|
61
|
+
If you're using `workflow.json`, set the token and endpoint on
|
|
62
|
+
`workflow.credentials`:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"workflow": {
|
|
67
|
+
"steps": [ ... ],
|
|
68
|
+
"credentials": {
|
|
69
|
+
"collections_endpoint": "http://localhost:4000/collections",
|
|
70
|
+
"collections_token": "...paste the token from the app..."
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
And make sure that any steps which use collections have multiple adaptors set:
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"workflow": {
|
|
81
|
+
"steps": [
|
|
82
|
+
{
|
|
83
|
+
"expression": "...",
|
|
84
|
+
"adaptors": ["@openfn/language-http", "@openfn/language-collections"]
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Note that steps cannot generally use multiple adaptors. Collections are special
|
|
92
|
+
in this regard.
|
|
93
|
+
|
|
94
|
+
## Deployments
|
|
95
|
+
|
|
96
|
+
Collections must be configured for your platform instance (v2 only).
|
|
97
|
+
|
|
98
|
+
Set the `WORKER_COLLECTIONS_URL` env var on the Worker instance to enable
|
|
99
|
+
collections for that worker. The URL should point to
|
|
100
|
+
`https://<lightning>/collections`
|
|
101
|
+
|
|
14
102
|
## Development
|
|
15
103
|
|
|
16
104
|
Clone the [adaptors monorepo](https://github.com/OpenFn/adaptors). Follow the
|
package/dist/index.cjs
CHANGED
|
@@ -70,22 +70,36 @@ function API() {
|
|
|
70
70
|
}
|
|
71
71
|
collections[name][key] = value;
|
|
72
72
|
};
|
|
73
|
-
const fetch = (name, key, query) => {
|
|
73
|
+
const fetch = (name, key, query = {}) => {
|
|
74
|
+
const { cursor = 0, limit = Infinity } = query;
|
|
74
75
|
if (!(name in collections)) {
|
|
75
76
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
76
77
|
}
|
|
77
78
|
const col = collections[name];
|
|
78
79
|
const items = [];
|
|
80
|
+
let idx = 0;
|
|
81
|
+
let count2 = 0;
|
|
79
82
|
const regex = new RegExp(key.replace("*", "(.*)"));
|
|
80
83
|
for (const key2 in col) {
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
if (idx >= cursor) {
|
|
85
|
+
if (regex.test(key2)) {
|
|
86
|
+
count2++;
|
|
87
|
+
items.push({
|
|
88
|
+
key: key2,
|
|
89
|
+
value: col[key2]
|
|
90
|
+
});
|
|
91
|
+
}
|
|
86
92
|
}
|
|
93
|
+
idx++;
|
|
94
|
+
if (count2 >= limit) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const result = { items, count: count2 };
|
|
99
|
+
if (idx < Object.keys(col).length) {
|
|
100
|
+
result.cursor = idx;
|
|
87
101
|
}
|
|
88
|
-
return
|
|
102
|
+
return result;
|
|
89
103
|
};
|
|
90
104
|
const byKey = (name, key) => {
|
|
91
105
|
return collections[name][key];
|
|
@@ -93,6 +107,9 @@ function API() {
|
|
|
93
107
|
const asJSON = (name, key) => {
|
|
94
108
|
return JSON.parse(collections[name][key]);
|
|
95
109
|
};
|
|
110
|
+
const count = (name) => {
|
|
111
|
+
return Object.keys(collections[name]).length;
|
|
112
|
+
};
|
|
96
113
|
const remove2 = (name, key) => {
|
|
97
114
|
if (!(name in collections)) {
|
|
98
115
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
@@ -116,7 +133,8 @@ function API() {
|
|
|
116
133
|
fetch,
|
|
117
134
|
remove: remove2,
|
|
118
135
|
byKey,
|
|
119
|
-
asJSON
|
|
136
|
+
asJSON,
|
|
137
|
+
count
|
|
120
138
|
};
|
|
121
139
|
return api;
|
|
122
140
|
}
|
|
@@ -163,9 +181,14 @@ function createServer(url = "https://app.openfn.org") {
|
|
|
163
181
|
} else {
|
|
164
182
|
const params = new URLSearchParams(req.query || req.path.split("?")[1]);
|
|
165
183
|
key = params.get("key") ?? "*";
|
|
166
|
-
const
|
|
184
|
+
const limit = params.get("limit") ?? Infinity;
|
|
185
|
+
const cursor = params.get("cursor") ?? 0;
|
|
186
|
+
const { items, cursor: finalCursor } = api.fetch(name, key, {
|
|
187
|
+
limit,
|
|
188
|
+
cursor
|
|
189
|
+
});
|
|
167
190
|
body = {
|
|
168
|
-
cursor:
|
|
191
|
+
cursor: (finalCursor && `${finalCursor}`) ?? null,
|
|
169
192
|
items
|
|
170
193
|
};
|
|
171
194
|
}
|
|
@@ -285,33 +308,40 @@ function get(name, query = {}) {
|
|
|
285
308
|
if (typeof resolvedQuery === "string") {
|
|
286
309
|
resolvedQuery = { key: resolvedQuery };
|
|
287
310
|
}
|
|
288
|
-
const {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
const response = await request(state, getClient(state), path, { query: q });
|
|
311
|
+
const {
|
|
312
|
+
key = "*",
|
|
313
|
+
limit,
|
|
314
|
+
pageSize,
|
|
315
|
+
...userQuery
|
|
316
|
+
} = expandQuery(resolvedQuery);
|
|
317
|
+
console.log(`Collections: Downloading data from "${name}"...`);
|
|
297
318
|
let data;
|
|
298
|
-
if (
|
|
319
|
+
if (key.match(/\*/) || Object.keys(userQuery).length) {
|
|
299
320
|
data = [];
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
321
|
+
const scopedState = {
|
|
322
|
+
...state,
|
|
323
|
+
data: {}
|
|
324
|
+
};
|
|
325
|
+
await each(name, resolvedQuery, (state2, value, key2) => {
|
|
326
|
+
data.push({ value, key: key2 });
|
|
327
|
+
})(scopedState);
|
|
328
|
+
console.log(
|
|
329
|
+
`Collections: Fetched total of ${data.length} values from "${name}"`
|
|
330
|
+
);
|
|
331
|
+
data.cursor = scopedState.data.cursor;
|
|
307
332
|
} else {
|
|
333
|
+
const response = await request(
|
|
334
|
+
state,
|
|
335
|
+
getClient(state),
|
|
336
|
+
`${resolvedName}/${key}`
|
|
337
|
+
);
|
|
308
338
|
const body = await response.body.json();
|
|
309
339
|
if (body.value) {
|
|
310
340
|
data = JSON.parse(body.value);
|
|
311
|
-
console.log(`Fetched "${key}" from
|
|
341
|
+
console.log(`Collections: Fetched "${key}" from "${name}"`);
|
|
312
342
|
} else {
|
|
313
343
|
data = {};
|
|
314
|
-
console.warn(`Key "${key}" not found in
|
|
344
|
+
console.warn(`Collections: Key "${key}" not found in "${name}"`);
|
|
315
345
|
}
|
|
316
346
|
}
|
|
317
347
|
state.data = data;
|
|
@@ -320,6 +350,7 @@ function get(name, query = {}) {
|
|
|
320
350
|
}
|
|
321
351
|
function set(name, keyGen, values) {
|
|
322
352
|
return async (state) => {
|
|
353
|
+
const batchSize = 1e3;
|
|
323
354
|
const [resolvedName, resolvedValues] = (0, import_util.expandReferences)(
|
|
324
355
|
state,
|
|
325
356
|
name,
|
|
@@ -331,26 +362,32 @@ function set(name, keyGen, values) {
|
|
|
331
362
|
key: keyGenFn(value, index),
|
|
332
363
|
value: JSON.stringify(value)
|
|
333
364
|
}));
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
body: JSON.stringify({ items: pairs }),
|
|
337
|
-
headers: {
|
|
338
|
-
"content-type": "application/json"
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
if (response.statusCode >= 400) {
|
|
365
|
+
while (pairs.length) {
|
|
366
|
+
const batch = pairs.splice(0, batchSize);
|
|
342
367
|
console.log(
|
|
343
|
-
`
|
|
368
|
+
`Collections: uploading batch of ${batch.length} values to "${name}"...`
|
|
344
369
|
);
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
370
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
371
|
+
method: "POST",
|
|
372
|
+
body: JSON.stringify({ items: batch }),
|
|
373
|
+
headers: {
|
|
374
|
+
"content-type": "application/json"
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
if (response.statusCode >= 400) {
|
|
378
|
+
console.log(
|
|
379
|
+
`Collections: Error setting ${batch.length} values in "${name}"`
|
|
380
|
+
);
|
|
381
|
+
const text = await response.body.text();
|
|
382
|
+
const e = new Error("ERROR from collections server:" + 400);
|
|
383
|
+
e.body = text;
|
|
384
|
+
throw e;
|
|
385
|
+
}
|
|
386
|
+
const result = await response.body.json();
|
|
387
|
+
console.log(`Collections: set ${result.upserted} values in "${name}"`);
|
|
388
|
+
if (result.error) {
|
|
389
|
+
console.log(`Collections: errors reported on set:`, result.error);
|
|
390
|
+
}
|
|
354
391
|
}
|
|
355
392
|
return state;
|
|
356
393
|
};
|
|
@@ -374,7 +411,7 @@ function remove(name, query = {}, options = {}) {
|
|
|
374
411
|
query: q
|
|
375
412
|
});
|
|
376
413
|
const result = await response.body.json();
|
|
377
|
-
console.log(`Removed ${result.deleted} values in
|
|
414
|
+
console.log(`Collections: Removed ${result.deleted} values in "${name}"`);
|
|
378
415
|
return state;
|
|
379
416
|
};
|
|
380
417
|
}
|
|
@@ -388,13 +425,36 @@ function each(name, query = {}, callback = () => {
|
|
|
388
425
|
} else {
|
|
389
426
|
q = resolvedQuery;
|
|
390
427
|
}
|
|
391
|
-
const {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
}
|
|
428
|
+
const {
|
|
429
|
+
key = "*",
|
|
430
|
+
pageSize = 1e3,
|
|
431
|
+
limit = Infinity,
|
|
432
|
+
...finalQuery
|
|
433
|
+
} = expandQuery(resolvedQuery);
|
|
434
|
+
let { cursor } = resolvedQuery;
|
|
435
|
+
let count = 0;
|
|
436
|
+
do {
|
|
437
|
+
let batchSize = 0;
|
|
438
|
+
const q2 = {
|
|
439
|
+
...finalQuery,
|
|
440
|
+
key,
|
|
441
|
+
limit: limit < Infinity ? Math.min(pageSize, limit - count) : pageSize
|
|
442
|
+
};
|
|
443
|
+
if (cursor) {
|
|
444
|
+
q2.cursor = cursor;
|
|
445
|
+
}
|
|
446
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
447
|
+
query: q2
|
|
448
|
+
});
|
|
449
|
+
cursor = await streamResponse(response, async ({ key: key2, value }) => {
|
|
450
|
+
batchSize++;
|
|
451
|
+
await callback(state, JSON.parse(value), key2);
|
|
452
|
+
});
|
|
453
|
+
count += batchSize;
|
|
454
|
+
console.log(
|
|
455
|
+
`Collections: fetched chunk of ${batchSize} values from "${name}"`
|
|
456
|
+
);
|
|
457
|
+
} while (cursor && count < limit);
|
|
398
458
|
state.data = {
|
|
399
459
|
cursor
|
|
400
460
|
};
|
|
@@ -423,6 +483,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
423
483
|
if (next.value.value === "cursor") {
|
|
424
484
|
const strValue = await waitFor("stringChunk", "nullValue");
|
|
425
485
|
if (strValue.name === "nullValue") {
|
|
486
|
+
cursor = null;
|
|
426
487
|
continue;
|
|
427
488
|
}
|
|
428
489
|
cursor = strValue.value.value;
|
|
@@ -468,9 +529,7 @@ var expandQuery = (query) => {
|
|
|
468
529
|
};
|
|
469
530
|
var queryMaps = {
|
|
470
531
|
createdBefore: "created_before",
|
|
471
|
-
createdAfter: "created_after"
|
|
472
|
-
updatedBefore: "updated_before",
|
|
473
|
-
updatedAfter: "updated_after"
|
|
532
|
+
createdAfter: "created_after"
|
|
474
533
|
};
|
|
475
534
|
var parseQuery = (options = {}) => {
|
|
476
535
|
return Object.keys(options.query ?? {}).reduce((result, next) => {
|
package/dist/index.js
CHANGED
|
@@ -45,22 +45,36 @@ function API() {
|
|
|
45
45
|
}
|
|
46
46
|
collections[name][key] = value;
|
|
47
47
|
};
|
|
48
|
-
const fetch = (name, key, query) => {
|
|
48
|
+
const fetch = (name, key, query = {}) => {
|
|
49
|
+
const { cursor = 0, limit = Infinity } = query;
|
|
49
50
|
if (!(name in collections)) {
|
|
50
51
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
51
52
|
}
|
|
52
53
|
const col = collections[name];
|
|
53
54
|
const items = [];
|
|
55
|
+
let idx = 0;
|
|
56
|
+
let count2 = 0;
|
|
54
57
|
const regex = new RegExp(key.replace("*", "(.*)"));
|
|
55
58
|
for (const key2 in col) {
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
if (idx >= cursor) {
|
|
60
|
+
if (regex.test(key2)) {
|
|
61
|
+
count2++;
|
|
62
|
+
items.push({
|
|
63
|
+
key: key2,
|
|
64
|
+
value: col[key2]
|
|
65
|
+
});
|
|
66
|
+
}
|
|
61
67
|
}
|
|
68
|
+
idx++;
|
|
69
|
+
if (count2 >= limit) {
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const result = { items, count: count2 };
|
|
74
|
+
if (idx < Object.keys(col).length) {
|
|
75
|
+
result.cursor = idx;
|
|
62
76
|
}
|
|
63
|
-
return
|
|
77
|
+
return result;
|
|
64
78
|
};
|
|
65
79
|
const byKey = (name, key) => {
|
|
66
80
|
return collections[name][key];
|
|
@@ -68,6 +82,9 @@ function API() {
|
|
|
68
82
|
const asJSON = (name, key) => {
|
|
69
83
|
return JSON.parse(collections[name][key]);
|
|
70
84
|
};
|
|
85
|
+
const count = (name) => {
|
|
86
|
+
return Object.keys(collections[name]).length;
|
|
87
|
+
};
|
|
71
88
|
const remove2 = (name, key) => {
|
|
72
89
|
if (!(name in collections)) {
|
|
73
90
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
@@ -91,7 +108,8 @@ function API() {
|
|
|
91
108
|
fetch,
|
|
92
109
|
remove: remove2,
|
|
93
110
|
byKey,
|
|
94
|
-
asJSON
|
|
111
|
+
asJSON,
|
|
112
|
+
count
|
|
95
113
|
};
|
|
96
114
|
return api;
|
|
97
115
|
}
|
|
@@ -138,9 +156,14 @@ function createServer(url = "https://app.openfn.org") {
|
|
|
138
156
|
} else {
|
|
139
157
|
const params = new URLSearchParams(req.query || req.path.split("?")[1]);
|
|
140
158
|
key = params.get("key") ?? "*";
|
|
141
|
-
const
|
|
159
|
+
const limit = params.get("limit") ?? Infinity;
|
|
160
|
+
const cursor = params.get("cursor") ?? 0;
|
|
161
|
+
const { items, cursor: finalCursor } = api.fetch(name, key, {
|
|
162
|
+
limit,
|
|
163
|
+
cursor
|
|
164
|
+
});
|
|
142
165
|
body = {
|
|
143
|
-
cursor:
|
|
166
|
+
cursor: (finalCursor && `${finalCursor}`) ?? null,
|
|
144
167
|
items
|
|
145
168
|
};
|
|
146
169
|
}
|
|
@@ -260,33 +283,40 @@ function get(name, query = {}) {
|
|
|
260
283
|
if (typeof resolvedQuery === "string") {
|
|
261
284
|
resolvedQuery = { key: resolvedQuery };
|
|
262
285
|
}
|
|
263
|
-
const {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
const response = await request(state, getClient(state), path, { query: q });
|
|
286
|
+
const {
|
|
287
|
+
key = "*",
|
|
288
|
+
limit,
|
|
289
|
+
pageSize,
|
|
290
|
+
...userQuery
|
|
291
|
+
} = expandQuery(resolvedQuery);
|
|
292
|
+
console.log(`Collections: Downloading data from "${name}"...`);
|
|
272
293
|
let data;
|
|
273
|
-
if (
|
|
294
|
+
if (key.match(/\*/) || Object.keys(userQuery).length) {
|
|
274
295
|
data = [];
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
296
|
+
const scopedState = {
|
|
297
|
+
...state,
|
|
298
|
+
data: {}
|
|
299
|
+
};
|
|
300
|
+
await each(name, resolvedQuery, (state2, value, key2) => {
|
|
301
|
+
data.push({ value, key: key2 });
|
|
302
|
+
})(scopedState);
|
|
303
|
+
console.log(
|
|
304
|
+
`Collections: Fetched total of ${data.length} values from "${name}"`
|
|
305
|
+
);
|
|
306
|
+
data.cursor = scopedState.data.cursor;
|
|
282
307
|
} else {
|
|
308
|
+
const response = await request(
|
|
309
|
+
state,
|
|
310
|
+
getClient(state),
|
|
311
|
+
`${resolvedName}/${key}`
|
|
312
|
+
);
|
|
283
313
|
const body = await response.body.json();
|
|
284
314
|
if (body.value) {
|
|
285
315
|
data = JSON.parse(body.value);
|
|
286
|
-
console.log(`Fetched "${key}" from
|
|
316
|
+
console.log(`Collections: Fetched "${key}" from "${name}"`);
|
|
287
317
|
} else {
|
|
288
318
|
data = {};
|
|
289
|
-
console.warn(`Key "${key}" not found in
|
|
319
|
+
console.warn(`Collections: Key "${key}" not found in "${name}"`);
|
|
290
320
|
}
|
|
291
321
|
}
|
|
292
322
|
state.data = data;
|
|
@@ -295,6 +325,7 @@ function get(name, query = {}) {
|
|
|
295
325
|
}
|
|
296
326
|
function set(name, keyGen, values) {
|
|
297
327
|
return async (state) => {
|
|
328
|
+
const batchSize = 1e3;
|
|
298
329
|
const [resolvedName, resolvedValues] = expandReferences(
|
|
299
330
|
state,
|
|
300
331
|
name,
|
|
@@ -306,26 +337,32 @@ function set(name, keyGen, values) {
|
|
|
306
337
|
key: keyGenFn(value, index),
|
|
307
338
|
value: JSON.stringify(value)
|
|
308
339
|
}));
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
body: JSON.stringify({ items: pairs }),
|
|
312
|
-
headers: {
|
|
313
|
-
"content-type": "application/json"
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
if (response.statusCode >= 400) {
|
|
340
|
+
while (pairs.length) {
|
|
341
|
+
const batch = pairs.splice(0, batchSize);
|
|
317
342
|
console.log(
|
|
318
|
-
`
|
|
343
|
+
`Collections: uploading batch of ${batch.length} values to "${name}"...`
|
|
319
344
|
);
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
345
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
346
|
+
method: "POST",
|
|
347
|
+
body: JSON.stringify({ items: batch }),
|
|
348
|
+
headers: {
|
|
349
|
+
"content-type": "application/json"
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
if (response.statusCode >= 400) {
|
|
353
|
+
console.log(
|
|
354
|
+
`Collections: Error setting ${batch.length} values in "${name}"`
|
|
355
|
+
);
|
|
356
|
+
const text = await response.body.text();
|
|
357
|
+
const e = new Error("ERROR from collections server:" + 400);
|
|
358
|
+
e.body = text;
|
|
359
|
+
throw e;
|
|
360
|
+
}
|
|
361
|
+
const result = await response.body.json();
|
|
362
|
+
console.log(`Collections: set ${result.upserted} values in "${name}"`);
|
|
363
|
+
if (result.error) {
|
|
364
|
+
console.log(`Collections: errors reported on set:`, result.error);
|
|
365
|
+
}
|
|
329
366
|
}
|
|
330
367
|
return state;
|
|
331
368
|
};
|
|
@@ -349,7 +386,7 @@ function remove(name, query = {}, options = {}) {
|
|
|
349
386
|
query: q
|
|
350
387
|
});
|
|
351
388
|
const result = await response.body.json();
|
|
352
|
-
console.log(`Removed ${result.deleted} values in
|
|
389
|
+
console.log(`Collections: Removed ${result.deleted} values in "${name}"`);
|
|
353
390
|
return state;
|
|
354
391
|
};
|
|
355
392
|
}
|
|
@@ -363,13 +400,36 @@ function each(name, query = {}, callback = () => {
|
|
|
363
400
|
} else {
|
|
364
401
|
q = resolvedQuery;
|
|
365
402
|
}
|
|
366
|
-
const {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
403
|
+
const {
|
|
404
|
+
key = "*",
|
|
405
|
+
pageSize = 1e3,
|
|
406
|
+
limit = Infinity,
|
|
407
|
+
...finalQuery
|
|
408
|
+
} = expandQuery(resolvedQuery);
|
|
409
|
+
let { cursor } = resolvedQuery;
|
|
410
|
+
let count = 0;
|
|
411
|
+
do {
|
|
412
|
+
let batchSize = 0;
|
|
413
|
+
const q2 = {
|
|
414
|
+
...finalQuery,
|
|
415
|
+
key,
|
|
416
|
+
limit: limit < Infinity ? Math.min(pageSize, limit - count) : pageSize
|
|
417
|
+
};
|
|
418
|
+
if (cursor) {
|
|
419
|
+
q2.cursor = cursor;
|
|
420
|
+
}
|
|
421
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
422
|
+
query: q2
|
|
423
|
+
});
|
|
424
|
+
cursor = await streamResponse(response, async ({ key: key2, value }) => {
|
|
425
|
+
batchSize++;
|
|
426
|
+
await callback(state, JSON.parse(value), key2);
|
|
427
|
+
});
|
|
428
|
+
count += batchSize;
|
|
429
|
+
console.log(
|
|
430
|
+
`Collections: fetched chunk of ${batchSize} values from "${name}"`
|
|
431
|
+
);
|
|
432
|
+
} while (cursor && count < limit);
|
|
373
433
|
state.data = {
|
|
374
434
|
cursor
|
|
375
435
|
};
|
|
@@ -398,6 +458,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
398
458
|
if (next.value.value === "cursor") {
|
|
399
459
|
const strValue = await waitFor("stringChunk", "nullValue");
|
|
400
460
|
if (strValue.name === "nullValue") {
|
|
461
|
+
cursor = null;
|
|
401
462
|
continue;
|
|
402
463
|
}
|
|
403
464
|
cursor = strValue.value.value;
|
|
@@ -443,9 +504,7 @@ var expandQuery = (query) => {
|
|
|
443
504
|
};
|
|
444
505
|
var queryMaps = {
|
|
445
506
|
createdBefore: "created_before",
|
|
446
|
-
createdAfter: "created_after"
|
|
447
|
-
updatedBefore: "updated_before",
|
|
448
|
-
updatedAfter: "updated_after"
|
|
507
|
+
createdAfter: "created_after"
|
|
449
508
|
};
|
|
450
509
|
var parseQuery = (options = {}) => {
|
|
451
510
|
return Object.keys(options.query ?? {}).reduce((result, next) => {
|
package/package.json
CHANGED
package/types/collections.d.ts
CHANGED
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
* @property {string} key - key or key pattern to match against. Patterns support wildcards, eg `2024-01*`
|
|
6
6
|
* @property {string} createdBefore - matches values that were created before the start of the provided date
|
|
7
7
|
* @property {string} createdAfter - matches values that were created after the end of the provided date
|
|
8
|
-
* @property {
|
|
9
|
-
* @property {
|
|
10
|
-
* @property {number} limit - limit the maximum amount of results. Defaults to 1000.
|
|
8
|
+
* @property {number} limit - limit the maximum amount of results. If Infinity or unset, all items will be fetched. Default: Infnity.
|
|
9
|
+
* @property {number} pageSize - specify the number of values downloaded per page (or chunk). Default 1000.
|
|
11
10
|
* @property {string} cursor - set the cursor position to start searching from a specific index.
|
|
12
11
|
*/
|
|
13
12
|
/**
|
|
@@ -69,6 +68,8 @@ export function remove(name: string, query?: string | QueryOptions, options?: {}
|
|
|
69
68
|
* You can pass a string key-pattern as a query, or pass a query object.
|
|
70
69
|
* The callback function will be invoked for each value with three parameters:
|
|
71
70
|
* `state`, `value` and `key`.
|
|
71
|
+
* Changing the page size does not affect the callback function (only one item is
|
|
72
|
+
* ever passed at a time).
|
|
72
73
|
* @public
|
|
73
74
|
* @function
|
|
74
75
|
* @param {string} name - The name of the collection to remove from
|
|
@@ -80,7 +81,7 @@ export function remove(name: string, query?: string | QueryOptions, options?: {}
|
|
|
80
81
|
* state.cumulativeCost += value.cost;
|
|
81
82
|
* })
|
|
82
83
|
* @example <caption>Iterate over a range of values with date filters</caption>
|
|
83
|
-
* collections.each('my-collection', {
|
|
84
|
+
* collections.each('my-collection', { createdBefore: new Date().toString() }, (state, value, key) => {
|
|
84
85
|
* state.cumulativeCost += value.cost;
|
|
85
86
|
* })
|
|
86
87
|
*/
|
package/types/mock.d.ts
CHANGED
|
@@ -3,15 +3,17 @@ export function API(): {
|
|
|
3
3
|
createCollection: (name: any, values?: {}) => void;
|
|
4
4
|
reset: () => void;
|
|
5
5
|
upsert: (name: any, key: any, value: any) => void;
|
|
6
|
-
fetch: (name: any, key: any, query
|
|
6
|
+
fetch: (name: any, key: any, query?: {}) => {
|
|
7
7
|
items: {
|
|
8
8
|
key: string;
|
|
9
9
|
value: any;
|
|
10
10
|
}[];
|
|
11
|
+
count: number;
|
|
11
12
|
};
|
|
12
13
|
remove: (name: any, key: any) => string[];
|
|
13
14
|
byKey: (name: any, key: any) => any;
|
|
14
15
|
asJSON: (name: any, key: any) => any;
|
|
16
|
+
count: (name: any) => number;
|
|
15
17
|
};
|
|
16
18
|
export function createServer(url?: string): {
|
|
17
19
|
api: any;
|