Skip to content

Commit

Permalink
Merge pull request #524 from OpenFn/cursor-surveycto
Browse files Browse the repository at this point in the history
surveycto: add new cursor
  • Loading branch information
josephjclark committed May 8, 2024
2 parents 2c7aa2f + e892ccb commit dadff02
Show file tree
Hide file tree
Showing 43 changed files with 413 additions and 85 deletions.
7 changes: 7 additions & 0 deletions packages/bigquery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-bigquery

## 2.0.8

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 2.0.7

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/bigquery/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-bigquery",
"version": "2.0.7",
"version": "2.0.8",
"description": "A Google BigQuery language package for use with Open Function",
"main": "dist/index.cjs",
"scripts": {
Expand All @@ -21,7 +21,7 @@
],
"dependencies": {
"@google-cloud/bigquery": "^5.12.0",
"@openfn/language-common": "workspace:1.13.2",
"@openfn/language-common": "workspace:1.13.3",
"csv-parse": "^4.16.3",
"import": "0.0.6",
"json2csv": "^5.0.7",
Expand Down
7 changes: 7 additions & 0 deletions packages/commcare/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-commcare

## 1.6.14

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 1.6.13

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/commcare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-commcare",
"version": "1.6.13",
"version": "1.6.14",
"description": "Commcare Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand All @@ -25,7 +25,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "workspace:1.13.2",
"@openfn/language-common": "workspace:1.13.3",
"@openfn/language-http": "workspace:^6.0.0",
"JSONPath": "^0.10.0",
"form-data": "^4.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
v0.4.0

## 1.13.3

### Patch Changes

- 88f99a8f: cursor: support format option

## 1.13.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-common",
"version": "1.13.2",
"version": "1.13.3",
"description": "Common Expressions for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
26 changes: 18 additions & 8 deletions packages/common/src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { parse } from 'csv-parse';
import { Readable } from 'node:stream';

import { request } from 'undici';
import { format } from 'date-fns';
import dateFns from 'date-fns';

import { expandReferences as newExpandReferences, parseDate } from './util';

Expand Down Expand Up @@ -787,6 +787,8 @@ let cursorKey = 'cursor';
* Supports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,
* which will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones
* are not yet supported.
* You can provide a formatter to customise the final cursor value, which is useful for normalising
* different inputs. The custom formatter runs after natural language date conversion.
* See the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}
* @public
* @example <caption>Use a cursor from state if present, or else use the default value</caption>
Expand All @@ -798,15 +800,18 @@ let cursorKey = 'cursor';
* @param {object} options - options to control the cursor.
* @param {string} options.key - set the cursor key. Will persist through the whole run.
* @param {any} options.defaultValue - the value to use if value is falsy
* @param {Function} options.format - custom formatter for the final cursor value
* @returns {Operation}
*/
export function cursor(value, options = {}) {
return (state) => {
const [resolvedValue, resolvedOptions] = newExpandReferences(state, value, options);
const { format, ...optionsWithoutFormat } = options;
const [resolvedValue, resolvedOptions] = newExpandReferences(state, value, optionsWithoutFormat);

const {
defaultValue, // if there is no cursor on state, this will be used
key, // the key to use on state
// format // pulled out before reference resolution else or it'll be treated as a ref!
} = resolvedOptions;

if (key) {
Expand All @@ -821,16 +826,21 @@ export function cursor(value, options = {}) {
if (typeof cursor === 'string') {
const date = parseDate(cursor, cursorStart)
if (date instanceof Date && date.toString !== "Invalid Date") {
state[cursorKey] = date.toISOString();
// Log the converted date in a very international, human-friendly format
// See https://date-fns.org/v3.6.0/docs/format
const formatted = format(date, 'HH:MM d MMM yyyy (OOO)')
state[cursorKey] = format?.(date) ?? date.toISOString();

const formatted = format
? state[cursorKey]
// If no custom formatter is provided,
// Log the converted date in a very international, human-friendly format
// See https://date-fns.org/v3.6.0/docs/format
: dateFns.format(date, 'HH:MM d MMM yyyy (OOO)');

console.log(`Setting cursor "${cursor}" to: ${formatted}`);
return state;
}
}
state[cursorKey] = cursor;
console.log('Setting cursor to:', cursor);
state[cursorKey] = format?.(cursor) ?? cursor;
console.log('Setting cursor to:', state[cursorKey]);

return state;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/common/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,4 +932,29 @@ describe('cursor', () => {
expect(result.cursor).to.eql(c)
});

it('should apply a custom formatter', () => {
const state = {};
const result = cursor('abc', {
format: (c) => c.toUpperCase()
})(state);
expect(result.cursor).to.eql('ABC')
})

it('should format "today"', () => {
const state = {};
const date = new Date().toDateString();
const result = cursor('today', {
format: (c) => c.toDateString()
})(state);
expect(result.cursor).to.eql(date)
})

it('should format a number to an arbitrary object', () => {
const state = {};
const result = cursor(3, {
format: (c) => ({ page: c })
})(state);
expect(result.cursor).to.eql({ page: 3 })
})

});
7 changes: 7 additions & 0 deletions packages/dynamics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-dynamics

## 0.4.12

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 0.4.11

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/dynamics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-dynamics",
"version": "0.4.11",
"version": "0.4.12",
"description": "A Microsoft Dynamics Language Pack for OpenFn",
"main": "dist/index.cjs",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "1.13.2",
"@openfn/language-common": "1.13.3",
"request": "^2.72.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/msgraph/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-msgraph

## 0.5.3

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 0.5.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/msgraph/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-msgraph",
"version": "0.5.2",
"version": "0.5.3",
"description": "Microsoft Graph Language Pack for OpenFn",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/mssql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-mssql

## 4.2.2

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 4.2.1

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/mssql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-mssql",
"version": "4.2.1",
"version": "4.2.2",
"description": "A Microsoft SQL language pack for OpenFn",
"exports": {
".": {
Expand Down Expand Up @@ -31,7 +31,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "workspace:1.13.2",
"@openfn/language-common": "workspace:1.13.3",
"tedious": "15.1.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/mysql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-mysql

## 1.4.13

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 1.4.12

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-mysql",
"version": "1.4.12",
"version": "1.4.13",
"description": "A MySQL Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"main": "dist/index.cjs",
Expand All @@ -25,7 +25,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "1.13.2",
"@openfn/language-common": "1.13.3",
"json-sql": "^0.3.10",
"mysql": "^2.13.0",
"squel": "^5.8.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/nexmo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-nexmo

## 0.4.7

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 0.4.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/nexmo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-nexmo",
"version": "0.4.6",
"version": "0.4.7",
"description": "An Language Package for Nexmo",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/ocl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-ocl

## 1.1.11

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 1.1.10

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/ocl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-ocl",
"version": "1.1.10",
"version": "1.1.11",
"description": "An OCL language package for use with Open Function",
"main": "dist/index.cjs",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "1.13.2"
"@openfn/language-common": "1.13.3"
},
"devDependencies": {
"@openfn/simple-ast": "^0.4.1",
Expand Down
7 changes: 7 additions & 0 deletions packages/openfn/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-openfn

## 1.3.12

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 1.3.11

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/openfn/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-openfn",
"version": "1.3.11",
"version": "1.3.12",
"description": "An (experimental) adaptor for accessing the OpenFn web API",
"homepage": "https://docs.openfn.org",
"repository": {
Expand All @@ -25,7 +25,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "1.13.2",
"@openfn/language-common": "1.13.3",
"axios": "^0.21.1"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/openmrs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-openmrs

## 3.0.4

### Patch Changes

- Updated dependencies [88f99a8f]
- @openfn/language-common@1.13.3

## 3.0.3

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/openmrs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-openmrs",
"version": "3.0.3",
"version": "3.0.4",
"description": "OpenMRS Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand All @@ -25,7 +25,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "1.13.2"
"@openfn/language-common": "1.13.3"
},
"devDependencies": {
"@openfn/simple-ast": "^0.4.1",
Expand Down

0 comments on commit dadff02

Please sign in to comment.