Skip to content

Commit

Permalink
New Components - grist (PipedreamHQ#11399)
Browse files Browse the repository at this point in the history
* grist init

* Tested components
  • Loading branch information
jcortes authored Apr 17, 2024
1 parent 5223ef0 commit 810603e
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 22 deletions.
3 changes: 0 additions & 3 deletions components/grist/.gitignore

This file was deleted.

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

export default {
key: "grist-add-records",
name: "Add Records",
description: "Appends new records to a chosen table in Grist. [See the documentation](https://support.getgrist.com/api/#tag/records/operation/addRecords)",
version: "0.0.1",
type: "action",
props: {
app,
docId: {
propDefinition: [
app,
"docId",
],
},
tableId: {
propDefinition: [
app,
"tableId",
({ docId }) => ({
docId,
}),
],
},
records: {
propDefinition: [
app,
"records",
],
},
noParse: {
propDefinition: [
app,
"noParse",
],
},
},
async run({ $ }) {
const {
app,
docId,
tableId,
noParse,
records,
} = this;

const response = await app.addRecords({
$,
docId,
tableId,
params: {
noparse: noParse,
},
data: {
records: utils.parseArray(records),
},
});

$.export("$summary", `Successfully added \`${response.records.length}\` record(s) to the table.`);
return response;
},
};
116 changes: 116 additions & 0 deletions components/grist/actions/add-update-records/add-update-records.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import app from "../../grist.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "grist-add-update-records",
name: "Add Or Update Records",
description: "Add records in a specified table or updates existing matching records. [See the documentation](https://support.getgrist.com/api/#tag/records/operation/replaceRecords)",
version: "0.0.1",
type: "action",
props: {
app,
docId: {
propDefinition: [
app,
"docId",
],
},
tableId: {
propDefinition: [
app,
"tableId",
({ docId }) => ({
docId,
}),
],
},
noParse: {
propDefinition: [
app,
"noParse",
],
},
onMany: {
type: "string",
label: "On Many",
description: "Which records to update if multiple records are found to match.",
optional: true,
options: [
{
value: "first",
label: "The first matching record (default)",
},
{
value: "none",
label: "Do not update anything",
},
{
value: "all",
label: "Update all matches",
},
],
},
noAdd: {
type: "boolean",
label: "No Add",
description: "Set to true to prohibit adding records.",
optional: true,
},
noUpdate: {
type: "boolean",
label: "No Update",
description: "Set to true to prohibit updating records.",
optional: true,
},
records: {
description: app.propDefinitions.records.description + " Instead of an id, a `require` object is provided, with the same structure as `fields`. If no query parameter options are set, then the operation is as follows. First, we check if a record exists matching the values specified for columns in `require`. If so, we update it by setting the values specified for columns in fields. If not, we create a new record with a combination of the values in `require` and `fields`, with `fields` taking priority if the same column is specified in both. The query parameters allow for variations on this behavior. Eg. `[ { \"require\": { \"pet\": \"cat\" }, \"fields\": { \"popularity\": 67 } } ]`",
propDefinition: [
app,
"records",
],
},
},
methods: {
addUpdateRecords({
docId, tableId, ...args
} = {}) {
return this.app.put({
path: `/docs/${docId}/tables/${tableId}/records`,
...args,
});
},
},
async run({ $ }) {
const {
addUpdateRecords,
docId,
tableId,
noParse: noparse,
onMany: onmany,
noAdd: noadd,
noUpdate: noupdate,
records,
} = this;

await addUpdateRecords({
$,
docId,
tableId,
params: {
noparse,
onmany,
noadd,
noupdate,
},
data: {
records: utils.parseArray(records),
},
});

$.export("$summary", "Successfully ran this action");

return {
success: true,
};
},
};
75 changes: 75 additions & 0 deletions components/grist/actions/find-records/find-records.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import utils from "../../common/utils.mjs";
import app from "../../grist.app.mjs";

export default {
key: "grist-find-records",
name: "Find Records",
description: "Searches for records in a specified table. [See the documentation](https://support.getgrist.com/api/#tag/records/operation/listRecords)",
version: "0.0.1",
type: "action",
props: {
app,
docId: {
propDefinition: [
app,
"docId",
],
},
tableId: {
propDefinition: [
app,
"tableId",
({ docId }) => ({
docId,
}),
],
},
filter: {
type: "string",
label: "Filter",
description: "This is a JSON object mapping column names to arrays of allowed values. For example, to filter column pet for values cat and dog, the filter would be `{\"pet\": [\"cat\", \"dog\"]}`. Multiple columns can be filtered. For example the filter for pet being either `cat` or `dog`, AND `size` being either `tiny` or `outrageously small`, would be `{\"pet\": [\"cat\", \"dog\"], \"size\": [\"tiny\", \"outrageously small\"]}`",
optional: true,
},
limit: {
type: "integer",
label: "Limit",
description: "Return at most this number of rows. A value of 0 is equivalent to having no limit.",
optional: true,
},
},
methods: {
findRecords({
docId, tableId, ...args
} = {}) {
return this.app._makeRequest({
path: `/docs/${docId}/tables/${tableId}/records`,
...args,
});
},
},
async run({ $ }) {
const {
findRecords,
docId,
tableId,
filter,
limit,
} = this;

filter && utils.valueToObject(filter);

const response = await findRecords({
$,
docId,
tableId,
params: {
filter,
limit,
},
});

$.export("$summary", `Successfully found \`${response.records.length}\` record(s) in the table.`);

return response;
},
};
13 changes: 0 additions & 13 deletions components/grist/app/grist.app.ts

This file was deleted.

9 changes: 9 additions & 0 deletions components/grist/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const BASE_URL = "https://docs.getgrist.com";
const VERSION_PATH = "/api";
const WEBHOOK_ID = "webhookId";

export default {
BASE_URL,
VERSION_PATH,
WEBHOOK_ID,
};
50 changes: 50 additions & 0 deletions components/grist/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ConfigurationError } from "@pipedream/platform";

function isJson(value) {
if (typeof(value) !== "string") {
return false;
}

try {
JSON.parse(value);
} catch (e) {
return false;
}

return true;
}

function valueToObject(value) {
if (!isJson(value)) {
throw new ConfigurationError(`Make sure the custom expression contains a valid JSON object: ${value}`);
}
return JSON.parse(value);
}

function parseArray(value) {
try {
if (!value) {
return [];
}

if (Array.isArray(value)) {
return value;
}

const parsedValue = JSON.parse(value);

if (!Array.isArray(parsedValue)) {
throw new Error("Not an array");
}

return parsedValue;

} catch (e) {
throw new ConfigurationError("Make sure the custom expression contains a valid JSON array object");
}
}

export default {
valueToObject,
parseArray: (value) => parseArray(value).map(valueToObject),
};
Loading

0 comments on commit 810603e

Please sign in to comment.