Skip to content

Commit e3e8595

Browse files
committed
user api components
1 parent e421de5 commit e3e8595

File tree

7 files changed

+247
-3
lines changed

7 files changed

+247
-3
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import nile from "../../nile_database.app.mjs";
2+
3+
export default {
4+
key: "nile_database-create-user",
5+
name: "Create User",
6+
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)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
nile,
11+
workspace: {
12+
propDefinition: [
13+
nile,
14+
"workspace",
15+
],
16+
},
17+
database: {
18+
propDefinition: [
19+
nile,
20+
"database",
21+
],
22+
},
23+
email: {
24+
type: "string",
25+
label: "Email",
26+
description: "Email address of the user",
27+
},
28+
password: {
29+
type: "string",
30+
label: "Password",
31+
description: "Password for the user",
32+
},
33+
preferredName: {
34+
type: "string",
35+
label: "Preferred Name",
36+
description: "The preferred name of the user",
37+
optional: true,
38+
},
39+
},
40+
async run({ $ }) {
41+
const response = await this.nile.createUser({
42+
$,
43+
workspace: this.workspace,
44+
database: this.database,
45+
data: {
46+
email: this.email,
47+
password: this.password,
48+
preferredName: this.preferredName,
49+
},
50+
});
51+
$.export("$summary", `Successfully created user with ID: ${response.id}`);
52+
return response;
53+
},
54+
};

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ export default {
2929
description: "The port to connect to the database. Example: `5432`",
3030
},
3131
database: {
32-
type: "string",
33-
label: "Database",
34-
description: "The name of the database to connect to",
32+
propDefinition: [
33+
nile,
34+
"database",
35+
],
3536
},
3637
query: {
3738
type: "string",

components/nile_database/nile_database.app.mjs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,101 @@
1+
import { axios } from "@pipedream/platform";
12
import pg from "pg";
23

34
export default {
45
type: "app",
56
app: "nile_database",
7+
propDefinitions: {
8+
workspace: {
9+
type: "string",
10+
label: "Workspace",
11+
description: "Your workspace slug",
12+
async options() {
13+
const { workspaces } = await this.getAuthenticatedUser();
14+
return workspaces?.map(({ slug }) => slug) || [];
15+
},
16+
},
17+
database: {
18+
type: "string",
19+
label: "Database",
20+
description: "The database name",
21+
async options() {
22+
const { databases } = await this.getAuthenticatedUser();
23+
return databases?.map(({ name }) => name) || [];
24+
},
25+
},
26+
},
627
methods: {
28+
_globalBaseUrl() {
29+
return "https://global.thenile.dev";
30+
},
31+
async _getBaseUrl({
32+
workspace, database, ...opts
33+
}) {
34+
const { apiHost } = await this._makeRequest({
35+
url: `${this._globalBaseUrl()}/workspaces/${workspace}/databases/${database}`,
36+
workspace,
37+
database,
38+
...opts,
39+
});
40+
return apiHost;
41+
},
42+
async _makeRequest({
43+
$ = this,
44+
workspace,
45+
database,
46+
url,
47+
path,
48+
...opts
49+
}) {
50+
return axios($, {
51+
url: url || `${await this._getBaseUrl({
52+
workspace,
53+
database,
54+
$,
55+
})}${path}`,
56+
headers: {
57+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
58+
},
59+
...opts,
60+
});
61+
},
62+
getAuthenticatedUser(opts = {}) {
63+
return this._makeRequest({
64+
url: `${this._globalBaseUrl()}/developers/me/full`,
65+
...opts,
66+
});
67+
},
68+
listUsers({
69+
workspace, database, ...opts
70+
}) {
71+
return this._makeRequest({
72+
path: "/users",
73+
workspace,
74+
database,
75+
...opts,
76+
});
77+
},
78+
listTenants({
79+
workspace, database, ...opts
80+
}) {
81+
return this._makeRequest({
82+
path: "/tenants",
83+
workspace,
84+
database,
85+
...opts,
86+
});
87+
},
88+
createUser({
89+
workspace, database, ...opts
90+
}) {
91+
return this._makeRequest({
92+
method: "POST",
93+
path: "/users",
94+
workspace,
95+
database,
96+
...opts,
97+
});
98+
},
799
async _getClient(config) {
8100
const pool = new pg.Pool(config);
9101
const client = await pool.connect();

components/nile_database/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
1617
"pg": "^8.13.0"
1718
}
1819
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import nile from "../../nile_database.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
props: {
6+
nile,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
workspace: {
15+
propDefinition: [
16+
nile,
17+
"workspace",
18+
],
19+
},
20+
database: {
21+
propDefinition: [
22+
nile,
23+
"database",
24+
],
25+
},
26+
},
27+
methods: {
28+
getResourceFn() {
29+
throw new Error("getResourceFn is not implemented");
30+
},
31+
generateMeta() {
32+
throw new Error("generateMeta is not implemented");
33+
},
34+
},
35+
async run() {
36+
const resourceFn = this.getResourceFn();
37+
38+
const results = await resourceFn({
39+
workspace: this.workspace,
40+
database: this.database,
41+
}); console.log(results);
42+
43+
for (const item of results) {
44+
const meta = this.generateMeta(item);
45+
this.$emit(item, meta);
46+
}
47+
},
48+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "nile_database-new-tenant-created",
6+
name: "New Tenant Created",
7+
description: "Emit new event when a new tenant is added to a Nile Database",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getResourceFn() {
14+
return this.nile.listTenants;
15+
},
16+
generateMeta(tenant) {
17+
return {
18+
id: tenant.id,
19+
summary: `New Tenant ID: ${tenant.id}`,
20+
ts: Date.now(),
21+
};
22+
},
23+
},
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "nile_database-new-user-created",
6+
name: "New User Created",
7+
description: "Emit new event when a new user is added in a Nile Database",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getResourceFn() {
14+
return this.nile.listUsers;
15+
},
16+
generateMeta(user) {
17+
return {
18+
id: user.id,
19+
summary: `New User ID: ${user.id}`,
20+
ts: Date.parse(user.created),
21+
};
22+
},
23+
},
24+
};

0 commit comments

Comments
 (0)