- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.5k
New Components - nile_database #14223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3788613
              
                wip
              
              
                michelle0927 1119cff
              
                execute-query-action
              
              
                michelle0927 b173730
              
                pnpm-lock.yaml
              
              
                michelle0927 e421de5
              
                execute-query
              
              
                michelle0927 e3e8595
              
                user api components
              
              
                michelle0927 aedfa14
              
                pnpm-lock.yaml
              
              
                michelle0927 5127e5f
              
                remove console.log
              
              
                michelle0927 9fb079c
              
                update execute-query
              
              
                michelle0927 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            54 changes: 54 additions & 0 deletions
          
          54 
        
  components/nile_database/actions/create-user/create-user.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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; | ||
| }, | ||
| }; | 
        
          
          
            56 changes: 56 additions & 0 deletions
          
          56 
        
  components/nile_database/actions/execute-query/execute-query.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| 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, | ||
| workspace: { | ||
| propDefinition: [ | ||
| nile, | ||
| "workspace", | ||
| ], | ||
| }, | ||
| database: { | ||
| propDefinition: [ | ||
| nile, | ||
| "database", | ||
| ], | ||
| }, | ||
| user: { | ||
| type: "string", | ||
| label: "Username", | ||
| description: "The username or userId of the database user. Note: Credentials are generated in the Nile Dashboard under Settings -> Credentials", | ||
| }, | ||
| password: { | ||
| type: "string", | ||
| label: "Password", | ||
| description: "The password of the database user", | ||
| }, | ||
| query: { | ||
| type: "string", | ||
| label: "Query", | ||
| description: "The PostgreSQL query to execute", | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const config = { | ||
| user: this.user, | ||
| password: this.password, | ||
| host: await this.nile.getHost({ | ||
| workspace: this.workspace, | ||
| database: this.database, | ||
| }), | ||
| port: "5432", | ||
| database: this.database, | ||
| }; | ||
| const data = await this.nile.executeQuery(config, this.query); | ||
| $.export("$summary", `Returned ${data.length} ${data.length === 1 | ||
| ? "row" | ||
| : "rows"}`); | ||
| return data; | ||
| }, | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,11 +1,137 @@ | ||
| 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.getDatabase({ | ||
| 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, | ||
| }); | ||
| }, | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| getDatabase({ | ||
| workspace, database, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| url: `${this._globalBaseUrl()}/workspaces/${workspace}/databases/${database}`, | ||
| workspace, | ||
| database, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| getAuthenticatedUser(opts = {}) { | ||
| return this._makeRequest({ | ||
| url: `${this._globalBaseUrl()}/developers/me/full`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| async getHost({ | ||
| workspace, database, ...opts | ||
| }) { | ||
| const { dbHost } = await this.getDatabase({ | ||
| workspace, | ||
| database, | ||
| ...opts, | ||
| }); | ||
| const host = dbHost.match(/postgres:\/\/([^/]+)\//); | ||
| return host[1]; | ||
| }, | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| 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, | ||
| }); | ||
| }, | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| async _getClient(config) { | ||
| const pool = new pg.Pool(config); | ||
| const client = await pool.connect(); | ||
| return client; | ||
| }, | ||
| async _endClient(client) { | ||
| return client.release(); | ||
| }, | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| async executeQuery(config, query) { | ||
| const client = await this._getClient(config); | ||
| try { | ||
| const { rows } = await client.query(query); | ||
| return rows; | ||
| } finally { | ||
| await this._endClient(client); | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | 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); | ||
| } | ||
| }, | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| }; | ||
        
          
          
            24 changes: 24 additions & 0 deletions
          
          24 
        
  components/nile_database/sources/new-tenant-created/new-tenant-created.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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(), | ||
| }; | ||
| }, | ||
| }, | ||
| }; | 
        
          
          
            24 changes: 24 additions & 0 deletions
          
          24 
        
  components/nile_database/sources/new-user-created/new-user-created.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | 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), | ||
| }; | ||
| }, | ||
| }, | ||
| }; | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.