Skip to content

Commit e421de5

Browse files
committed
execute-query
1 parent b173730 commit e421de5

File tree

2 files changed

+41
-80
lines changed

2 files changed

+41
-80
lines changed

components/nile_database/actions/execute-query/execute-query.mjs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,51 @@ import nile from "../../nile_database.app.mjs";
33
export default {
44
name: "Execute Query",
55
key: "nile_database-execute-query",
6-
description: "Execute a custom PostgreSQL query. See [our docs](https://pipedream.com/docs/databases/working-with-sql) to learn more about working with SQL in Pipedream.",
6+
description: "Execute a custom PostgreSQL query.",
77
version: "0.0.1",
88
type: "action",
99
props: {
1010
nile,
11-
// eslint-disable-next-line pipedream/props-description
12-
sql: {
13-
type: "sql",
14-
auth: {
15-
app: "nile",
16-
},
17-
label: "PostreSQL Query",
11+
user: {
12+
type: "string",
13+
label: "Username",
14+
description: "The username of the database user",
15+
},
16+
password: {
17+
type: "string",
18+
label: "Password",
19+
description: "The password of the database user",
20+
},
21+
host: {
22+
type: "string",
23+
label: "Host",
24+
description: "The host of the database",
25+
},
26+
port: {
27+
type: "string",
28+
label: "Port",
29+
description: "The port to connect to the database. Example: `5432`",
30+
},
31+
database: {
32+
type: "string",
33+
label: "Database",
34+
description: "The name of the database to connect to",
35+
},
36+
query: {
37+
type: "string",
38+
label: "Query",
39+
description: "The PostgreSQL query to execute",
1840
},
1941
},
2042
async run({ $ }) {
21-
const args = this.nile.executeQueryAdapter(this.sql);
22-
const data = await this.nile.executeQuery(args);
43+
const config = {
44+
user: this.user,
45+
password: this.password,
46+
host: this.host,
47+
port: this.port,
48+
database: this.database,
49+
};
50+
const data = await this.nile.executeQuery(config, this.query);
2351
$.export("$summary", `Returned ${data.length} ${data.length === 1
2452
? "row"
2553
: "rows"}`);

components/nile_database/nile_database.app.mjs

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,89 +4,22 @@ export default {
44
type: "app",
55
app: "nile_database",
66
methods: {
7-
getClientConfiguration() {
8-
const {
9-
username,
10-
password,
11-
host,
12-
port,
13-
database,
14-
} = this.$auth;
15-
16-
return {
17-
user: username,
18-
password,
19-
host,
20-
port,
21-
database,
22-
};
23-
},
24-
async _getClient() {
25-
const config = this.getClientConfiguration();
7+
async _getClient(config) {
268
const pool = new pg.Pool(config);
279
const client = await pool.connect();
2810
return client;
2911
},
3012
async _endClient(client) {
3113
return client.release();
3214
},
33-
async executeQuery(query) {
34-
const client = await this._getClient();
35-
15+
async executeQuery(config, query) {
16+
const client = await this._getClient(config);
3617
try {
3718
const { rows } = await client.query(query);
3819
return rows;
3920
} finally {
4021
await this._endClient(client);
4122
}
4223
},
43-
executeQueryAdapter(proxyArgs = {}) {
44-
const {
45-
query: text = "",
46-
params: values = [],
47-
} = proxyArgs;
48-
return {
49-
text,
50-
values,
51-
};
52-
},
53-
proxyAdapter(query) {
54-
if (typeof query === "string") {
55-
return {
56-
query,
57-
};
58-
}
59-
60-
return {
61-
query: query.text,
62-
params: query.values,
63-
};
64-
},
65-
async getSchema() {
66-
const text = `
67-
SELECT table_name AS "tableName",
68-
column_name AS "columnName",
69-
is_nullable AS "isNullable",
70-
data_type AS "dataType",
71-
column_default AS "columnDefault"
72-
FROM information_schema.columns
73-
WHERE table_schema NOT IN ('pg_catalog', 'information_schema', 'users', 'auth')
74-
ORDER BY table_name,
75-
ordinal_position
76-
`;
77-
const rows = await this.executeQuery({
78-
text,
79-
});
80-
return rows.reduce((acc, row) => {
81-
acc[row.tableName] ??= {
82-
metadata: {},
83-
schema: {},
84-
};
85-
acc[row.tableName].schema[row.columnName] = {
86-
...row,
87-
};
88-
return acc;
89-
}, {});
90-
},
9124
},
9225
};

0 commit comments

Comments
 (0)