@openfn/language-collections 0.5.0 → 0.5.2
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/dist/index.cjs +36 -29
- package/dist/index.js +36 -29
- package/package.json +1 -2
- package/types/mock.d.ts +1 -0
package/dist/index.cjs
CHANGED
|
@@ -46,11 +46,7 @@ __export(collections_exports, {
|
|
|
46
46
|
var import_node_path = __toESM(require("path"), 1);
|
|
47
47
|
var import_undici2 = __toESM(require("undici"), 1);
|
|
48
48
|
var import_util = require("@openfn/language-common/util");
|
|
49
|
-
var import_stream_chain = __toESM(require("stream-chain"), 1);
|
|
50
49
|
var import_stream_json = __toESM(require("stream-json"), 1);
|
|
51
|
-
var import_Pick = __toESM(require("stream-json/filters/Pick"), 1);
|
|
52
|
-
var import_StreamArray = __toESM(require("stream-json/streamers/StreamArray"), 1);
|
|
53
|
-
var import_StreamValues = __toESM(require("stream-json/streamers/StreamValues"), 1);
|
|
54
50
|
|
|
55
51
|
// src/mock.js
|
|
56
52
|
var import_undici = require("undici");
|
|
@@ -78,12 +74,12 @@ function API() {
|
|
|
78
74
|
const col = collections[name];
|
|
79
75
|
const items = [];
|
|
80
76
|
let idx = 0;
|
|
81
|
-
let
|
|
77
|
+
let count2 = 0;
|
|
82
78
|
const regex = new RegExp(key.replace("*", "(.*)"));
|
|
83
79
|
for (const key2 in col) {
|
|
84
80
|
if (idx >= cursor) {
|
|
85
81
|
if (regex.test(key2)) {
|
|
86
|
-
|
|
82
|
+
count2++;
|
|
87
83
|
items.push({
|
|
88
84
|
key: key2,
|
|
89
85
|
value: col[key2]
|
|
@@ -91,11 +87,11 @@ function API() {
|
|
|
91
87
|
}
|
|
92
88
|
}
|
|
93
89
|
idx++;
|
|
94
|
-
if (
|
|
90
|
+
if (count2 >= limit) {
|
|
95
91
|
break;
|
|
96
92
|
}
|
|
97
93
|
}
|
|
98
|
-
const result = { items, count };
|
|
94
|
+
const result = { items, count: count2 };
|
|
99
95
|
if (idx < Object.keys(col).length) {
|
|
100
96
|
result.cursor = idx;
|
|
101
97
|
}
|
|
@@ -107,6 +103,9 @@ function API() {
|
|
|
107
103
|
const asJSON = (name, key) => {
|
|
108
104
|
return JSON.parse(collections[name][key]);
|
|
109
105
|
};
|
|
106
|
+
const count = (name) => {
|
|
107
|
+
return Object.keys(collections[name]).length;
|
|
108
|
+
};
|
|
110
109
|
const remove2 = (name, key) => {
|
|
111
110
|
if (!(name in collections)) {
|
|
112
111
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
@@ -130,7 +129,8 @@ function API() {
|
|
|
130
129
|
fetch,
|
|
131
130
|
remove: remove2,
|
|
132
131
|
byKey,
|
|
133
|
-
asJSON
|
|
132
|
+
asJSON,
|
|
133
|
+
count
|
|
134
134
|
};
|
|
135
135
|
return api;
|
|
136
136
|
}
|
|
@@ -346,6 +346,7 @@ function get(name, query = {}) {
|
|
|
346
346
|
}
|
|
347
347
|
function set(name, keyGen, values) {
|
|
348
348
|
return async (state) => {
|
|
349
|
+
const batchSize = 1e3;
|
|
349
350
|
const [resolvedName, resolvedValues] = (0, import_util.expandReferences)(
|
|
350
351
|
state,
|
|
351
352
|
name,
|
|
@@ -357,26 +358,32 @@ function set(name, keyGen, values) {
|
|
|
357
358
|
key: keyGenFn(value, index),
|
|
358
359
|
value: JSON.stringify(value)
|
|
359
360
|
}));
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
body: JSON.stringify({ items: pairs }),
|
|
363
|
-
headers: {
|
|
364
|
-
"content-type": "application/json"
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
if (response.statusCode >= 400) {
|
|
361
|
+
while (pairs.length) {
|
|
362
|
+
const batch = pairs.splice(0, batchSize);
|
|
368
363
|
console.log(
|
|
369
|
-
`Collections:
|
|
364
|
+
`Collections: uploading batch of ${batch.length} values to "${name}"...`
|
|
370
365
|
);
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
366
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
367
|
+
method: "POST",
|
|
368
|
+
body: JSON.stringify({ items: batch }),
|
|
369
|
+
headers: {
|
|
370
|
+
"content-type": "application/json"
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
if (response.statusCode >= 400) {
|
|
374
|
+
console.log(
|
|
375
|
+
`Collections: Error setting ${batch.length} values in "${name}"`
|
|
376
|
+
);
|
|
377
|
+
const text = await response.body.text();
|
|
378
|
+
const e = new Error("ERROR from collections server:" + 400);
|
|
379
|
+
e.body = text;
|
|
380
|
+
throw e;
|
|
381
|
+
}
|
|
382
|
+
const result = await response.body.json();
|
|
383
|
+
console.log(`Collections: set ${result.upserted} values in "${name}"`);
|
|
384
|
+
if (result.error) {
|
|
385
|
+
console.log(`Collections: errors reported on set:`, result.error);
|
|
386
|
+
}
|
|
380
387
|
}
|
|
381
388
|
return state;
|
|
382
389
|
};
|
|
@@ -451,7 +458,7 @@ function each(name, query = {}, callback = () => {
|
|
|
451
458
|
};
|
|
452
459
|
}
|
|
453
460
|
var streamResponse = async (response, onValue) => {
|
|
454
|
-
const pipeline =
|
|
461
|
+
const pipeline = response.body.pipe((0, import_stream_json.default)());
|
|
455
462
|
let isInsideItems = false;
|
|
456
463
|
let cursor;
|
|
457
464
|
const it = pipeline.iterator();
|
|
@@ -498,7 +505,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
498
505
|
key: key.value.value,
|
|
499
506
|
value: value.value.value
|
|
500
507
|
});
|
|
501
|
-
waitFor("endObject");
|
|
508
|
+
await waitFor("endObject");
|
|
502
509
|
}
|
|
503
510
|
if (isInsideItems && token.name === "endArray") {
|
|
504
511
|
isInsideItems = false;
|
package/dist/index.js
CHANGED
|
@@ -21,11 +21,7 @@ __export(collections_exports, {
|
|
|
21
21
|
import nodepath from "path";
|
|
22
22
|
import undici from "undici";
|
|
23
23
|
import { throwError, expandReferences } from "@openfn/language-common/util";
|
|
24
|
-
import chain from "stream-chain";
|
|
25
24
|
import parser from "stream-json";
|
|
26
|
-
import Pick from "stream-json/filters/Pick";
|
|
27
|
-
import streamArray from "stream-json/streamers/StreamArray";
|
|
28
|
-
import streamValues from "stream-json/streamers/StreamValues";
|
|
29
25
|
|
|
30
26
|
// src/mock.js
|
|
31
27
|
import { MockAgent } from "undici";
|
|
@@ -53,12 +49,12 @@ function API() {
|
|
|
53
49
|
const col = collections[name];
|
|
54
50
|
const items = [];
|
|
55
51
|
let idx = 0;
|
|
56
|
-
let
|
|
52
|
+
let count2 = 0;
|
|
57
53
|
const regex = new RegExp(key.replace("*", "(.*)"));
|
|
58
54
|
for (const key2 in col) {
|
|
59
55
|
if (idx >= cursor) {
|
|
60
56
|
if (regex.test(key2)) {
|
|
61
|
-
|
|
57
|
+
count2++;
|
|
62
58
|
items.push({
|
|
63
59
|
key: key2,
|
|
64
60
|
value: col[key2]
|
|
@@ -66,11 +62,11 @@ function API() {
|
|
|
66
62
|
}
|
|
67
63
|
}
|
|
68
64
|
idx++;
|
|
69
|
-
if (
|
|
65
|
+
if (count2 >= limit) {
|
|
70
66
|
break;
|
|
71
67
|
}
|
|
72
68
|
}
|
|
73
|
-
const result = { items, count };
|
|
69
|
+
const result = { items, count: count2 };
|
|
74
70
|
if (idx < Object.keys(col).length) {
|
|
75
71
|
result.cursor = idx;
|
|
76
72
|
}
|
|
@@ -82,6 +78,9 @@ function API() {
|
|
|
82
78
|
const asJSON = (name, key) => {
|
|
83
79
|
return JSON.parse(collections[name][key]);
|
|
84
80
|
};
|
|
81
|
+
const count = (name) => {
|
|
82
|
+
return Object.keys(collections[name]).length;
|
|
83
|
+
};
|
|
85
84
|
const remove2 = (name, key) => {
|
|
86
85
|
if (!(name in collections)) {
|
|
87
86
|
throw new Error(COLLECTION_NOT_FOUND);
|
|
@@ -105,7 +104,8 @@ function API() {
|
|
|
105
104
|
fetch,
|
|
106
105
|
remove: remove2,
|
|
107
106
|
byKey,
|
|
108
|
-
asJSON
|
|
107
|
+
asJSON,
|
|
108
|
+
count
|
|
109
109
|
};
|
|
110
110
|
return api;
|
|
111
111
|
}
|
|
@@ -321,6 +321,7 @@ function get(name, query = {}) {
|
|
|
321
321
|
}
|
|
322
322
|
function set(name, keyGen, values) {
|
|
323
323
|
return async (state) => {
|
|
324
|
+
const batchSize = 1e3;
|
|
324
325
|
const [resolvedName, resolvedValues] = expandReferences(
|
|
325
326
|
state,
|
|
326
327
|
name,
|
|
@@ -332,26 +333,32 @@ function set(name, keyGen, values) {
|
|
|
332
333
|
key: keyGenFn(value, index),
|
|
333
334
|
value: JSON.stringify(value)
|
|
334
335
|
}));
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
body: JSON.stringify({ items: pairs }),
|
|
338
|
-
headers: {
|
|
339
|
-
"content-type": "application/json"
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
if (response.statusCode >= 400) {
|
|
336
|
+
while (pairs.length) {
|
|
337
|
+
const batch = pairs.splice(0, batchSize);
|
|
343
338
|
console.log(
|
|
344
|
-
`Collections:
|
|
339
|
+
`Collections: uploading batch of ${batch.length} values to "${name}"...`
|
|
345
340
|
);
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
341
|
+
const response = await request(state, getClient(state), resolvedName, {
|
|
342
|
+
method: "POST",
|
|
343
|
+
body: JSON.stringify({ items: batch }),
|
|
344
|
+
headers: {
|
|
345
|
+
"content-type": "application/json"
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
if (response.statusCode >= 400) {
|
|
349
|
+
console.log(
|
|
350
|
+
`Collections: Error setting ${batch.length} values in "${name}"`
|
|
351
|
+
);
|
|
352
|
+
const text = await response.body.text();
|
|
353
|
+
const e = new Error("ERROR from collections server:" + 400);
|
|
354
|
+
e.body = text;
|
|
355
|
+
throw e;
|
|
356
|
+
}
|
|
357
|
+
const result = await response.body.json();
|
|
358
|
+
console.log(`Collections: set ${result.upserted} values in "${name}"`);
|
|
359
|
+
if (result.error) {
|
|
360
|
+
console.log(`Collections: errors reported on set:`, result.error);
|
|
361
|
+
}
|
|
355
362
|
}
|
|
356
363
|
return state;
|
|
357
364
|
};
|
|
@@ -426,7 +433,7 @@ function each(name, query = {}, callback = () => {
|
|
|
426
433
|
};
|
|
427
434
|
}
|
|
428
435
|
var streamResponse = async (response, onValue) => {
|
|
429
|
-
const pipeline =
|
|
436
|
+
const pipeline = response.body.pipe(parser());
|
|
430
437
|
let isInsideItems = false;
|
|
431
438
|
let cursor;
|
|
432
439
|
const it = pipeline.iterator();
|
|
@@ -473,7 +480,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
473
480
|
key: key.value.value,
|
|
474
481
|
value: value.value.value
|
|
475
482
|
});
|
|
476
|
-
waitFor("endObject");
|
|
483
|
+
await waitFor("endObject");
|
|
477
484
|
}
|
|
478
485
|
if (isInsideItems && token.name === "endArray") {
|
|
479
486
|
isInsideItems = false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-collections",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "OpenFn collections adaptor",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"configuration-schema.json"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"stream-chain": "^3.3.2",
|
|
24
23
|
"stream-json": "^1.8.0",
|
|
25
24
|
"undici": "^5.22.1",
|
|
26
25
|
"@openfn/language-common": "2.1.1"
|
package/types/mock.d.ts
CHANGED