Skip to content

Commit

Permalink
Airtable Create & Update Record actions - Column names as distinct pr…
Browse files Browse the repository at this point in the history
…ops (PipedreamHQ#2600)

* Add fields as additional props to Airtable actions

Updates Create Single Record and Update Record to use $.airtable.table prop

* Add fields as additional props to Airtable actions

Updates Create Single Record and Update Record to use $.airtable.table prop

* Convert Airtable components from CJS to ESM

* Rev Airtable component versions

Increments major version for Create Single Record & Update Record
actions

Increment patch version of other Airtable components

* Add JSDocs for Airtable utils

* Fix .eslintrc putout/multiple-properties-destructuring minProperties

Changes minProperties from 1 -> 2 to avoid conflicting linter errors
when destructuring a single property

* Add ConfigurationError in additionalProps & async options

in Airtable Create Single Record & Update Record actions

Refactored/removed some guard clauses

* Update prop descriptions for tableId,viewId,sortFieldId

Instructs to use a custom expression when referencing previous props
dynamically

* Show record prop on additionalProps error if manual table input
  • Loading branch information
js07 authored May 24, 2022
1 parent fc280dd commit 6238aec
Show file tree
Hide file tree
Showing 27 changed files with 699 additions and 398 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"putout/multiple-properties-destructuring": [
"error",
{
"minProperties": 1
"minProperties": 2
}
],
"putout/single-property-destructuring": "error",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Shared code for list-* actions
const airtable = require("../airtable.app.js");
import airtable from "../airtable.app.mjs";

module.exports = {
export default {
props: {
sortFieldId: {
propDefinition: [
airtable,
"sortFieldId",
({
baseId, tableId,
}) => ({
baseId,
tableId,
}),
],
},
sortDirection: {
Expand All @@ -28,12 +34,16 @@ module.exports = {
],
},
},
async run() {
const base = this.airtable.base(this.baseId);
async run({ $ }) {
const baseId = this.baseId?.value ?? this.baseId;
const tableId = this.tableId?.value ?? this.tableId;
const viewId = this.viewId?.value ?? this.viewId;

const base = this.airtable.base(baseId);
const data = [];
const config = {};

if (this.viewId) { config.view = this.viewId; }
if (viewId) { config.view = viewId; }
if (this.filterByFormula) { config.filterByFormula = this.filterByFormula; }
if (this.maxRecords) { config.maxRecords = this.maxRecords; }
if (this.sortFieldId && this.sortDirection) {
Expand All @@ -45,7 +55,7 @@ module.exports = {
];
}

await base(this.tableId).select({
await base(tableId).select({
...config,
})
.eachPage(function page(records, fetchNextPage) {
Expand All @@ -60,6 +70,11 @@ module.exports = {
fetchNextPage();
});

const l = data.length;
$.export("$summary", `Fetched ${l} record${l === 1
? ""
// eslint-disable-next-line multiline-ternary
: "s"} from ${this.baseId?.label || baseId}: [${this.tableId?.label || tableId}](https://airtable.com/${baseId}/${tableId}${viewId ? `/${viewId}` : ""})`);
return data;
},
};
15 changes: 0 additions & 15 deletions components/airtable/actions/common.js

This file was deleted.

24 changes: 24 additions & 0 deletions components/airtable/actions/common.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import airtable from "../airtable.app.mjs";

export default {
props: {
airtable,
baseId: {
propDefinition: [
airtable,
"baseId",
],
withLabel: true,
},
tableId: {
propDefinition: [
airtable,
"tableId",
({ baseId }) => ({
baseId,
}),
],
withLabel: true,
},
},
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const chunk = require("lodash.chunk");
const airtable = require("../../airtable.app.js");
const common = require("../common.js");
import chunk from "lodash.chunk";
import airtable from "../../airtable.app.mjs";
import common from "../common.mjs";

const BATCH_SIZE = 10; // The Airtable API allows us to update up to 10 rows per request.

module.exports = {
export default {
key: "airtable-create-multiple-records",
name: "Create Multiple Records",
description: "Create one or more records in a table by passing an array of objects containing field names and values as key/value pairs.",
version: "0.1.2",
version: "0.2.0",
type: "action",
props: {
...common.props,
Expand All @@ -25,8 +25,11 @@ module.exports = {
],
},
},
async run() {
const table = this.airtable.base(this.baseId)(this.tableId);
async run({ $ }) {
const baseId = this.baseId?.value ?? this.baseId;
const tableId = this.tableId?.value ?? this.tableId;

const table = this.airtable.base(baseId)(tableId);

let data = this.records;
if (!Array.isArray(data)) {
Expand All @@ -52,6 +55,11 @@ module.exports = {
}
}

const l = responses.length;
$.export("$summary", `Added ${l} record${l === 1
? ""
: "s"} to ${this.baseId?.label || baseId}: [${this.tableId?.label || tableId}](https://airtable.com/${baseId}/${tableId})`);

return responses;
},
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import airtable from "../../airtable.app.mjs";
import {
makeFieldProps,
makeRecord,
} from "../../common/utils.mjs";
import common from "../common.mjs";
import { ConfigurationError } from "@pipedream/platform";

export default {
key: "airtable-create-single-record",
name: "Create single record",
description: "Adds a record to a table.",
version: "1.0.0",
type: "action",
props: {
...common.props,
// eslint-disable-next-line pipedream/props-label,pipedream/props-description
tableId: {
...common.props.tableId,
reloadProps: true,
},
typecast: {
propDefinition: [
airtable,
"typecast",
],
},
},
async additionalProps() {
const baseId = this.baseId?.value ?? this.baseId;
const tableId = this.tableId?.value ?? this.tableId;
try {
const tableSchema = await this.airtable.table(baseId, tableId);
return makeFieldProps(tableSchema);
} catch (err) {
const hasManualTableInput = !this.tableId?.label;
// If manual input and .table throws error, return a record prop
// otherwise, throw ConfigurationError
if (hasManualTableInput) {
return {
// Use record propDefinition directly to workaround lack of support
// for propDefinition in additionalProps
record: airtable.propDefinitions.record,
};
}
throw new ConfigurationError("Could not find a table for the specified base ID and table ID. Please adjust the action configuration to continue.");
}
},
async run({ $ }) {
const baseId = this.baseId?.value ?? this.baseId;
const tableId = this.tableId?.value ?? this.tableId;

const table = this.airtable.base(baseId)(tableId);

const record = this.record ?? makeRecord(this);

this.airtable.validateRecord(record);

const data = [
{
fields: record,
},
];

const params = {
typecast: this.typecast,
};

let response;
try {
[
response,
] = await table.create(data, params);
} catch (err) {
this.airtable.throwFormattedError(err);
}

$.export("$summary", `Added 1 record to ${this.baseId?.label || baseId}: [${this.tableId?.label || tableId}](https://airtable.com/${baseId}/${tableId})`);
return response;
},
};
28 changes: 0 additions & 28 deletions components/airtable/actions/delete-record/delete-record.js

This file was deleted.

36 changes: 36 additions & 0 deletions components/airtable/actions/delete-record/delete-record.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import airtable from "../../airtable.app.mjs";
import common from "../common.mjs";

export default {
key: "airtable-delete-record",
name: "Delete Record",
description: "Delete a record from a table by record ID.",
version: "0.2.0",
type: "action",
props: {
...common.props,
recordId: {
propDefinition: [
airtable,
"recordId",
],
},
},
async run({ $ }) {
const baseId = this.baseId?.value ?? this.baseId;
const tableId = this.tableId?.value ?? this.tableId;
const recordId = this.recordId?.value ?? this.recordId;

this.airtable.validateRecordID(recordId);
const base = this.airtable.base(baseId);
let response;
try {
response = await base(this.tableId.value).destroy(recordId);
} catch (err) {
this.airtable.throwFormattedError(err);
}

$.export("$summary", `Deleted record "${this.recordId?.label || recordId}" from ${this.baseId?.label || baseId}: [${this.tableId?.label || tableId}](https://airtable.com/${baseId}/${tableId})`);
return response;
},
};
28 changes: 0 additions & 28 deletions components/airtable/actions/get-record/get-record.js

This file was deleted.

Loading

0 comments on commit 6238aec

Please sign in to comment.