Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from OpenFn/use_expandreferences_from_lang_common
Browse files Browse the repository at this point in the history
Use expandReferences from language-common
  • Loading branch information
taylordowns2000 committed May 6, 2021
2 parents 9f288be + 6000f67 commit be0106d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 153 deletions.
87 changes: 15 additions & 72 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
});
exports.execute = execute;
exports.steps = steps;
exports.recursivelyExpandReferences = recursivelyExpandReferences;
Object.defineProperty(exports, "alterState", {
enumerable: true,
get: function () {
Expand Down Expand Up @@ -185,9 +184,6 @@ const describe = (0, _lodashFp.curry)(function (sObject, state) {
return { ...state,
references: [result, ...state.references]
};
}).catch(function (err) {
console.error(err);
return err;
});
});
/**
Expand All @@ -208,7 +204,7 @@ const retrieve = (0, _lodashFp.curry)(function (sObject, id, callback, state) {
let {
connection
} = state;
const finalId = recursivelyExpandReferences(id)(state);
const finalId = (0, _languageCommon.expandReferences)(id)(state);
return connection.sobject(sObject).retrieve(finalId).then(result => {
return { ...state,
references: [result, ...state.references]
Expand All @@ -219,13 +215,12 @@ const retrieve = (0, _lodashFp.curry)(function (sObject, id, callback, state) {
}

return state;
}).catch(function (err) {
console.error(err);
return err;
});
});
/**
* Execute an SOQL query.
* Note that in an event of a query error,
* error logs will be printed but the operation will not throw the error.
* @public
* @example
* query(`SELECT Id FROM Patient__c WHERE Health_ID__c = '${state.data.field1}'`);
Expand All @@ -238,8 +233,7 @@ const retrieve = (0, _lodashFp.curry)(function (sObject, id, callback, state) {
exports.retrieve = retrieve;
const query = (0, _lodashFp.curry)(function (qs, state) {
let {
connection,
references
connection
} = state;
console.log(`Executing query: ${qs}`);
return connection.query(qs, function (err, result) {
Expand Down Expand Up @@ -274,8 +268,7 @@ const query = (0, _lodashFp.curry)(function (qs, state) {
exports.query = query;
const bulk = (0, _lodashFp.curry)(function (sObject, operation, options, fun, state) {
let {
connection,
references
connection
} = state;
let {
failOnError,
Expand Down Expand Up @@ -339,10 +332,9 @@ const bulk = (0, _lodashFp.curry)(function (sObject, operation, options, fun, st
exports.bulk = bulk;
const create = (0, _lodashFp.curry)(function (sObject, attrs, state) {
let {
connection,
references
connection
} = state;
const finalAttrs = expandReferences(state, attrs);
const finalAttrs = (0, _languageCommon.expandReferences)(attrs)(state);
console.info(`Creating ${sObject}`, finalAttrs);
return connection.create(sObject, finalAttrs).then(function (recordResult) {
console.log('Result : ' + JSON.stringify(recordResult));
Expand Down Expand Up @@ -370,10 +362,9 @@ const create = (0, _lodashFp.curry)(function (sObject, attrs, state) {
exports.create = create;
const createIf = (0, _lodashFp.curry)(function (logical, sObject, attrs, state) {
let {
connection,
references
connection
} = state;
const finalAttrs = expandReferences(state, attrs);
const finalAttrs = (0, _languageCommon.expandReferences)(attrs)(state);

if (logical) {
console.info(`Creating ${sObject}`, finalAttrs);
Expand Down Expand Up @@ -412,10 +403,9 @@ const createIf = (0, _lodashFp.curry)(function (logical, sObject, attrs, state)
exports.createIf = createIf;
const upsert = (0, _lodashFp.curry)(function (sObject, externalId, attrs, state) {
let {
connection,
references
connection
} = state;
const finalAttrs = expandReferences(state, attrs);
const finalAttrs = (0, _languageCommon.expandReferences)(attrs)(state);
console.info(`Upserting ${sObject} with externalId`, externalId, ':', finalAttrs);
return connection.upsert(sObject, finalAttrs, externalId).then(function (recordResult) {
console.log('Result : ' + JSON.stringify(recordResult));
Expand Down Expand Up @@ -444,10 +434,9 @@ const upsert = (0, _lodashFp.curry)(function (sObject, externalId, attrs, state)
exports.upsert = upsert;
const upsertIf = (0, _lodashFp.curry)(function (logical, sObject, externalId, attrs, state) {
let {
connection,
references
connection
} = state;
const finalAttrs = expandReferences(state, attrs);
const finalAttrs = (0, _languageCommon.expandReferences)(attrs)(state);

if (logical) {
console.info(`Upserting ${sObject} with externalId`, externalId, ':', finalAttrs);
Expand Down Expand Up @@ -485,10 +474,9 @@ const upsertIf = (0, _lodashFp.curry)(function (logical, sObject, externalId, at
exports.upsertIf = upsertIf;
const update = (0, _lodashFp.curry)(function (sObject, attrs, state) {
let {
connection,
references
connection
} = state;
const finalAttrs = expandReferences(state, attrs);
const finalAttrs = (0, _languageCommon.expandReferences)(attrs)(state);
console.info(`Updating ${sObject}`, finalAttrs);
return connection.update(sObject, finalAttrs).then(function (recordResult) {
console.log('Result : ' + JSON.stringify(recordResult));
Expand Down Expand Up @@ -625,50 +613,5 @@ function cleanupState(state) {
function steps(...operations) {
return (0, _lodashFp.flatten)(operations);
}
/**
* Recursively expand object|number|string|boolean|array, each time resolving function calls and returning the resolved values
* @param {any} thing - Thing to expand
* @returns {object|number|string|boolean|array} expandedResult
*/


function recursivelyExpandReferences(thing) {
return state => {
if (typeof thing !== 'object') return typeof thing == 'function' ? thing(state) : thing;
let result = (0, _lodashFp.mapValues)(function (value) {
if (Array.isArray(value)) {
return value.map(item => {
return recursivelyExpandReferences(item)(state);
});
} else {
return recursivelyExpandReferences(value)(state);
}
})(thing);
if (Array.isArray(thing)) result = Object.values(result);
return result;
};
}
/**
* Expands references.
* @example
* expandReferences(
* state,
* {
* attr1: "foo",
* attr2: "bar"
* }
* )
* @function
* @param {State} state - Runtime state.
* @param {Object} attrs - Field attributes for the new object.
* @returns {State}
*/


function expandReferences(state, attrs) {
return (0, _lodashFp.mapValues)(function (value) {
return typeof value == 'function' ? value(state) : value;
})(attrs);
}

exports.axios = _axios.default;
21 changes: 11 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Adaptor = exports.FakeAdaptor = undefined;
exports.FakeAdaptor = exports.Adaptor = exports.default = void 0;

var _Adaptor = require('./Adaptor');
var Adaptor = _interopRequireWildcard(require("./Adaptor"));

var Adaptor = _interopRequireWildcard(_Adaptor);
exports.Adaptor = Adaptor;

var _FakeAdaptor = require('./FakeAdaptor');
var FakeAdaptor = _interopRequireWildcard(require("./FakeAdaptor"));

var FakeAdaptor = _interopRequireWildcard(_FakeAdaptor);
exports.FakeAdaptor = FakeAdaptor;

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

exports.default = Adaptor;
exports.FakeAdaptor = FakeAdaptor;
exports.Adaptor = Adaptor;
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var _default = Adaptor;
exports.default = _default;
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"lib/"
],
"dependencies": {
"@openfn/language-common": "1.2.6",
"@openfn/language-common": "1.2.8",
"JSONPath": "^0.10.0",
"jsforce": "^1.9.3",
"lodash-fp": "^0.10.2",
Expand Down
Loading

0 comments on commit be0106d

Please sign in to comment.