Skip to content

Commit 100750b

Browse files
committed
[Components] Amazon Redshift - new components
1 parent eb08d0a commit 100750b

File tree

10 files changed

+1817
-120
lines changed

10 files changed

+1817
-120
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import app from "../../aws.app.mjs";
2+
3+
export default {
4+
key: "aws-redshift-create-rows",
5+
name: "Redshift - Create Rows",
6+
description: "Insert rows into a table. [See the documentation](https://docs.aws.amazon.com/redshift/latest/APIReference/API_ExecuteStatement.html)",
7+
type: "action",
8+
version: "0.0.1",
9+
props: {
10+
app,
11+
region: {
12+
propDefinition: [
13+
app,
14+
"region",
15+
],
16+
},
17+
workgroupName: {
18+
propDefinition: [
19+
app,
20+
"workgroupName",
21+
({ region }) => ({
22+
region,
23+
}),
24+
],
25+
},
26+
database: {
27+
propDefinition: [
28+
app,
29+
"database",
30+
({
31+
region, workgroupName,
32+
}) => ({
33+
region,
34+
workgroupName,
35+
}),
36+
],
37+
},
38+
schema: {
39+
propDefinition: [
40+
app,
41+
"schema",
42+
({
43+
region, database, workgroupName,
44+
}) => ({
45+
region,
46+
database,
47+
workgroupName,
48+
}),
49+
],
50+
},
51+
table: {
52+
propDefinition: [
53+
app,
54+
"table",
55+
({
56+
region, workgroupName, database, schema,
57+
}) => ({
58+
region,
59+
workgroupName,
60+
database,
61+
schema,
62+
}),
63+
],
64+
},
65+
columns: {
66+
type: "string[]",
67+
label: "Columns",
68+
description: "An array of column names for the new rows (e.g., `[\"id\", \"name\"]`).",
69+
},
70+
rows: {
71+
type: "string[]",
72+
label: "Rows (as JSON arrays)",
73+
description: "An array of data for the new rows. Each element must be a JSON array string with values corresponding to the specified columns (e.g., `\"[1, \"Pipedream\"]).",
74+
},
75+
},
76+
async run({ $ }) {
77+
const {
78+
app,
79+
region,
80+
workgroupName,
81+
database,
82+
schema,
83+
table,
84+
columns,
85+
rows: rowStrings,
86+
} = this;
87+
88+
if (!Array.isArray(columns) || columns.length === 0) {
89+
throw new Error("The 'Columns' prop must be a non-empty array.");
90+
}
91+
92+
if (!Array.isArray(rowStrings) || rowStrings.length === 0) {
93+
throw new Error("The 'Rows' prop must be a non-empty array.");
94+
}
95+
96+
const rows = rowStrings.map((row) => JSON.parse(row));
97+
98+
const sql = `
99+
INSERT INTO ${schema}.${table} (${columns.join(", ")})
100+
VALUES ${rows.map((_, rowIndex) =>
101+
`(${columns.map((_, colIndex) => `:p${rowIndex}${colIndex}`).join(", ")})`).join(", ")}
102+
`;
103+
104+
const parameters = rows.flatMap((row, rowIndex) =>
105+
row?.map((value, colIndex) => ({
106+
name: `p${rowIndex}${colIndex}`,
107+
value: String(value),
108+
})));
109+
110+
const response = await app.executeStatement({
111+
region,
112+
workgroupName,
113+
database,
114+
sql,
115+
parameters,
116+
});
117+
118+
$.export("$summary", `Successfully created \`${response.ResultRows}\` row(s) in table \`${schema}.${table}\`.`);
119+
120+
return response;
121+
},
122+
};
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import app from "../../aws.app.mjs";
2+
3+
export default {
4+
key: "aws-redshift-delete-rows",
5+
name: "Redshift - Delete Rows",
6+
description: "Deletes row(s) in an existing table in Redshift. [See the documentation](https://docs.aws.amazon.com/redshift/latest/APIReference/API_ExecuteStatement.html)",
7+
type: "action",
8+
version: "0.0.1",
9+
props: {
10+
app,
11+
region: {
12+
propDefinition: [
13+
app,
14+
"region",
15+
],
16+
},
17+
workgroupName: {
18+
propDefinition: [
19+
app,
20+
"workgroupName",
21+
({ region }) => ({
22+
region,
23+
}),
24+
],
25+
},
26+
database: {
27+
propDefinition: [
28+
app,
29+
"database",
30+
({
31+
region,
32+
workgroupName,
33+
}) => ({
34+
region,
35+
workgroupName,
36+
}),
37+
],
38+
},
39+
schema: {
40+
propDefinition: [
41+
app,
42+
"schema",
43+
({
44+
region,
45+
database,
46+
workgroupName,
47+
}) => ({
48+
region,
49+
database,
50+
workgroupName,
51+
}),
52+
],
53+
},
54+
table: {
55+
propDefinition: [
56+
app,
57+
"table",
58+
({
59+
region,
60+
database,
61+
workgroupName,
62+
schema,
63+
}) => ({
64+
region,
65+
database,
66+
workgroupName,
67+
schema,
68+
}),
69+
],
70+
},
71+
where: {
72+
type: "string",
73+
label: "WHERE Clause",
74+
description: "The WHERE clause to filter rows to delete. e.g. `id = :id`. Use named parameters to avoid SQL injection.",
75+
},
76+
sqlParameters: {
77+
type: "object",
78+
label: "Parameters for WHERE clause",
79+
description: "An object with parameters to use in the WHERE clause. e.g. `{ \"id\": 1 }`",
80+
optional: true,
81+
},
82+
},
83+
async run({ $ }) {
84+
const {
85+
region,
86+
workgroupName,
87+
database,
88+
schema,
89+
table,
90+
where,
91+
sqlParameters,
92+
} = this;
93+
94+
const sql = `DELETE FROM ${schema}.${table} WHERE ${where}`;
95+
96+
const parameters = Object.entries(sqlParameters || {})
97+
.map(([
98+
name,
99+
value,
100+
]) => ({
101+
name,
102+
value: String(value),
103+
}));
104+
105+
const response = await this.app.executeStatement({
106+
region,
107+
workgroupName,
108+
database,
109+
sql,
110+
parameters,
111+
});
112+
113+
$.export("$summary", `Successfully deleted \`${response.ResultRows}\` row(s) from table \`${schema}.${table}\`.`);
114+
return response;
115+
},
116+
};
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import app from "../../aws.app.mjs";
2+
3+
export default {
4+
key: "aws-redshift-query-database",
5+
name: "Redshift - Query Database",
6+
description: "Run a SELECT query on a database. [See the documentation](https://docs.aws.amazon.com/redshift/latest/APIReference/API_ExecuteStatement.html)",
7+
type: "action",
8+
version: "0.0.1",
9+
props: {
10+
app,
11+
region: {
12+
propDefinition: [
13+
app,
14+
"region",
15+
],
16+
},
17+
workgroupName: {
18+
propDefinition: [
19+
app,
20+
"workgroupName",
21+
({ region }) => ({
22+
region,
23+
}),
24+
],
25+
},
26+
database: {
27+
propDefinition: [
28+
app,
29+
"database",
30+
({
31+
region,
32+
workgroupName,
33+
}) => ({
34+
region,
35+
workgroupName,
36+
}),
37+
],
38+
},
39+
columns: {
40+
type: "string[]",
41+
label: "SELECT Columns Clause",
42+
description: "List of columns to retrieve.",
43+
optional: true,
44+
},
45+
from: {
46+
type: "string",
47+
label: "FROM Clause",
48+
description: "The FROM clause to select from. e.g. `schema_name.table_name`",
49+
},
50+
where: {
51+
type: "string",
52+
label: "WHERE Clause",
53+
description: "The WHERE clause to filter rows. e.g. `id = :id`. Use named parameters to avoid SQL injection.",
54+
optional: true,
55+
},
56+
orderBy: {
57+
type: "string",
58+
label: "Order By",
59+
description: "Column to order the results by (e.g., `column_name ASC`, `column_name DESC`).",
60+
optional: true,
61+
},
62+
limit: {
63+
type: "integer",
64+
label: "Limit",
65+
description: "Maximum number of rows to return.",
66+
optional: true,
67+
default: 10,
68+
},
69+
sqlParameters: {
70+
type: "object",
71+
label: "Parameters",
72+
description: "An object with parameters to use in the WHERE clause. e.g. `{ \"id\": 1 }`",
73+
optional: true,
74+
},
75+
},
76+
async run({ $ }) {
77+
const {
78+
region,
79+
workgroupName,
80+
database,
81+
columns,
82+
from,
83+
where,
84+
sqlParameters,
85+
orderBy,
86+
limit,
87+
} = this;
88+
89+
const effectiveColumns = Array.isArray(columns) && columns.length
90+
? columns.join(", ")
91+
: "*";
92+
93+
let sql = `SELECT ${effectiveColumns} FROM ${from}`;
94+
95+
if (where) {
96+
sql += ` WHERE ${where}`;
97+
}
98+
if (orderBy) {
99+
sql += ` ORDER BY ${orderBy}`;
100+
}
101+
if (limit) {
102+
sql += " LIMIT :limit";
103+
}
104+
const parameters = Object.entries({
105+
...sqlParameters,
106+
limit,
107+
})
108+
.map(([
109+
name,
110+
value,
111+
]) => ({
112+
name,
113+
value: String(value),
114+
}));
115+
116+
const response = await this.app.executeStatement({
117+
region,
118+
workgroupName,
119+
database,
120+
sql,
121+
parameters,
122+
});
123+
$.export("$summary", `Successfully found \`${response.TotalNumRows}\` row(s).`);
124+
return response;
125+
},
126+
};

0 commit comments

Comments
 (0)