@openfn/language-collections 0.6.2 → 0.7.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 +13 -4
- package/dist/index.js +13 -4
- package/package.json +1 -1
- package/types/collections.d.ts +5 -4
package/dist/index.cjs
CHANGED
|
@@ -385,10 +385,19 @@ function set(name, keyGen, values) {
|
|
|
385
385
|
} else {
|
|
386
386
|
const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
|
|
387
387
|
const keyGenFn = typeof keyGen === "string" ? () => keyGen : keyGen;
|
|
388
|
-
kvPairs = dataArray.map((value, index) =>
|
|
389
|
-
key
|
|
390
|
-
|
|
391
|
-
|
|
388
|
+
kvPairs = dataArray.map((value, index) => {
|
|
389
|
+
const key = keyGenFn(value, state, index);
|
|
390
|
+
if (typeof key !== "string") {
|
|
391
|
+
const e = new Error("KEYGEN_ERROR");
|
|
392
|
+
e.description = "The key generator function returned a non-string value which is not a valid key";
|
|
393
|
+
e.fix = `The second argument to set() is a key or key-generator function. If you pass a function, make sure the function returns a string (a key). Note that this function is NOT a state reference and does not support lazy state ($)`;
|
|
394
|
+
throw e;
|
|
395
|
+
}
|
|
396
|
+
return {
|
|
397
|
+
key,
|
|
398
|
+
value: JSON.stringify(value)
|
|
399
|
+
};
|
|
400
|
+
});
|
|
392
401
|
}
|
|
393
402
|
while (kvPairs.length) {
|
|
394
403
|
const batch = kvPairs.splice(0, batchSize);
|
package/dist/index.js
CHANGED
|
@@ -360,10 +360,19 @@ function set(name, keyGen, values) {
|
|
|
360
360
|
} else {
|
|
361
361
|
const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
|
|
362
362
|
const keyGenFn = typeof keyGen === "string" ? () => keyGen : keyGen;
|
|
363
|
-
kvPairs = dataArray.map((value, index) =>
|
|
364
|
-
key
|
|
365
|
-
|
|
366
|
-
|
|
363
|
+
kvPairs = dataArray.map((value, index) => {
|
|
364
|
+
const key = keyGenFn(value, state, index);
|
|
365
|
+
if (typeof key !== "string") {
|
|
366
|
+
const e = new Error("KEYGEN_ERROR");
|
|
367
|
+
e.description = "The key generator function returned a non-string value which is not a valid key";
|
|
368
|
+
e.fix = `The second argument to set() is a key or key-generator function. If you pass a function, make sure the function returns a string (a key). Note that this function is NOT a state reference and does not support lazy state ($)`;
|
|
369
|
+
throw e;
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
key,
|
|
373
|
+
value: JSON.stringify(value)
|
|
374
|
+
};
|
|
375
|
+
});
|
|
367
376
|
}
|
|
368
377
|
while (kvPairs.length) {
|
|
369
378
|
const batch = kvPairs.splice(0, batchSize);
|
package/package.json
CHANGED
package/types/collections.d.ts
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* @example <caption>Get a range of values from a collection with a key pattern</caption>
|
|
26
26
|
* collections.get('my-collection', '2024*')
|
|
27
27
|
* @example <caption>Get all values created since the end of January 2024</caption>
|
|
28
|
-
* collections.get('my-collection', { createdAfter: '202401')
|
|
28
|
+
* collections.get('my-collection', { createdAfter: '202401'})
|
|
29
29
|
*/
|
|
30
30
|
export function get(name: string, query?: string | QueryOptions): (state: any) => Promise<any>;
|
|
31
31
|
/**
|
|
@@ -36,6 +36,7 @@ export function get(name: string, query?: string | QueryOptions): (state: any) =
|
|
|
36
36
|
* a string key.
|
|
37
37
|
* @public
|
|
38
38
|
* @function
|
|
39
|
+
* @param {string} name - The name of the collection to fetch from
|
|
39
40
|
* @param keygen - a function which generates a key for each value: (value, index) => key. Pass a string to set a static key for a single item.
|
|
40
41
|
* @param values - an array of values to set, or a single value.
|
|
41
42
|
* @example <caption>Set a number of values using each value's id property as a key</caption>
|
|
@@ -43,9 +44,9 @@ export function get(name: string, query?: string | QueryOptions): (state: any) =
|
|
|
43
44
|
* @example <caption>Set a number of values, generating an id from a string template</caption>
|
|
44
45
|
* collections.set('my-collection', (item) => `${item.category}-${Date.now()}`, $.data)
|
|
45
46
|
* @example <caption>Set a single value with a static key</caption>
|
|
46
|
-
* collections.set('my-collection', 'city-codes', { NY: 'New York', LDN: 'London' }
|
|
47
|
+
* collections.set('my-collection', 'city-codes', { NY: 'New York', LDN: 'London' })
|
|
47
48
|
*/
|
|
48
|
-
export function set(name:
|
|
49
|
+
export function set(name: string, keyGen: any, values: any, ...args: any[]): (state: any) => Promise<any>;
|
|
49
50
|
/**
|
|
50
51
|
* Remove one or more values from a collection.
|
|
51
52
|
* You can pass a specific key as a string to only fetch one item, or pass a query
|
|
@@ -59,7 +60,7 @@ export function set(name: any, keyGen: any, values: any, ...args: any[]): (state
|
|
|
59
60
|
* @example <caption>Remove a range of values from a collection with a key pattern</caption>
|
|
60
61
|
* collections.remove('my-collection', '2024*')
|
|
61
62
|
* @example <caption>Remove all values created since the end of January 2024</caption>
|
|
62
|
-
* collections.remove('my-collection', { createdAfter: '202401')
|
|
63
|
+
* collections.remove('my-collection', { createdAfter: '202401'})
|
|
63
64
|
*/
|
|
64
65
|
export function remove(name: string, query?: string | QueryOptions, options?: {}): (state: any) => Promise<any>;
|
|
65
66
|
/**
|