@openfn/language-collections 0.3.0 → 0.5.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.
- package/README.md +92 -4
- package/dist/index.cjs +119 -56
- package/dist/index.js +119 -56
- package/package.json +1 -1
- package/types/collections.d.ts +5 -4
- package/types/mock.d.ts +3 -2
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 count = 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
|
+
count++;
|
|
87
|
+
items.push({
|
|
88
|
+
key: key2,
|
|
89
|
+
value: col[key2]
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
idx++;
|
|
94
|
+
if (count >= limit) {
|
|
95
|
+
break;
|
|
86
96
|
}
|
|
87
97
|
}
|
|
88
|
-
|
|
98
|
+
const result = { items, count };
|
|
99
|
+
if (idx < Object.keys(col).length) {
|
|
100
|
+
result.cursor = idx;
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
89
103
|
};
|
|
90
104
|
const byKey = (name, key) => {
|
|
91
105
|
return collections[name][key];
|
|
@@ -98,8 +112,15 @@ function API() {
|
|
|
98
112
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
99
113
|
}
|
|
100
114
|
const col = collections[name];
|
|
101
|
-
|
|
102
|
-
|
|
115
|
+
const regex = new RegExp(key.replace("*", "(.*)"));
|
|
116
|
+
const removed = [];
|
|
117
|
+
for (const key2 in col) {
|
|
118
|
+
if (regex.test(key2)) {
|
|
119
|
+
delete col[key2];
|
|
120
|
+
removed.push(key2);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return removed;
|
|
103
124
|
};
|
|
104
125
|
const api = {
|
|
105
126
|
collections,
|
|
@@ -156,9 +177,14 @@ function createServer(url = "https://app.openfn.org") {
|
|
|
156
177
|
} else {
|
|
157
178
|
const params = new URLSearchParams(req.query || req.path.split("?")[1]);
|
|
158
179
|
key = params.get("key") ?? "*";
|
|
159
|
-
const
|
|
180
|
+
const limit = params.get("limit") ?? Infinity;
|
|
181
|
+
const cursor = params.get("cursor") ?? 0;
|
|
182
|
+
const { items, cursor: finalCursor } = api.fetch(name, key, {
|
|
183
|
+
limit,
|
|
184
|
+
cursor
|
|
185
|
+
});
|
|
160
186
|
body = {
|
|
161
|
-
cursor:
|
|
187
|
+
cursor: (finalCursor && `${finalCursor}`) ?? null,
|
|
162
188
|
items
|
|
163
189
|
};
|
|
164
190
|
}
|
|
@@ -211,7 +237,11 @@ function createServer(url = "https://app.openfn.org") {
|
|
|
211
237
|
return { statusCode: 403 };
|
|
212
238
|
}
|
|
213
239
|
try {
|
|
214
|
-
|
|
240
|
+
let { name, key } = parsePath(req.path);
|
|
241
|
+
if (!key) {
|
|
242
|
+
const params = new URLSearchParams(req.query || req.path.split("?")[1]);
|
|
243
|
+
key = params.get("key") ?? "*";
|
|
244
|
+
}
|
|
215
245
|
const keys = api.remove(name, key);
|
|
216
246
|
return {
|
|
217
247
|
statusCode: 200,
|
|
@@ -274,33 +304,40 @@ function get(name, query = {}) {
|
|
|
274
304
|
if (typeof resolvedQuery === "string") {
|
|
275
305
|
resolvedQuery = { key: resolvedQuery };
|
|
276
306
|
}
|
|
277
|
-
const {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
const response = await request(state, getClient(state), path, { query: q });
|
|
307
|
+
const {
|
|
308
|
+
key = "*",
|
|
309
|
+
limit,
|
|
310
|
+
pageSize,
|
|
311
|
+
...userQuery
|
|
312
|
+
} = expandQuery(resolvedQuery);
|
|
313
|
+
console.log(`Collections: Downloading data from "${name}"...`);
|
|
286
314
|
let data;
|
|
287
|
-
if (
|
|
315
|
+
if (key.match(/\*/) || Object.keys(userQuery).length) {
|
|
288
316
|
data = [];
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
317
|
+
const scopedState = {
|
|
318
|
+
...state,
|
|
319
|
+
data: {}
|
|
320
|
+
};
|
|
321
|
+
await each(name, resolvedQuery, (state2, value, key2) => {
|
|
322
|
+
data.push({ value, key: key2 });
|
|
323
|
+
})(scopedState);
|
|
324
|
+
console.log(
|
|
325
|
+
`Collections: Fetched total of ${data.length} values from "${name}"`
|
|
326
|
+
);
|
|
327
|
+
data.cursor = scopedState.data.cursor;
|
|
296
328
|
} else {
|
|
329
|
+
const response = await request(
|
|
330
|
+
state,
|
|
331
|
+
getClient(state),
|
|
332
|
+
`${resolvedName}/${key}`
|
|
333
|
+
);
|
|
297
334
|
const body = await response.body.json();
|
|
298
335
|
if (body.value) {
|
|
299
336
|
data = JSON.parse(body.value);
|
|
300
|
-
console.log(`Fetched "${key}" from
|
|
337
|
+
console.log(`Collections: Fetched "${key}" from "${name}"`);
|
|
301
338
|
} else {
|
|
302
339
|
data = {};
|
|
303
|
-
console.warn(`Key "${key}" not found in
|
|
340
|
+
console.warn(`Collections: Key "${key}" not found in "${name}"`);
|
|
304
341
|
}
|
|
305
342
|
}
|
|
306
343
|
state.data = data;
|
|
@@ -329,7 +366,7 @@ function set(name, keyGen, values) {
|
|
|
329
366
|
});
|
|
330
367
|
if (response.statusCode >= 400) {
|
|
331
368
|
console.log(
|
|
332
|
-
`Error setting ${pairs.length} values in
|
|
369
|
+
`Collections: Error setting ${pairs.length} values in "${name}"`
|
|
333
370
|
);
|
|
334
371
|
const text = await response.body.text();
|
|
335
372
|
const e = new Error("ERROR from collections server:" + 400);
|
|
@@ -337,28 +374,33 @@ function set(name, keyGen, values) {
|
|
|
337
374
|
throw e;
|
|
338
375
|
}
|
|
339
376
|
const result = await response.body.json();
|
|
340
|
-
console.log(`
|
|
377
|
+
console.log(`Collections: set ${result.upserted} values in "${name}"`);
|
|
341
378
|
if (result.error) {
|
|
342
|
-
console.log(`
|
|
379
|
+
console.log(`Collections: errors reported on set:`, result.error);
|
|
343
380
|
}
|
|
344
381
|
return state;
|
|
345
382
|
};
|
|
346
383
|
}
|
|
347
384
|
function remove(name, query = {}, options = {}) {
|
|
348
385
|
return async (state) => {
|
|
349
|
-
|
|
386
|
+
let [resolvedName, resolvedQuery] = (0, import_util.expandReferences)(state, name, query);
|
|
387
|
+
if (typeof resolvedQuery === "string") {
|
|
388
|
+
resolvedQuery = { key: resolvedQuery };
|
|
389
|
+
}
|
|
350
390
|
const { key, ...rest } = expandQuery(resolvedQuery);
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
391
|
+
let q;
|
|
392
|
+
let path = resolvedName;
|
|
393
|
+
if (key.match(/\*/) || Object.keys(rest).length) {
|
|
394
|
+
q = resolvedQuery;
|
|
395
|
+
} else {
|
|
396
|
+
path = `${resolvedName}/${key}`;
|
|
397
|
+
}
|
|
398
|
+
const response = await request(state, getClient(state), path, {
|
|
399
|
+
method: "DELETE",
|
|
400
|
+
query: q
|
|
401
|
+
});
|
|
360
402
|
const result = await response.body.json();
|
|
361
|
-
console.log(`
|
|
403
|
+
console.log(`Collections: Removed ${result.deleted} values in "${name}"`);
|
|
362
404
|
return state;
|
|
363
405
|
};
|
|
364
406
|
}
|
|
@@ -372,14 +414,36 @@ function each(name, query = {}, callback = () => {
|
|
|
372
414
|
} else {
|
|
373
415
|
q = resolvedQuery;
|
|
374
416
|
}
|
|
375
|
-
const {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
417
|
+
const {
|
|
418
|
+
key = "*",
|
|
419
|
+
pageSize = 1e3,
|
|
420
|
+
limit = Infinity,
|
|
421
|
+
...finalQuery
|
|
422
|
+
} = expandQuery(resolvedQuery);
|
|
423
|
+
let { cursor } = resolvedQuery;
|
|
424
|
+
let count = 0;
|
|
425
|
+
do {
|
|
426
|
+
let batchSize = 0;
|
|
427
|
+
const q2 = {
|
|
428
|
+
...finalQuery,
|
|
429
|
+
key,
|
|
430
|
+
limit: limit < Infinity ? Math.min(pageSize, limit - count) : pageSize
|
|
431
|
+
};
|
|
432
|
+
if (cursor) {
|
|
433
|
+
q2.cursor = cursor;
|
|
434
|
+
}
|
|
435
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
436
|
+
query: q2
|
|
437
|
+
});
|
|
438
|
+
cursor = await streamResponse(response, async ({ key: key2, value }) => {
|
|
439
|
+
batchSize++;
|
|
440
|
+
await callback(state, JSON.parse(value), key2);
|
|
441
|
+
});
|
|
442
|
+
count += batchSize;
|
|
443
|
+
console.log(
|
|
444
|
+
`Collections: fetched chunk of ${batchSize} values from "${name}"`
|
|
445
|
+
);
|
|
446
|
+
} while (cursor && count < limit);
|
|
383
447
|
state.data = {
|
|
384
448
|
cursor
|
|
385
449
|
};
|
|
@@ -408,6 +472,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
408
472
|
if (next.value.value === "cursor") {
|
|
409
473
|
const strValue = await waitFor("stringChunk", "nullValue");
|
|
410
474
|
if (strValue.name === "nullValue") {
|
|
475
|
+
cursor = null;
|
|
411
476
|
continue;
|
|
412
477
|
}
|
|
413
478
|
cursor = strValue.value.value;
|
|
@@ -453,9 +518,7 @@ var expandQuery = (query) => {
|
|
|
453
518
|
};
|
|
454
519
|
var queryMaps = {
|
|
455
520
|
createdBefore: "created_before",
|
|
456
|
-
createdAfter: "created_after"
|
|
457
|
-
updatedBefore: "updated_before",
|
|
458
|
-
updatedAfter: "updated_after"
|
|
521
|
+
createdAfter: "created_after"
|
|
459
522
|
};
|
|
460
523
|
var parseQuery = (options = {}) => {
|
|
461
524
|
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 count = 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
|
+
count++;
|
|
62
|
+
items.push({
|
|
63
|
+
key: key2,
|
|
64
|
+
value: col[key2]
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
idx++;
|
|
69
|
+
if (count >= limit) {
|
|
70
|
+
break;
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
|
-
|
|
73
|
+
const result = { items, count };
|
|
74
|
+
if (idx < Object.keys(col).length) {
|
|
75
|
+
result.cursor = idx;
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
64
78
|
};
|
|
65
79
|
const byKey = (name, key) => {
|
|
66
80
|
return collections[name][key];
|
|
@@ -73,8 +87,15 @@ function API() {
|
|
|
73
87
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
74
88
|
}
|
|
75
89
|
const col = collections[name];
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
const regex = new RegExp(key.replace("*", "(.*)"));
|
|
91
|
+
const removed = [];
|
|
92
|
+
for (const key2 in col) {
|
|
93
|
+
if (regex.test(key2)) {
|
|
94
|
+
delete col[key2];
|
|
95
|
+
removed.push(key2);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return removed;
|
|
78
99
|
};
|
|
79
100
|
const api = {
|
|
80
101
|
collections,
|
|
@@ -131,9 +152,14 @@ function createServer(url = "https://app.openfn.org") {
|
|
|
131
152
|
} else {
|
|
132
153
|
const params = new URLSearchParams(req.query || req.path.split("?")[1]);
|
|
133
154
|
key = params.get("key") ?? "*";
|
|
134
|
-
const
|
|
155
|
+
const limit = params.get("limit") ?? Infinity;
|
|
156
|
+
const cursor = params.get("cursor") ?? 0;
|
|
157
|
+
const { items, cursor: finalCursor } = api.fetch(name, key, {
|
|
158
|
+
limit,
|
|
159
|
+
cursor
|
|
160
|
+
});
|
|
135
161
|
body = {
|
|
136
|
-
cursor:
|
|
162
|
+
cursor: (finalCursor && `${finalCursor}`) ?? null,
|
|
137
163
|
items
|
|
138
164
|
};
|
|
139
165
|
}
|
|
@@ -186,7 +212,11 @@ function createServer(url = "https://app.openfn.org") {
|
|
|
186
212
|
return { statusCode: 403 };
|
|
187
213
|
}
|
|
188
214
|
try {
|
|
189
|
-
|
|
215
|
+
let { name, key } = parsePath(req.path);
|
|
216
|
+
if (!key) {
|
|
217
|
+
const params = new URLSearchParams(req.query || req.path.split("?")[1]);
|
|
218
|
+
key = params.get("key") ?? "*";
|
|
219
|
+
}
|
|
190
220
|
const keys = api.remove(name, key);
|
|
191
221
|
return {
|
|
192
222
|
statusCode: 200,
|
|
@@ -249,33 +279,40 @@ function get(name, query = {}) {
|
|
|
249
279
|
if (typeof resolvedQuery === "string") {
|
|
250
280
|
resolvedQuery = { key: resolvedQuery };
|
|
251
281
|
}
|
|
252
|
-
const {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
const response = await request(state, getClient(state), path, { query: q });
|
|
282
|
+
const {
|
|
283
|
+
key = "*",
|
|
284
|
+
limit,
|
|
285
|
+
pageSize,
|
|
286
|
+
...userQuery
|
|
287
|
+
} = expandQuery(resolvedQuery);
|
|
288
|
+
console.log(`Collections: Downloading data from "${name}"...`);
|
|
261
289
|
let data;
|
|
262
|
-
if (
|
|
290
|
+
if (key.match(/\*/) || Object.keys(userQuery).length) {
|
|
263
291
|
data = [];
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
292
|
+
const scopedState = {
|
|
293
|
+
...state,
|
|
294
|
+
data: {}
|
|
295
|
+
};
|
|
296
|
+
await each(name, resolvedQuery, (state2, value, key2) => {
|
|
297
|
+
data.push({ value, key: key2 });
|
|
298
|
+
})(scopedState);
|
|
299
|
+
console.log(
|
|
300
|
+
`Collections: Fetched total of ${data.length} values from "${name}"`
|
|
301
|
+
);
|
|
302
|
+
data.cursor = scopedState.data.cursor;
|
|
271
303
|
} else {
|
|
304
|
+
const response = await request(
|
|
305
|
+
state,
|
|
306
|
+
getClient(state),
|
|
307
|
+
`${resolvedName}/${key}`
|
|
308
|
+
);
|
|
272
309
|
const body = await response.body.json();
|
|
273
310
|
if (body.value) {
|
|
274
311
|
data = JSON.parse(body.value);
|
|
275
|
-
console.log(`Fetched "${key}" from
|
|
312
|
+
console.log(`Collections: Fetched "${key}" from "${name}"`);
|
|
276
313
|
} else {
|
|
277
314
|
data = {};
|
|
278
|
-
console.warn(`Key "${key}" not found in
|
|
315
|
+
console.warn(`Collections: Key "${key}" not found in "${name}"`);
|
|
279
316
|
}
|
|
280
317
|
}
|
|
281
318
|
state.data = data;
|
|
@@ -304,7 +341,7 @@ function set(name, keyGen, values) {
|
|
|
304
341
|
});
|
|
305
342
|
if (response.statusCode >= 400) {
|
|
306
343
|
console.log(
|
|
307
|
-
`Error setting ${pairs.length} values in
|
|
344
|
+
`Collections: Error setting ${pairs.length} values in "${name}"`
|
|
308
345
|
);
|
|
309
346
|
const text = await response.body.text();
|
|
310
347
|
const e = new Error("ERROR from collections server:" + 400);
|
|
@@ -312,28 +349,33 @@ function set(name, keyGen, values) {
|
|
|
312
349
|
throw e;
|
|
313
350
|
}
|
|
314
351
|
const result = await response.body.json();
|
|
315
|
-
console.log(`
|
|
352
|
+
console.log(`Collections: set ${result.upserted} values in "${name}"`);
|
|
316
353
|
if (result.error) {
|
|
317
|
-
console.log(`
|
|
354
|
+
console.log(`Collections: errors reported on set:`, result.error);
|
|
318
355
|
}
|
|
319
356
|
return state;
|
|
320
357
|
};
|
|
321
358
|
}
|
|
322
359
|
function remove(name, query = {}, options = {}) {
|
|
323
360
|
return async (state) => {
|
|
324
|
-
|
|
361
|
+
let [resolvedName, resolvedQuery] = expandReferences(state, name, query);
|
|
362
|
+
if (typeof resolvedQuery === "string") {
|
|
363
|
+
resolvedQuery = { key: resolvedQuery };
|
|
364
|
+
}
|
|
325
365
|
const { key, ...rest } = expandQuery(resolvedQuery);
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
366
|
+
let q;
|
|
367
|
+
let path = resolvedName;
|
|
368
|
+
if (key.match(/\*/) || Object.keys(rest).length) {
|
|
369
|
+
q = resolvedQuery;
|
|
370
|
+
} else {
|
|
371
|
+
path = `${resolvedName}/${key}`;
|
|
372
|
+
}
|
|
373
|
+
const response = await request(state, getClient(state), path, {
|
|
374
|
+
method: "DELETE",
|
|
375
|
+
query: q
|
|
376
|
+
});
|
|
335
377
|
const result = await response.body.json();
|
|
336
|
-
console.log(`
|
|
378
|
+
console.log(`Collections: Removed ${result.deleted} values in "${name}"`);
|
|
337
379
|
return state;
|
|
338
380
|
};
|
|
339
381
|
}
|
|
@@ -347,14 +389,36 @@ function each(name, query = {}, callback = () => {
|
|
|
347
389
|
} else {
|
|
348
390
|
q = resolvedQuery;
|
|
349
391
|
}
|
|
350
|
-
const {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
392
|
+
const {
|
|
393
|
+
key = "*",
|
|
394
|
+
pageSize = 1e3,
|
|
395
|
+
limit = Infinity,
|
|
396
|
+
...finalQuery
|
|
397
|
+
} = expandQuery(resolvedQuery);
|
|
398
|
+
let { cursor } = resolvedQuery;
|
|
399
|
+
let count = 0;
|
|
400
|
+
do {
|
|
401
|
+
let batchSize = 0;
|
|
402
|
+
const q2 = {
|
|
403
|
+
...finalQuery,
|
|
404
|
+
key,
|
|
405
|
+
limit: limit < Infinity ? Math.min(pageSize, limit - count) : pageSize
|
|
406
|
+
};
|
|
407
|
+
if (cursor) {
|
|
408
|
+
q2.cursor = cursor;
|
|
409
|
+
}
|
|
410
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
411
|
+
query: q2
|
|
412
|
+
});
|
|
413
|
+
cursor = await streamResponse(response, async ({ key: key2, value }) => {
|
|
414
|
+
batchSize++;
|
|
415
|
+
await callback(state, JSON.parse(value), key2);
|
|
416
|
+
});
|
|
417
|
+
count += batchSize;
|
|
418
|
+
console.log(
|
|
419
|
+
`Collections: fetched chunk of ${batchSize} values from "${name}"`
|
|
420
|
+
);
|
|
421
|
+
} while (cursor && count < limit);
|
|
358
422
|
state.data = {
|
|
359
423
|
cursor
|
|
360
424
|
};
|
|
@@ -383,6 +447,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
383
447
|
if (next.value.value === "cursor") {
|
|
384
448
|
const strValue = await waitFor("stringChunk", "nullValue");
|
|
385
449
|
if (strValue.name === "nullValue") {
|
|
450
|
+
cursor = null;
|
|
386
451
|
continue;
|
|
387
452
|
}
|
|
388
453
|
cursor = strValue.value.value;
|
|
@@ -428,9 +493,7 @@ var expandQuery = (query) => {
|
|
|
428
493
|
};
|
|
429
494
|
var queryMaps = {
|
|
430
495
|
createdBefore: "created_before",
|
|
431
|
-
createdAfter: "created_after"
|
|
432
|
-
updatedBefore: "updated_before",
|
|
433
|
-
updatedAfter: "updated_after"
|
|
496
|
+
createdAfter: "created_after"
|
|
434
497
|
};
|
|
435
498
|
var parseQuery = (options = {}) => {
|
|
436
499
|
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,13 +3,14 @@ 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
|
-
remove: (name: any, key: any) =>
|
|
13
|
+
remove: (name: any, key: any) => string[];
|
|
13
14
|
byKey: (name: any, key: any) => any;
|
|
14
15
|
asJSON: (name: any, key: any) => any;
|
|
15
16
|
};
|