Skip to content

Commit af92d5a

Browse files
committed
updates
1 parent ea358df commit af92d5a

File tree

24 files changed

+318
-345
lines changed

24 files changed

+318
-345
lines changed

components/neon_postgres/actions/delete-rows/delete-rows.mjs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,16 @@ export default {
5555
value,
5656
} = this;
5757

58-
try {
59-
const rows = await this.neon.deleteRows(
60-
schema,
61-
table,
62-
column,
63-
value,
64-
);
65-
$.export("$summary", `Deleted ${rows.length} rows from ${table}`);
66-
return rows;
67-
} catch (error) {
68-
let errorMsg = "Row not deleted due to an error. ";
69-
errorMsg += `${error}`.includes("SSL verification failed")
70-
? "This could be because SSL verification failed. To resolve this, reconnect your account and set SSL Verification Mode: Skip Verification, and try again."
71-
: `${error}`;
72-
throw new Error(errorMsg);
73-
}
58+
const errorMsg = "Row not deleted due to an error. ";
59+
60+
const rows = await this.neon.deleteRows(
61+
schema,
62+
table,
63+
column,
64+
value,
65+
errorMsg,
66+
);
67+
$.export("$summary", `Deleted ${rows.length} rows from ${table}`);
68+
return rows;
7469
},
7570
};

components/neon_postgres/actions/find-row-custom-query/find-row-custom-query.mjs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,12 @@ export default {
4242
throw new Error("Need be a `SELECT` statement query. Read more about [SELECT queries here](https://www.w3schools.com/sql/sql_select.asp)");
4343
}
4444

45-
try {
46-
const res = await this.neon.executeQuery({
47-
text: query,
48-
values,
49-
});
50-
$.export("$summary", "Successfully executed query");
51-
return res;
52-
} catch (error) {
53-
let errorMsg = "Query not executed due to an error. ";
54-
errorMsg += `${error}`.includes("SSL verification failed")
55-
? "This could be because SSL verification failed. To resolve this, reconnect your account and set SSL Verification Mode: Skip Verification, and try again."
56-
: `${error}`;
57-
throw new Error(errorMsg);
58-
}
45+
const res = await this.neon.executeQuery({
46+
text: query,
47+
values,
48+
errorMsg: "Query not executed due to an error. ",
49+
});
50+
$.export("$summary", "Successfully executed query");
51+
return res;
5952
},
6053
};

components/neon_postgres/actions/find-row/find-row.mjs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,20 @@ export default {
5454
column,
5555
value,
5656
} = this;
57-
try {
58-
const res = await this.neon.findRowByValue(
59-
schema,
60-
table,
61-
column,
62-
value,
63-
);
64-
const summary = res
65-
? "Row found"
66-
: "Row not found";
67-
$.export("$summary", summary);
68-
return res;
69-
} catch (error) {
70-
let errorMsg = "Row not retrieved due to an error. ";
71-
errorMsg += `${error}`.includes("SSL verification failed")
72-
? "This could be because SSL verification failed. To resolve this, reconnect your account and set SSL Verification Mode: Skip Verification, and try again."
73-
: `${error}`;
74-
throw new Error(errorMsg);
75-
}
57+
58+
const errorMsg = "Row not found due to an error. ";
59+
60+
const res = await this.neon.findRowByValue(
61+
schema,
62+
table,
63+
column,
64+
value,
65+
errorMsg,
66+
);
67+
const summary = res
68+
? "Row found"
69+
: "Row not found";
70+
$.export("$summary", summary);
71+
return res;
7672
},
7773
};

components/neon_postgres/actions/insert-row/insert-row.mjs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import neon from "../../neon_postgres.app.mjs";
2+
import { parseRowValues } from "../../common/utils.mjs";
23

34
export default {
45
name: "Insert Row",
@@ -28,6 +29,7 @@ export default {
2829
neon,
2930
"rowValues",
3031
],
32+
description: "JSON representation of your table rows. Accept a single row (JSON Object) or multiple rows (JSON array). For example: `{ \"product_id\": 1, \"product_name\": \"Laptop Pro 15\", \"price\": 1200.50, \"stock_quantity\": 50, \"created_at\": \"2023-10-26T10:00:00Z\" }`",
3133
},
3234
},
3335
async run({ $ }) {
@@ -36,23 +38,31 @@ export default {
3638
table,
3739
rowValues,
3840
} = this;
39-
const columns = Object.keys(rowValues);
40-
const values = Object.values(rowValues);
41-
try {
41+
const results = [];
42+
const parsedRowValues = parseRowValues(rowValues);
43+
const parsedRowValuesArray = Array.isArray(parsedRowValues)
44+
? parsedRowValues
45+
: [
46+
parsedRowValues,
47+
];
48+
49+
const errorMsg = "New row(s) not inserted due to an error. ";
50+
51+
for (const row of parsedRowValuesArray) {
52+
const columns = Object.keys(row);
53+
const values = Object.values(row);
4254
const res = await this.neon.insertRow(
4355
schema,
4456
table,
4557
columns,
4658
values,
59+
errorMsg,
4760
);
48-
$.export("$summary", "New row inserted");
49-
return res;
50-
} catch (error) {
51-
let errorMsg = "New row not inserted due to an error. ";
52-
errorMsg += `${error}`.includes("SSL verification failed")
53-
? "This could be because SSL verification failed. To resolve this, reconnect your account and set SSL Verification Mode: Skip Verification, and try again."
54-
: `${error}`;
55-
throw new Error(errorMsg);
61+
results.push(res);
5662
}
63+
$.export("$summary", `Successfully inserted ${results.length} row${results.length === 1
64+
? ""
65+
: "s"}`);
66+
return results;
5767
},
5868
};

components/neon_postgres/actions/update-row/update-row.mjs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import neon from "../../neon_postgres.app.mjs";
2+
import { parseRowValues } from "../../common/utils.mjs";
23

34
export default {
45
name: "Update Row",
@@ -51,6 +52,7 @@ export default {
5152
neon,
5253
"rowValues",
5354
],
55+
description: "JSON representation of your new table row values. For example: `{ \"product_name\": \"Laptop Pro 15\", \"price\": 1200.50, \"stock_quantity\": 50 }`",
5456
},
5557
},
5658
async run({ $ }) {
@@ -61,25 +63,22 @@ export default {
6163
value,
6264
rowValues,
6365
} = this;
64-
try {
65-
const res = await this.neon.updateRow(
66-
schema,
67-
table,
68-
column,
69-
value,
70-
rowValues,
71-
);
72-
const summary = res
73-
? "Row updated"
74-
: "Row not found";
75-
$.export("$summary", summary);
76-
return res;
77-
} catch (error) {
78-
let errorMsg = "Row not updated due to an error. ";
79-
errorMsg += `${error}`.includes("SSL verification failed")
80-
? "This could be because SSL verification failed. To resolve this, reconnect your account and set SSL Verification Mode: Skip Verification, and try again."
81-
: `${error}`;
82-
throw new Error(errorMsg);
83-
}
66+
67+
const parsedRowValues = parseRowValues(rowValues);
68+
const errorMsg = "Row not updated due to an error. ";
69+
70+
const res = await this.neon.updateRow(
71+
schema,
72+
table,
73+
column,
74+
value,
75+
parsedRowValues,
76+
errorMsg,
77+
);
78+
const summary = res
79+
? "Row updated"
80+
: "Row not found";
81+
$.export("$summary", summary);
82+
return res;
8483
},
8584
};
Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import neon from "../../neon_postgres.app.mjs";
2-
import format from "pg-format";
2+
import { parseRowValues } from "../../common/utils.mjs";
33

44
export default {
55
name: "Upsert Row",
@@ -41,61 +41,30 @@ export default {
4141
neon,
4242
"rowValues",
4343
],
44-
},
45-
},
46-
methods: {
47-
upsertRow({
48-
schema, table, columns, values, conflictTarget = "id",
49-
} = {}) {
50-
const placeholders = this.neon.getPlaceholders({
51-
values,
52-
});
53-
54-
const updates = columns
55-
.filter((column) => column !== conflictTarget)
56-
.map((column) => `${column}=EXCLUDED.${column}`);
57-
58-
const query = `
59-
INSERT INTO ${schema}.${table} (${columns})
60-
VALUES (${placeholders})
61-
ON CONFLICT (${conflictTarget})
62-
DO UPDATE SET ${updates}
63-
RETURNING *
64-
`;
65-
66-
return this.neon.executeQuery({
67-
text: format(query, schema, table),
68-
values,
69-
});
44+
description: "JSON representation of your table row values. For example: `{ \"product_name\": \"Laptop Pro 15\", \"price\": 1200.50, \"stock_quantity\": 50 }`",
7045
},
7146
},
7247
async run({ $ }) {
7348
const {
74-
upsertRow,
7549
rowValues,
7650
...args
7751
} = this;
7852

79-
const columns = Object.keys(rowValues);
80-
const values = Object.values(rowValues);
53+
const parsedRowValues = parseRowValues(rowValues);
54+
55+
const columns = Object.keys(parsedRowValues);
56+
const values = Object.values(parsedRowValues);
8157

82-
try {
83-
const res = await upsertRow({
84-
columns,
85-
values,
86-
...args,
87-
});
88-
const summary = res
89-
? "Row upserted"
90-
: "Row not upserted";
91-
$.export("$summary", summary);
92-
return res;
93-
} catch (error) {
94-
let errorMsg = "Row not upserted due to an error. ";
95-
errorMsg += `${error}`.includes("SSL verification failed")
96-
? "This could be because SSL verification failed. To resolve this, reconnect your account and set SSL Verification Mode: Skip Verification, and try again."
97-
: `${error}`;
98-
throw new Error(errorMsg);
99-
}
58+
const res = await this.neon.upsertRow({
59+
columns,
60+
values,
61+
errorMsg: "Row not upserted due to an error. ",
62+
...args,
63+
});
64+
const summary = res
65+
? "Row upserted"
66+
: "Row not upserted";
67+
$.export("$summary", summary);
68+
return res;
10069
},
10170
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function parseRowValues(rowValues) {
2+
if (!rowValues) {
3+
return undefined;
4+
}
5+
if (Array.isArray(rowValues)) {
6+
return rowValues.map(parseRowValues);
7+
}
8+
if (typeof rowValues === "string") {
9+
try {
10+
return JSON.parse(rowValues);
11+
} catch (error) {
12+
return rowValues;
13+
}
14+
}
15+
return rowValues;
16+
}

components/neon_postgres/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/neon_postgres",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Pipedream Neon Postgres Components",
55
"main": "neon_postgres.app.mjs",
66
"keywords": [

components/neon_postgres/sources/new-row-custom-query/new-row-custom-query.mjs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default {
4040
common.props.postgresql,
4141
"query",
4242
],
43+
description: "Specify the query to select new or updated rows since the last poll. For example, `SELECT * FROM users WHERE country = 'US'`",
4344
},
4445
values: {
4546
propDefinition: [
@@ -48,6 +49,23 @@ export default {
4849
],
4950
},
5051
},
52+
hooks: {
53+
async deploy() {
54+
if (this.values && !Array.isArray(this.values)) {
55+
throw new Error("No valid values provided. The values property must be an array.");
56+
}
57+
58+
const numberOfValues = this.query?.match(/\$/g)?.length || 0;
59+
if (this.values.length !== numberOfValues) {
60+
throw new Error("The number of values provided does not match the number of values in the query.");
61+
}
62+
63+
const isColumnUnique = await this.isColumnUnique(this.schema, this.table, this.column);
64+
if (!isColumnUnique) {
65+
throw new Error("The column selected contains duplicate values. Column must be unique");
66+
}
67+
},
68+
},
5169
methods: {
5270
...common.methods,
5371
generateMeta(row, column) {
@@ -59,34 +77,12 @@ export default {
5977
},
6078
},
6179
async run() {
62-
const {
63-
schema,
64-
table,
65-
column,
66-
query,
67-
values = [],
68-
} = this;
69-
70-
if (!Array.isArray(values)) {
71-
throw new Error("No valid values provided. The values property must be an array.");
72-
}
73-
74-
const numberOfValues = query?.match(/\$/g)?.length || 0;
75-
if (values.length !== numberOfValues) {
76-
throw new Error("The number of values provided does not match the number of values in the query.");
77-
}
78-
79-
const isColumnUnique = await this.isColumnUnique(schema, table, column);
80-
if (!isColumnUnique) {
81-
throw new Error("The column selected contains duplicate values. Column must be unique");
82-
}
83-
8480
const rows = await this.postgresql.executeQuery({
85-
text: query,
86-
values,
81+
text: this.query,
82+
values: this.values,
8783
});
8884
for (const row of rows) {
89-
const meta = this.generateMeta(row, column);
85+
const meta = this.generateMeta(row, this.column);
9086
this.$emit(row, meta);
9187
}
9288
},

0 commit comments

Comments
 (0)