Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions components/nile_database/actions/create-user/create-user.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import nile from "../../nile_database.app.mjs";

export default {
key: "nile_database-create-user",
name: "Create User",
description: "Create a new database user by providing an email address and password. [See the documentation](https://www.thenile.dev/docs/reference/api-reference/users/create-user)",
version: "0.0.1",
type: "action",
props: {
nile,
workspace: {
propDefinition: [
nile,
"workspace",
],
},
database: {
propDefinition: [
nile,
"database",
],
},
email: {
type: "string",
label: "Email",
description: "Email address of the user",
},
password: {
type: "string",
label: "Password",
description: "Password for the user",
},
preferredName: {
type: "string",
label: "Preferred Name",
description: "The preferred name of the user",
optional: true,
},
},
async run({ $ }) {
const response = await this.nile.createUser({
$,
workspace: this.workspace,
database: this.database,
data: {
email: this.email,
password: this.password,
preferredName: this.preferredName,
},
});
$.export("$summary", `Successfully created user with ID: ${response.id}`);
return response;
},
};
57 changes: 57 additions & 0 deletions components/nile_database/actions/execute-query/execute-query.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import nile from "../../nile_database.app.mjs";

export default {
name: "Execute Query",
key: "nile_database-execute-query",
description: "Execute a custom PostgreSQL query.",
version: "0.0.1",
type: "action",
props: {
nile,
user: {
type: "string",
label: "Username",
description: "The username of the database user",
},
password: {
type: "string",
label: "Password",
description: "The password of the database user",
},
host: {
type: "string",
label: "Host",
description: "The host of the database",
},
port: {
type: "string",
label: "Port",
description: "The port to connect to the database. Example: `5432`",
},
database: {
propDefinition: [
nile,
"database",
],
},
query: {
type: "string",
label: "Query",
description: "The PostgreSQL query to execute",
},
},
async run({ $ }) {
const config = {
user: this.user,
password: this.password,
host: this.host,
port: this.port,
database: this.database,
};
const data = await this.nile.executeQuery(config, this.query);
$.export("$summary", `Returned ${data.length} ${data.length === 1
? "row"
: "rows"}`);
return data;
},
};
116 changes: 111 additions & 5 deletions components/nile_database/nile_database.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,117 @@
import { axios } from "@pipedream/platform";
import pg from "pg";

export default {
type: "app",
app: "nile_database",
propDefinitions: {},
propDefinitions: {
workspace: {
type: "string",
label: "Workspace",
description: "Your workspace slug",
async options() {
const { workspaces } = await this.getAuthenticatedUser();
return workspaces?.map(({ slug }) => slug) || [];
},
},
database: {
type: "string",
label: "Database",
description: "The database name",
async options() {
const { databases } = await this.getAuthenticatedUser();
return databases?.map(({ name }) => name) || [];
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_globalBaseUrl() {
return "https://global.thenile.dev";
},
async _getBaseUrl({
workspace, database, ...opts
}) {
const { apiHost } = await this._makeRequest({
url: `${this._globalBaseUrl()}/workspaces/${workspace}/databases/${database}`,
workspace,
database,
...opts,
});
return apiHost;
},
async _makeRequest({
$ = this,
workspace,
database,
url,
path,
...opts
}) {
return axios($, {
url: url || `${await this._getBaseUrl({
workspace,
database,
$,
})}${path}`,
headers: {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
...opts,
});
},
getAuthenticatedUser(opts = {}) {
return this._makeRequest({
url: `${this._globalBaseUrl()}/developers/me/full`,
...opts,
});
},
listUsers({
workspace, database, ...opts
}) {
return this._makeRequest({
path: "/users",
workspace,
database,
...opts,
});
},
listTenants({
workspace, database, ...opts
}) {
return this._makeRequest({
path: "/tenants",
workspace,
database,
...opts,
});
},
createUser({
workspace, database, ...opts
}) {
return this._makeRequest({
method: "POST",
path: "/users",
workspace,
database,
...opts,
});
},
async _getClient(config) {
const pool = new pg.Pool(config);
const client = await pool.connect();
return client;
},
async _endClient(client) {
return client.release();
},
async executeQuery(config, query) {
const client = await this._getClient(config);
try {
const { rows } = await client.query(query);
return rows;
} finally {
await this._endClient(client);
}
},
},
};
};
8 changes: 6 additions & 2 deletions components/nile_database/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/nile_database",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Nile Database Components",
"main": "nile_database.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3",
"pg": "^8.13.0"
}
}
}
48 changes: 48 additions & 0 deletions components/nile_database/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import nile from "../../nile_database.app.mjs";
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";

export default {
props: {
nile,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
workspace: {
propDefinition: [
nile,
"workspace",
],
},
database: {
propDefinition: [
nile,
"database",
],
},
},
methods: {
getResourceFn() {
throw new Error("getResourceFn is not implemented");
},
generateMeta() {
throw new Error("generateMeta is not implemented");
},
},
async run() {
const resourceFn = this.getResourceFn();

const results = await resourceFn({
workspace: this.workspace,
database: this.database,
});

for (const item of results) {
const meta = this.generateMeta(item);
this.$emit(item, meta);
}
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import common from "../common/base.mjs";

export default {
...common,
key: "nile_database-new-tenant-created",
name: "New Tenant Created",
description: "Emit new event when a new tenant is added to a Nile Database",
version: "0.0.1",
type: "source",
dedupe: "unique",
methods: {
...common.methods,
getResourceFn() {
return this.nile.listTenants;
},
generateMeta(tenant) {
return {
id: tenant.id,
summary: `New Tenant ID: ${tenant.id}`,
ts: Date.now(),
};
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import common from "../common/base.mjs";

export default {
...common,
key: "nile_database-new-user-created",
name: "New User Created",
description: "Emit new event when a new user is added in a Nile Database",
version: "0.0.1",
type: "source",
dedupe: "unique",
methods: {
...common.methods,
getResourceFn() {
return this.nile.listUsers;
},
generateMeta(user) {
return {
id: user.id,
summary: `New User ID: ${user.id}`,
ts: Date.parse(user.created),
};
},
},
};
Loading
Loading