@openfn/language-collections 0.5.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/dist/index.cjs CHANGED
@@ -78,12 +78,12 @@ function API() {
78
78
  const col = collections[name];
79
79
  const items = [];
80
80
  let idx = 0;
81
- let count = 0;
81
+ let count2 = 0;
82
82
  const regex = new RegExp(key.replace("*", "(.*)"));
83
83
  for (const key2 in col) {
84
84
  if (idx >= cursor) {
85
85
  if (regex.test(key2)) {
86
- count++;
86
+ count2++;
87
87
  items.push({
88
88
  key: key2,
89
89
  value: col[key2]
@@ -91,11 +91,11 @@ function API() {
91
91
  }
92
92
  }
93
93
  idx++;
94
- if (count >= limit) {
94
+ if (count2 >= limit) {
95
95
  break;
96
96
  }
97
97
  }
98
- const result = { items, count };
98
+ const result = { items, count: count2 };
99
99
  if (idx < Object.keys(col).length) {
100
100
  result.cursor = idx;
101
101
  }
@@ -107,6 +107,9 @@ function API() {
107
107
  const asJSON = (name, key) => {
108
108
  return JSON.parse(collections[name][key]);
109
109
  };
110
+ const count = (name) => {
111
+ return Object.keys(collections[name]).length;
112
+ };
110
113
  const remove2 = (name, key) => {
111
114
  if (!(name in collections)) {
112
115
  throw new Error(COLLECTION_NOT_FOUND);
@@ -130,7 +133,8 @@ function API() {
130
133
  fetch,
131
134
  remove: remove2,
132
135
  byKey,
133
- asJSON
136
+ asJSON,
137
+ count
134
138
  };
135
139
  return api;
136
140
  }
@@ -346,6 +350,7 @@ function get(name, query = {}) {
346
350
  }
347
351
  function set(name, keyGen, values) {
348
352
  return async (state) => {
353
+ const batchSize = 1e3;
349
354
  const [resolvedName, resolvedValues] = (0, import_util.expandReferences)(
350
355
  state,
351
356
  name,
@@ -357,26 +362,32 @@ function set(name, keyGen, values) {
357
362
  key: keyGenFn(value, index),
358
363
  value: JSON.stringify(value)
359
364
  }));
360
- const response = await request(state, getClient(state), resolvedName, {
361
- method: "POST",
362
- body: JSON.stringify({ items: pairs }),
363
- headers: {
364
- "content-type": "application/json"
365
- }
366
- });
367
- if (response.statusCode >= 400) {
365
+ while (pairs.length) {
366
+ const batch = pairs.splice(0, batchSize);
368
367
  console.log(
369
- `Collections: Error setting ${pairs.length} values in "${name}"`
368
+ `Collections: uploading batch of ${batch.length} values to "${name}"...`
370
369
  );
371
- const text = await response.body.text();
372
- const e = new Error("ERROR from collections server:" + 400);
373
- e.body = text;
374
- throw e;
375
- }
376
- const result = await response.body.json();
377
- console.log(`Collections: set ${result.upserted} values in "${name}"`);
378
- if (result.error) {
379
- console.log(`Collections: errors reported on set:`, result.error);
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
+ }
380
391
  }
381
392
  return state;
382
393
  };
package/dist/index.js CHANGED
@@ -53,12 +53,12 @@ function API() {
53
53
  const col = collections[name];
54
54
  const items = [];
55
55
  let idx = 0;
56
- let count = 0;
56
+ let count2 = 0;
57
57
  const regex = new RegExp(key.replace("*", "(.*)"));
58
58
  for (const key2 in col) {
59
59
  if (idx >= cursor) {
60
60
  if (regex.test(key2)) {
61
- count++;
61
+ count2++;
62
62
  items.push({
63
63
  key: key2,
64
64
  value: col[key2]
@@ -66,11 +66,11 @@ function API() {
66
66
  }
67
67
  }
68
68
  idx++;
69
- if (count >= limit) {
69
+ if (count2 >= limit) {
70
70
  break;
71
71
  }
72
72
  }
73
- const result = { items, count };
73
+ const result = { items, count: count2 };
74
74
  if (idx < Object.keys(col).length) {
75
75
  result.cursor = idx;
76
76
  }
@@ -82,6 +82,9 @@ function API() {
82
82
  const asJSON = (name, key) => {
83
83
  return JSON.parse(collections[name][key]);
84
84
  };
85
+ const count = (name) => {
86
+ return Object.keys(collections[name]).length;
87
+ };
85
88
  const remove2 = (name, key) => {
86
89
  if (!(name in collections)) {
87
90
  throw new Error(COLLECTION_NOT_FOUND);
@@ -105,7 +108,8 @@ function API() {
105
108
  fetch,
106
109
  remove: remove2,
107
110
  byKey,
108
- asJSON
111
+ asJSON,
112
+ count
109
113
  };
110
114
  return api;
111
115
  }
@@ -321,6 +325,7 @@ function get(name, query = {}) {
321
325
  }
322
326
  function set(name, keyGen, values) {
323
327
  return async (state) => {
328
+ const batchSize = 1e3;
324
329
  const [resolvedName, resolvedValues] = expandReferences(
325
330
  state,
326
331
  name,
@@ -332,26 +337,32 @@ function set(name, keyGen, values) {
332
337
  key: keyGenFn(value, index),
333
338
  value: JSON.stringify(value)
334
339
  }));
335
- const response = await request(state, getClient(state), resolvedName, {
336
- method: "POST",
337
- body: JSON.stringify({ items: pairs }),
338
- headers: {
339
- "content-type": "application/json"
340
- }
341
- });
342
- if (response.statusCode >= 400) {
340
+ while (pairs.length) {
341
+ const batch = pairs.splice(0, batchSize);
343
342
  console.log(
344
- `Collections: Error setting ${pairs.length} values in "${name}"`
343
+ `Collections: uploading batch of ${batch.length} values to "${name}"...`
345
344
  );
346
- const text = await response.body.text();
347
- const e = new Error("ERROR from collections server:" + 400);
348
- e.body = text;
349
- throw e;
350
- }
351
- const result = await response.body.json();
352
- console.log(`Collections: set ${result.upserted} values in "${name}"`);
353
- if (result.error) {
354
- console.log(`Collections: errors reported on set:`, result.error);
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
+ }
355
366
  }
356
367
  return state;
357
368
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-collections",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "OpenFn collections adaptor",
5
5
  "type": "module",
6
6
  "exports": {
package/types/mock.d.ts CHANGED
@@ -13,6 +13,7 @@ export function API(): {
13
13
  remove: (name: any, key: any) => string[];
14
14
  byKey: (name: any, key: any) => any;
15
15
  asJSON: (name: any, key: any) => any;
16
+ count: (name: any) => number;
16
17
  };
17
18
  export function createServer(url?: string): {
18
19
  api: any;